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