android_StreamPlayer.h revision bc0e642e6c1a51b3ae3a02d490d94b03e718e6b5
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            const void* user, bool userIsAudioPlayer,
35            void *appContext,
36            const void *caller,
37            const sp<CallbackProtector> &callbackProtector,
38            const sp<StreamPlayer> &player);
39    virtual ~StreamSourceAppProxy();
40
41    // store an item structure to indicate a processed buffer
42    static const SLuint32 kItemProcessed[NB_BUFFEREVENT_ITEM_FIELDS];
43
44    // IStreamSource implementation
45    virtual void setListener(const sp<IStreamListener> &listener);
46    virtual void setBuffers(const Vector<sp<IMemory> > &buffers);
47    virtual void onBufferAvailable(size_t index);
48
49    // Consumption from ABQ
50    void pullFromBuffQueue();
51
52    void receivedCmd_l(IStreamListener::Command cmd, const sp<AMessage> &msg = NULL);
53    void receivedBuffer_l(size_t buffIndex, size_t buffLength);
54
55private:
56    // for mListener and mAvailableBuffers
57    Mutex mLock;
58    sp<IStreamListener> mListener;
59    // array of shared memory buffers
60    Vector<sp<IMemory> > mBuffers;
61    // list of available buffers in shared memory, identified by their index
62    List<size_t> mAvailableBuffers;
63
64    const void* mUser;
65    bool mUserIsAudioPlayer;
66    // the Android Buffer Queue from which data is consumed and written to shared memory
67    IAndroidBufferQueue* mAndroidBufferQueue;
68
69    void *mAppContext;
70    const void *mCaller;
71
72    sp<CallbackProtector> mCallbackProtector;
73    const sp<StreamPlayer> mPlayer;
74
75    DISALLOW_EVIL_CONSTRUCTORS(StreamSourceAppProxy);
76};
77
78
79//--------------------------------------------------------------------------------------------------
80class StreamPlayer : public GenericMediaPlayer
81{
82public:
83    StreamPlayer(AudioPlayback_Parameters* params, bool hasVideo);
84    virtual ~StreamPlayer();
85
86    // overridden from GenericPlayer
87    virtual void onMessageReceived(const sp<AMessage> &msg);
88
89    void registerQueueCallback(
90            const void* user, bool userIsAudioPlayer,
91            void *context,
92            const void *caller);
93    void queueRefilled();
94    void appClear_l();
95
96protected:
97
98    enum {
99        // message to asynchronously notify mAppProxy it should try to pull from the Android
100        //    Buffer Queue and push to shared memory (media server), either because the buffer queue
101        //    was refilled, or because during playback, the shared memory buffers should remain
102        //    filled to prevent it from draining (this can happen if the ABQ is not ready
103        //    whenever a shared memory buffer becomes available)
104        kWhatPullFromAbq = 'plfq'
105    };
106
107    sp<StreamSourceAppProxy> mAppProxy; // application proxy for the android buffer queue source
108
109    // overridden from GenericMediaPlayer
110    virtual void onPrepare();
111    virtual void onPlay();
112
113    void onPullFromAndroidBufferQueue();
114
115    Mutex mAppProxyLock;
116
117
118private:
119    DISALLOW_EVIL_CONSTRUCTORS(StreamPlayer);
120};
121
122} // namespace android
123
124
125//--------------------------------------------------------------------------------------------------
126/*
127 * xxx_l functions are called with a lock on the CAudioPlayer mObject
128 */
129extern void android_StreamPlayer_realize_l(CAudioPlayer *ap, const notif_cbf_t cbf,
130        void* notifUser);
131