android_StreamPlayer.h revision 63c002ab68761be0eace98f28320d8eb2f3f7695
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
20// number of SLuint32 fields to store a buffer event message in an item, by mapping each
21//   to the item key (SLuint32), the item size (SLuint32), and the item data (mask on SLuint32)
22#define NB_BUFFEREVENT_ITEM_FIELDS 3
23
24//--------------------------------------------------------------------------------------------------
25namespace android {
26
27//--------------------------------------------------------------------------------------------------
28class StreamSourceAppProxy : public BnStreamSource {
29public:
30    StreamSourceAppProxy(
31            const void* user, bool userIsAudioPlayer,
32            void *appContext,
33            const void *caller);
34    virtual ~StreamSourceAppProxy();
35
36    // store an item structure to indicate a processed buffer
37    static const SLuint32 kItemProcessed[NB_BUFFEREVENT_ITEM_FIELDS];
38
39    // IStreamSource implementation
40    virtual void setListener(const sp<IStreamListener> &listener);
41    virtual void setBuffers(const Vector<sp<IMemory> > &buffers);
42    virtual void onBufferAvailable(size_t index);
43
44    // Consumption from ABQ
45    void pullFromBuffQueue();
46
47    void receivedCmd_l(IStreamListener::Command cmd, const sp<AMessage> &msg = NULL);
48    void receivedBuffer_l(size_t buffIndex, size_t buffLength);
49
50private:
51    // for mListener and mAvailableBuffers
52    Mutex mLock;
53    sp<IStreamListener> mListener;
54    // array of shared memory buffers
55    Vector<sp<IMemory> > mBuffers;
56    // list of available buffers in shared memory, identified by their index
57    List<size_t> mAvailableBuffers;
58
59    const void* mUser;
60    bool mUserIsAudioPlayer;
61    // the Android Buffer Queue from which data is consumed and written to shared memory
62    IAndroidBufferQueue* mAndroidBufferQueue;
63
64    void *mAppContext;
65    const void *mCaller;
66
67    DISALLOW_EVIL_CONSTRUCTORS(StreamSourceAppProxy);
68};
69
70
71//--------------------------------------------------------------------------------------------------
72class StreamPlayer : public GenericMediaPlayer
73{
74public:
75    StreamPlayer(AudioPlayback_Parameters* params, bool hasVideo);
76    virtual ~StreamPlayer();
77
78    // overridden from GenericPlayer
79    virtual void onMessageReceived(const sp<AMessage> &msg);
80
81    void registerQueueCallback(
82            const void* user, bool userIsAudioPlayer,
83            void *context,
84            const void *caller);
85    void queueRefilled_l();
86    void appClear_l();
87
88protected:
89
90    enum {
91        // message to asynchronously notify mAppProxy the Android Buffer Queue was refilled
92        kWhatQueueRefilled = 'qrfi'
93    };
94
95    sp<StreamSourceAppProxy> mAppProxy; // application proxy for the android buffer queue source
96
97    // overridden from GenericMediaPlayer
98    virtual void onPrepare();
99
100    void onQueueRefilled();
101
102    Mutex mAppProxyLock;
103
104
105private:
106    DISALLOW_EVIL_CONSTRUCTORS(StreamPlayer);
107};
108
109} // namespace android
110
111
112//--------------------------------------------------------------------------------------------------
113/*
114 * xxx_l functions are called with a lock on the CAudioPlayer mObject
115 */
116extern void android_StreamPlayer_realize_l(CAudioPlayer *ap, const notif_cbf_t cbf,
117        void* notifUser);
118