MediaDecoderReaderWrapper.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/MozPromise.h"
  6. #include "MediaDecoderReaderWrapper.h"
  7. namespace mozilla {
  8. extern LazyLogModule gMediaDecoderLog;
  9. #undef LOG
  10. #define LOG(...) \
  11. MOZ_LOG(gMediaDecoderLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
  12. // StartTimeRendezvous is a helper class that quarantines the first sample
  13. // until it gets a sample from both channels, such that we can be guaranteed
  14. // to know the start time by the time On{Audio,Video}Decoded is called on MDSM.
  15. class StartTimeRendezvous {
  16. typedef MediaDecoderReader::MediaDataPromise MediaDataPromise;
  17. NS_INLINE_DECL_THREADSAFE_REFCOUNTING(StartTimeRendezvous);
  18. public:
  19. StartTimeRendezvous(AbstractThread* aOwnerThread,
  20. bool aHasAudio,
  21. bool aHasVideo,
  22. bool aForceZeroStartTime)
  23. : mOwnerThread(aOwnerThread)
  24. {
  25. if (aForceZeroStartTime) {
  26. mAudioStartTime.emplace(0);
  27. mVideoStartTime.emplace(0);
  28. return;
  29. }
  30. if (!aHasAudio) {
  31. mAudioStartTime.emplace(INT64_MAX);
  32. }
  33. if (!aHasVideo) {
  34. mVideoStartTime.emplace(INT64_MAX);
  35. }
  36. }
  37. void Destroy()
  38. {
  39. mAudioStartTime = Some(mAudioStartTime.refOr(INT64_MAX));
  40. mVideoStartTime = Some(mVideoStartTime.refOr(INT64_MAX));
  41. mHaveStartTimePromise.RejectIfExists(false, __func__);
  42. }
  43. RefPtr<HaveStartTimePromise> AwaitStartTime()
  44. {
  45. if (HaveStartTime()) {
  46. return HaveStartTimePromise::CreateAndResolve(true, __func__);
  47. }
  48. return mHaveStartTimePromise.Ensure(__func__);
  49. }
  50. template<MediaData::Type SampleType>
  51. RefPtr<MediaDataPromise>
  52. ProcessFirstSample(MediaData* aData)
  53. {
  54. typedef typename MediaDataPromise::Private PromisePrivate;
  55. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  56. MaybeSetChannelStartTime<SampleType>(aData->mTime);
  57. RefPtr<PromisePrivate> p = new PromisePrivate(__func__);
  58. RefPtr<MediaData> data = aData;
  59. RefPtr<StartTimeRendezvous> self = this;
  60. AwaitStartTime()->Then(
  61. mOwnerThread, __func__,
  62. [p, data, self] () {
  63. MOZ_ASSERT(self->mOwnerThread->IsCurrentThreadIn());
  64. p->Resolve(data, __func__);
  65. },
  66. [p] () {
  67. p->Reject(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
  68. });
  69. return p.forget();
  70. }
  71. template<MediaData::Type SampleType>
  72. void FirstSampleRejected(const MediaResult& aError)
  73. {
  74. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  75. if (aError == NS_ERROR_DOM_MEDIA_END_OF_STREAM) {
  76. LOG("StartTimeRendezvous=%p SampleType(%d) Has no samples.",
  77. this, SampleType);
  78. MaybeSetChannelStartTime<SampleType>(INT64_MAX);
  79. } else if (aError != NS_ERROR_DOM_MEDIA_WAITING_FOR_DATA) {
  80. mHaveStartTimePromise.RejectIfExists(false, __func__);
  81. }
  82. }
  83. bool HaveStartTime() const
  84. {
  85. return mAudioStartTime.isSome() && mVideoStartTime.isSome();
  86. }
  87. int64_t StartTime() const
  88. {
  89. int64_t time = std::min(mAudioStartTime.ref(), mVideoStartTime.ref());
  90. return time == INT64_MAX ? 0 : time;
  91. }
  92. private:
  93. ~StartTimeRendezvous() {}
  94. template<MediaData::Type SampleType>
  95. void MaybeSetChannelStartTime(int64_t aStartTime)
  96. {
  97. if (ChannelStartTime(SampleType).isSome()) {
  98. // If we're initialized with aForceZeroStartTime=true, the channel start
  99. // times are already set.
  100. return;
  101. }
  102. LOG("StartTimeRendezvous=%p Setting SampleType(%d) start time to %lld",
  103. this, SampleType, aStartTime);
  104. ChannelStartTime(SampleType).emplace(aStartTime);
  105. if (HaveStartTime()) {
  106. mHaveStartTimePromise.ResolveIfExists(true, __func__);
  107. }
  108. }
  109. Maybe<int64_t>& ChannelStartTime(MediaData::Type aType)
  110. {
  111. return aType == MediaData::AUDIO_DATA ? mAudioStartTime : mVideoStartTime;
  112. }
  113. MozPromiseHolder<HaveStartTimePromise> mHaveStartTimePromise;
  114. RefPtr<AbstractThread> mOwnerThread;
  115. Maybe<int64_t> mAudioStartTime;
  116. Maybe<int64_t> mVideoStartTime;
  117. };
  118. MediaDecoderReaderWrapper::MediaDecoderReaderWrapper(AbstractThread* aOwnerThread,
  119. MediaDecoderReader* aReader)
  120. : mForceZeroStartTime(aReader->ForceZeroStartTime())
  121. , mOwnerThread(aOwnerThread)
  122. , mReader(aReader)
  123. {}
  124. MediaDecoderReaderWrapper::~MediaDecoderReaderWrapper()
  125. {}
  126. media::TimeUnit
  127. MediaDecoderReaderWrapper::StartTime() const
  128. {
  129. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  130. MOZ_ASSERT(!mShutdown);
  131. return media::TimeUnit::FromMicroseconds(mStartTimeRendezvous->StartTime());
  132. }
  133. RefPtr<MediaDecoderReaderWrapper::MetadataPromise>
  134. MediaDecoderReaderWrapper::ReadMetadata()
  135. {
  136. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  137. MOZ_ASSERT(!mShutdown);
  138. return InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
  139. &MediaDecoderReader::AsyncReadMetadata)
  140. ->Then(mOwnerThread, __func__, this,
  141. &MediaDecoderReaderWrapper::OnMetadataRead,
  142. &MediaDecoderReaderWrapper::OnMetadataNotRead)
  143. ->CompletionPromise();
  144. }
  145. RefPtr<HaveStartTimePromise>
  146. MediaDecoderReaderWrapper::AwaitStartTime()
  147. {
  148. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  149. MOZ_ASSERT(!mShutdown);
  150. return mStartTimeRendezvous->AwaitStartTime();
  151. }
  152. void
  153. MediaDecoderReaderWrapper::RequestAudioData()
  154. {
  155. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  156. MOZ_ASSERT(!mShutdown);
  157. auto p = InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
  158. &MediaDecoderReader::RequestAudioData);
  159. if (!mStartTimeRendezvous->HaveStartTime()) {
  160. p = p->Then(mOwnerThread, __func__, mStartTimeRendezvous.get(),
  161. &StartTimeRendezvous::ProcessFirstSample<MediaData::AUDIO_DATA>,
  162. &StartTimeRendezvous::FirstSampleRejected<MediaData::AUDIO_DATA>)
  163. ->CompletionPromise();
  164. }
  165. RefPtr<MediaDecoderReaderWrapper> self = this;
  166. mAudioDataRequest.Begin(p->Then(mOwnerThread, __func__,
  167. [self] (MediaData* aAudioSample) {
  168. self->mAudioDataRequest.Complete();
  169. aAudioSample->AdjustForStartTime(self->StartTime().ToMicroseconds());
  170. self->mAudioCallback.Notify(AsVariant(aAudioSample));
  171. },
  172. [self] (const MediaResult& aError) {
  173. self->mAudioDataRequest.Complete();
  174. self->mAudioCallback.Notify(AsVariant(aError));
  175. }));
  176. }
  177. void
  178. MediaDecoderReaderWrapper::RequestVideoData(bool aSkipToNextKeyframe,
  179. media::TimeUnit aTimeThreshold)
  180. {
  181. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  182. MOZ_ASSERT(!mShutdown);
  183. // Time the video decode and send this value back to callbacks who accept
  184. // a TimeStamp as its second parameter.
  185. TimeStamp videoDecodeStartTime = TimeStamp::Now();
  186. if (aTimeThreshold.ToMicroseconds() > 0 &&
  187. mStartTimeRendezvous->HaveStartTime()) {
  188. aTimeThreshold += StartTime();
  189. }
  190. auto p = InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
  191. &MediaDecoderReader::RequestVideoData,
  192. aSkipToNextKeyframe, aTimeThreshold.ToMicroseconds());
  193. if (!mStartTimeRendezvous->HaveStartTime()) {
  194. p = p->Then(mOwnerThread, __func__, mStartTimeRendezvous.get(),
  195. &StartTimeRendezvous::ProcessFirstSample<MediaData::VIDEO_DATA>,
  196. &StartTimeRendezvous::FirstSampleRejected<MediaData::VIDEO_DATA>)
  197. ->CompletionPromise();
  198. }
  199. RefPtr<MediaDecoderReaderWrapper> self = this;
  200. mVideoDataRequest.Begin(p->Then(mOwnerThread, __func__,
  201. [self, videoDecodeStartTime] (MediaData* aVideoSample) {
  202. self->mVideoDataRequest.Complete();
  203. aVideoSample->AdjustForStartTime(self->StartTime().ToMicroseconds());
  204. self->mVideoCallback.Notify(AsVariant(MakeTuple(aVideoSample, videoDecodeStartTime)));
  205. },
  206. [self] (const MediaResult& aError) {
  207. self->mVideoDataRequest.Complete();
  208. self->mVideoCallback.Notify(AsVariant(aError));
  209. }));
  210. }
  211. bool
  212. MediaDecoderReaderWrapper::IsRequestingAudioData() const
  213. {
  214. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  215. return mAudioDataRequest.Exists();
  216. }
  217. bool
  218. MediaDecoderReaderWrapper::IsRequestingVideoData() const
  219. {
  220. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  221. return mVideoDataRequest.Exists();
  222. }
  223. bool
  224. MediaDecoderReaderWrapper::IsWaitingAudioData() const
  225. {
  226. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  227. return mAudioWaitRequest.Exists();
  228. }
  229. bool
  230. MediaDecoderReaderWrapper::IsWaitingVideoData() const
  231. {
  232. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  233. return mVideoWaitRequest.Exists();
  234. }
  235. RefPtr<MediaDecoderReader::SeekPromise>
  236. MediaDecoderReaderWrapper::Seek(SeekTarget aTarget, media::TimeUnit aEndTime)
  237. {
  238. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  239. aTarget.SetTime(aTarget.GetTime() + StartTime());
  240. return InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
  241. &MediaDecoderReader::Seek, aTarget,
  242. aEndTime.ToMicroseconds());
  243. }
  244. void
  245. MediaDecoderReaderWrapper::WaitForData(MediaData::Type aType)
  246. {
  247. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  248. auto p = InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
  249. &MediaDecoderReader::WaitForData, aType);
  250. RefPtr<MediaDecoderReaderWrapper> self = this;
  251. WaitRequestRef(aType).Begin(p->Then(mOwnerThread, __func__,
  252. [self] (MediaData::Type aType) {
  253. self->WaitRequestRef(aType).Complete();
  254. self->WaitCallbackRef(aType).Notify(AsVariant(aType));
  255. },
  256. [self, aType] (WaitForDataRejectValue aRejection) {
  257. self->WaitRequestRef(aType).Complete();
  258. self->WaitCallbackRef(aType).Notify(AsVariant(aRejection));
  259. }));
  260. }
  261. MediaCallbackExc<WaitCallbackData>&
  262. MediaDecoderReaderWrapper::WaitCallbackRef(MediaData::Type aType)
  263. {
  264. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  265. return aType == MediaData::AUDIO_DATA ? mAudioWaitCallback : mVideoWaitCallback;
  266. }
  267. MozPromiseRequestHolder<MediaDecoderReader::WaitForDataPromise>&
  268. MediaDecoderReaderWrapper::WaitRequestRef(MediaData::Type aType)
  269. {
  270. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  271. return aType == MediaData::AUDIO_DATA ? mAudioWaitRequest : mVideoWaitRequest;
  272. }
  273. RefPtr<MediaDecoderReaderWrapper::BufferedUpdatePromise>
  274. MediaDecoderReaderWrapper::UpdateBufferedWithPromise()
  275. {
  276. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  277. return InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
  278. &MediaDecoderReader::UpdateBufferedWithPromise);
  279. }
  280. void
  281. MediaDecoderReaderWrapper::ReleaseResources()
  282. {
  283. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  284. nsCOMPtr<nsIRunnable> r =
  285. NewRunnableMethod(mReader, &MediaDecoderReader::ReleaseResources);
  286. mReader->OwnerThread()->Dispatch(r.forget());
  287. }
  288. void
  289. MediaDecoderReaderWrapper::SetIdle()
  290. {
  291. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  292. nsCOMPtr<nsIRunnable> r =
  293. NewRunnableMethod(mReader, &MediaDecoderReader::SetIdle);
  294. mReader->OwnerThread()->Dispatch(r.forget());
  295. }
  296. void
  297. MediaDecoderReaderWrapper::ResetDecode(TrackSet aTracks)
  298. {
  299. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  300. if (aTracks.contains(TrackInfo::kAudioTrack)) {
  301. mAudioDataRequest.DisconnectIfExists();
  302. mAudioWaitRequest.DisconnectIfExists();
  303. }
  304. if (aTracks.contains(TrackInfo::kVideoTrack)) {
  305. mVideoDataRequest.DisconnectIfExists();
  306. mVideoWaitRequest.DisconnectIfExists();
  307. }
  308. nsCOMPtr<nsIRunnable> r =
  309. NewRunnableMethod<TrackSet>(mReader,
  310. &MediaDecoderReader::ResetDecode,
  311. aTracks);
  312. mReader->OwnerThread()->Dispatch(r.forget());
  313. }
  314. RefPtr<ShutdownPromise>
  315. MediaDecoderReaderWrapper::Shutdown()
  316. {
  317. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  318. MOZ_ASSERT(!mAudioDataRequest.Exists());
  319. MOZ_ASSERT(!mVideoDataRequest.Exists());
  320. mShutdown = true;
  321. if (mStartTimeRendezvous) {
  322. mStartTimeRendezvous->Destroy();
  323. mStartTimeRendezvous = nullptr;
  324. }
  325. return InvokeAsync(mReader->OwnerThread(), mReader.get(), __func__,
  326. &MediaDecoderReader::Shutdown);
  327. }
  328. void
  329. MediaDecoderReaderWrapper::OnMetadataRead(MetadataHolder* aMetadata)
  330. {
  331. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  332. if (mShutdown) {
  333. return;
  334. }
  335. // Set up the start time rendezvous if it doesn't already exist (which is
  336. // generally the case, unless we're coming out of dormant mode).
  337. if (!mStartTimeRendezvous) {
  338. mStartTimeRendezvous = new StartTimeRendezvous(
  339. mOwnerThread, aMetadata->mInfo.HasAudio(),
  340. aMetadata->mInfo.HasVideo(), mForceZeroStartTime);
  341. RefPtr<MediaDecoderReaderWrapper> self = this;
  342. mStartTimeRendezvous->AwaitStartTime()->Then(
  343. mOwnerThread, __func__,
  344. [self] () {
  345. NS_ENSURE_TRUE_VOID(!self->mShutdown);
  346. self->mReader->DispatchSetStartTime(self->StartTime().ToMicroseconds());
  347. },
  348. [] () {
  349. NS_WARNING("Setting start time on reader failed");
  350. });
  351. }
  352. }
  353. void
  354. MediaDecoderReaderWrapper::SetVideoBlankDecode(bool aIsBlankDecode)
  355. {
  356. MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
  357. nsCOMPtr<nsIRunnable> r =
  358. NewRunnableMethod<bool>(mReader, &MediaDecoderReader::SetVideoBlankDecode,
  359. aIsBlankDecode);
  360. mReader->OwnerThread()->Dispatch(r.forget());
  361. }
  362. } // namespace mozilla