android_StreamPlayer.h revision d1e9fd4cff80becfef5077090fc90328ba63999a
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <media/IStreamSource.h>
18#include <binder/IServiceManager.h>
19#include "android/android_GenericMediaPlayer.h"
20
21// number of SLuint32 fields to store a buffer event message in an item, by mapping each
22//   to the item key (SLuint32), the item size (SLuint32), and the item data (mask on SLuint32)
23#define NB_BUFFEREVENT_ITEM_FIELDS 3
24
25//--------------------------------------------------------------------------------------------------
26namespace android {
27
28//--------------------------------------------------------------------------------------------------
29class StreamPlayer;
30
31class StreamSourceAppProxy : public BnStreamSource {
32public:
33    StreamSourceAppProxy(
34            IAndroidBufferQueue *androidBufferQueue,
35            const sp<CallbackProtector> &callbackProtector,
36            const sp<StreamPlayer> &player);
37    virtual ~StreamSourceAppProxy();
38
39    // store an item structure to indicate a processed buffer
40    static const SLuint32 kItemProcessed[NB_BUFFEREVENT_ITEM_FIELDS];
41
42    // IStreamSource implementation
43    virtual void setListener(const sp<IStreamListener> &listener);
44    virtual void setBuffers(const Vector<sp<IMemory> > &buffers);
45    virtual void onBufferAvailable(size_t index);
46
47    // Consumption from ABQ
48    void pullFromBuffQueue();
49
50    void receivedCmd_l(IStreamListener::Command cmd, const sp<AMessage> &msg = NULL);
51    void receivedBuffer_l(size_t buffIndex, size_t buffLength);
52
53private:
54    // for mListener and mAvailableBuffers
55    Mutex mLock;
56    sp<IStreamListener> mListener;
57    // array of shared memory buffers
58    Vector<sp<IMemory> > mBuffers;
59    // list of available buffers in shared memory, identified by their index
60    List<size_t> mAvailableBuffers;
61
62    // the Android Buffer Queue from which data is consumed and written to shared memory
63    IAndroidBufferQueue* mAndroidBufferQueue;
64
65    const sp<CallbackProtector> mCallbackProtector;
66    const sp<StreamPlayer> mPlayer;
67
68    DISALLOW_EVIL_CONSTRUCTORS(StreamSourceAppProxy);
69};
70
71
72//--------------------------------------------------------------------------------------------------
73class StreamPlayer : public GenericMediaPlayer
74{
75public:
76    StreamPlayer(AudioPlayback_Parameters* params, bool hasVideo,
77           IAndroidBufferQueue *androidBufferQueue, const sp<CallbackProtector> &callbackProtector);
78    virtual ~StreamPlayer();
79
80    // overridden from GenericPlayer
81    virtual void onMessageReceived(const sp<AMessage> &msg);
82    virtual void preDestroy();
83
84    void registerQueueCallback(IAndroidBufferQueue *androidBufferQueue);
85    void queueRefilled();
86    void appClear_l();
87
88protected:
89
90    enum {
91        // message to asynchronously notify mAppProxy it should try to pull from the Android
92        //    Buffer Queue and push to shared memory (media server), either because the buffer queue
93        //    was refilled, or because during playback, the shared memory buffers should remain
94        //    filled to prevent it from draining (this can happen if the ABQ is not ready
95        //    whenever a shared memory buffer becomes available)
96        kWhatPullFromAbq    = 'plfq',
97        kWhatStopForDestroy = 's4ds'
98    };
99
100    const sp<StreamSourceAppProxy> mAppProxy; // application proxy for the shared memory source
101
102    // overridden from GenericMediaPlayer
103    virtual void onPrepare();
104    virtual void onPlay();
105
106    void onPullFromAndroidBufferQueue();
107
108private:
109    void onStopForDestroy();
110
111    Mutex mStopForDestroyLock;
112    Condition mStopForDestroyCondition;
113    bool mStopForDestroyCompleted;
114
115    DISALLOW_EVIL_CONSTRUCTORS(StreamPlayer);
116};
117
118} // namespace android
119