android_GenericPlayer.h revision e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91
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// Parameters for kWhatSetPlayEvents 35#define WHATPARAM_SETPLAYEVENTS_FLAGS "setPlayEventsFlags" 36#define WHATPARAM_SETPLAYEVENTS_MARKER "setPlayEventsMarker" 37#define WHATPARAM_SETPLAYEVENTS_UPDATE "setPlayEventsUpdate" 38// Parameters for kWhatOneShot (see explanation at definition of kWhatOneShot below) 39#define WHATPARAM_ONESHOT_GENERATION "oneShotGeneration" 40 41namespace android { 42 43// abstract base class 44class GenericPlayer : public AHandler 45{ 46public: 47 48 enum { 49 kEventPrepared = 'prep', 50 kEventHasVideoSize = 'vsiz', 51 kEventPrefetchStatusChange = 'pfsc', 52 kEventPrefetchFillLevelUpdate = 'pflu', 53 kEventEndOfStream = 'eos', 54 kEventChannelCount = 'ccnt', 55 kEventPlay = 'play', // SL_PLAYEVENT_* 56 kEventErrorAfterPrepare = 'easp', // error after successful prepare 57 }; 58 59 60 GenericPlayer(const AudioPlayback_Parameters* params); 61 virtual ~GenericPlayer(); 62 63 virtual void init(const notif_cbf_t cbf, void* notifUser); 64 virtual void preDestroy(); 65 66 void setDataSource(const char *uri); 67 void setDataSource(int fd, int64_t offset, int64_t length, bool closeAfterUse = false); 68 69 void prepare(); 70 virtual void play(); 71 virtual void pause(); 72 virtual void stop(); 73 // timeMsec must be >= 0 or == ANDROID_UNKNOWN_TIME (used by StreamPlayer after discontinuity) 74 virtual void seek(int64_t timeMsec); 75 virtual void loop(bool loop); 76 virtual void setBufferingUpdateThreshold(int16_t thresholdPercent); 77 78 virtual void getDurationMsec(int* msec); //msec != NULL, ANDROID_UNKNOWN_TIME if unknown 79 virtual void getPositionMsec(int* msec) = 0; //msec != NULL, ANDROID_UNKNOWN_TIME if unknown 80 81 virtual void setVideoSurface(const sp<Surface> &surface) {} 82 virtual void setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {} 83 84 void setVolume(float leftVol, float rightVol); 85 void attachAuxEffect(int32_t effectId); 86 void setAuxEffectSendLevel(float level); 87 88 // Call after changing any of the IPlay settings related to SL_PLAYEVENT_* 89 void setPlayEvents(int32_t eventFlags, int32_t markerPosition, int32_t positionUpdatePeriod); 90 91protected: 92 // mutex used for set vs use of volume, duration, and cache (fill, threshold) settings 93 Mutex mSettingsLock; 94 95 void resetDataLocator(); 96 DataLocator2 mDataLocator; 97 int mDataLocatorType; 98 99 // Constants used to identify the messages in this player's AHandler message loop 100 // in onMessageReceived() 101 enum { 102 kWhatPrepare = 'prep', // start preparation 103 kWhatNotif = 'noti', // send a notification to client 104 kWhatPlay = 'play', // start player 105 kWhatPause = 'paus', // pause or stop player 106 kWhatSeek = 'seek', // request a seek to specified position 107 kWhatSeekComplete = 'skcp', // seek request has completed 108 kWhatLoop = 'loop', // set the player's looping status 109 kWhatVolumeUpdate = 'volu', // set the channel gains to specified values 110 kWhatBufferingUpdate = 'bufu', 111 kWhatBuffUpdateThres = 'buut', 112 kWhatAttachAuxEffect = 'aaux', 113 kWhatSetAuxEffectSendLevel = 'saux', 114 kWhatSetPlayEvents = 'spev', // process new IPlay settings related to SL_PLAYEVENT_* 115 kWhatOneShot = 'ones', // deferred (non-0 timeout) handler for SL_PLAYEVENT_* 116 // As used here, "one-shot" is the software equivalent of a "retriggerable monostable 117 // multivibrator" from electronics. Briefly, a one-shot is a timer that can be triggered 118 // to fire at some point in the future. It is "retriggerable" because while the timer 119 // is active, it is possible to replace the current timeout value by a new value. 120 // This is done by cancelling the current timer (using a generation count), 121 // and then posting another timer with the new desired value. 122 }; 123 124 // Send a notification to one of the event listeners 125 virtual void notify(const char* event, int data1, bool async); 126 virtual void notify(const char* event, int data1, int data2, bool async); 127 128 // AHandler implementation 129 virtual void onMessageReceived(const sp<AMessage> &msg); 130 131 // Async event handlers (called from GenericPlayer's event loop) 132 virtual void onPrepare(); 133 virtual void onNotify(const sp<AMessage> &msg); 134 virtual void onPlay(); 135 virtual void onPause(); 136 virtual void onSeek(const sp<AMessage> &msg); 137 virtual void onLoop(const sp<AMessage> &msg); 138 virtual void onVolumeUpdate(); 139 virtual void onSeekComplete(); 140 virtual void onBufferingUpdate(const sp<AMessage> &msg); 141 virtual void onSetBufferingUpdateThreshold(const sp<AMessage> &msg); 142 virtual void onAttachAuxEffect(const sp<AMessage> &msg); 143 virtual void onSetAuxEffectSendLevel(const sp<AMessage> &msg); 144 void onSetPlayEvents(const sp<AMessage> &msg); 145 void onOneShot(const sp<AMessage> &msg); 146 147 // Convenience methods 148 // for async notifications of prefetch status and cache fill level, needs to be called 149 // with mSettingsLock locked 150 void notifyStatus(); 151 void notifyCacheFill(); 152 // for internal async notification to update state that the player is no longer seeking 153 void seekComplete(); 154 void bufferingUpdate(int16_t fillLevelPerMille); 155 156 // Event notification from GenericPlayer to OpenSL ES / OpenMAX AL framework 157 notif_cbf_t mNotifyClient; 158 void* mNotifyUser; 159 // lock to protect mNotifyClient and mNotifyUser updates 160 Mutex mNotifyClientLock; 161 162 // Bits for mStateFlags 163 enum { 164 kFlagPrepared = 1 << 0, // use only for successful preparation 165 kFlagPreparing = 1 << 1, 166 kFlagPlaying = 1 << 2, 167 kFlagBuffering = 1 << 3, 168 kFlagSeeking = 1 << 4, // set if we (not Stagefright) initiated a seek 169 kFlagLooping = 1 << 5, // set if looping is enabled 170 kFlagPreparedUnsuccessfully = 1 << 6, 171 }; 172 173 // Only accessed from event loop, does not need a mutex 174 uint32_t mStateFlags; 175 176 sp<ALooper> mLooper; 177 178 const AudioPlayback_Parameters mPlaybackParams; 179 180 // protected by mSettingsLock after construction 181 AndroidAudioLevels mAndroidAudioLevels; 182 183 // protected by mSettingsLock 184 int32_t mDurationMsec; 185 186 CacheStatus_t mCacheStatus; 187 int16_t mCacheFill; // cache fill level + played back level in permille 188 int16_t mLastNotifiedCacheFill; // last cache fill level communicated to the listener 189 int16_t mCacheFillNotifThreshold; // threshold in cache fill level for cache fill to be reported 190 191 // Call any time any of the IPlay copies, current position, or play state changes, and 192 // supply the latest known position or ANDROID_UNKNOWN_TIME if position is unknown to caller. 193 void updateOneShot(int positionMs = ANDROID_UNKNOWN_TIME); 194 195 virtual bool advancesPositionInRealTime() const { return true; } 196 197private: 198 199 // Our copy of some important IPlay member variables, except in Android units 200 int32_t mEventFlags; 201 int32_t mMarkerPositionMs; 202 int32_t mPositionUpdatePeriodMs; 203 204 // We need to be able to cancel any pending one-shot event(s) prior to posting 205 // a new one-shot. As AMessage does not currently support cancellation by 206 // "what" category, we simulate this by keeping a generation counter for 207 // one-shots. When a one-shot event is delivered, it checks to see if it is 208 // still the current one-shot. If not, it returns immediately, thus 209 // effectively cancelling itself. Note that counter wrap-around is possible 210 // but unlikely and benign. 211 int32_t mOneShotGeneration; 212 213 // Play position at time of the most recently delivered SL_PLAYEVENT_HEADATNEWPOS, 214 // or ANDROID_UNKNOWN_TIME if a SL_PLAYEVENT_HEADATNEWPOS has never been delivered. 215 int32_t mDeliveredNewPosMs; 216 217 // Play position most recently observed by updateOneShot, or ANDROID_UNKNOWN_TIME 218 // if the play position has never been observed. 219 int32_t mObservedPositionMs; 220 221 DISALLOW_EVIL_CONSTRUCTORS(GenericPlayer); 222}; 223 224} // namespace android 225 226extern void android_player_volumeUpdate(float *pVolumes /*[2]*/, const IVolume *volumeItf, 227 unsigned channelCount, float amplFromDirectLevel, const bool *audibilityFactors /*[2]*/); 228 229#endif /* __ANDROID_GENERICPLAYER_H__ */ 230