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