189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project/*
289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** Copyright 2008, The Android Open Source Project
489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License");
689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** you may not use this file except in compliance with the License.
789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** You may obtain a copy of the License at
889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**     http://www.apache.org/licenses/LICENSE-2.0
1089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project**
1189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** Unless required by applicable law or agreed to in writing, software
1289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** distributed under the License is distributed on an "AS IS" BASIS,
1389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** See the License for the specific language governing permissions and
1589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project** limitations under the License.
1689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project*/
1789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#ifndef ANDROID_MEDIAPLAYERSERVICE_H
1989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#define ANDROID_MEDIAPLAYERSERVICE_H
2089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
21c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossman#include <arpa/inet.h>
22c795b64060c3af9d7961fc1371e4ccfa8ee3e450John Grossman
23273d098cc72a88c797d01c7dff0c1350f315f05fMathias Agopian#include <utils/threads.h>
24273d098cc72a88c797d01c7dff0c1350f315f05fMathias Agopian#include <utils/Errors.h>
2589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <utils/KeyedVector.h>
262db8455d8f4468a637109d31f319ce02d9d743ecAndreas Huber#include <utils/String8.h>
27a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania#include <utils/Vector.h>
2889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#include <media/MediaPlayerInterface.h>
30a64c8c79af1a15911c55306d83a797fa50969f77niko#include <media/Metadata.h>
316b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen#include <media/stagefright/foundation/ABase.h>
3289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
3364760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
34fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
3589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectnamespace android {
3689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
37a3f1fa308728976fc9ca1b4f37d26e633b32b9acGlenn Kastenclass AudioTrack;
3889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectclass IMediaRecorder;
3989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectclass IMediaMetadataRetriever;
4020111aa043c5f404472bc63b90bc5aad906b1101Andreas Huberclass IOMX;
412013a54981d4ffb036dff279b88cc9f08c0ee1c2Jeff Brownclass IRemoteDisplay;
422013a54981d4ffb036dff279b88cc9f08c0ee1c2Jeff Brownclass IRemoteDisplayClient;
43dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wangclass MediaRecorderClient;
4489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
4589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#define CALLBACK_ANTAGONIZER 0
4689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#if CALLBACK_ANTAGONIZER
4789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectclass Antagonizer {
4889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectpublic:
4989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    Antagonizer(notify_callback_f cb, void* client);
5089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    void start() { mActive = true; }
5189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    void stop() { mActive = false; }
5289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    void kill();
5389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectprivate:
5489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    static const int interval;
5589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    Antagonizer();
5689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    static int callbackThread(void* cookie);
5789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    Mutex               mLock;
5889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    Condition           mCondition;
5989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    bool                mExit;
6089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    bool                mActive;
6189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    void*               mClient;
6289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    notify_callback_f   mCb;
6389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project};
6489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project#endif
6589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
6689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectclass MediaPlayerService : public BnMediaPlayerService
6789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project{
6889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    class Client;
6989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
7089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    class AudioOutput : public MediaPlayerBase::AudioSink
7189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    {
726b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen        class CallbackData;
736b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen
746b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen     public:
75d9d7fa0873796ac661c44a7fcd6ad5ff697ff01fJean-Michel Trivi                                AudioOutput(int sessionId, int uid, int pid,
76d9d7fa0873796ac661c44a7fcd6ad5ff697ff01fJean-Michel Trivi                                        const audio_attributes_t * attr);
7789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual                 ~AudioOutput();
7889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
792799d743ee2ae5a25fe869a7f9c052acc029559fGlenn Kasten        virtual bool            ready() const { return mTrack != 0; }
8089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual bool            realtime() const { return true; }
8189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual ssize_t         bufferSize() const;
8289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual ssize_t         frameCount() const;
8389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual ssize_t         channelCount() const;
8489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual ssize_t         frameSize() const;
8589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual uint32_t        latency() const;
8689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual float           msecsPerFrame() const;
874110c101c3d0dd8dbc44c8d2d0edd3e2e7d6652fMarco Nelissen        virtual status_t        getPosition(uint32_t *position) const;
8806ad1528e6dd4c866c085d3cad9235d2752eb3edLajos Molnar        virtual status_t        getTimestamp(AudioTimestamp &ts) const;
894110c101c3d0dd8dbc44c8d2d0edd3e2e7d6652fMarco Nelissen        virtual status_t        getFramesWritten(uint32_t *frameswritten) const;
904110c101c3d0dd8dbc44c8d2d0edd3e2e7d6652fMarco Nelissen        virtual int             getSessionId() const;
916f59db12a64f4496866952a251122ccb77a36c6bEric Laurent        virtual uint32_t        getSampleRate() const;
9220111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
9320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        virtual status_t        open(
94786618ffe881aceb64d65a6a2e2d76ede6e01ec0Jean-Michel Trivi                uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
95e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten                audio_format_t format, int bufferCount,
961948eb3ea6eee336e8cdab9b0c693f93f5f19993Eric Laurent                AudioCallback cb, void *cookie,
97ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
98ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                const audio_offload_info_t *offloadInfo = NULL);
9920111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
100d89532e133b881c7e0dac089333ad7642fc510f1Richard Fitzgerald        virtual status_t        start();
10189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual ssize_t         write(const void* buffer, size_t size);
10289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual void            stop();
10389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual void            flush();
10489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual void            pause();
10589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual void            close();
10622612345ae97648d5b6db43cc1aa30aad77f7bbaEric Laurent                void            setAudioStreamType(audio_stream_type_t streamType) {
10722612345ae97648d5b6db43cc1aa30aad77f7bbaEric Laurent                                                                        mStreamType = streamType; }
10822612345ae97648d5b6db43cc1aa30aad77f7bbaEric Laurent        virtual audio_stream_type_t getAudioStreamType() const { return mStreamType; }
109d9d7fa0873796ac661c44a7fcd6ad5ff697ff01fJean-Michel Trivi                void            setAudioAttributes(const audio_attributes_t * attributes);
11022612345ae97648d5b6db43cc1aa30aad77f7bbaEric Laurent
11189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                void            setVolume(float left, float right);
1127a8b0ed6419e57dd8b41d3806893d63d3df91aabJean-Michel Trivi        virtual status_t        setPlaybackRatePermille(int32_t ratePermille);
1132beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent                status_t        setAuxEffectSendLevel(float level);
1142beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent                status_t        attachAuxEffect(int effectId);
11589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        dump(int fd, const Vector<String16>& args) const;
11689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
11789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        static bool             isOnEmulator();
11889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        static int              getMinBufferCount();
1196b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen                void            setNextOutput(const sp<AudioOutput>& nextOutput);
1206b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen                void            switchToNextOutput();
1216b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen        virtual bool            needsTrailingPadding() { return mNextOutput == NULL; }
122d89532e133b881c7e0dac089333ad7642fc510f1Richard Fitzgerald        virtual status_t        setParameters(const String8& keyValuePairs);
123d89532e133b881c7e0dac089333ad7642fc510f1Richard Fitzgerald        virtual String8         getParameters(const String8& keys);
1246b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen
12589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    private:
12689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        static void             setMinBufferCount();
12720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        static void             CallbackWrapper(
128d217a8c4632b3e3065f8c2a26b9ce4dc4c97171fGlenn Kasten                int event, void *me, void *info);
129d89532e133b881c7e0dac089333ad7642fc510f1Richard Fitzgerald               void             deleteRecycledTrack();
13089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1312799d743ee2ae5a25fe869a7f9c052acc029559fGlenn Kasten        sp<AudioTrack>          mTrack;
1322799d743ee2ae5a25fe869a7f9c052acc029559fGlenn Kasten        sp<AudioTrack>          mRecycledTrack;
1336b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen        sp<AudioOutput>         mNextOutput;
13420111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        AudioCallback           mCallback;
13520111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        void *                  mCallbackCookie;
1366b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen        CallbackData *          mCallbackData;
1374110c101c3d0dd8dbc44c8d2d0edd3e2e7d6652fMarco Nelissen        uint64_t                mBytesWritten;
138fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        audio_stream_type_t     mStreamType;
139d9d7fa0873796ac661c44a7fcd6ad5ff697ff01fJean-Michel Trivi        const audio_attributes_t *mAttributes;
14089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        float                   mLeftVolume;
14189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        float                   mRightVolume;
1427a8b0ed6419e57dd8b41d3806893d63d3df91aabJean-Michel Trivi        int32_t                 mPlaybackRatePermille;
1437a8b0ed6419e57dd8b41d3806893d63d3df91aabJean-Michel Trivi        uint32_t                mSampleRateHz; // sample rate of the content, as set in open()
14489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        float                   mMsecsPerFrame;
145a514bdb58b5de4986679f72b7204b4764f7a2778Eric Laurent        int                     mSessionId;
1469cae217050aa1347d4ac5053c305754879e3f97fMarco Nelissen        int                     mUid;
147d457c970c8d08519cd77280a90b61ae1e342cfe3Marco Nelissen        int                     mPid;
1482beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent        float                   mSendLevel;
1492beeb50b1bba9e92f6cacfeca37fe9fa9d36ead1Eric Laurent        int                     mAuxEffectId;
15089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        static bool             mIsOnEmulator;
15189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        static int              mMinBufferCount;  // 12 for emulator; otherwise 4
1521948eb3ea6eee336e8cdab9b0c693f93f5f19993Eric Laurent        audio_output_flags_t    mFlags;
15389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
1546b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen        // CallbackData is what is passed to the AudioTrack as the "user" data.
1556b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen        // We need to be able to target this to a different Output on the fly,
1566b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen        // so we can't use the Output itself for this.
1576b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen        class CallbackData {
1586b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen        public:
1596b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            CallbackData(AudioOutput *cookie) {
1606b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen                mData = cookie;
1616b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen                mSwitching = false;
1626b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            }
1636b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            AudioOutput *   getOutput() { return mData;}
1646b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            void            setOutput(AudioOutput* newcookie) { mData = newcookie; }
1656b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            // lock/unlock are used by the callback before accessing the payload of this object
1666b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            void            lock() { mLock.lock(); }
1676b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            void            unlock() { mLock.unlock(); }
1686b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            // beginTrackSwitch/endTrackSwitch are used when this object is being handed over
1696b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            // to the next sink.
1706b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            void            beginTrackSwitch() { mLock.lock(); mSwitching = true; }
1716b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            void            endTrackSwitch() {
1726b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen                if (mSwitching) {
1736b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen                    mLock.unlock();
1746b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen                }
1756b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen                mSwitching = false;
1766b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            }
1776b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen        private:
1786b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            AudioOutput *   mData;
1796b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            mutable Mutex   mLock;
1806b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            bool            mSwitching;
1816b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen            DISALLOW_EVIL_CONSTRUCTORS(CallbackData);
1826b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen        };
1836b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen
1846b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen    }; // AudioOutput
1856b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen
18689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
18789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    class AudioCache : public MediaPlayerBase::AudioSink
18889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    {
18989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    public:
1903d00aa6de95fb46e36f2bab4e3facdf0b96acf06Eric Laurent                                AudioCache(const sp<IMemoryHeap>& heap);
19189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual                 ~AudioCache() {}
19289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
19389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual bool            ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); }
19489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual bool            realtime() const { return false; }
19589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual ssize_t         bufferSize() const { return frameSize() * mFrameCount; }
19689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual ssize_t         frameCount() const { return mFrameCount; }
19789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual ssize_t         channelCount() const { return (ssize_t)mChannelCount; }
198a31335a4ec96ba351f25f3b26fa79a78c2723a13Andy Hung        virtual ssize_t         frameSize() const { return (ssize_t)mFrameSize; }
19989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual uint32_t        latency() const;
20089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual float           msecsPerFrame() const;
2014110c101c3d0dd8dbc44c8d2d0edd3e2e7d6652fMarco Nelissen        virtual status_t        getPosition(uint32_t *position) const;
20206ad1528e6dd4c866c085d3cad9235d2752eb3edLajos Molnar        virtual status_t        getTimestamp(AudioTimestamp &ts) const;
2034110c101c3d0dd8dbc44c8d2d0edd3e2e7d6652fMarco Nelissen        virtual status_t        getFramesWritten(uint32_t *frameswritten) const;
2044110c101c3d0dd8dbc44c8d2d0edd3e2e7d6652fMarco Nelissen        virtual int             getSessionId() const;
2056f59db12a64f4496866952a251122ccb77a36c6bEric Laurent        virtual uint32_t        getSampleRate() const;
20620111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
20720111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber        virtual status_t        open(
208786618ffe881aceb64d65a6a2e2d76ede6e01ec0Jean-Michel Trivi                uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
209786618ffe881aceb64d65a6a2e2d76ede6e01ec0Jean-Michel Trivi                audio_format_t format, int bufferCount = 1,
2101948eb3ea6eee336e8cdab9b0c693f93f5f19993Eric Laurent                AudioCallback cb = NULL, void *cookie = NULL,
211ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE,
212ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                const audio_offload_info_t *offloadInfo = NULL);
21320111aa043c5f404472bc63b90bc5aad906b1101Andreas Huber
214d89532e133b881c7e0dac089333ad7642fc510f1Richard Fitzgerald        virtual status_t        start();
21589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual ssize_t         write(const void* buffer, size_t size);
2167d5b8a70c28c0d5746a600467b2887822dbff88eAndreas Huber        virtual void            stop();
21789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual void            flush() {}
21889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual void            pause() {}
21989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual void            close() {}
22080520381d0bdf3451bd17c9ddef73d3fe71c58c2Glenn Kasten                void            setAudioStreamType(audio_stream_type_t streamType __unused) {}
22122612345ae97648d5b6db43cc1aa30aad77f7bbaEric Laurent                // stream type is not used for AudioCache
22222612345ae97648d5b6db43cc1aa30aad77f7bbaEric Laurent        virtual audio_stream_type_t getAudioStreamType() const { return AUDIO_STREAM_DEFAULT; }
22322612345ae97648d5b6db43cc1aa30aad77f7bbaEric Laurent
22480520381d0bdf3451bd17c9ddef73d3fe71c58c2Glenn Kasten                void            setVolume(float left __unused, float right __unused) {}
22580520381d0bdf3451bd17c9ddef73d3fe71c58c2Glenn Kasten        virtual status_t        setPlaybackRatePermille(int32_t ratePermille __unused) { return INVALID_OPERATION; }
22689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                uint32_t        sampleRate() const { return mSampleRate; }
227e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten                audio_format_t  format() const { return mFormat; }
22889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                size_t          size() const { return mSize; }
22989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                status_t        wait();
23089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
23189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                sp<IMemoryHeap> getHeap() const { return mHeap; }
23289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
233b483c4724846c0b8d4e82afcbb7c17f671bae81cGloria Wang        static  void            notify(void* cookie, int msg,
234b483c4724846c0b8d4e82afcbb7c17f671bae81cGloria Wang                                       int ext1, int ext2, const Parcel *obj);
23589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        dump(int fd, const Vector<String16>& args) const;
23689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
23789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    private:
23889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project                                AudioCache();
23989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
24089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        Mutex               mLock;
24189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        Condition           mSignal;
2423d00aa6de95fb46e36f2bab4e3facdf0b96acf06Eric Laurent        sp<IMemoryHeap>     mHeap;
24389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        float               mMsecsPerFrame;
24489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint16_t            mChannelCount;
245e1c3962e268ffc12bfd1bd9ea84da1f135f36960Glenn Kasten        audio_format_t      mFormat;
24689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        ssize_t             mFrameCount;
24789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t            mSampleRate;
24889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        uint32_t            mSize;
249a31335a4ec96ba351f25f3b26fa79a78c2723a13Andy Hung        size_t              mFrameSize;
25089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        int                 mError;
25189fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        bool                mCommandComplete;
2527d5b8a70c28c0d5746a600467b2887822dbff88eAndreas Huber
2537d5b8a70c28c0d5746a600467b2887822dbff88eAndreas Huber        sp<Thread>          mCallbackThread;
2546b74d671a1321a6ecc4a40b6c87beedfecc1ec44Marco Nelissen    }; // AudioCache
25589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
25689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectpublic:
25789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    static  void                instantiate();
25889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
25989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    // IMediaPlayerService interface
260f37971f624164c3ed185d3f976404a6f60f49b9aGlenn Kasten    virtual sp<IMediaRecorder>  createMediaRecorder();
261dac6a31a33ba53fb93850670cdddd1e6515dadceGloria Wang    void    removeMediaRecorderClient(wp<MediaRecorderClient> client);
262f37971f624164c3ed185d3f976404a6f60f49b9aGlenn Kasten    virtual sp<IMediaMetadataRetriever> createMetadataRetriever();
26389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
264f37971f624164c3ed185d3f976404a6f60f49b9aGlenn Kasten    virtual sp<IMediaPlayer>    create(const sp<IMediaPlayerClient>& client, int audioSessionId);
265e2b1028852120bcfded33b8f06f66b780437fe92Andreas Huber
2661b86fe063badb5f28c467ade39be0f4008688947Andreas Huber    virtual status_t            decode(
2671b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            const sp<IMediaHTTPService> &httpService,
2681b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            const char* url,
2691b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            uint32_t *pSampleRate,
2701b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            int* pNumChannels,
2711b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            audio_format_t* pFormat,
2721b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            const sp<IMemoryHeap>& heap,
2731b86fe063badb5f28c467ade39be0f4008688947Andreas Huber            size_t *pSize);
2741b86fe063badb5f28c467ade39be0f4008688947Andreas Huber
2753d00aa6de95fb46e36f2bab4e3facdf0b96acf06Eric Laurent    virtual status_t            decode(int fd, int64_t offset, int64_t length,
2763d00aa6de95fb46e36f2bab4e3facdf0b96acf06Eric Laurent                                       uint32_t *pSampleRate, int* pNumChannels,
2773d00aa6de95fb46e36f2bab4e3facdf0b96acf06Eric Laurent                                       audio_format_t* pFormat,
2783d00aa6de95fb46e36f2bab4e3facdf0b96acf06Eric Laurent                                       const sp<IMemoryHeap>& heap, size_t *pSize);
2791381d4b5c0385aec3741073e5998773b064c1fb0Lajos Molnar    virtual sp<IMediaCodecList> getCodecList() const;
280318ad9c1d9d6515026dfc2c021359d27decaa7a1Andreas Huber    virtual sp<IOMX>            getOMX();
281ed3e3e046840d5bf1ca84a8c0cc097425e89d6d6Andreas Huber    virtual sp<ICrypto>         makeCrypto();
282cc82dc6d500023eba6048616301a4b12682458dbJeff Tinker    virtual sp<IDrm>            makeDrm();
283279dcd89ab1d2de91dfe95e461412a0f577a6891Andreas Huber    virtual sp<IHDCP>           makeHDCP(bool createEncryptionModule);
2842013a54981d4ffb036dff279b88cc9f08c0ee1c2Jeff Brown
2852013a54981d4ffb036dff279b88cc9f08c0ee1c2Jeff Brown    virtual sp<IRemoteDisplay> listenForRemoteDisplay(const sp<IRemoteDisplayClient>& client,
2862013a54981d4ffb036dff279b88cc9f08c0ee1c2Jeff Brown            const String8& iface);
28789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    virtual status_t            dump(int fd, const Vector<String16>& args);
28889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
28989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project            void                removeClient(wp<Client> client);
29089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
2917cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    // For battery usage tracking purpose
2927cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    struct BatteryUsageInfo {
2937cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        // how many streams are being played by one UID
2947cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        int     refCount;
2957cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        // a temp variable to store the duration(ms) of audio codecs
2967cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        // when we start a audio codec, we minus the system time from audioLastTime
2977cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        // when we pause it, we add the system time back to the audioLastTime
2987cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        // so after the pause, audioLastTime = pause time - start time
2997cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        // if multiple audio streams are played (or recorded), then audioLastTime
3007cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        // = the total playing time of all the streams
3017cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        int32_t audioLastTime;
3027cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        // when all the audio streams are being paused, we assign audioLastTime to
3037cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        // this variable, so this value could be provided to the battery app
3047cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        // in the next pullBatteryData call
3057cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        int32_t audioTotalTime;
3067cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang
3077cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        int32_t videoLastTime;
3087cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang        int32_t videoTotalTime;
3097cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    };
3107cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    KeyedVector<int, BatteryUsageInfo>    mBatteryData;
311a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania
3129ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    enum {
3139ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        SPEAKER,
3149ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        OTHER_AUDIO_DEVICE,
3159ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        SPEAKER_AND_OTHER,
3169ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        NUM_AUDIO_DEVICES
3179ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    };
3189ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
3199ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    struct BatteryAudioFlingerUsageInfo {
3209ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        int refCount; // how many audio streams are being played
3219ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        int deviceOn[NUM_AUDIO_DEVICES]; // whether the device is currently used
3229ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        int32_t lastTime[NUM_AUDIO_DEVICES]; // in ms
3239ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        // totalTime[]: total time of audio output devices usage
3249ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang        int32_t totalTime[NUM_AUDIO_DEVICES]; // in ms
3259ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    };
3269ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
3279ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    // This varialble is used to record the usage of audio output device
3289ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    // for battery app
3299ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang    BatteryAudioFlingerUsageInfo mBatteryAudio;
3309ee159b79022b2e1a050acb3890ce948e99e9ccbGloria Wang
3317cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    // Collect info of the codec usage from media player and media recorder
3327cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    virtual void                addBatteryData(uint32_t params);
3337cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    // API for the Battery app to pull the data of codecs usage
3347cf180c9bff69e5cc4a2f4e53b432db45ebbebabGloria Wang    virtual status_t            pullBatteryData(Parcel* reply);
33589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Projectprivate:
33689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project
33789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project    class Client : public BnMediaPlayer {
33889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        // IMediaPlayer interface
33989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual void            disconnect();
3401173118eace0e9e347cb007f0da817cee87579edGlenn Kasten        virtual status_t        setVideoSurfaceTexture(
341484566c410afdfbc4b6bc5aa1732aef37428a5b8Andy McFadden                                        const sp<IGraphicBufferProducer>& bufferProducer);
34289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        prepareAsync();
34389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        start();
34489fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        stop();
34589fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        pause();
34689fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        isPlaying(bool* state);
34789fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        seekTo(int msec);
34889fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        getCurrentPosition(int* msec);
34989fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        getDuration(int* msec);
35089fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        reset();
351fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        virtual status_t        setAudioStreamType(audio_stream_type_t type);
35289fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        setLooping(int loop);
35389fa4ad53f2f4d57adbc97ae1149fc00c9b6f3c5The Android Open Source Project        virtual status_t        setVolume(float leftVolume, float rightVolume);
3541d187f1a86855f5f0694d7ec30efc9833bf7c589Nicolas Catania        virtual status_t        invoke(const Parcel& request, Parcel *reply);
355a7e0e8b4c429fc68eb1bd5b5a30f5b91352288f9Nicolas Catania        virtual status_t        setMetadataFilter(const Parcel& filter);
3564829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania        virtual status_t        getMetadata(bool update_only,
3574829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania                                            bool apply_filter,
3584829038419910aa6e75ce8992d45a223452d5c67Nicolas Catania                                            Parcel *reply);
359