MediaPlayerService.h revision ee0a0e39acdcf8f97e0d6945c31ff36a06a36e9d
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 <system/audio.h>
34
35namespace android {
36
37class AudioTrack;
38class IMediaRecorder;
39class IMediaMetadataRetriever;
40class IOMX;
41class IRemoteDisplay;
42class IRemoteDisplayClient;
43class MediaRecorderClient;
44
45#define CALLBACK_ANTAGONIZER 0
46#if CALLBACK_ANTAGONIZER
47class Antagonizer {
48public:
49    Antagonizer(notify_callback_f cb, void* client);
50    void start() { mActive = true; }
51    void stop() { mActive = false; }
52    void kill();
53private:
54    static const int interval;
55    Antagonizer();
56    static int callbackThread(void* cookie);
57    Mutex               mLock;
58    Condition           mCondition;
59    bool                mExit;
60    bool                mActive;
61    void*               mClient;
62    notify_callback_f   mCb;
63};
64#endif
65
66class MediaPlayerService : public BnMediaPlayerService
67{
68    class Client;
69
70    class AudioOutput : public MediaPlayerBase::AudioSink
71    {
72        class CallbackData;
73
74     public:
75                                AudioOutput(int sessionId, int uid, int pid,
76                                        const audio_attributes_t * attr);
77        virtual                 ~AudioOutput();
78
79        virtual bool            ready() const { return mTrack != 0; }
80        virtual bool            realtime() const { return true; }
81        virtual ssize_t         bufferSize() const;
82        virtual ssize_t         frameCount() const;
83        virtual ssize_t         channelCount() const;
84        virtual ssize_t         frameSize() const;
85        virtual uint32_t        latency() const;
86        virtual float           msecsPerFrame() const;
87        virtual status_t        getPosition(uint32_t *position) const;
88        virtual status_t        getTimestamp(AudioTimestamp &ts) const;
89        virtual status_t        getFramesWritten(uint32_t *frameswritten) const;
90        virtual int             getSessionId() const;
91        virtual uint32_t        getSampleRate() const;
92
93        virtual status_t        open(
94                uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
95                audio_format_t format, int bufferCount,
96                AudioCallback cb, void *cookie,
97                audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
98                const audio_offload_info_t *offloadInfo = NULL);
99
100        virtual status_t        start();
101        virtual ssize_t         write(const void* buffer, size_t size);
102        virtual void            stop();
103        virtual void            flush();
104        virtual void            pause();
105        virtual void            close();
106                void            setAudioStreamType(audio_stream_type_t streamType) {
107                                                                        mStreamType = streamType; }
108        virtual audio_stream_type_t getAudioStreamType() const { return mStreamType; }
109                void            setAudioAttributes(const audio_attributes_t * attributes);
110
111                void            setVolume(float left, float right);
112        virtual status_t        setPlaybackRatePermille(int32_t ratePermille);
113                status_t        setAuxEffectSendLevel(float level);
114                status_t        attachAuxEffect(int effectId);
115        virtual status_t        dump(int fd, const Vector<String16>& args) const;
116
117        static bool             isOnEmulator();
118        static int              getMinBufferCount();
119                void            setNextOutput(const sp<AudioOutput>& nextOutput);
120                void            switchToNextOutput();
121        virtual bool            needsTrailingPadding() { return mNextOutput == NULL; }
122        virtual status_t        setParameters(const String8& keyValuePairs);
123        virtual String8         getParameters(const String8& keys);
124
125    private:
126        static void             setMinBufferCount();
127        static void             CallbackWrapper(
128                int event, void *me, void *info);
129               void             deleteRecycledTrack();
130
131        sp<AudioTrack>          mTrack;
132        sp<AudioTrack>          mRecycledTrack;
133        sp<AudioOutput>         mNextOutput;
134        AudioCallback           mCallback;
135        void *                  mCallbackCookie;
136        CallbackData *          mCallbackData;
137        uint64_t                mBytesWritten;
138        audio_stream_type_t     mStreamType;
139        const audio_attributes_t *mAttributes;
140        float                   mLeftVolume;
141        float                   mRightVolume;
142        int32_t                 mPlaybackRatePermille;
143        uint32_t                mSampleRateHz; // sample rate of the content, as set in open()
144        float                   mMsecsPerFrame;
145        int                     mSessionId;
146        int                     mUid;
147        int                     mPid;
148        float                   mSendLevel;
149        int                     mAuxEffectId;
150        static bool             mIsOnEmulator;
151        static int              mMinBufferCount;  // 12 for emulator; otherwise 4
152        audio_output_flags_t    mFlags;
153
154        // CallbackData is what is passed to the AudioTrack as the "user" data.
155        // We need to be able to target this to a different Output on the fly,
156        // so we can't use the Output itself for this.
157        class CallbackData {
158        public:
159            CallbackData(AudioOutput *cookie) {
160                mData = cookie;
161                mSwitching = false;
162            }
163            AudioOutput *   getOutput() { return mData;}
164            void            setOutput(AudioOutput* newcookie) { mData = newcookie; }
165            // lock/unlock are used by the callback before accessing the payload of this object
166            void            lock() { mLock.lock(); }
167            void            unlock() { mLock.unlock(); }
168            // beginTrackSwitch/endTrackSwitch are used when this object is being handed over
169            // to the next sink.
170            void            beginTrackSwitch() { mLock.lock(); mSwitching = true; }
171            void            endTrackSwitch() {
172                if (mSwitching) {
173                    mLock.unlock();
174                }
175                mSwitching = false;
176            }
177        private:
178            AudioOutput *   mData;
179            mutable Mutex   mLock;
180            bool            mSwitching;
181            DISALLOW_EVIL_CONSTRUCTORS(CallbackData);
182        };
183
184    }; // AudioOutput
185
186
187    class AudioCache : public MediaPlayerBase::AudioSink
188    {
189    public:
190                                AudioCache(const sp<IMemoryHeap>& heap);
191        virtual                 ~AudioCache() {}
192
193        virtual bool            ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); }
194        virtual bool            realtime() const { return false; }
195        virtual ssize_t         bufferSize() const { return frameSize() * mFrameCount; }
196        virtual ssize_t         frameCount() const { return mFrameCount; }
197        virtual ssize_t         channelCount() const { return (ssize_t)mChannelCount; }
198        virtual ssize_t         frameSize() const { return (ssize_t)mFrameSize; }
199        virtual uint32_t        latency() const;
200        virtual float           msecsPerFrame() const;
201        virtual status_t        getPosition(uint32_t *position) const;
202        virtual status_t        getTimestamp(AudioTimestamp &ts) const;
203        virtual status_t        getFramesWritten(uint32_t *frameswritten) const;
204        virtual int             getSessionId() const;
205        virtual uint32_t        getSampleRate() const;
206
207        virtual status_t        open(
208                uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
209                audio_format_t format, int bufferCount = 1,
210                AudioCallback cb = NULL, void *cookie = NULL,
211                audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
212                const audio_offload_info_t *offloadInfo = NULL);
213
214        virtual status_t        start();
215        virtual ssize_t         write(const void* buffer, size_t size);
216        virtual void            stop();
217        virtual void            flush() {}
218        virtual void            pause() {}
219        virtual void            close() {}
220                void            setAudioStreamType(audio_stream_type_t streamType __unused) {}
221                // stream type is not used for AudioCache
222        virtual audio_stream_type_t getAudioStreamType() const { return AUDIO_STREAM_DEFAULT; }
223
224                void            setVolume(float left __unused, float right __unused) {}
225        virtual status_t        setPlaybackRatePermille(int32_t ratePermille __unused) { return INVALID_OPERATION; }
226                uint32_t        sampleRate() const { return mSampleRate; }
227                audio_format_t  format() const { return mFormat; }
228                size_t          size() const { return mSize; }
229                status_t        wait();
230
231                sp<IMemoryHeap> getHeap() const { return mHeap; }
232
233        static  void            notify(void* cookie, int msg,
234                                       int ext1, int ext2, const Parcel *obj);
235        virtual status_t        dump(int fd, const Vector<String16>& args) const;
236
237    private:
238                                AudioCache();
239
240        Mutex               mLock;
241        Condition           mSignal;
242        sp<IMemoryHeap>     mHeap;
243        float               mMsecsPerFrame;
244        uint16_t            mChannelCount;
245        audio_format_t      mFormat;
246        ssize_t             mFrameCount;
247        uint32_t            mSampleRate;
248        uint32_t            mSize;
249        size_t              mFrameSize;
250        int                 mError;
251        bool                mCommandComplete;
252
253        sp<Thread>          mCallbackThread;
254    }; // AudioCache
255
256public:
257    static  void                instantiate();
258
259    // IMediaPlayerService interface
260    virtual sp<IMediaRecorder>  createMediaRecorder();
261    void    removeMediaRecorderClient(wp<MediaRecorderClient> client);
262    virtual sp<IMediaMetadataRetriever> createMetadataRetriever();
263
264    virtual sp<IMediaPlayer>    create(const sp<IMediaPlayerClient>& client, int audioSessionId);
265
266    virtual status_t            decode(
267            const sp<IMediaHTTPService> &httpService,
268            const char* url,
269            uint32_t *pSampleRate,
270            int* pNumChannels,
271            audio_format_t* pFormat,
272            const sp<IMemoryHeap>& heap,
273            size_t *pSize);
274
275    virtual status_t            decode(int fd, int64_t offset, int64_t length,
276                                       uint32_t *pSampleRate, int* pNumChannels,
277                                       audio_format_t* pFormat,
278                                       const sp<IMemoryHeap>& heap, size_t *pSize);
279    virtual sp<IMediaCodecList> getCodecList() const;
280    virtual sp<IOMX>            getOMX();
281    virtual sp<ICrypto>         makeCrypto();
282    virtual sp<IDrm>            makeDrm();
283    virtual sp<IHDCP>           makeHDCP(bool createEncryptionModule);
284
285    virtual sp<IRemoteDisplay> listenForRemoteDisplay(const sp<IRemoteDisplayClient>& client,
286            const String8& iface);
287    virtual status_t            dump(int fd, const Vector<String16>& args);
288
289            void                removeClient(wp<Client> client);
290            bool                hasClient(wp<Client> client);
291
292    // For battery usage tracking purpose
293    struct BatteryUsageInfo {
294        // how many streams are being played by one UID
295        int     refCount;
296        // a temp variable to store the duration(ms) of audio codecs
297        // when we start a audio codec, we minus the system time from audioLastTime
298        // when we pause it, we add the system time back to the audioLastTime
299        // so after the pause, audioLastTime = pause time - start time
300        // if multiple audio streams are played (or recorded), then audioLastTime
301        // = the total playing time of all the streams
302        int32_t audioLastTime;
303        // when all the audio streams are being paused, we assign audioLastTime to
304        // this variable, so this value could be provided to the battery app
305        // in the next pullBatteryData call
306        int32_t audioTotalTime;
307
308        int32_t videoLastTime;
309        int32_t videoTotalTime;
310    };
311    KeyedVector<int, BatteryUsageInfo>    mBatteryData;
312
313    enum {
314        SPEAKER,
315        OTHER_AUDIO_DEVICE,
316        SPEAKER_AND_OTHER,
317        NUM_AUDIO_DEVICES
318    };
319
320    struct BatteryAudioFlingerUsageInfo {
321        int refCount; // how many audio streams are being played
322        int deviceOn[NUM_AUDIO_DEVICES]; // whether the device is currently used
323        int32_t lastTime[NUM_AUDIO_DEVICES]; // in ms
324        // totalTime[]: total time of audio output devices usage
325        int32_t totalTime[NUM_AUDIO_DEVICES]; // in ms
326    };
327
328    // This varialble is used to record the usage of audio output device
329    // for battery app
330    BatteryAudioFlingerUsageInfo mBatteryAudio;
331
332    // Collect info of the codec usage from media player and media recorder
333    virtual void                addBatteryData(uint32_t params);
334    // API for the Battery app to pull the data of codecs usage
335    virtual status_t            pullBatteryData(Parcel* reply);
336private:
337
338    class Client : public BnMediaPlayer {
339        // IMediaPlayer interface
340        virtual void            disconnect();
341        virtual status_t        setVideoSurfaceTexture(
342                                        const sp<IGraphicBufferProducer>& bufferProducer);
343        virtual status_t        prepareAsync();
344        virtual status_t        start();
345        virtual status_t        stop();
346        virtual status_t        pause();
347        virtual status_t        isPlaying(bool* state);
348        virtual status_t        seekTo(int msec);
349        virtual status_t        getCurrentPosition(int* msec);
350        virtual status_t        getDuration(int* msec);
351        virtual status_t        reset();
352        virtual status_t        setAudioStreamType(audio_stream_type_t type);
353        virtual status_t        setLooping(int loop);
354        virtual status_t        setVolume(float leftVolume, float rightVolume);
355        virtual status_t        invoke(const Parcel& request, Parcel *reply);
356        virtual status_t        setMetadataFilter(const Parcel& filter);
357        virtual status_t        getMetadata(bool update_only,
358                                            bool apply_filter,
359                                            Parcel *reply);
360        virtual status_t        setAuxEffectSendLevel(float level);
361        virtual status_t        attachAuxEffect(int effectId);
362        virtual status_t        setParameter(int key, const Parcel &request);
363        virtual status_t        getParameter(int key, Parcel *reply);
364        virtual status_t        setRetransmitEndpoint(const struct sockaddr_in* endpoint);
365        virtual status_t        getRetransmitEndpoint(struct sockaddr_in* endpoint);
366        virtual status_t        setNextPlayer(const sp<IMediaPlayer>& player);
367
368        sp<MediaPlayerBase>     createPlayer(player_type playerType);
369
370        virtual status_t        setDataSource(
371                        const sp<IMediaHTTPService> &httpService,
372                        const char *url,
373                        const KeyedVector<String8, String8> *headers);
374
375        virtual status_t        setDataSource(int fd, int64_t offset, int64_t length);
376
377        virtual status_t        setDataSource(const sp<IStreamSource> &source);
378
379        sp<MediaPlayerBase>     setDataSource_pre(player_type playerType);
380        void                    setDataSource_post(const sp<MediaPlayerBase>& p,
381                                                   status_t status);
382
383        static  void            notify(void* cookie, int msg,
384                                       int ext1, int ext2, const Parcel *obj);
385
386                pid_t           pid() const { return mPid; }
387        virtual status_t        dump(int fd, const Vector<String16>& args) const;
388
389                int             getAudioSessionId() { return mAudioSessionId; }
390
391    private:
392        friend class MediaPlayerService;
393                                Client( const sp<MediaPlayerService>& service,
394                                        pid_t pid,
395                                        int32_t connId,
396                                        const sp<IMediaPlayerClient>& client,
397                                        int audioSessionId,
398                                        uid_t uid);
399                                Client();
400        virtual                 ~Client();
401
402                void            deletePlayer();
403
404        sp<MediaPlayerBase>     getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
405
406
407
408        // @param type Of the metadata to be tested.
409        // @return true if the metadata should be dropped according to
410        //              the filters.
411        bool shouldDropMetadata(media::Metadata::Type type) const;
412
413        // Add a new element to the set of metadata updated. Noop if
414        // the element exists already.
415        // @param type Of the metadata to be recorded.
416        void addNewMetadataUpdate(media::Metadata::Type type);
417
418        // Disconnect from the currently connected ANativeWindow.
419        void disconnectNativeWindow();
420
421        status_t setAudioAttributes_l(const Parcel &request);
422
423        mutable     Mutex                       mLock;
424                    sp<MediaPlayerBase>         mPlayer;
425                    sp<MediaPlayerService>      mService;
426                    sp<IMediaPlayerClient>      mClient;
427                    sp<AudioOutput>             mAudioOutput;
428                    pid_t                       mPid;
429                    status_t                    mStatus;
430                    bool                        mLoop;
431                    int32_t                     mConnId;
432                    int                         mAudioSessionId;
433                    audio_attributes_t *        mAudioAttributes;
434                    uid_t                       mUID;
435                    sp<ANativeWindow>           mConnectedWindow;
436                    sp<IBinder>                 mConnectedWindowBinder;
437                    struct sockaddr_in          mRetransmitEndpoint;
438                    bool                        mRetransmitEndpointValid;
439                    sp<Client>                  mNextClient;
440
441        // Metadata filters.
442        media::Metadata::Filter mMetadataAllow;  // protected by mLock
443        media::Metadata::Filter mMetadataDrop;  // protected by mLock
444
445        // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
446        // notification we try to update mMetadataUpdated which is a
447        // set: no duplicate.
448        // getMetadata clears this set.
449        media::Metadata::Filter mMetadataUpdated;  // protected by mLock
450
451#if CALLBACK_ANTAGONIZER
452                    Antagonizer*                mAntagonizer;
453#endif
454    }; // Client
455
456// ----------------------------------------------------------------------------
457
458                            MediaPlayerService();
459    virtual                 ~MediaPlayerService();
460
461    mutable     Mutex                       mLock;
462                SortedVector< wp<Client> >  mClients;
463                SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients;
464                int32_t                     mNextConnId;
465                sp<IOMX>                    mOMX;
466                sp<ICrypto>                 mCrypto;
467};
468
469// ----------------------------------------------------------------------------
470
471}; // namespace android
472
473#endif // ANDROID_MEDIAPLAYERSERVICE_H
474