android_GenericPlayer.h revision 3610785fa93586ce84a27a27530feb77b8035229
1/*
2 * Copyright (C) 2011 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#ifndef __ANDROID_GENERICPLAYER_H__
18#define __ANDROID_GENERICPLAYER_H__
19
20#include <media/stagefright/foundation/AHandler.h>
21#include <media/stagefright/foundation/ALooper.h>
22#include <media/stagefright/foundation/AMessage.h>
23
24//--------------------------------------------------------------------------------------------------
25/**
26 * Message parameters for AHandler messages, see list in GenericPlayer::kWhatxxx
27 */
28#define WHATPARAM_SEEK_SEEKTIME_MS                  "seekTimeMs"
29#define WHATPARAM_LOOP_LOOPING                      "looping"
30#define WHATPARAM_BUFFERING_UPDATE                  "bufferingUpdate"
31#define WHATPARAM_BUFFERING_UPDATETHRESHOLD_PERCENT "buffUpdateThreshold"
32#define WHATPARAM_ATTACHAUXEFFECT                   "attachAuxEffect"
33#define WHATPARAM_SETAUXEFFECTSENDLEVEL             "setAuxEffectSendLevel"
34
35namespace android {
36
37class GenericPlayer : public AHandler
38{
39public:
40
41    enum {
42        kEventPrepared                = 'prep',
43        kEventHasVideoSize            = 'vsiz',
44        kEventPrefetchStatusChange    = 'pfsc',
45        kEventPrefetchFillLevelUpdate = 'pflu',
46        kEventEndOfStream             = 'eos'
47    };
48
49
50    GenericPlayer(const AudioPlayback_Parameters* params);
51    virtual ~GenericPlayer();
52
53    virtual void init(const notif_cbf_t cbf, void* notifUser);
54    virtual void preDestroy();
55
56    virtual void setDataSource(const char *uri);
57    virtual void setDataSource(int fd, int64_t offset, int64_t length);
58
59    virtual void prepare();
60    virtual void play();
61    virtual void pause();
62    virtual void stop();
63    virtual void seek(int64_t timeMsec);
64    virtual void loop(bool loop);
65    virtual void setBufferingUpdateThreshold(int16_t thresholdPercent);
66
67    virtual void getDurationMsec(int* msec); //msec != NULL, ANDROID_UNKNOWN_TIME if unknown
68    virtual void getPositionMsec(int* msec); //msec != NULL, ANDROID_UNKNOWN_TIME if unknown
69    virtual void getSampleRate(uint32_t* hz);// hz  != NULL, UNKNOWN_SAMPLERATE if unknown
70
71    void setVolume(bool mute, bool useStereoPos, XApermille stereoPos, XAmillibel volume);
72    void attachAuxEffect(int32_t effectId);
73    void setAuxEffectSendLevel(float level);
74
75protected:
76    // mutex used for set vs use of volume and cache (fill, threshold) settings
77    Mutex mSettingsLock;
78
79    void resetDataLocator();
80    DataLocator2 mDataLocator;
81    int          mDataLocatorType;
82
83    // Constants used to identify the messages in this player's AHandler message loop
84    //   in onMessageReceived()
85    enum {
86        kWhatPrepare         = 'prep',
87        kWhatNotif           = 'noti',
88        kWhatPlay            = 'play',
89        kWhatPause           = 'paus',
90        kWhatSeek            = 'seek',
91        kWhatSeekComplete    = 'skcp',
92        kWhatLoop            = 'loop',
93        kWhatVolumeUpdate    = 'volu',
94        kWhatBufferingUpdate = 'bufu',
95        kWhatBuffUpdateThres = 'buut',
96        kWhatMediaPlayerInfo = 'mpin',
97        kWhatAttachAuxEffect = 'aaux',
98        kWhatSetAuxEffectSendLevel = 'saux',
99    };
100
101    // Send a notification to one of the event listeners
102    virtual void notify(const char* event, int data1, bool async);
103    virtual void notify(const char* event, int data1, int data2, bool async);
104
105    // AHandler implementation
106    virtual void onMessageReceived(const sp<AMessage> &msg);
107
108    // Async event handlers (called from GenericPlayer's event loop)
109    virtual void onPrepare();
110    virtual void onNotify(const sp<AMessage> &msg);
111    virtual void onPlay();
112    virtual void onPause();
113    virtual void onSeek(const sp<AMessage> &msg);
114    virtual void onLoop(const sp<AMessage> &msg);
115    virtual void onVolumeUpdate();
116    virtual void onSeekComplete();
117    virtual void onBufferingUpdate(const sp<AMessage> &msg);
118    virtual void onSetBufferingUpdateThreshold(const sp<AMessage> &msg);
119    virtual void onAttachAuxEffect(const sp<AMessage> &msg);
120    virtual void onSetAuxEffectSendLevel(const sp<AMessage> &msg);
121
122    // Convenience methods
123    //   for async notifications of prefetch status and cache fill level, needs to be called
124    //     with mSettingsLock locked
125    void notifyStatus();
126    void notifyCacheFill();
127    //   for internal async notification to update state that the player is no longer seeking
128    void seekComplete();
129    void bufferingUpdate(int16_t fillLevelPerMille);
130
131    // Event notification from GenericPlayer to OpenSL ES / OpenMAX AL framework
132    notif_cbf_t mNotifyClient;
133    void*       mNotifyUser;
134    // lock to protect mNotifyClient and mNotifyUser updates
135    Mutex       mNotifyClientLock;
136
137    // Bits for mStateFlags
138    enum {
139        kFlagPrepared               = 1 << 0,   // use only for successful preparation
140        kFlagPreparing              = 1 << 1,
141        kFlagPlaying                = 1 << 2,
142        kFlagBuffering              = 1 << 3,
143        kFlagSeeking                = 1 << 4,
144        kFlagLooping                = 1 << 5,
145        kFlagPreparedUnsuccessfully = 1 << 6,
146    };
147
148    uint32_t mStateFlags;
149
150    sp<ALooper> mLooper;
151    int32_t mLooperPriority;
152
153    AudioPlayback_Parameters mPlaybackParams;
154
155    AndroidAudioLevels mAndroidAudioLevels;
156    int mChannelCount; // this is used for the panning law, and is not exposed outside of the object
157    int32_t mDurationMsec;
158    // position is not protected by any lock in this generic class, may be different in subclasses
159    int32_t mPositionMsec;
160    uint32_t mSampleRateHz;
161
162    CacheStatus_t mCacheStatus;
163    int16_t mCacheFill; // cache fill level + played back level in permille
164    int16_t mLastNotifiedCacheFill; // last cache fill level communicated to the listener
165    int16_t mCacheFillNotifThreshold; // threshold in cache fill level for cache fill to be reported
166
167private:
168    DISALLOW_EVIL_CONSTRUCTORS(GenericPlayer);
169};
170
171} // namespace android
172
173#endif /* __ANDROID_GENERICPLAYER_H__ */
174