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