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 #ifndef NU_PLAYER_H_ 18 19 #define NU_PLAYER_H_ 20 21 #include <media/AudioResamplerPublic.h> 22 #include <mediadrm/ICrypto.h> 23 #include <media/MediaPlayerInterface.h> 24 #include <media/stagefright/foundation/AHandler.h> 25 26 namespace android { 27 28 struct ABuffer; 29 struct AMessage; 30 struct AVSyncSettings; 31 class IDataSource; 32 struct MediaClock; 33 class MetaData; 34 struct NuPlayerDriver; 35 36 struct NuPlayer : public AHandler { 37 explicit NuPlayer(pid_t pid, const sp<MediaClock> &mediaClock); 38 39 void setUID(uid_t uid); 40 41 void init(const wp<NuPlayerDriver> &driver); 42 43 void setDataSourceAsync(const sp<IStreamSource> &source); 44 45 void setDataSourceAsync( 46 const sp<IMediaHTTPService> &httpService, 47 const char *url, 48 const KeyedVector<String8, String8> *headers); 49 50 void setDataSourceAsync(int fd, int64_t offset, int64_t length); 51 52 void setDataSourceAsync(const sp<DataSource> &source); 53 54 status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */); 55 status_t setBufferingSettings(const BufferingSettings& buffering); 56 57 void prepareAsync(); 58 59 void setVideoSurfaceTextureAsync( 60 const sp<IGraphicBufferProducer> &bufferProducer); 61 62 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink); 63 status_t setPlaybackSettings(const AudioPlaybackRate &rate); 64 status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */); 65 status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint); 66 status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */); 67 68 void start(); 69 70 void pause(); 71 72 // Will notify the driver through "notifyResetComplete" once finished. 73 void resetAsync(); 74 75 // Request a notification when specified media time is reached. 76 status_t notifyAt(int64_t mediaTimeUs); 77 78 // Will notify the driver through "notifySeekComplete" once finished 79 // and needNotify is true. 80 void seekToAsync( 81 int64_t seekTimeUs, 82 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC, 83 bool needNotify = false); 84 85 status_t setVideoScalingMode(int32_t mode); 86 status_t getTrackInfo(Parcel* reply) const; 87 status_t getSelectedTrack(int32_t type, Parcel* reply) const; 88 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs); 89 status_t getCurrentPosition(int64_t *mediaUs); 90 void getStats(Vector<sp<AMessage> > *trackStats); 91 92 sp<MetaData> getFileMeta(); 93 float getFrameRate(); 94 95 // Modular DRM 96 status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId); 97 status_t releaseDrm(); 98 99 const char *getDataSourceType(); 100 101 void updateInternalTimers(); 102 103 protected: 104 virtual ~NuPlayer(); 105 106 virtual void onMessageReceived(const sp<AMessage> &msg); 107 108 public: 109 struct NuPlayerStreamListener; 110 struct Source; 111 112 private: 113 struct Decoder; 114 struct DecoderBase; 115 struct DecoderPassThrough; 116 struct CCDecoder; 117 struct GenericSource; 118 struct HTTPLiveSource; 119 struct Renderer; 120 struct RTSPSource; 121 struct StreamingSource; 122 struct Action; 123 struct SeekAction; 124 struct SetSurfaceAction; 125 struct ResumeDecoderAction; 126 struct FlushDecoderAction; 127 struct PostMessageAction; 128 struct SimpleAction; 129 130 enum { 131 kWhatSetDataSource = '=DaS', 132 kWhatPrepare = 'prep', 133 kWhatSetVideoSurface = '=VSu', 134 kWhatSetAudioSink = '=AuS', 135 kWhatMoreDataQueued = 'more', 136 kWhatConfigPlayback = 'cfPB', 137 kWhatConfigSync = 'cfSy', 138 kWhatGetPlaybackSettings = 'gPbS', 139 kWhatGetSyncSettings = 'gSyS', 140 kWhatStart = 'strt', 141 kWhatScanSources = 'scan', 142 kWhatVideoNotify = 'vidN', 143 kWhatAudioNotify = 'audN', 144 kWhatClosedCaptionNotify = 'capN', 145 kWhatRendererNotify = 'renN', 146 kWhatReset = 'rset', 147 kWhatNotifyTime = 'nfyT', 148 kWhatSeek = 'seek', 149 kWhatPause = 'paus', 150 kWhatResume = 'rsme', 151 kWhatPollDuration = 'polD', 152 kWhatSourceNotify = 'srcN', 153 kWhatGetTrackInfo = 'gTrI', 154 kWhatGetSelectedTrack = 'gSel', 155 kWhatSelectTrack = 'selT', 156 kWhatGetBufferingSettings = 'gBus', 157 kWhatSetBufferingSettings = 'sBuS', 158 kWhatPrepareDrm = 'pDrm', 159 kWhatReleaseDrm = 'rDrm', 160 kWhatMediaClockNotify = 'mckN', 161 }; 162 163 wp<NuPlayerDriver> mDriver; 164 bool mUIDValid; 165 uid_t mUID; 166 pid_t mPID; 167 const sp<MediaClock> mMediaClock; 168 Mutex mSourceLock; // guard |mSource|. 169 sp<Source> mSource; 170 uint32_t mSourceFlags; 171 sp<Surface> mSurface; 172 sp<MediaPlayerBase::AudioSink> mAudioSink; 173 sp<DecoderBase> mVideoDecoder; 174 bool mOffloadAudio; 175 sp<DecoderBase> mAudioDecoder; 176 Mutex mDecoderLock; // guard |mAudioDecoder| and |mVideoDecoder|. 177 sp<CCDecoder> mCCDecoder; 178 sp<Renderer> mRenderer; 179 sp<ALooper> mRendererLooper; 180 int32_t mAudioDecoderGeneration; 181 int32_t mVideoDecoderGeneration; 182 int32_t mRendererGeneration; 183 184 Mutex mPlayingTimeLock; 185 int64_t mLastStartedPlayingTimeNs; 186 void updatePlaybackTimer(bool stopping, const char *where); 187 void startPlaybackTimer(const char *where); 188 189 int64_t mLastStartedRebufferingTimeNs; 190 void startRebufferingTimer(); 191 void updateRebufferingTimer(bool stopping, bool exitingPlayback); 192 193 int64_t mPreviousSeekTimeUs; 194 195 List<sp<Action> > mDeferredActions; 196 197 bool mAudioEOS; 198 bool mVideoEOS; 199 200 bool mScanSourcesPending; 201 int32_t mScanSourcesGeneration; 202 203 int32_t mPollDurationGeneration; 204 int32_t mTimedTextGeneration; 205 206 enum FlushStatus { 207 NONE, 208 FLUSHING_DECODER, 209 FLUSHING_DECODER_SHUTDOWN, 210 SHUTTING_DOWN_DECODER, 211 FLUSHED, 212 SHUT_DOWN, 213 }; 214 215 enum FlushCommand { 216 FLUSH_CMD_NONE, 217 FLUSH_CMD_FLUSH, 218 FLUSH_CMD_SHUTDOWN, 219 }; 220 221 // Status of flush responses from the decoder and renderer. 222 bool mFlushComplete[2][2]; 223 224 FlushStatus mFlushingAudio; 225 FlushStatus mFlushingVideo; 226 227 // Status of flush responses from the decoder and renderer. 228 bool mResumePending; 229 230 int32_t mVideoScalingMode; 231 232 AudioPlaybackRate mPlaybackSettings; 233 AVSyncSettings mSyncSettings; 234 float mVideoFpsHint; 235 bool mStarted; 236 bool mPrepared; 237 bool mResetting; 238 bool mSourceStarted; 239 bool mAudioDecoderError; 240 bool mVideoDecoderError; 241 242 // Actual pause state, either as requested by client or due to buffering. 243 bool mPaused; 244 245 // Pause state as requested by client. Note that if mPausedByClient is 246 // true, mPaused is always true; if mPausedByClient is false, mPaused could 247 // still become true, when we pause internally due to buffering. 248 bool mPausedByClient; 249 250 // Pause state as requested by source (internally) due to buffering 251 bool mPausedForBuffering; 252 253 // Modular DRM 254 sp<ICrypto> mCrypto; 255 bool mIsDrmProtected; 256 257 typedef enum { 258 DATA_SOURCE_TYPE_NONE, 259 DATA_SOURCE_TYPE_HTTP_LIVE, 260 DATA_SOURCE_TYPE_RTSP, 261 DATA_SOURCE_TYPE_GENERIC_URL, 262 DATA_SOURCE_TYPE_GENERIC_FD, 263 DATA_SOURCE_TYPE_MEDIA, 264 DATA_SOURCE_TYPE_STREAM, 265 } DATA_SOURCE_TYPE; 266 267 std::atomic<DATA_SOURCE_TYPE> mDataSourceType; 268 getDecoderNuPlayer269 inline const sp<DecoderBase> &getDecoder(bool audio) { 270 return audio ? mAudioDecoder : mVideoDecoder; 271 } 272 clearFlushCompleteNuPlayer273 inline void clearFlushComplete() { 274 mFlushComplete[0][0] = false; 275 mFlushComplete[0][1] = false; 276 mFlushComplete[1][0] = false; 277 mFlushComplete[1][1] = false; 278 } 279 280 void tryOpenAudioSinkForOffload( 281 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo); 282 void closeAudioSink(); 283 void restartAudio( 284 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder); 285 void determineAudioModeChange(const sp<AMessage> &audioFormat); 286 287 status_t instantiateDecoder( 288 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true); 289 290 status_t onInstantiateSecureDecoders(); 291 292 void updateVideoSize( 293 const sp<AMessage> &inputFormat, 294 const sp<AMessage> &outputFormat = NULL); 295 296 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL); 297 298 void handleFlushComplete(bool audio, bool isDecoder); 299 void finishFlushIfPossible(); 300 301 void onStart( 302 int64_t startPositionUs = -1, 303 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC); 304 void onResume(); 305 void onPause(); 306 307 bool audioDecoderStillNeeded(); 308 309 void flushDecoder(bool audio, bool needShutdown); 310 311 void finishResume(); 312 void notifyDriverSeekComplete(); 313 314 void postScanSources(); 315 316 void schedulePollDuration(); 317 void cancelPollDuration(); 318 319 void processDeferredActions(); 320 321 void performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode); 322 void performDecoderFlush(FlushCommand audio, FlushCommand video); 323 void performReset(); 324 void performScanSources(); 325 void performSetSurface(const sp<Surface> &wrapper); 326 void performResumeDecoders(bool needNotify); 327 328 void onSourceNotify(const sp<AMessage> &msg); 329 void onClosedCaptionNotify(const sp<AMessage> &msg); 330 331 void queueDecoderShutdown( 332 bool audio, bool video, const sp<AMessage> &reply); 333 334 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex); 335 void sendTimedMetaData(const sp<ABuffer> &buffer); 336 void sendTimedTextData(const sp<ABuffer> &buffer); 337 338 void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const; 339 340 status_t onPrepareDrm(const sp<AMessage> &msg); 341 status_t onReleaseDrm(); 342 343 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer); 344 }; 345 346 } // namespace android 347 348 #endif // NU_PLAYER_H_ 349