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/Log.h>
24#include <utils/threads.h>
25#include <utils/List.h>
26#include <utils/Errors.h>
27#include <utils/KeyedVector.h>
28#include <utils/String8.h>
29#include <utils/Vector.h>
30
31#include <media/IMediaPlayerService.h>
32#include <media/MediaPlayerInterface.h>
33#include <media/Metadata.h>
34#include <media/stagefright/foundation/ABase.h>
35
36#include <system/audio.h>
37
38namespace android {
39
40class AudioTrack;
41class IMediaRecorder;
42class IMediaMetadataRetriever;
43class IOMX;
44class IRemoteDisplay;
45class IRemoteDisplayClient;
46class MediaRecorderClient;
47
48#define CALLBACK_ANTAGONIZER 0
49#if CALLBACK_ANTAGONIZER
50class Antagonizer {
51public:
52    Antagonizer(notify_callback_f cb, void* client);
53    void start() { mActive = true; }
54    void stop() { mActive = false; }
55    void kill();
56private:
57    static const int interval;
58    Antagonizer();
59    static int callbackThread(void* cookie);
60    Mutex               mLock;
61    Condition           mCondition;
62    bool                mExit;
63    bool                mActive;
64    void*               mClient;
65    notify_callback_f   mCb;
66};
67#endif
68
69class MediaPlayerService : public BnMediaPlayerService
70{
71    class Client;
72
73    class AudioOutput : public MediaPlayerBase::AudioSink
74    {
75        class CallbackData;
76
77     public:
78                                AudioOutput(int sessionId);
79        virtual                 ~AudioOutput();
80
81        virtual bool            ready() const { return mTrack != NULL; }
82        virtual bool            realtime() const { return true; }
83        virtual ssize_t         bufferSize() const;
84        virtual ssize_t         frameCount() const;
85        virtual ssize_t         channelCount() const;
86        virtual ssize_t         frameSize() const;
87        virtual uint32_t        latency() const;
88        virtual float           msecsPerFrame() const;
89        virtual status_t        getPosition(uint32_t *position) const;
90        virtual status_t        getFramesWritten(uint32_t *frameswritten) const;
91        virtual int             getSessionId() 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
99        virtual void            start();
100        virtual ssize_t         write(const void* buffer, size_t size);
101        virtual void            stop();
102        virtual void            flush();
103        virtual void            pause();
104        virtual void            close();
105                void            setAudioStreamType(audio_stream_type_t streamType) { mStreamType = streamType; }
106                void            setVolume(float left, float right);
107        virtual status_t        setPlaybackRatePermille(int32_t ratePermille);
108                status_t        setAuxEffectSendLevel(float level);
109                status_t        attachAuxEffect(int effectId);
110        virtual status_t        dump(int fd, const Vector<String16>& args) const;
111
112        static bool             isOnEmulator();
113        static int              getMinBufferCount();
114                void            setNextOutput(const sp<AudioOutput>& nextOutput);
115                void            switchToNextOutput();
116        virtual bool            needsTrailingPadding() { return mNextOutput == NULL; }
117
118    private:
119        static void             setMinBufferCount();
120        static void             CallbackWrapper(
121                int event, void *me, void *info);
122
123        AudioTrack*             mTrack;
124        AudioTrack*             mRecycledTrack;
125        sp<AudioOutput>         mNextOutput;
126        AudioCallback           mCallback;
127        void *                  mCallbackCookie;
128        CallbackData *          mCallbackData;
129        uint64_t                mBytesWritten;
130        audio_stream_type_t     mStreamType;
131        float                   mLeftVolume;
132        float                   mRightVolume;
133        int32_t                 mPlaybackRatePermille;
134        uint32_t                mSampleRateHz; // sample rate of the content, as set in open()
135        float                   mMsecsPerFrame;
136        int                     mSessionId;
137        float                   mSendLevel;
138        int                     mAuxEffectId;
139        static bool             mIsOnEmulator;
140        static int              mMinBufferCount;  // 12 for emulator; otherwise 4
141        audio_output_flags_t    mFlags;
142
143        // CallbackData is what is passed to the AudioTrack as the "user" data.
144        // We need to be able to target this to a different Output on the fly,
145        // so we can't use the Output itself for this.
146        class CallbackData {
147        public:
148            CallbackData(AudioOutput *cookie) {
149                mData = cookie;
150                mSwitching = false;
151            }
152            AudioOutput *   getOutput() { return mData;}
153            void            setOutput(AudioOutput* newcookie) { mData = newcookie; }
154            // lock/unlock are used by the callback before accessing the payload of this object
155            void            lock() { mLock.lock(); }
156            void            unlock() { mLock.unlock(); }
157            // beginTrackSwitch/endTrackSwitch are used when this object is being handed over
158            // to the next sink.
159            void            beginTrackSwitch() { mLock.lock(); mSwitching = true; }
160            void            endTrackSwitch() {
161                if (mSwitching) {
162                    mLock.unlock();
163                }
164                mSwitching = false;
165            }
166        private:
167            AudioOutput *   mData;
168            mutable Mutex   mLock;
169            bool            mSwitching;
170            DISALLOW_EVIL_CONSTRUCTORS(CallbackData);
171        };
172
173    }; // AudioOutput
174
175
176    class AudioCache : public MediaPlayerBase::AudioSink
177    {
178    public:
179                                AudioCache(const char* name);
180        virtual                 ~AudioCache() {}
181
182        virtual bool            ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); }
183        virtual bool            realtime() const { return false; }
184        virtual ssize_t         bufferSize() const { return frameSize() * mFrameCount; }
185        virtual ssize_t         frameCount() const { return mFrameCount; }
186        virtual ssize_t         channelCount() const { return (ssize_t)mChannelCount; }
187        virtual ssize_t         frameSize() const { return ssize_t(mChannelCount * ((mFormat == AUDIO_FORMAT_PCM_16_BIT)?sizeof(int16_t):sizeof(u_int8_t))); }
188        virtual uint32_t        latency() const;
189        virtual float           msecsPerFrame() const;
190        virtual status_t        getPosition(uint32_t *position) const;
191        virtual status_t        getFramesWritten(uint32_t *frameswritten) const;
192        virtual int             getSessionId() const;
193
194        virtual status_t        open(
195                uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
196                audio_format_t format, int bufferCount = 1,
197                AudioCallback cb = NULL, void *cookie = NULL,
198                audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE);
199
200        virtual void            start();
201        virtual ssize_t         write(const void* buffer, size_t size);
202        virtual void            stop();
203        virtual void            flush() {}
204        virtual void            pause() {}
205        virtual void            close() {}
206                void            setAudioStreamType(audio_stream_type_t streamType) {}
207                void            setVolume(float left, float right) {}
208        virtual status_t        setPlaybackRatePermille(int32_t ratePermille) { return INVALID_OPERATION; }
209                uint32_t        sampleRate() const { return mSampleRate; }
210                audio_format_t  format() const { return mFormat; }
211                size_t          size() const { return mSize; }
212                status_t        wait();
213
214                sp<IMemoryHeap> getHeap() const { return mHeap; }
215
216        static  void            notify(void* cookie, int msg,
217                                       int ext1, int ext2, const Parcel *obj);
218        virtual status_t        dump(int fd, const Vector<String16>& args) const;
219
220    private:
221                                AudioCache();
222
223        Mutex               mLock;
224        Condition           mSignal;
225        sp<MemoryHeapBase>  mHeap;
226        float               mMsecsPerFrame;
227        uint16_t            mChannelCount;
228        audio_format_t      mFormat;
229        ssize_t             mFrameCount;
230        uint32_t            mSampleRate;
231        uint32_t            mSize;
232        int                 mError;
233        bool                mCommandComplete;
234
235        sp<Thread>          mCallbackThread;
236    }; // AudioCache
237
238public:
239    static  void                instantiate();
240
241    // IMediaPlayerService interface
242    virtual sp<IMediaRecorder>  createMediaRecorder(pid_t pid);
243    void    removeMediaRecorderClient(wp<MediaRecorderClient> client);
244    virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid);
245
246    virtual sp<IMediaPlayer>    create(pid_t pid, const sp<IMediaPlayerClient>& client, int audioSessionId);
247
248    virtual sp<IMemory>         decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat);
249    virtual sp<IMemory>         decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat);
250    virtual sp<IOMX>            getOMX();
251    virtual sp<ICrypto>         makeCrypto();
252    virtual sp<IHDCP>           makeHDCP();
253
254    virtual sp<IRemoteDisplay> listenForRemoteDisplay(const sp<IRemoteDisplayClient>& client,
255            const String8& iface);
256    virtual status_t            dump(int fd, const Vector<String16>& args);
257
258            void                removeClient(wp<Client> client);
259
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    // Collect info of the codec usage from media player and media recorder
301    virtual void                addBatteryData(uint32_t params);
302    // API for the Battery app to pull the data of codecs usage
303    virtual status_t            pullBatteryData(Parcel* reply);
304private:
305
306    class Client : public BnMediaPlayer {
307        // IMediaPlayer interface
308        virtual void            disconnect();
309        virtual status_t        setVideoSurfaceTexture(
310                                        const sp<ISurfaceTexture>& surfaceTexture);
311        virtual status_t        prepareAsync();
312        virtual status_t        start();
313        virtual status_t        stop();
314        virtual status_t        pause();
315        virtual status_t        isPlaying(bool* state);
316        virtual status_t        seekTo(int msec);
317        virtual status_t        getCurrentPosition(int* msec);
318        virtual status_t        getDuration(int* msec);
319        virtual status_t        reset();
320        virtual status_t        setAudioStreamType(audio_stream_type_t type);
321        virtual status_t        setLooping(int loop);
322        virtual status_t        setVolume(float leftVolume, float rightVolume);
323        virtual status_t        invoke(const Parcel& request, Parcel *reply);
324        virtual status_t        setMetadataFilter(const Parcel& filter);
325        virtual status_t        getMetadata(bool update_only,
326                                            bool apply_filter,
327                                            Parcel *reply);
328        virtual status_t        setAuxEffectSendLevel(float level);
329        virtual status_t        attachAuxEffect(int effectId);
330        virtual status_t        setParameter(int key, const Parcel &request);
331        virtual status_t        getParameter(int key, Parcel *reply);
332        virtual status_t        setRetransmitEndpoint(const struct sockaddr_in* endpoint);
333        virtual status_t        getRetransmitEndpoint(struct sockaddr_in* endpoint);
334        virtual status_t        setNextPlayer(const sp<IMediaPlayer>& player);
335
336        sp<MediaPlayerBase>     createPlayer(player_type playerType);
337
338        virtual status_t        setDataSource(
339                        const char *url,
340                        const KeyedVector<String8, String8> *headers);
341
342        virtual status_t        setDataSource(int fd, int64_t offset, int64_t length);
343
344        virtual status_t        setDataSource(const sp<IStreamSource> &source);
345
346        sp<MediaPlayerBase>     setDataSource_pre(player_type playerType);
347        void                    setDataSource_post(const sp<MediaPlayerBase>& p,
348                                                   status_t status);
349
350        static  void            notify(void* cookie, int msg,
351                                       int ext1, int ext2, const Parcel *obj);
352
353                pid_t           pid() const { return mPid; }
354        virtual status_t        dump(int fd, const Vector<String16>& args) const;
355
356                int             getAudioSessionId() { return mAudioSessionId; }
357
358    private:
359        friend class MediaPlayerService;
360                                Client( const sp<MediaPlayerService>& service,
361                                        pid_t pid,
362                                        int32_t connId,
363                                        const sp<IMediaPlayerClient>& client,
364                                        int audioSessionId,
365                                        uid_t uid);
366                                Client();
367        virtual                 ~Client();
368
369                void            deletePlayer();
370
371        sp<MediaPlayerBase>     getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
372
373
374
375        // @param type Of the metadata to be tested.
376        // @return true if the metadata should be dropped according to
377        //              the filters.
378        bool shouldDropMetadata(media::Metadata::Type type) const;
379
380        // Add a new element to the set of metadata updated. Noop if
381        // the element exists already.
382        // @param type Of the metadata to be recorded.
383        void addNewMetadataUpdate(media::Metadata::Type type);
384
385        // Disconnect from the currently connected ANativeWindow.
386        void disconnectNativeWindow();
387
388        mutable     Mutex                       mLock;
389                    sp<MediaPlayerBase>         mPlayer;
390                    sp<MediaPlayerService>      mService;
391                    sp<IMediaPlayerClient>      mClient;
392                    sp<AudioOutput>             mAudioOutput;
393                    pid_t                       mPid;
394                    status_t                    mStatus;
395                    bool                        mLoop;
396                    int32_t                     mConnId;
397                    int                         mAudioSessionId;
398                    uid_t                       mUID;
399                    sp<ANativeWindow>           mConnectedWindow;
400                    sp<IBinder>                 mConnectedWindowBinder;
401                    struct sockaddr_in          mRetransmitEndpoint;
402                    bool                        mRetransmitEndpointValid;
403                    sp<Client>                  mNextClient;
404
405        // Metadata filters.
406        media::Metadata::Filter mMetadataAllow;  // protected by mLock
407        media::Metadata::Filter mMetadataDrop;  // protected by mLock
408
409        // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
410        // notification we try to update mMetadataUpdated which is a
411        // set: no duplicate.
412        // getMetadata clears this set.
413        media::Metadata::Filter mMetadataUpdated;  // protected by mLock
414
415#if CALLBACK_ANTAGONIZER
416                    Antagonizer*                mAntagonizer;
417#endif
418    }; // Client
419
420// ----------------------------------------------------------------------------
421
422                            MediaPlayerService();
423    virtual                 ~MediaPlayerService();
424
425    mutable     Mutex                       mLock;
426                SortedVector< wp<Client> >  mClients;
427                SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients;
428                int32_t                     mNextConnId;
429                sp<IOMX>                    mOMX;
430                sp<ICrypto>                 mCrypto;
431};
432
433// ----------------------------------------------------------------------------
434
435}; // namespace android
436
437#endif // ANDROID_MEDIAPLAYERSERVICE_H
438