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