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