NuPlayer2.h 11 KB

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