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