android_StreamPlayer.h revision 3ac5dcc05fe321e4f01918aef2e3e54e22c9a5c1
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 queueRefilled();
85    void appClear_l();
86
87protected:
88
89    enum {
90        // message to asynchronously notify mAppProxy it should try to pull from the Android
91        //    Buffer Queue and push to shared memory (media server), either because the buffer queue
92        //    was refilled, or because during playback, the shared memory buffers should remain
93        //    filled to prevent it from draining (this can happen if the ABQ is not ready
94        //    whenever a shared memory buffer becomes available)
95        kWhatPullFromAbq    = 'plfq',
96        kWhatStopForDestroy = 's4ds'
97    };
98
99    const sp<StreamSourceAppProxy> mAppProxy; // application proxy for the shared memory source
100
101    // overridden from GenericMediaPlayer
102    virtual void onPrepare();
103    virtual void onPlay();
104
105    void onPullFromAndroidBufferQueue();
106
107private:
108    void onStopForDestroy();
109
110    Mutex mStopForDestroyLock;
111    Condition mStopForDestroyCondition;
112    bool mStopForDestroyCompleted;
113
114    DISALLOW_EVIL_CONSTRUCTORS(StreamPlayer);
115};
116
117} // namespace android
118