MediaPlayerService.h revision 22612345ae97648d5b6db43cc1aa30aad77f7bba
11d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi/*
21d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi**
31d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi** Copyright 2008, The Android Open Source Project
41d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi**
51d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi** Licensed under the Apache License, Version 2.0 (the "License");
61d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi** you may not use this file except in compliance with the License.
71d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi** You may obtain a copy of the License at
81d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi**
91d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi**     http://www.apache.org/licenses/LICENSE-2.0
101d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi**
111d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi** Unless required by applicable law or agreed to in writing, software
121d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi** distributed under the License is distributed on an "AS IS" BASIS,
131d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
141d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi** See the License for the specific language governing permissions and
151d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi** limitations under the License.
161d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi*/
171d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi
181d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi#ifndef ANDROID_MEDIAPLAYERSERVICE_H
198bad3fec0f54959958352c0bef90b813cd0d823adestradaa#define ANDROID_MEDIAPLAYERSERVICE_H
201d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi
211d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi#include <arpa/inet.h>
221d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi
231d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi#include <utils/threads.h>
241d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi#include <utils/Errors.h>
251d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi#include <utils/KeyedVector.h>
261d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi#include <utils/String8.h>
271d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi#include <utils/Vector.h>
281d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi
291d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi#include <media/MediaPlayerInterface.h>
309ff7d2235427b211344fa58b608424805a21aa24Peng Xu#include <media/Metadata.h>
311d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi#include <media/stagefright/foundation/ABase.h>
32b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
33b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi#include <system/audio.h>
341d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi
358bad3fec0f54959958352c0bef90b813cd0d823adestradaanamespace android {
368bad3fec0f54959958352c0bef90b813cd0d823adestradaa
378bad3fec0f54959958352c0bef90b813cd0d823adestradaaclass AudioTrack;
381d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshiclass IMediaRecorder;
39b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshiclass IMediaMetadataRetriever;
40b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshiclass IOMX;
41b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshiclass IRemoteDisplay;
42b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshiclass IRemoteDisplayClient;
43b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshiclass MediaRecorderClient;
44b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
45b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi#define CALLBACK_ANTAGONIZER 0
46b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi#if CALLBACK_ANTAGONIZER
47b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshiclass Antagonizer {
48b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshipublic:
49b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    Antagonizer(notify_callback_f cb, void* client);
50b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    void start() { mActive = true; }
51b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    void stop() { mActive = false; }
52b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    void kill();
53b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshiprivate:
54b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    static const int interval;
55b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    Antagonizer();
569ff7d2235427b211344fa58b608424805a21aa24Peng Xu    static int callbackThread(void* cookie);
579ff7d2235427b211344fa58b608424805a21aa24Peng Xu    Mutex               mLock;
581d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi    Condition           mCondition;
591d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi    bool                mExit;
609ff7d2235427b211344fa58b608424805a21aa24Peng Xu    bool                mActive;
619ff7d2235427b211344fa58b608424805a21aa24Peng Xu    void*               mClient;
629ff7d2235427b211344fa58b608424805a21aa24Peng Xu    notify_callback_f   mCb;
63b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi};
64b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi#endif
65b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
66b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshiclass MediaPlayerService : public BnMediaPlayerService
679ff7d2235427b211344fa58b608424805a21aa24Peng Xu{
689ff7d2235427b211344fa58b608424805a21aa24Peng Xu    class Client;
699ff7d2235427b211344fa58b608424805a21aa24Peng Xu
70b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    class AudioOutput : public MediaPlayerBase::AudioSink
711d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi    {
729ff7d2235427b211344fa58b608424805a21aa24Peng Xu        class CallbackData;
73b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
749ff7d2235427b211344fa58b608424805a21aa24Peng Xu     public:
759ff7d2235427b211344fa58b608424805a21aa24Peng Xu                                AudioOutput(int sessionId, int uid);
769ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual                 ~AudioOutput();
771d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi
789ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual bool            ready() const { return mTrack != 0; }
79b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        virtual bool            realtime() const { return true; }
808bad3fec0f54959958352c0bef90b813cd0d823adestradaa        virtual ssize_t         bufferSize() const;
81b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        virtual ssize_t         frameCount() const;
82b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        virtual ssize_t         channelCount() const;
831d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        virtual ssize_t         frameSize() const;
849ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual uint32_t        latency() const;
851d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        virtual float           msecsPerFrame() const;
861d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        virtual status_t        getPosition(uint32_t *position) const;
871d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        virtual status_t        getFramesWritten(uint32_t *frameswritten) const;
889ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual int             getSessionId() const;
898bad3fec0f54959958352c0bef90b813cd0d823adestradaa
909ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual status_t        open(
911d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi                uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
929ff7d2235427b211344fa58b608424805a21aa24Peng Xu                audio_format_t format, int bufferCount,
939ff7d2235427b211344fa58b608424805a21aa24Peng Xu                AudioCallback cb, void *cookie,
949ff7d2235427b211344fa58b608424805a21aa24Peng Xu                audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
959ff7d2235427b211344fa58b608424805a21aa24Peng Xu                const audio_offload_info_t *offloadInfo = NULL);
961d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi
971d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        virtual status_t        start();
989ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual ssize_t         write(const void* buffer, size_t size);
999ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual void            stop();
1001d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        virtual void            flush();
1019ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual void            pause();
1029ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual void            close();
1038bad3fec0f54959958352c0bef90b813cd0d823adestradaa                void            setAudioStreamType(audio_stream_type_t streamType) {
1049ff7d2235427b211344fa58b608424805a21aa24Peng Xu                                                                        mStreamType = streamType; }
1059ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual audio_stream_type_t getAudioStreamType() const { return mStreamType; }
1069ff7d2235427b211344fa58b608424805a21aa24Peng Xu
1071d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi                void            setVolume(float left, float right);
1081d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        virtual status_t        setPlaybackRatePermille(int32_t ratePermille);
1099ff7d2235427b211344fa58b608424805a21aa24Peng Xu                status_t        setAuxEffectSendLevel(float level);
1109ff7d2235427b211344fa58b608424805a21aa24Peng Xu                status_t        attachAuxEffect(int effectId);
1111d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        virtual status_t        dump(int fd, const Vector<String16>& args) const;
1129ff7d2235427b211344fa58b608424805a21aa24Peng Xu
1139ff7d2235427b211344fa58b608424805a21aa24Peng Xu        static bool             isOnEmulator();
1148bad3fec0f54959958352c0bef90b813cd0d823adestradaa        static int              getMinBufferCount();
1159ff7d2235427b211344fa58b608424805a21aa24Peng Xu                void            setNextOutput(const sp<AudioOutput>& nextOutput);
1161d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi                void            switchToNextOutput();
1179ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual bool            needsTrailingPadding() { return mNextOutput == NULL; }
1189ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual status_t        setParameters(const String8& keyValuePairs);
1199ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual String8         getParameters(const String8& keys);
1201d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi
1219ff7d2235427b211344fa58b608424805a21aa24Peng Xu    private:
122b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        static void             setMinBufferCount();
123b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        static void             CallbackWrapper(
124b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                int event, void *me, void *info);
125b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi               void             deleteRecycledTrack();
126b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
127b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        sp<AudioTrack>          mTrack;
128b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        sp<AudioTrack>          mRecycledTrack;
129b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        sp<AudioOutput>         mNextOutput;
130b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        AudioCallback           mCallback;
131b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        void *                  mCallbackCookie;
132b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        CallbackData *          mCallbackData;
1339ff7d2235427b211344fa58b608424805a21aa24Peng Xu        uint64_t                mBytesWritten;
1341d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        audio_stream_type_t     mStreamType;
1358bad3fec0f54959958352c0bef90b813cd0d823adestradaa        float                   mLeftVolume;
1368bad3fec0f54959958352c0bef90b813cd0d823adestradaa        float                   mRightVolume;
1378bad3fec0f54959958352c0bef90b813cd0d823adestradaa        int32_t                 mPlaybackRatePermille;
1389ff7d2235427b211344fa58b608424805a21aa24Peng Xu        uint32_t                mSampleRateHz; // sample rate of the content, as set in open()
1399ff7d2235427b211344fa58b608424805a21aa24Peng Xu        float                   mMsecsPerFrame;
1408bad3fec0f54959958352c0bef90b813cd0d823adestradaa        int                     mSessionId;
1411d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        int                     mUid;
1421d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        float                   mSendLevel;
1439ff7d2235427b211344fa58b608424805a21aa24Peng Xu        int                     mAuxEffectId;
144b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        static bool             mIsOnEmulator;
145b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        static int              mMinBufferCount;  // 12 for emulator; otherwise 4
146b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        audio_output_flags_t    mFlags;
147b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
148b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        // CallbackData is what is passed to the AudioTrack as the "user" data.
149b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        // We need to be able to target this to a different Output on the fly,
150b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        // so we can't use the Output itself for this.
151b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        class CallbackData {
152b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        public:
1531d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi            CallbackData(AudioOutput *cookie) {
154b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                mData = cookie;
155b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                mSwitching = false;
1568bad3fec0f54959958352c0bef90b813cd0d823adestradaa            }
1571d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi            AudioOutput *   getOutput() { return mData;}
1589ff7d2235427b211344fa58b608424805a21aa24Peng Xu            void            setOutput(AudioOutput* newcookie) { mData = newcookie; }
1598bad3fec0f54959958352c0bef90b813cd0d823adestradaa            // lock/unlock are used by the callback before accessing the payload of this object
1608bad3fec0f54959958352c0bef90b813cd0d823adestradaa            void            lock() { mLock.lock(); }
1618bad3fec0f54959958352c0bef90b813cd0d823adestradaa            void            unlock() { mLock.unlock(); }
1629ff7d2235427b211344fa58b608424805a21aa24Peng Xu            // beginTrackSwitch/endTrackSwitch are used when this object is being handed over
1639ff7d2235427b211344fa58b608424805a21aa24Peng Xu            // to the next sink.
1649ff7d2235427b211344fa58b608424805a21aa24Peng Xu            void            beginTrackSwitch() { mLock.lock(); mSwitching = true; }
1659ff7d2235427b211344fa58b608424805a21aa24Peng Xu            void            endTrackSwitch() {
1669ff7d2235427b211344fa58b608424805a21aa24Peng Xu                if (mSwitching) {
1679ff7d2235427b211344fa58b608424805a21aa24Peng Xu                    mLock.unlock();
1689ff7d2235427b211344fa58b608424805a21aa24Peng Xu                }
1699ff7d2235427b211344fa58b608424805a21aa24Peng Xu                mSwitching = false;
1701d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi            }
1719ff7d2235427b211344fa58b608424805a21aa24Peng Xu        private:
1729ff7d2235427b211344fa58b608424805a21aa24Peng Xu            AudioOutput *   mData;
1738bad3fec0f54959958352c0bef90b813cd0d823adestradaa            mutable Mutex   mLock;
1749ff7d2235427b211344fa58b608424805a21aa24Peng Xu            bool            mSwitching;
1759ff7d2235427b211344fa58b608424805a21aa24Peng Xu            DISALLOW_EVIL_CONSTRUCTORS(CallbackData);
1769ff7d2235427b211344fa58b608424805a21aa24Peng Xu        };
1779ff7d2235427b211344fa58b608424805a21aa24Peng Xu
1781d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi    }; // AudioOutput
179b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
1809ff7d2235427b211344fa58b608424805a21aa24Peng Xu
1811d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi    class AudioCache : public MediaPlayerBase::AudioSink
1829ff7d2235427b211344fa58b608424805a21aa24Peng Xu    {
1831d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi    public:
1849ff7d2235427b211344fa58b608424805a21aa24Peng Xu                                AudioCache(const sp<IMemoryHeap>& heap);
1859ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual                 ~AudioCache() {}
1869ff7d2235427b211344fa58b608424805a21aa24Peng Xu
1871d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        virtual bool            ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); }
1881d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        virtual bool            realtime() const { return false; }
1899ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual ssize_t         bufferSize() const { return frameSize() * mFrameCount; }
1909ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual ssize_t         frameCount() const { return mFrameCount; }
1911d1ac5409597ef668e52c6f27293eaf9af575e33Ashutosh Joshi        virtual ssize_t         channelCount() const { return (ssize_t)mChannelCount; }
1929ff7d2235427b211344fa58b608424805a21aa24Peng Xu        virtual ssize_t         frameSize() const { return ssize_t(mChannelCount * ((mFormat == AUDIO_FORMAT_PCM_16_BIT)?sizeof(int16_t):sizeof(u_int8_t))); }
1938bad3fec0f54959958352c0bef90b813cd0d823adestradaa        virtual uint32_t        latency() const;
1948bad3fec0f54959958352c0bef90b813cd0d823adestradaa        virtual float           msecsPerFrame() const;
1958bad3fec0f54959958352c0bef90b813cd0d823adestradaa        virtual status_t        getPosition(uint32_t *position) const;
196b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        virtual status_t        getFramesWritten(uint32_t *frameswritten) const;
197b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        virtual int             getSessionId() const;
198b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
199b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        virtual status_t        open(
200b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
201b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                audio_format_t format, int bufferCount = 1,
2029ff7d2235427b211344fa58b608424805a21aa24Peng Xu                AudioCallback cb = NULL, void *cookie = NULL,
2039ff7d2235427b211344fa58b608424805a21aa24Peng Xu                audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
2049ff7d2235427b211344fa58b608424805a21aa24Peng Xu                const audio_offload_info_t *offloadInfo = NULL);
2058bad3fec0f54959958352c0bef90b813cd0d823adestradaa
2068bad3fec0f54959958352c0bef90b813cd0d823adestradaa        virtual status_t        start();
2078bad3fec0f54959958352c0bef90b813cd0d823adestradaa        virtual ssize_t         write(const void* buffer, size_t size);
2088bad3fec0f54959958352c0bef90b813cd0d823adestradaa        virtual void            stop();
2092c697fb4a8b8f8c0a2acf1fb15027f363f74c2dcAshutosh Joshi        virtual void            flush() {}
210b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        virtual void            pause() {}
211b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        virtual void            close() {}
212b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                void            setAudioStreamType(audio_stream_type_t streamType) {}
213b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                // stream type is not used for AudioCache
214b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        virtual audio_stream_type_t getAudioStreamType() const { return AUDIO_STREAM_DEFAULT; }
215b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
216b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                void            setVolume(float left, float right) {}
217b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        virtual status_t        setPlaybackRatePermille(int32_t ratePermille) { return INVALID_OPERATION; }
218b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                uint32_t        sampleRate() const { return mSampleRate; }
219b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                audio_format_t  format() const { return mFormat; }
220b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                size_t          size() const { return mSize; }
221b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                status_t        wait();
222b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
223b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                sp<IMemoryHeap> getHeap() const { return mHeap; }
224b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
225b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        static  void            notify(void* cookie, int msg,
226b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                                       int ext1, int ext2, const Parcel *obj);
227b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        virtual status_t        dump(int fd, const Vector<String16>& args) const;
228b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
229b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    private:
230b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi                                AudioCache();
231b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
232b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        Mutex               mLock;
233b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        Condition           mSignal;
234b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        sp<IMemoryHeap>     mHeap;
235b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        float               mMsecsPerFrame;
236b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        uint16_t            mChannelCount;
237b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        audio_format_t      mFormat;
238b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        ssize_t             mFrameCount;
239b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        uint32_t            mSampleRate;
240b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        uint32_t            mSize;
241b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        int                 mError;
242b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        bool                mCommandComplete;
243b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
244b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi        sp<Thread>          mCallbackThread;
245b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    }; // AudioCache
246b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
247b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshipublic:
248b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    static  void                instantiate();
249b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
250b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    // IMediaPlayerService interface
251b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    virtual sp<IMediaRecorder>  createMediaRecorder();
252b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    void    removeMediaRecorderClient(wp<MediaRecorderClient> client);
253b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    virtual sp<IMediaMetadataRetriever> createMetadataRetriever();
254b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
255b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    virtual sp<IMediaPlayer>    create(const sp<IMediaPlayerClient>& client, int audioSessionId);
256b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi
257b741e3b374e7eebf96e2104dec5caccf723b2a39Ashutosh Joshi    virtual status_t            decode(const char* url, uint32_t *pSampleRate, int* pNumChannels,
258                                       audio_format_t* pFormat,
259                                       const sp<IMemoryHeap>& heap, size_t *pSize);
260    virtual status_t            decode(int fd, int64_t offset, int64_t length,
261                                       uint32_t *pSampleRate, int* pNumChannels,
262                                       audio_format_t* pFormat,
263                                       const sp<IMemoryHeap>& heap, size_t *pSize);
264    virtual sp<IOMX>            getOMX();
265    virtual sp<ICrypto>         makeCrypto();
266    virtual sp<IDrm>            makeDrm();
267    virtual sp<IHDCP>           makeHDCP(bool createEncryptionModule);
268
269    virtual sp<IRemoteDisplay> listenForRemoteDisplay(const sp<IRemoteDisplayClient>& client,
270            const String8& iface);
271    virtual status_t            dump(int fd, const Vector<String16>& args);
272
273    virtual status_t        updateProxyConfig(
274            const char *host, int32_t port, const char *exclusionList);
275
276            void                removeClient(wp<Client> client);
277
278    // For battery usage tracking purpose
279    struct BatteryUsageInfo {
280        // how many streams are being played by one UID
281        int     refCount;
282        // a temp variable to store the duration(ms) of audio codecs
283        // when we start a audio codec, we minus the system time from audioLastTime
284        // when we pause it, we add the system time back to the audioLastTime
285        // so after the pause, audioLastTime = pause time - start time
286        // if multiple audio streams are played (or recorded), then audioLastTime
287        // = the total playing time of all the streams
288        int32_t audioLastTime;
289        // when all the audio streams are being paused, we assign audioLastTime to
290        // this variable, so this value could be provided to the battery app
291        // in the next pullBatteryData call
292        int32_t audioTotalTime;
293
294        int32_t videoLastTime;
295        int32_t videoTotalTime;
296    };
297    KeyedVector<int, BatteryUsageInfo>    mBatteryData;
298
299    enum {
300        SPEAKER,
301        OTHER_AUDIO_DEVICE,
302        SPEAKER_AND_OTHER,
303        NUM_AUDIO_DEVICES
304    };
305
306    struct BatteryAudioFlingerUsageInfo {
307        int refCount; // how many audio streams are being played
308        int deviceOn[NUM_AUDIO_DEVICES]; // whether the device is currently used
309        int32_t lastTime[NUM_AUDIO_DEVICES]; // in ms
310        // totalTime[]: total time of audio output devices usage
311        int32_t totalTime[NUM_AUDIO_DEVICES]; // in ms
312    };
313
314    // This varialble is used to record the usage of audio output device
315    // for battery app
316    BatteryAudioFlingerUsageInfo mBatteryAudio;
317
318    // Collect info of the codec usage from media player and media recorder
319    virtual void                addBatteryData(uint32_t params);
320    // API for the Battery app to pull the data of codecs usage
321    virtual status_t            pullBatteryData(Parcel* reply);
322private:
323
324    class Client : public BnMediaPlayer {
325        // IMediaPlayer interface
326        virtual void            disconnect();
327        virtual status_t        setVideoSurfaceTexture(
328                                        const sp<IGraphicBufferProducer>& bufferProducer);
329        virtual status_t        prepareAsync();
330        virtual status_t        start();
331        virtual status_t        stop();
332        virtual status_t        pause();
333        virtual status_t        isPlaying(bool* state);
334        virtual status_t        seekTo(int msec);
335        virtual status_t        getCurrentPosition(int* msec);
336        virtual status_t        getDuration(int* msec);
337        virtual status_t        reset();
338        virtual status_t        setAudioStreamType(audio_stream_type_t type);
339        virtual status_t        setLooping(int loop);
340        virtual status_t        setVolume(float leftVolume, float rightVolume);
341        virtual status_t        invoke(const Parcel& request, Parcel *reply);
342        virtual status_t        setMetadataFilter(const Parcel& filter);
343        virtual status_t        getMetadata(bool update_only,
344                                            bool apply_filter,
345                                            Parcel *reply);
346        virtual status_t        setAuxEffectSendLevel(float level);
347        virtual status_t        attachAuxEffect(int effectId);
348        virtual status_t        setParameter(int key, const Parcel &request);
349        virtual status_t        getParameter(int key, Parcel *reply);
350        virtual status_t        setRetransmitEndpoint(const struct sockaddr_in* endpoint);
351        virtual status_t        getRetransmitEndpoint(struct sockaddr_in* endpoint);
352        virtual status_t        setNextPlayer(const sp<IMediaPlayer>& player);
353
354        sp<MediaPlayerBase>     createPlayer(player_type playerType);
355
356        virtual status_t        setDataSource(
357                        const char *url,
358                        const KeyedVector<String8, String8> *headers);
359
360        virtual status_t        setDataSource(int fd, int64_t offset, int64_t length);
361
362        virtual status_t        setDataSource(const sp<IStreamSource> &source);
363
364        sp<MediaPlayerBase>     setDataSource_pre(player_type playerType);
365        void                    setDataSource_post(const sp<MediaPlayerBase>& p,
366                                                   status_t status);
367
368        static  void            notify(void* cookie, int msg,
369                                       int ext1, int ext2, const Parcel *obj);
370
371                pid_t           pid() const { return mPid; }
372        virtual status_t        dump(int fd, const Vector<String16>& args) const;
373
374                int             getAudioSessionId() { return mAudioSessionId; }
375
376    private:
377        friend class MediaPlayerService;
378                                Client( const sp<MediaPlayerService>& service,
379                                        pid_t pid,
380                                        int32_t connId,
381                                        const sp<IMediaPlayerClient>& client,
382                                        int audioSessionId,
383                                        uid_t uid);
384                                Client();
385        virtual                 ~Client();
386
387                void            deletePlayer();
388
389        sp<MediaPlayerBase>     getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
390
391
392
393        // @param type Of the metadata to be tested.
394        // @return true if the metadata should be dropped according to
395        //              the filters.
396        bool shouldDropMetadata(media::Metadata::Type type) const;
397
398        // Add a new element to the set of metadata updated. Noop if
399        // the element exists already.
400        // @param type Of the metadata to be recorded.
401        void addNewMetadataUpdate(media::Metadata::Type type);
402
403        // Disconnect from the currently connected ANativeWindow.
404        void disconnectNativeWindow();
405
406        mutable     Mutex                       mLock;
407                    sp<MediaPlayerBase>         mPlayer;
408                    sp<MediaPlayerService>      mService;
409                    sp<IMediaPlayerClient>      mClient;
410                    sp<AudioOutput>             mAudioOutput;
411                    pid_t                       mPid;
412                    status_t                    mStatus;
413                    bool                        mLoop;
414                    int32_t                     mConnId;
415                    int                         mAudioSessionId;
416                    uid_t                       mUID;
417                    sp<ANativeWindow>           mConnectedWindow;
418                    sp<IBinder>                 mConnectedWindowBinder;
419                    struct sockaddr_in          mRetransmitEndpoint;
420                    bool                        mRetransmitEndpointValid;
421                    sp<Client>                  mNextClient;
422
423        // Metadata filters.
424        media::Metadata::Filter mMetadataAllow;  // protected by mLock
425        media::Metadata::Filter mMetadataDrop;  // protected by mLock
426
427        // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
428        // notification we try to update mMetadataUpdated which is a
429        // set: no duplicate.
430        // getMetadata clears this set.
431        media::Metadata::Filter mMetadataUpdated;  // protected by mLock
432
433#if CALLBACK_ANTAGONIZER
434                    Antagonizer*                mAntagonizer;
435#endif
436    }; // Client
437
438// ----------------------------------------------------------------------------
439
440                            MediaPlayerService();
441    virtual                 ~MediaPlayerService();
442
443    mutable     Mutex                       mLock;
444                SortedVector< wp<Client> >  mClients;
445                SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients;
446                int32_t                     mNextConnId;
447                sp<IOMX>                    mOMX;
448                sp<ICrypto>                 mCrypto;
449};
450
451// ----------------------------------------------------------------------------
452
453}; // namespace android
454
455#endif // ANDROID_MEDIAPLAYERSERVICE_H
456