2016-03-31 11:32:03 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/MozPromise.h"
|
2017-07-19 12:01:32 +03:00
|
|
|
#include "MediaFormatReader.h"
|
2017-07-27 09:49:57 +03:00
|
|
|
#include "ReaderProxy.h"
|
|
|
|
#include "TimeUnits.h"
|
2016-03-31 11:32:03 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2017-07-27 09:49:57 +03:00
|
|
|
ReaderProxy::ReaderProxy(AbstractThread* aOwnerThread,
|
|
|
|
MediaFormatReader* aReader)
|
2016-11-23 12:34:12 +03:00
|
|
|
: mOwnerThread(aOwnerThread)
|
2016-03-31 11:32:03 +03:00
|
|
|
, mReader(aReader)
|
2017-07-06 06:45:02 +03:00
|
|
|
, mWatchManager(this, aReader->OwnerThread())
|
|
|
|
, mDuration(aReader->OwnerThread(),
|
2017-07-27 09:49:57 +03:00
|
|
|
media::NullableTimeUnit(),
|
|
|
|
"ReaderProxy::mDuration (Mirror)")
|
2017-11-22 06:17:09 +03:00
|
|
|
, mSeamlessLoopingBlocked(false)
|
2017-11-10 07:06:12 +03:00
|
|
|
, mSeamlessLoopingEnabled(false)
|
2016-12-21 12:37:56 +03:00
|
|
|
{
|
|
|
|
// Must support either heuristic buffering or WaitForData().
|
|
|
|
MOZ_ASSERT(mReader->UseBufferingHeuristics() ||
|
|
|
|
mReader->IsWaitForDataSupported());
|
|
|
|
}
|
2016-03-31 11:32:03 +03:00
|
|
|
|
2017-07-27 09:49:57 +03:00
|
|
|
ReaderProxy::~ReaderProxy()
|
2016-03-31 11:32:03 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
media::TimeUnit
|
2017-07-27 09:49:57 +03:00
|
|
|
ReaderProxy::StartTime() const
|
2016-03-31 11:32:03 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2016-10-14 14:10:24 +03:00
|
|
|
return mStartTime.ref();
|
2016-03-31 11:32:03 +03:00
|
|
|
}
|
|
|
|
|
2017-07-27 09:49:57 +03:00
|
|
|
RefPtr<ReaderProxy::MetadataPromise>
|
|
|
|
ReaderProxy::ReadMetadata()
|
2016-03-31 11:32:03 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
MOZ_ASSERT(!mShutdown);
|
2017-07-19 12:01:32 +03:00
|
|
|
return InvokeAsync(mReader->OwnerThread(),
|
|
|
|
mReader.get(),
|
|
|
|
__func__,
|
|
|
|
&MediaFormatReader::AsyncReadMetadata)
|
|
|
|
->Then(mOwnerThread,
|
|
|
|
__func__,
|
|
|
|
this,
|
2017-07-27 09:49:57 +03:00
|
|
|
&ReaderProxy::OnMetadataRead,
|
|
|
|
&ReaderProxy::OnMetadataNotRead);
|
2016-03-31 11:32:03 +03:00
|
|
|
}
|
|
|
|
|
2017-11-10 06:19:04 +03:00
|
|
|
RefPtr<ReaderProxy::AudioDataPromise>
|
|
|
|
ReaderProxy::OnAudioDataRequestCompleted(RefPtr<AudioData> aAudio)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
|
2017-11-10 08:50:38 +03:00
|
|
|
// Subtract the start time and add the looping-offset time.
|
|
|
|
int64_t offset =
|
|
|
|
StartTime().ToMicroseconds() - mLoopingOffset.ToMicroseconds();
|
|
|
|
aAudio->AdjustForStartTime(offset);
|
|
|
|
mLastAudioEndTime = aAudio->mTime;
|
2017-11-10 06:19:04 +03:00
|
|
|
return AudioDataPromise::CreateAndResolve(aAudio.forget(), __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ReaderProxy::AudioDataPromise>
|
|
|
|
ReaderProxy::OnAudioDataRequestFailed(const MediaResult& aError)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
|
2017-11-22 06:17:09 +03:00
|
|
|
if (mSeamlessLoopingBlocked || !mSeamlessLoopingEnabled ||
|
|
|
|
aError.Code() != NS_ERROR_DOM_MEDIA_END_OF_STREAM) {
|
|
|
|
return AudioDataPromise::CreateAndReject(aError, __func__);
|
|
|
|
}
|
|
|
|
|
2017-11-10 08:50:38 +03:00
|
|
|
// The data time in the audio queue is assumed to be increased linearly,
|
|
|
|
// so we need to add the last ending time as the offset to correct the
|
|
|
|
// audio data time in the next round when seamless looping is enabled.
|
|
|
|
mLoopingOffset = mLastAudioEndTime;
|
|
|
|
|
2017-11-24 05:28:42 +03:00
|
|
|
// Save the duration of the audio track if it hasn't been set.
|
|
|
|
if (!mAudioDuration.IsValid()) {
|
|
|
|
mAudioDuration = mLastAudioEndTime;
|
|
|
|
}
|
|
|
|
|
2017-11-22 06:17:09 +03:00
|
|
|
// For seamless looping, the demuxer is sought to the beginning and then
|
|
|
|
// keep requesting decoded data in advance, upon receiving EOS.
|
|
|
|
// The MDSM will not be aware of the EOS and keep receiving decoded data
|
|
|
|
// as usual while looping is on.
|
|
|
|
RefPtr<ReaderProxy> self = this;
|
|
|
|
RefPtr<MediaFormatReader> reader = mReader;
|
|
|
|
ResetDecode(TrackInfo::kAudioTrack);
|
2017-11-10 08:50:38 +03:00
|
|
|
return SeekInternal(SeekTarget(media::TimeUnit::Zero(), SeekTarget::Accurate))
|
2017-11-22 06:17:09 +03:00
|
|
|
->Then(mReader->OwnerThread(),
|
|
|
|
__func__,
|
|
|
|
[reader]() { return reader->RequestAudioData(); },
|
|
|
|
[](const SeekRejectValue& aReject) {
|
|
|
|
return AudioDataPromise::CreateAndReject(aReject.mError, __func__);
|
|
|
|
})
|
|
|
|
->Then(mOwnerThread,
|
|
|
|
__func__,
|
|
|
|
[self](RefPtr<AudioData> aAudio) {
|
|
|
|
return self->OnAudioDataRequestCompleted(aAudio.forget());
|
|
|
|
},
|
|
|
|
[](const MediaResult& aError) {
|
|
|
|
return AudioDataPromise::CreateAndReject(aError, __func__);
|
|
|
|
});
|
2017-11-10 06:19:04 +03:00
|
|
|
}
|
|
|
|
|
2017-07-27 09:49:57 +03:00
|
|
|
RefPtr<ReaderProxy::AudioDataPromise>
|
|
|
|
ReaderProxy::RequestAudioData()
|
2016-03-31 11:32:03 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
MOZ_ASSERT(!mShutdown);
|
|
|
|
|
2017-11-22 06:17:09 +03:00
|
|
|
mSeamlessLoopingBlocked = false;
|
2017-07-19 12:01:32 +03:00
|
|
|
return InvokeAsync(mReader->OwnerThread(),
|
|
|
|
mReader.get(),
|
|
|
|
__func__,
|
|
|
|
&MediaFormatReader::RequestAudioData)
|
|
|
|
->Then(mOwnerThread,
|
|
|
|
__func__,
|
2017-11-10 06:19:04 +03:00
|
|
|
this,
|
|
|
|
&ReaderProxy::OnAudioDataRequestCompleted,
|
|
|
|
&ReaderProxy::OnAudioDataRequestFailed);
|
2016-03-31 11:32:03 +03:00
|
|
|
}
|
|
|
|
|
2017-07-27 09:49:57 +03:00
|
|
|
RefPtr<ReaderProxy::VideoDataPromise>
|
|
|
|
ReaderProxy::RequestVideoData(const media::TimeUnit& aTimeThreshold)
|
2016-03-31 11:32:03 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
MOZ_ASSERT(!mShutdown);
|
2016-04-27 09:50:23 +03:00
|
|
|
|
2017-11-22 06:17:09 +03:00
|
|
|
mSeamlessLoopingBlocked = false;
|
2017-06-19 05:45:55 +03:00
|
|
|
const auto threshold = aTimeThreshold > media::TimeUnit::Zero()
|
|
|
|
? aTimeThreshold + StartTime()
|
|
|
|
: aTimeThreshold;
|
2016-03-31 11:32:03 +03:00
|
|
|
|
2016-12-20 08:17:05 +03:00
|
|
|
int64_t startTime = StartTime().ToMicroseconds();
|
2017-07-19 12:01:32 +03:00
|
|
|
return InvokeAsync(mReader->OwnerThread(),
|
|
|
|
mReader.get(),
|
|
|
|
__func__,
|
|
|
|
&MediaFormatReader::RequestVideoData,
|
|
|
|
threshold)
|
|
|
|
->Then(mOwnerThread,
|
|
|
|
__func__,
|
|
|
|
[startTime](RefPtr<VideoData> aVideo) {
|
|
|
|
aVideo->AdjustForStartTime(startTime);
|
|
|
|
return VideoDataPromise::CreateAndResolve(aVideo.forget(),
|
|
|
|
__func__);
|
|
|
|
},
|
|
|
|
[](const MediaResult& aError) {
|
|
|
|
return VideoDataPromise::CreateAndReject(aError, __func__);
|
|
|
|
});
|
2016-03-31 11:32:03 +03:00
|
|
|
}
|
|
|
|
|
2017-07-27 09:49:57 +03:00
|
|
|
RefPtr<ReaderProxy::SeekPromise>
|
|
|
|
ReaderProxy::Seek(const SeekTarget& aTarget)
|
2016-03-31 11:32:03 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2017-11-22 06:17:09 +03:00
|
|
|
mSeamlessLoopingBlocked = true;
|
2017-11-10 08:50:38 +03:00
|
|
|
// Reset the members for seamless looping if the seek is triggered outside.
|
|
|
|
mLoopingOffset = media::TimeUnit::Zero();
|
|
|
|
mLastAudioEndTime = media::TimeUnit::Zero();
|
2017-11-24 05:28:42 +03:00
|
|
|
mAudioDuration = media::TimeUnit::Invalid();
|
2017-11-10 08:50:38 +03:00
|
|
|
return SeekInternal(aTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ReaderProxy::SeekPromise>
|
|
|
|
ReaderProxy::SeekInternal(const SeekTarget& aTarget)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2016-11-13 09:42:19 +03:00
|
|
|
SeekTarget adjustedTarget = aTarget;
|
|
|
|
adjustedTarget.SetTime(adjustedTarget.GetTime() + StartTime());
|
2017-07-19 12:01:32 +03:00
|
|
|
return InvokeAsync(mReader->OwnerThread(),
|
|
|
|
mReader.get(),
|
|
|
|
__func__,
|
|
|
|
&MediaFormatReader::Seek,
|
|
|
|
Move(adjustedTarget));
|
2016-03-31 11:32:03 +03:00
|
|
|
}
|
|
|
|
|
2017-07-27 09:49:57 +03:00
|
|
|
RefPtr<ReaderProxy::WaitForDataPromise>
|
|
|
|
ReaderProxy::WaitForData(MediaData::Type aType)
|
2016-04-19 11:14:30 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
2016-12-21 12:37:56 +03:00
|
|
|
MOZ_ASSERT(mReader->IsWaitForDataSupported());
|
2017-07-19 12:01:32 +03:00
|
|
|
return InvokeAsync(mReader->OwnerThread(),
|
|
|
|
mReader.get(),
|
|
|
|
__func__,
|
|
|
|
&MediaFormatReader::WaitForData,
|
|
|
|
aType);
|
2016-04-19 11:14:30 +03:00
|
|
|
}
|
|
|
|
|
2016-03-31 11:32:03 +03:00
|
|
|
void
|
2017-07-27 09:49:57 +03:00
|
|
|
ReaderProxy::ReleaseResources()
|
2016-04-19 11:14:30 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
nsCOMPtr<nsIRunnable> r =
|
2017-07-19 12:01:32 +03:00
|
|
|
NewRunnableMethod("MediaFormatReader::ReleaseResources",
|
2017-06-12 22:34:10 +03:00
|
|
|
mReader,
|
2017-07-19 12:01:32 +03:00
|
|
|
&MediaFormatReader::ReleaseResources);
|
2017-11-15 09:58:03 +03:00
|
|
|
nsresult rv = mReader->OwnerThread()->Dispatch(r.forget());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2016-04-19 11:14:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-07-27 09:49:57 +03:00
|
|
|
ReaderProxy::ResetDecode(TrackSet aTracks)
|
2016-04-19 11:14:30 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
nsCOMPtr<nsIRunnable> r =
|
2017-07-19 12:01:32 +03:00
|
|
|
NewRunnableMethod<TrackSet>("MediaFormatReader::ResetDecode",
|
2017-06-12 22:34:10 +03:00
|
|
|
mReader,
|
2017-07-19 12:01:32 +03:00
|
|
|
&MediaFormatReader::ResetDecode,
|
2016-05-31 07:32:37 +03:00
|
|
|
aTracks);
|
2017-11-15 09:58:03 +03:00
|
|
|
nsresult rv = mReader->OwnerThread()->Dispatch(r.forget());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2016-04-19 11:14:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ShutdownPromise>
|
2017-07-27 09:49:57 +03:00
|
|
|
ReaderProxy::Shutdown()
|
2016-03-31 11:32:03 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
mShutdown = true;
|
2017-07-27 09:49:57 +03:00
|
|
|
RefPtr<ReaderProxy> self = this;
|
2017-07-06 06:45:02 +03:00
|
|
|
return InvokeAsync(mReader->OwnerThread(), __func__, [self]() {
|
|
|
|
self->mDuration.DisconnectIfConnected();
|
|
|
|
self->mWatchManager.Shutdown();
|
|
|
|
return self->mReader->Shutdown();
|
|
|
|
});
|
2016-03-31 11:32:03 +03:00
|
|
|
}
|
|
|
|
|
2017-07-27 09:49:57 +03:00
|
|
|
RefPtr<ReaderProxy::MetadataPromise>
|
|
|
|
ReaderProxy::OnMetadataRead(MetadataHolder&& aMetadata)
|
2016-03-31 11:32:03 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
if (mShutdown) {
|
2017-05-09 18:31:32 +03:00
|
|
|
return MetadataPromise::CreateAndReject(
|
|
|
|
NS_ERROR_DOM_MEDIA_ABORT_ERR, __func__);
|
2016-03-31 11:32:03 +03:00
|
|
|
}
|
2016-10-14 14:10:24 +03:00
|
|
|
|
|
|
|
if (mStartTime.isNothing()) {
|
2017-05-18 05:20:44 +03:00
|
|
|
mStartTime.emplace(aMetadata.mInfo->mStartTime);
|
2016-03-31 11:32:03 +03:00
|
|
|
}
|
2017-05-18 05:20:44 +03:00
|
|
|
return MetadataPromise::CreateAndResolve(Move(aMetadata), __func__);
|
2017-05-09 18:31:32 +03:00
|
|
|
}
|
|
|
|
|
2017-07-27 09:49:57 +03:00
|
|
|
RefPtr<ReaderProxy::MetadataPromise>
|
|
|
|
ReaderProxy::OnMetadataNotRead(const MediaResult& aError)
|
2017-05-09 18:31:32 +03:00
|
|
|
{
|
|
|
|
return MetadataPromise::CreateAndReject(aError, __func__);
|
2016-03-31 11:32:03 +03:00
|
|
|
}
|
|
|
|
|
2016-07-29 09:51:18 +03:00
|
|
|
void
|
2017-07-27 09:49:57 +03:00
|
|
|
ReaderProxy::SetVideoBlankDecode(bool aIsBlankDecode)
|
2016-07-29 09:51:18 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
nsCOMPtr<nsIRunnable> r =
|
2017-07-19 12:01:32 +03:00
|
|
|
NewRunnableMethod<bool>("MediaFormatReader::SetVideoNullDecode",
|
2017-06-12 22:34:10 +03:00
|
|
|
mReader,
|
2017-07-19 12:01:32 +03:00
|
|
|
&MediaFormatReader::SetVideoNullDecode,
|
2016-07-29 09:51:18 +03:00
|
|
|
aIsBlankDecode);
|
2017-11-15 09:58:03 +03:00
|
|
|
nsresult rv = mReader->OwnerThread()->Dispatch(r.forget());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2016-07-29 09:51:18 +03:00
|
|
|
}
|
|
|
|
|
2017-07-06 06:45:02 +03:00
|
|
|
void
|
2017-07-27 09:49:57 +03:00
|
|
|
ReaderProxy::UpdateDuration()
|
2017-07-06 06:45:02 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mReader->OwnerThread()->IsCurrentThreadIn());
|
|
|
|
mReader->UpdateDuration(mDuration.Ref().ref());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-07-27 09:49:57 +03:00
|
|
|
ReaderProxy::SetCanonicalDuration(
|
2017-07-06 06:45:02 +03:00
|
|
|
AbstractCanonical<media::NullableTimeUnit>* aCanonical)
|
|
|
|
{
|
|
|
|
using DurationT = AbstractCanonical<media::NullableTimeUnit>;
|
2017-07-27 09:49:57 +03:00
|
|
|
RefPtr<ReaderProxy> self = this;
|
2017-07-06 06:45:02 +03:00
|
|
|
RefPtr<DurationT> canonical = aCanonical;
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
2017-07-27 09:49:57 +03:00
|
|
|
"ReaderProxy::SetCanonicalDuration", [this, self, canonical]() {
|
2017-07-06 06:45:02 +03:00
|
|
|
mDuration.Connect(canonical);
|
2017-07-27 09:49:57 +03:00
|
|
|
mWatchManager.Watch(mDuration, &ReaderProxy::UpdateDuration);
|
2017-07-06 06:45:02 +03:00
|
|
|
});
|
2017-11-15 09:58:03 +03:00
|
|
|
nsresult rv = mReader->OwnerThread()->Dispatch(r.forget());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2017-07-06 06:45:02 +03:00
|
|
|
}
|
|
|
|
|
2017-11-10 07:06:12 +03:00
|
|
|
void
|
|
|
|
ReaderProxy::SetSeamlessLoopingEnabled(bool aEnabled)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
mSeamlessLoopingEnabled = aEnabled;
|
|
|
|
}
|
|
|
|
|
2017-11-24 05:28:42 +03:00
|
|
|
void
|
|
|
|
ReaderProxy::AdjustByLooping(media::TimeUnit& aTime)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwnerThread->IsCurrentThreadIn());
|
|
|
|
MOZ_ASSERT(!mShutdown);
|
|
|
|
MOZ_ASSERT(!mSeamlessLoopingEnabled || !mSeamlessLoopingBlocked);
|
|
|
|
if (mAudioDuration.IsValid() && mAudioDuration.IsPositive()) {
|
|
|
|
aTime = aTime % mAudioDuration.ToMicroseconds();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 11:32:03 +03:00
|
|
|
} // namespace mozilla
|