NuPlayer2Renderer.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 NUPLAYER2_RENDERER_H_
  17. #define NUPLAYER2_RENDERER_H_
  18. #include <media/AudioResamplerPublic.h>
  19. #include <media/AVSyncSettings.h>
  20. #include <mediaplayer2/JObjectHolder.h>
  21. #include "NuPlayer2.h"
  22. namespace android {
  23. class JWakeLock;
  24. struct MediaClock;
  25. class MediaCodecBuffer;
  26. struct VideoFrameSchedulerBase;
  27. struct NuPlayer2::Renderer : public AHandler {
  28. enum Flags {
  29. FLAG_REAL_TIME = 1,
  30. FLAG_OFFLOAD_AUDIO = 2,
  31. };
  32. Renderer(const sp<MediaPlayer2Interface::AudioSink> &sink,
  33. const sp<MediaClock> &mediaClock,
  34. const sp<AMessage> &notify,
  35. const sp<JObjectHolder> &context,
  36. uint32_t flags = 0);
  37. static size_t AudioSinkCallback(
  38. MediaPlayer2Interface::AudioSink *audioSink,
  39. void *data, size_t size, void *me,
  40. MediaPlayer2Interface::AudioSink::cb_event_t event);
  41. void queueBuffer(
  42. bool audio,
  43. const sp<MediaCodecBuffer> &buffer,
  44. const sp<AMessage> &notifyConsumed);
  45. void queueEOS(bool audio, status_t finalResult);
  46. status_t setPlaybackSettings(const AudioPlaybackRate &rate /* sanitized */);
  47. status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
  48. status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
  49. status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
  50. void flush(bool audio, bool notifyComplete);
  51. void signalTimeDiscontinuity();
  52. void signalDisableOffloadAudio();
  53. void signalEnableOffloadAudio();
  54. void pause();
  55. void resume();
  56. void setVideoFrameRate(float fps);
  57. status_t getCurrentPosition(int64_t *mediaUs);
  58. int64_t getVideoLateByUs();
  59. status_t openAudioSink(
  60. const sp<AMessage> &format,
  61. bool offloadOnly,
  62. bool hasVideo,
  63. uint32_t flags,
  64. bool *isOffloaded,
  65. bool isStreaming);
  66. void closeAudioSink();
  67. // re-open audio sink after all pending audio buffers played.
  68. void changeAudioFormat(
  69. const sp<AMessage> &format,
  70. bool offloadOnly,
  71. bool hasVideo,
  72. uint32_t flags,
  73. bool isStreaming,
  74. const sp<AMessage> &notify);
  75. enum {
  76. kWhatEOS = 'eos ',
  77. kWhatFlushComplete = 'fluC',
  78. kWhatPosition = 'posi',
  79. kWhatVideoRenderingStart = 'vdrd',
  80. kWhatMediaRenderingStart = 'mdrd',
  81. kWhatAudioTearDown = 'adTD',
  82. kWhatAudioOffloadPauseTimeout = 'aOPT',
  83. };
  84. enum AudioTearDownReason {
  85. kDueToError = 0, // Could restart with either offload or non-offload.
  86. kDueToTimeout,
  87. kForceNonOffload, // Restart only with non-offload.
  88. };
  89. protected:
  90. virtual ~Renderer();
  91. virtual void onMessageReceived(const sp<AMessage> &msg);
  92. private:
  93. enum {
  94. kWhatDrainAudioQueue = 'draA',
  95. kWhatDrainVideoQueue = 'draV',
  96. kWhatPostDrainVideoQueue = 'pDVQ',
  97. kWhatQueueBuffer = 'queB',
  98. kWhatQueueEOS = 'qEOS',
  99. kWhatConfigPlayback = 'cfPB',
  100. kWhatConfigSync = 'cfSy',
  101. kWhatGetPlaybackSettings = 'gPbS',
  102. kWhatGetSyncSettings = 'gSyS',
  103. kWhatFlush = 'flus',
  104. kWhatPause = 'paus',
  105. kWhatResume = 'resm',
  106. kWhatOpenAudioSink = 'opnA',
  107. kWhatCloseAudioSink = 'clsA',
  108. kWhatChangeAudioFormat = 'chgA',
  109. kWhatStopAudioSink = 'stpA',
  110. kWhatDisableOffloadAudio = 'noOA',
  111. kWhatEnableOffloadAudio = 'enOA',
  112. kWhatSetVideoFrameRate = 'sVFR',
  113. };
  114. // if mBuffer != nullptr, it's a buffer containing real data.
  115. // else if mNotifyConsumed == nullptr, it's EOS.
  116. // else it's a tag for re-opening audio sink in different format.
  117. struct QueueEntry {
  118. sp<MediaCodecBuffer> mBuffer;
  119. sp<AMessage> mMeta;
  120. sp<AMessage> mNotifyConsumed;
  121. size_t mOffset;
  122. status_t mFinalResult;
  123. int32_t mBufferOrdinal;
  124. };
  125. static const int64_t kMinPositionUpdateDelayUs;
  126. sp<MediaPlayer2Interface::AudioSink> mAudioSink;
  127. bool mUseVirtualAudioSink;
  128. sp<AMessage> mNotify;
  129. Mutex mLock;
  130. uint32_t mFlags;
  131. List<QueueEntry> mAudioQueue;
  132. List<QueueEntry> mVideoQueue;
  133. uint32_t mNumFramesWritten;
  134. sp<VideoFrameSchedulerBase> mVideoScheduler;
  135. bool mDrainAudioQueuePending;
  136. bool mDrainVideoQueuePending;
  137. int32_t mAudioQueueGeneration;
  138. int32_t mVideoQueueGeneration;
  139. int32_t mAudioDrainGeneration;
  140. int32_t mVideoDrainGeneration;
  141. int32_t mAudioEOSGeneration;
  142. const sp<MediaClock> mMediaClock;
  143. AudioPlaybackRate mPlaybackSettings;
  144. AVSyncSettings mSyncSettings;
  145. float mVideoFpsHint;
  146. int64_t mAudioFirstAnchorTimeMediaUs;
  147. int64_t mAnchorTimeMediaUs;
  148. int64_t mAnchorNumFramesWritten;
  149. int64_t mVideoLateByUs;
  150. int64_t mNextVideoTimeMediaUs;
  151. bool mHasAudio;
  152. bool mHasVideo;
  153. bool mNotifyCompleteAudio;
  154. bool mNotifyCompleteVideo;
  155. bool mSyncQueues;
  156. // modified on only renderer's thread.
  157. bool mPaused;
  158. int64_t mPauseDrainAudioAllowedUs; // time when we can drain/deliver audio in pause mode.
  159. bool mVideoSampleReceived;
  160. bool mVideoRenderingStarted;
  161. int32_t mVideoRenderingStartGeneration;
  162. int32_t mAudioRenderingStartGeneration;
  163. bool mRenderingDataDelivered;
  164. int64_t mNextAudioClockUpdateTimeUs;
  165. // the media timestamp of last audio sample right before EOS.
  166. int64_t mLastAudioMediaTimeUs;
  167. int32_t mAudioOffloadPauseTimeoutGeneration;
  168. bool mAudioTornDown;
  169. audio_offload_info_t mCurrentOffloadInfo;
  170. struct PcmInfo {
  171. audio_channel_mask_t mChannelMask;
  172. audio_output_flags_t mFlags;
  173. audio_format_t mFormat;
  174. int32_t mNumChannels;
  175. int32_t mSampleRate;
  176. };
  177. PcmInfo mCurrentPcmInfo;
  178. static const PcmInfo AUDIO_PCMINFO_INITIALIZER;
  179. int32_t mTotalBuffersQueued;
  180. int32_t mLastAudioBufferDrained;
  181. bool mUseAudioCallback;
  182. sp<JWakeLock> mWakeLock;
  183. status_t getCurrentPositionOnLooper(int64_t *mediaUs);
  184. status_t getCurrentPositionOnLooper(
  185. int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
  186. bool getCurrentPositionIfPaused_l(int64_t *mediaUs);
  187. status_t getCurrentPositionFromAnchor(
  188. int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false);
  189. void notifyEOSCallback();
  190. size_t fillAudioBuffer(void *buffer, size_t size);
  191. bool onDrainAudioQueue();
  192. void drainAudioQueueUntilLastEOS();
  193. int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs);
  194. void postDrainAudioQueue_l(int64_t delayUs = 0);
  195. void clearAnchorTime();
  196. void clearAudioFirstAnchorTime_l();
  197. void setAudioFirstAnchorTimeIfNeeded_l(int64_t mediaUs);
  198. void setVideoLateByUs(int64_t lateUs);
  199. void onNewAudioMediaTime(int64_t mediaTimeUs);
  200. int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs);
  201. void onDrainVideoQueue();
  202. void postDrainVideoQueue();
  203. void prepareForMediaRenderingStart_l();
  204. void notifyIfMediaRenderingStarted_l();
  205. void onQueueBuffer(const sp<AMessage> &msg);
  206. void onQueueEOS(const sp<AMessage> &msg);
  207. void onFlush(const sp<AMessage> &msg);
  208. void onAudioSinkChanged();
  209. void onDisableOffloadAudio();
  210. void onEnableOffloadAudio();
  211. status_t onConfigPlayback(const AudioPlaybackRate &rate /* sanitized */);
  212. status_t onGetPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
  213. status_t onConfigSync(const AVSyncSettings &sync, float videoFpsHint);
  214. status_t onGetSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
  215. void onPause();
  216. void onResume();
  217. void onSetVideoFrameRate(float fps);
  218. int32_t getQueueGeneration(bool audio);
  219. int32_t getDrainGeneration(bool audio);
  220. bool getSyncQueues();
  221. void onAudioTearDown(AudioTearDownReason reason);
  222. status_t onOpenAudioSink(
  223. const sp<AMessage> &format,
  224. bool offloadOnly,
  225. bool hasVideo,
  226. uint32_t flags,
  227. bool isStreaming);
  228. void onCloseAudioSink();
  229. void onChangeAudioFormat(const sp<AMessage> &meta, const sp<AMessage> &notify);
  230. void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0);
  231. void notifyEOS_l(bool audio, status_t finalResult, int64_t delayUs = 0);
  232. void notifyFlushComplete(bool audio);
  233. void notifyPosition();
  234. void notifyVideoLateBy(int64_t lateByUs);
  235. void notifyVideoRenderingStart();
  236. void notifyAudioTearDown(AudioTearDownReason reason);
  237. void flushQueue(List<QueueEntry> *queue);
  238. bool dropBufferIfStale(bool audio, const sp<AMessage> &msg);
  239. void syncQueuesDone_l();
  240. bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; }
  241. void startAudioOffloadPauseTimeout();
  242. void cancelAudioOffloadPauseTimeout();
  243. int64_t getDurationUsIfPlayedAtSampleRate(uint32_t numFrames);
  244. DISALLOW_EVIL_CONSTRUCTORS(Renderer);
  245. };
  246. } // namespace android
  247. #endif // NUPLAYER2_RENDERER_H_