• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 **
3 ** Copyright 2008, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #ifndef ANDROID_MEDIAPLAYERSERVICE_H
19 #define ANDROID_MEDIAPLAYERSERVICE_H
20 
21 #include <arpa/inet.h>
22 
23 #include <utils/threads.h>
24 #include <utils/Errors.h>
25 #include <utils/KeyedVector.h>
26 #include <utils/String8.h>
27 #include <utils/Vector.h>
28 
29 #include <media/MediaPlayerInterface.h>
30 #include <media/Metadata.h>
31 #include <media/stagefright/foundation/ABase.h>
32 
33 #include <android/hardware/media/omx/1.0/IOmx.h>
34 
35 #include <system/audio.h>
36 
37 namespace android {
38 
39 struct AudioPlaybackRate;
40 class AudioTrack;
41 struct AVSyncSettings;
42 class IDataSource;
43 class IMediaRecorder;
44 class IMediaMetadataRetriever;
45 class IOMX;
46 class IRemoteDisplay;
47 class IRemoteDisplayClient;
48 class MediaRecorderClient;
49 
50 #define CALLBACK_ANTAGONIZER 0
51 #if CALLBACK_ANTAGONIZER
52 class Antagonizer {
53 public:
54     Antagonizer(const sp<MediaPlayerBase::Listener> &listener);
start()55     void start() { mActive = true; }
stop()56     void stop() { mActive = false; }
57     void kill();
58 private:
59     static const int interval;
60     Antagonizer();
61     static int callbackThread(void* cookie);
62     Mutex                         mLock;
63     Condition                     mCondition;
64     bool                          mExit;
65     bool                          mActive;
66     sp<MediaPlayerBase::Listener> mListener;
67 };
68 #endif
69 
70 class MediaPlayerService : public BnMediaPlayerService
71 {
72     class Client;
73     typedef ::android::hardware::media::omx::V1_0::IOmx IOmx;
74 
75     class AudioOutput : public MediaPlayerBase::AudioSink
76     {
77         class CallbackData;
78 
79      public:
80                                 AudioOutput(audio_session_t sessionId, uid_t uid, int pid,
81                                         const audio_attributes_t * attr);
82         virtual                 ~AudioOutput();
83 
ready()84         virtual bool            ready() const { return mTrack != 0; }
85         virtual ssize_t         bufferSize() const;
86         virtual ssize_t         frameCount() const;
87         virtual ssize_t         channelCount() const;
88         virtual ssize_t         frameSize() const;
89         virtual uint32_t        latency() const;
90         virtual float           msecsPerFrame() const;
91         virtual status_t        getPosition(uint32_t *position) const;
92         virtual status_t        getTimestamp(AudioTimestamp &ts) const;
93         virtual int64_t         getPlayedOutDurationUs(int64_t nowUs) const;
94         virtual status_t        getFramesWritten(uint32_t *frameswritten) const;
95         virtual audio_session_t getSessionId() const;
96         virtual uint32_t        getSampleRate() const;
97         virtual int64_t         getBufferDurationInUs() const;
98 
99         virtual status_t        open(
100                 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
101                 audio_format_t format, int bufferCount,
102                 AudioCallback cb, void *cookie,
103                 audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
104                 const audio_offload_info_t *offloadInfo = NULL,
105                 bool doNotReconnect = false,
106                 uint32_t suggestedFrameCount = 0);
107 
108         virtual status_t        start();
109         virtual ssize_t         write(const void* buffer, size_t size, bool blocking = true);
110         virtual void            stop();
111         virtual void            flush();
112         virtual void            pause();
113         virtual void            close();
114                 void            setAudioStreamType(audio_stream_type_t streamType);
getAudioStreamType()115         virtual audio_stream_type_t getAudioStreamType() const { return mStreamType; }
116                 void            setAudioAttributes(const audio_attributes_t * attributes);
117 
118                 void            setVolume(float left, float right);
119         virtual status_t        setPlaybackRate(const AudioPlaybackRate& rate);
120         virtual status_t        getPlaybackRate(AudioPlaybackRate* rate /* nonnull */);
121 
122                 status_t        setAuxEffectSendLevel(float level);
123                 status_t        attachAuxEffect(int effectId);
124         virtual status_t        dump(int fd, const Vector<String16>& args) const;
125 
126         static bool             isOnEmulator();
127         static int              getMinBufferCount();
128                 void            setNextOutput(const sp<AudioOutput>& nextOutput);
129                 void            switchToNextOutput();
needsTrailingPadding()130         virtual bool            needsTrailingPadding() { return mNextOutput == NULL; }
131         virtual status_t        setParameters(const String8& keyValuePairs);
132         virtual String8         getParameters(const String8& keys);
133 
134         virtual VolumeShaper::Status applyVolumeShaper(
135                                         const sp<VolumeShaper::Configuration>& configuration,
136                                         const sp<VolumeShaper::Operation>& operation) override;
137         virtual sp<VolumeShaper::State> getVolumeShaperState(int id) override;
138 
139     private:
140         static void             setMinBufferCount();
141         static void             CallbackWrapper(
142                 int event, void *me, void *info);
143                void             deleteRecycledTrack_l();
144                void             close_l();
145            status_t             updateTrack();
146 
147         sp<AudioTrack>          mTrack;
148         sp<AudioTrack>          mRecycledTrack;
149         sp<AudioOutput>         mNextOutput;
150         AudioCallback           mCallback;
151         void *                  mCallbackCookie;
152         CallbackData *          mCallbackData;
153         audio_stream_type_t     mStreamType;
154         audio_attributes_t *    mAttributes;
155         float                   mLeftVolume;
156         float                   mRightVolume;
157         AudioPlaybackRate       mPlaybackRate;
158         uint32_t                mSampleRateHz; // sample rate of the content, as set in open()
159         float                   mMsecsPerFrame;
160         size_t                  mFrameSize;
161         audio_session_t         mSessionId;
162         uid_t                   mUid;
163         int                     mPid;
164         float                   mSendLevel;
165         int                     mAuxEffectId;
166         audio_output_flags_t    mFlags;
167         sp<VolumeHandler>       mVolumeHandler;
168         mutable Mutex           mLock;
169 
170         // static variables below not protected by mutex
171         static bool             mIsOnEmulator;
172         static int              mMinBufferCount;  // 12 for emulator; otherwise 4
173 
174         // CallbackData is what is passed to the AudioTrack as the "user" data.
175         // We need to be able to target this to a different Output on the fly,
176         // so we can't use the Output itself for this.
177         class CallbackData {
178             friend AudioOutput;
179         public:
CallbackData(AudioOutput * cookie)180             explicit CallbackData(AudioOutput *cookie) {
181                 mData = cookie;
182                 mSwitching = false;
183             }
getOutput()184             AudioOutput *   getOutput() const { return mData; }
setOutput(AudioOutput * newcookie)185             void            setOutput(AudioOutput* newcookie) { mData = newcookie; }
186             // lock/unlock are used by the callback before accessing the payload of this object
lock()187             void            lock() const { mLock.lock(); }
unlock()188             void            unlock() const { mLock.unlock(); }
189 
190             // tryBeginTrackSwitch/endTrackSwitch are used when the CallbackData is handed over
191             // to the next sink.
192 
193             // tryBeginTrackSwitch() returns true only if it obtains the lock.
tryBeginTrackSwitch()194             bool            tryBeginTrackSwitch() {
195                 LOG_ALWAYS_FATAL_IF(mSwitching, "tryBeginTrackSwitch() already called");
196                 if (mLock.tryLock() != OK) {
197                     return false;
198                 }
199                 mSwitching = true;
200                 return true;
201             }
endTrackSwitch()202             void            endTrackSwitch() {
203                 if (mSwitching) {
204                     mLock.unlock();
205                 }
206                 mSwitching = false;
207             }
208         private:
209             AudioOutput *   mData;
210             mutable Mutex   mLock; // a recursive mutex might make this unnecessary.
211             bool            mSwitching;
212             DISALLOW_EVIL_CONSTRUCTORS(CallbackData);
213         };
214 
215     }; // AudioOutput
216 
217 public:
218     static  void                instantiate();
219 
220     // IMediaPlayerService interface
221     virtual sp<IMediaRecorder>  createMediaRecorder(const String16 &opPackageName);
222     void    removeMediaRecorderClient(const wp<MediaRecorderClient>& client);
223     virtual sp<IMediaMetadataRetriever> createMetadataRetriever();
224 
225     virtual sp<IMediaPlayer>    create(const sp<IMediaPlayerClient>& client,
226                                        audio_session_t audioSessionId);
227 
228     virtual sp<IMediaCodecList> getCodecList() const;
229     virtual sp<IOMX>            getOMX();
230     virtual sp<IHDCP>           makeHDCP(bool createEncryptionModule);
231 
232     virtual sp<IRemoteDisplay> listenForRemoteDisplay(const String16 &opPackageName,
233             const sp<IRemoteDisplayClient>& client, const String8& iface);
234     virtual status_t            dump(int fd, const Vector<String16>& args);
235 
236             void                removeClient(const wp<Client>& client);
237             bool                hasClient(wp<Client> client);
238 
239     enum {
240         MEDIASERVER_PROCESS_DEATH = 0,
241         MEDIAEXTRACTOR_PROCESS_DEATH = 1,
242         MEDIACODEC_PROCESS_DEATH = 2,
243         AUDIO_PROCESS_DEATH = 3,   // currently no need to track this
244         CAMERA_PROCESS_DEATH = 4
245     };
246 
247     // Collect info of the codec usage from media player and media recorder
248     virtual void                addBatteryData(uint32_t params);
249     // API for the Battery app to pull the data of codecs usage
250     virtual status_t            pullBatteryData(Parcel* reply);
251 private:
252     struct BatteryTracker {
253         BatteryTracker();
254         // Collect info of the codec usage from media player and media recorder
255         void addBatteryData(uint32_t params);
256         // API for the Battery app to pull the data of codecs usage
257         status_t pullBatteryData(Parcel* reply);
258 
259     private:
260         // For battery usage tracking purpose
261         struct BatteryUsageInfo {
262             // how many streams are being played by one UID
263             int     refCount;
264             // a temp variable to store the duration(ms) of audio codecs
265             // when we start a audio codec, we minus the system time from audioLastTime
266             // when we pause it, we add the system time back to the audioLastTime
267             // so after the pause, audioLastTime = pause time - start time
268             // if multiple audio streams are played (or recorded), then audioLastTime
269             // = the total playing time of all the streams
270             int32_t audioLastTime;
271             // when all the audio streams are being paused, we assign audioLastTime to
272             // this variable, so this value could be provided to the battery app
273             // in the next pullBatteryData call
274             int32_t audioTotalTime;
275 
276             int32_t videoLastTime;
277             int32_t videoTotalTime;
278         };
279         KeyedVector<int, BatteryUsageInfo>    mBatteryData;
280 
281         enum {
282             SPEAKER,
283             OTHER_AUDIO_DEVICE,
284             SPEAKER_AND_OTHER,
285             NUM_AUDIO_DEVICES
286         };
287 
288         struct BatteryAudioFlingerUsageInfo {
289             int refCount; // how many audio streams are being played
290             int deviceOn[NUM_AUDIO_DEVICES]; // whether the device is currently used
291             int32_t lastTime[NUM_AUDIO_DEVICES]; // in ms
292             // totalTime[]: total time of audio output devices usage
293             int32_t totalTime[NUM_AUDIO_DEVICES]; // in ms
294         };
295 
296         // This varialble is used to record the usage of audio output device
297         // for battery app
298         BatteryAudioFlingerUsageInfo mBatteryAudio;
299 
300         mutable Mutex mLock;
301     };
302     BatteryTracker mBatteryTracker;
303 
304     class Client : public BnMediaPlayer {
305         // IMediaPlayer interface
306         virtual void            disconnect();
307         virtual status_t        setVideoSurfaceTexture(
308                                         const sp<IGraphicBufferProducer>& bufferProducer);
309         virtual status_t        setBufferingSettings(const BufferingSettings& buffering) override;
310         virtual status_t        getDefaultBufferingSettings(
311                                         BufferingSettings* buffering /* nonnull */) override;
312         virtual status_t        prepareAsync();
313         virtual status_t        start();
314         virtual status_t        stop();
315         virtual status_t        pause();
316         virtual status_t        isPlaying(bool* state);
317         virtual status_t        setPlaybackSettings(const AudioPlaybackRate& rate);
318         virtual status_t        getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */);
319         virtual status_t        setSyncSettings(const AVSyncSettings& rate, float videoFpsHint);
320         virtual status_t        getSyncSettings(AVSyncSettings* rate /* nonnull */,
321                                                 float* videoFps /* nonnull */);
322         virtual status_t        seekTo(
323                 int msec,
324                 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC);
325         virtual status_t        getCurrentPosition(int* msec);
326         virtual status_t        getDuration(int* msec);
327         virtual status_t        reset();
328         virtual status_t        setAudioStreamType(audio_stream_type_t type);
329         virtual status_t        setLooping(int loop);
330         virtual status_t        setVolume(float leftVolume, float rightVolume);
331         virtual status_t        invoke(const Parcel& request, Parcel *reply);
332         virtual status_t        setMetadataFilter(const Parcel& filter);
333         virtual status_t        getMetadata(bool update_only,
334                                             bool apply_filter,
335                                             Parcel *reply);
336         virtual status_t        setAuxEffectSendLevel(float level);
337         virtual status_t        attachAuxEffect(int effectId);
338         virtual status_t        setParameter(int key, const Parcel &request);
339         virtual status_t        getParameter(int key, Parcel *reply);
340         virtual status_t        setRetransmitEndpoint(const struct sockaddr_in* endpoint);
341         virtual status_t        getRetransmitEndpoint(struct sockaddr_in* endpoint);
342         virtual status_t        setNextPlayer(const sp<IMediaPlayer>& player);
343 
344         virtual VolumeShaper::Status applyVolumeShaper(
345                                         const sp<VolumeShaper::Configuration>& configuration,
346                                         const sp<VolumeShaper::Operation>& operation) override;
347         virtual sp<VolumeShaper::State> getVolumeShaperState(int id) override;
348 
349         sp<MediaPlayerBase>     createPlayer(player_type playerType);
350 
351         virtual status_t        setDataSource(
352                         const sp<IMediaHTTPService> &httpService,
353                         const char *url,
354                         const KeyedVector<String8, String8> *headers);
355 
356         virtual status_t        setDataSource(int fd, int64_t offset, int64_t length);
357 
358         virtual status_t        setDataSource(const sp<IStreamSource> &source);
359         virtual status_t        setDataSource(const sp<IDataSource> &source);
360 
361 
362         sp<MediaPlayerBase>     setDataSource_pre(player_type playerType);
363         status_t                setDataSource_post(const sp<MediaPlayerBase>& p,
364                                                    status_t status);
365 
366                 void            notify(int msg, int ext1, int ext2, const Parcel *obj);
367 
pid()368                 pid_t           pid() const { return mPid; }
369         virtual status_t        dump(int fd, const Vector<String16>& args);
370 
getAudioSessionId()371                 audio_session_t getAudioSessionId() { return mAudioSessionId; }
372         // Modular DRM
373         virtual status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId);
374         virtual status_t releaseDrm();
375 
376     private:
377         class ServiceDeathNotifier:
378                 public IBinder::DeathRecipient,
379                 public ::android::hardware::hidl_death_recipient
380         {
381         public:
382             ServiceDeathNotifier(
383                     const sp<IBinder>& service,
384                     const sp<MediaPlayerBase>& listener,
385                     int which);
386             ServiceDeathNotifier(
387                     const sp<IOmx>& omx,
388                     const sp<MediaPlayerBase>& listener,
389                     int which);
390             virtual ~ServiceDeathNotifier();
391             virtual void binderDied(const wp<IBinder>& who);
392             virtual void serviceDied(
393                     uint64_t cookie,
394                     const wp<::android::hidl::base::V1_0::IBase>& who);
395             void unlinkToDeath();
396 
397         private:
398             int mWhich;
399             sp<IBinder> mService;
400             sp<IOmx> mOmx;
401             wp<MediaPlayerBase> mListener;
402         };
403 
404         void clearDeathNotifiers_l();
405 
406         friend class MediaPlayerService;
407                                 Client( const sp<MediaPlayerService>& service,
408                                         pid_t pid,
409                                         int32_t connId,
410                                         const sp<IMediaPlayerClient>& client,
411                                         audio_session_t audioSessionId,
412                                         uid_t uid);
413                                 Client();
414         virtual                 ~Client();
415 
416                 void            deletePlayer();
417 
getPlayer()418         sp<MediaPlayerBase>     getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
419 
420 
421 
422         // @param type Of the metadata to be tested.
423         // @return true if the metadata should be dropped according to
424         //              the filters.
425         bool shouldDropMetadata(media::Metadata::Type type) const;
426 
427         // Add a new element to the set of metadata updated. Noop if
428         // the element exists already.
429         // @param type Of the metadata to be recorded.
430         void addNewMetadataUpdate(media::Metadata::Type type);
431 
432         // Disconnect from the currently connected ANativeWindow.
433         void disconnectNativeWindow_l();
434 
435         status_t setAudioAttributes_l(const Parcel &request);
436 
437         class Listener : public MediaPlayerBase::Listener {
438         public:
Listener(const wp<Client> & client)439             Listener(const wp<Client> &client) : mClient(client) {}
~Listener()440             virtual ~Listener() {}
notify(int msg,int ext1,int ext2,const Parcel * obj)441             virtual void notify(int msg, int ext1, int ext2, const Parcel *obj) {
442                 sp<Client> client = mClient.promote();
443                 if (client != NULL) {
444                     client->notify(msg, ext1, ext2, obj);
445                 }
446             }
447         private:
448             wp<Client> mClient;
449         };
450 
451         mutable     Mutex                         mLock;
452                     sp<MediaPlayerBase>           mPlayer;
453                     sp<MediaPlayerService>        mService;
454                     sp<IMediaPlayerClient>        mClient;
455                     sp<AudioOutput>               mAudioOutput;
456                     pid_t                         mPid;
457                     status_t                      mStatus;
458                     bool                          mLoop;
459                     int32_t                       mConnId;
460                     audio_session_t               mAudioSessionId;
461                     audio_attributes_t *          mAudioAttributes;
462                     uid_t                         mUid;
463                     sp<ANativeWindow>             mConnectedWindow;
464                     sp<IBinder>                   mConnectedWindowBinder;
465                     struct sockaddr_in            mRetransmitEndpoint;
466                     bool                          mRetransmitEndpointValid;
467                     sp<Client>                    mNextClient;
468                     sp<MediaPlayerBase::Listener> mListener;
469 
470         // Metadata filters.
471         media::Metadata::Filter mMetadataAllow;  // protected by mLock
472         media::Metadata::Filter mMetadataDrop;  // protected by mLock
473 
474         // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
475         // notification we try to update mMetadataUpdated which is a
476         // set: no duplicate.
477         // getMetadata clears this set.
478         media::Metadata::Filter mMetadataUpdated;  // protected by mLock
479 
480         sp<ServiceDeathNotifier> mExtractorDeathListener;
481         sp<ServiceDeathNotifier> mCodecDeathListener;
482 #if CALLBACK_ANTAGONIZER
483                     Antagonizer*                  mAntagonizer;
484 #endif
485     }; // Client
486 
487 // ----------------------------------------------------------------------------
488 
489                             MediaPlayerService();
490     virtual                 ~MediaPlayerService();
491 
492     mutable     Mutex                       mLock;
493                 SortedVector< wp<Client> >  mClients;
494                 SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients;
495                 int32_t                     mNextConnId;
496                 sp<IOMX>                    mOMX;
497 };
498 
499 // ----------------------------------------------------------------------------
500 
501 }; // namespace android
502 
503 #endif // ANDROID_MEDIAPLAYERSERVICE_H
504