2016-05-24 06:03:12 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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 "NextFrameSeekTask.h"
|
|
|
|
#include "MediaDecoderReaderWrapper.h"
|
|
|
|
#include "mozilla/AbstractThread.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
extern LazyLogModule gMediaSampleLog;
|
|
|
|
|
2016-07-01 13:10:57 +03:00
|
|
|
#define SAMPLE_LOG(x, ...) MOZ_LOG(gMediaSampleLog, LogLevel::Debug, \
|
|
|
|
("[NextFrameSeekTask] Decoder=%p " x, mDecoderID, ##__VA_ARGS__))
|
2016-05-24 06:03:12 +03:00
|
|
|
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
NextFrameSeekTask::NextFrameSeekTask(const void* aDecoderID,
|
2016-07-01 13:10:57 +03:00
|
|
|
AbstractThread* aThread,
|
|
|
|
MediaDecoderReaderWrapper* aReader,
|
2016-07-05 09:12:42 +03:00
|
|
|
const SeekTarget& aTarget,
|
2016-07-01 13:10:57 +03:00
|
|
|
const MediaInfo& aInfo,
|
|
|
|
const media::TimeUnit& aDuration,
|
|
|
|
int64_t aCurrentTime,
|
|
|
|
MediaQueue<MediaData>& aAudioQueue,
|
|
|
|
MediaQueue<MediaData>& aVideoQueue)
|
2016-07-05 09:12:42 +03:00
|
|
|
: SeekTask(aDecoderID, aThread, aReader, aTarget)
|
2016-05-24 06:03:12 +03:00
|
|
|
, mAudioQueue(aAudioQueue)
|
|
|
|
, mVideoQueue(aVideoQueue)
|
2016-07-01 10:59:56 +03:00
|
|
|
, mCurrentTime(aCurrentTime)
|
2016-05-24 06:03:12 +03:00
|
|
|
, mDuration(aDuration)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-06-30 12:36:36 +03:00
|
|
|
MOZ_ASSERT(aInfo.HasVideo());
|
2016-05-24 06:03:12 +03:00
|
|
|
|
|
|
|
// Configure MediaDecoderReaderWrapper.
|
2016-06-30 09:16:18 +03:00
|
|
|
SetCallbacks();
|
2016-05-24 06:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NextFrameSeekTask::~NextFrameSeekTask()
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(mIsDiscarded);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::Discard()
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
|
|
|
|
// Disconnect MDSM.
|
2016-09-10 17:45:34 +03:00
|
|
|
RejectIfExist(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2016-05-24 06:03:12 +03:00
|
|
|
|
|
|
|
// Disconnect MediaDecoderReader.
|
2016-06-30 09:16:18 +03:00
|
|
|
CancelCallbacks();
|
2016-05-24 06:03:12 +03:00
|
|
|
|
|
|
|
mIsDiscarded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
NextFrameSeekTask::NeedToResetMDSM() const
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-01 10:59:56 +03:00
|
|
|
/*
|
|
|
|
* Remove samples from the queue until aCompare() returns false.
|
|
|
|
* aCompare A function object with the signature bool(int64_t) which returns
|
|
|
|
* true for samples that should be removed.
|
|
|
|
*/
|
|
|
|
template <typename Function> static void
|
|
|
|
DiscardFrames(MediaQueue<MediaData>& aQueue, const Function& aCompare)
|
2016-05-24 06:03:12 +03:00
|
|
|
{
|
2016-07-01 10:59:56 +03:00
|
|
|
while(aQueue.GetSize() > 0) {
|
|
|
|
if (aCompare(aQueue.PeekFront()->mTime)) {
|
2016-05-24 06:03:12 +03:00
|
|
|
RefPtr<MediaData> releaseMe = aQueue.PopFront();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<NextFrameSeekTask::SeekTaskPromise>
|
|
|
|
NextFrameSeekTask::Seek(const media::TimeUnit&)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
|
2016-07-01 10:59:56 +03:00
|
|
|
auto currentTime = mCurrentTime;
|
|
|
|
DiscardFrames(mVideoQueue, [currentTime] (int64_t aSampleTime) {
|
|
|
|
return aSampleTime <= currentTime;
|
|
|
|
});
|
2016-05-24 06:03:12 +03:00
|
|
|
|
2016-07-01 10:59:56 +03:00
|
|
|
RefPtr<SeekTaskPromise> promise = mSeekTaskPromise.Ensure(__func__);
|
2016-07-01 12:49:25 +03:00
|
|
|
if (!IsVideoRequestPending() && NeedMoreVideo()) {
|
2016-07-01 12:55:41 +03:00
|
|
|
RequestVideoData();
|
2016-06-30 12:03:12 +03:00
|
|
|
}
|
2016-07-01 10:59:56 +03:00
|
|
|
MaybeFinishSeek(); // Might resolve mSeekTaskPromise and modify audio queue.
|
|
|
|
return promise;
|
2016-05-24 06:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::RequestVideoData()
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
mReader->RequestVideoData(false, media::TimeUnit());
|
|
|
|
}
|
|
|
|
|
2016-07-07 06:23:11 +03:00
|
|
|
bool
|
|
|
|
NextFrameSeekTask::NeedMoreVideo() const
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
// Need to request video when we have none and video queue is not finished.
|
|
|
|
return mVideoQueue.GetSize() == 0 &&
|
|
|
|
!mSeekedVideoData &&
|
|
|
|
!mVideoQueue.IsFinished() &&
|
|
|
|
!mIsVideoQueueFinished;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
NextFrameSeekTask::IsVideoRequestPending() const
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
return mReader->IsRequestingVideoData() || mReader->IsWaitingVideoData();
|
|
|
|
}
|
|
|
|
|
2016-05-24 06:03:12 +03:00
|
|
|
bool
|
2016-07-01 06:43:06 +03:00
|
|
|
NextFrameSeekTask::IsAudioSeekComplete() const
|
2016-05-24 06:03:12 +03:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-07-01 06:43:06 +03:00
|
|
|
// Don't finish seek until there are no pending requests. Otherwise, we might
|
|
|
|
// lose audio samples for the promise is resolved asynchronously.
|
2016-06-30 12:42:57 +03:00
|
|
|
return !mReader->IsRequestingAudioData() && !mReader->IsWaitingAudioData();
|
2016-05-24 06:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-07-07 06:23:11 +03:00
|
|
|
NextFrameSeekTask::IsVideoSeekComplete() const
|
2016-05-24 06:03:12 +03:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-07-07 06:23:11 +03:00
|
|
|
// Don't finish seek until there are no pending requests. Otherwise, we might
|
|
|
|
// lose video samples for the promise is resolved asynchronously.
|
|
|
|
return !IsVideoRequestPending() && !NeedMoreVideo();
|
2016-05-24 06:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-07-07 06:39:56 +03:00
|
|
|
NextFrameSeekTask::MaybeFinishSeek()
|
2016-05-24 06:03:12 +03:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-07-07 06:45:57 +03:00
|
|
|
if (IsAudioSeekComplete() && IsVideoSeekComplete()) {
|
2016-05-24 06:03:12 +03:00
|
|
|
UpdateSeekTargetTime();
|
2016-07-01 10:59:56 +03:00
|
|
|
|
2016-06-30 08:37:20 +03:00
|
|
|
auto time = mTarget.GetTime().ToMicroseconds();
|
2016-07-01 10:59:56 +03:00
|
|
|
DiscardFrames(mAudioQueue, [time] (int64_t aSampleTime) {
|
|
|
|
return aSampleTime < time;
|
|
|
|
});
|
|
|
|
|
2016-05-24 06:03:12 +03:00
|
|
|
Resolve(__func__); // Call to MDSM::SeekCompleted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::OnAudioDecoded(MediaData* aAudioSample)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(aAudioSample);
|
2016-06-30 12:27:55 +03:00
|
|
|
MOZ_ASSERT(!mSeekTaskPromise.IsEmpty(), "Seek shouldn't be finished");
|
2016-05-24 06:03:12 +03:00
|
|
|
|
|
|
|
// The MDSM::mDecodedAudioEndTime will be updated once the whole SeekTask is
|
|
|
|
// resolved.
|
|
|
|
|
2016-08-25 15:30:02 +03:00
|
|
|
SAMPLE_LOG("OnAudioDecoded [%lld,%lld]",
|
2016-06-30 09:18:37 +03:00
|
|
|
aAudioSample->mTime,
|
2016-08-25 15:30:02 +03:00
|
|
|
aAudioSample->GetEndTime());
|
2016-05-24 06:03:12 +03:00
|
|
|
|
|
|
|
// We accept any audio data here.
|
|
|
|
mSeekedAudioData = aAudioSample;
|
|
|
|
|
2016-07-07 06:39:56 +03:00
|
|
|
MaybeFinishSeek();
|
2016-05-24 06:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-10 09:48:53 +03:00
|
|
|
NextFrameSeekTask::OnAudioNotDecoded(const MediaResult& aError)
|
2016-05-24 06:03:12 +03:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-06-30 12:27:55 +03:00
|
|
|
MOZ_ASSERT(!mSeekTaskPromise.IsEmpty(), "Seek shouldn't be finished");
|
2016-05-24 06:03:12 +03:00
|
|
|
|
2016-09-10 09:48:53 +03:00
|
|
|
SAMPLE_LOG("OnAudioNotDecoded (aError=%u)", aError.Code());
|
2016-05-24 06:03:12 +03:00
|
|
|
|
|
|
|
// We don't really handle audio deocde error here. Let MDSM to trigger further
|
|
|
|
// audio decoding tasks if it needs to play audio, and MDSM will then receive
|
|
|
|
// the decoding state from MediaDecoderReader.
|
|
|
|
|
2016-07-07 06:39:56 +03:00
|
|
|
MaybeFinishSeek();
|
2016-05-24 06:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::OnVideoDecoded(MediaData* aVideoSample)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(aVideoSample);
|
2016-06-30 12:27:55 +03:00
|
|
|
MOZ_ASSERT(!mSeekTaskPromise.IsEmpty(), "Seek shouldn't be finished");
|
2016-05-24 06:03:12 +03:00
|
|
|
|
|
|
|
// The MDSM::mDecodedVideoEndTime will be updated once the whole SeekTask is
|
|
|
|
// resolved.
|
|
|
|
|
2016-08-25 15:30:02 +03:00
|
|
|
SAMPLE_LOG("OnVideoDecoded [%lld,%lld]",
|
2016-06-30 09:18:37 +03:00
|
|
|
aVideoSample->mTime,
|
2016-08-25 15:30:02 +03:00
|
|
|
aVideoSample->GetEndTime());
|
2016-05-24 06:03:12 +03:00
|
|
|
|
2016-07-01 10:59:56 +03:00
|
|
|
if (aVideoSample->mTime > mCurrentTime) {
|
2016-05-24 06:03:12 +03:00
|
|
|
mSeekedVideoData = aVideoSample;
|
|
|
|
}
|
|
|
|
|
2016-07-07 06:45:57 +03:00
|
|
|
if (NeedMoreVideo()) {
|
2016-07-01 12:55:41 +03:00
|
|
|
RequestVideoData();
|
2016-07-07 06:45:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-07 06:39:56 +03:00
|
|
|
MaybeFinishSeek();
|
2016-05-24 06:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-10 09:48:53 +03:00
|
|
|
NextFrameSeekTask::OnVideoNotDecoded(const MediaResult& aError)
|
2016-05-24 06:03:12 +03:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-06-30 12:27:55 +03:00
|
|
|
MOZ_ASSERT(!mSeekTaskPromise.IsEmpty(), "Seek shouldn't be finished");
|
2016-05-24 06:03:12 +03:00
|
|
|
|
2016-09-10 09:48:53 +03:00
|
|
|
SAMPLE_LOG("OnVideoNotDecoded (aError=%u)", aError.Code());
|
2016-05-24 06:03:12 +03:00
|
|
|
|
2016-09-10 09:48:53 +03:00
|
|
|
if (aError == NS_ERROR_DOM_MEDIA_END_OF_STREAM) {
|
2016-07-07 07:03:48 +03:00
|
|
|
mIsVideoQueueFinished = true;
|
2016-05-24 06:03:12 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 07:03:48 +03:00
|
|
|
// Video seek not finished.
|
|
|
|
if (NeedMoreVideo()) {
|
2016-09-10 09:48:53 +03:00
|
|
|
switch (aError.Code()) {
|
|
|
|
case NS_ERROR_DOM_MEDIA_WAITING_FOR_DATA:
|
|
|
|
mReader->WaitForData(MediaData::VIDEO_DATA);
|
|
|
|
break;
|
|
|
|
case NS_ERROR_DOM_MEDIA_CANCELED:
|
|
|
|
RequestVideoData();
|
|
|
|
break;
|
|
|
|
case NS_ERROR_DOM_MEDIA_END_OF_STREAM:
|
|
|
|
MOZ_ASSERT(false, "Shouldn't want more data for ended video.");
|
|
|
|
break;
|
|
|
|
default:
|
2016-07-01 11:09:32 +03:00
|
|
|
// We might lose the audio sample after canceling the callbacks.
|
|
|
|
// However it doesn't really matter because MDSM is gonna shut down
|
|
|
|
// when seek fails.
|
|
|
|
CancelCallbacks();
|
2016-07-07 07:03:48 +03:00
|
|
|
// Reject the promise since we can't finish video seek anyway.
|
2016-09-10 17:45:34 +03:00
|
|
|
RejectIfExist(aError, __func__);
|
2016-07-07 07:03:48 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-05-24 06:03:12 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-07 07:03:48 +03:00
|
|
|
MaybeFinishSeek();
|
2016-05-24 06:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-30 09:16:18 +03:00
|
|
|
NextFrameSeekTask::SetCallbacks()
|
2016-05-24 06:03:12 +03:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
|
|
|
|
// Register dummy callbcak for audio decoding since we don't need to handle
|
|
|
|
// the decoded audio samples.
|
2016-06-14 06:22:45 +03:00
|
|
|
mAudioCallback = mReader->AudioCallback().Connect(
|
|
|
|
OwnerThread(), [this] (AudioCallbackData aData) {
|
|
|
|
if (aData.is<MediaData*>()) {
|
|
|
|
OnAudioDecoded(aData.as<MediaData*>());
|
|
|
|
} else {
|
2016-09-10 09:48:53 +03:00
|
|
|
OnAudioNotDecoded(aData.as<MediaResult>());
|
2016-06-14 06:22:45 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mVideoCallback = mReader->VideoCallback().Connect(
|
|
|
|
OwnerThread(), [this] (VideoCallbackData aData) {
|
|
|
|
typedef Tuple<MediaData*, TimeStamp> Type;
|
|
|
|
if (aData.is<Type>()) {
|
|
|
|
OnVideoDecoded(Get<0>(aData.as<Type>()));
|
|
|
|
} else {
|
2016-09-10 09:48:53 +03:00
|
|
|
OnVideoNotDecoded(aData.as<MediaResult>());
|
2016-06-14 06:22:45 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mAudioWaitCallback = mReader->AudioWaitCallback().Connect(
|
|
|
|
OwnerThread(), [this] (WaitCallbackData aData) {
|
|
|
|
// We don't make an audio decode request here, instead, let MDSM to
|
|
|
|
// trigger further audio decode tasks if MDSM itself needs to play audio.
|
2016-07-07 06:39:56 +03:00
|
|
|
MaybeFinishSeek();
|
2016-06-14 06:22:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
mVideoWaitCallback = mReader->VideoWaitCallback().Connect(
|
|
|
|
OwnerThread(), [this] (WaitCallbackData aData) {
|
2016-07-07 07:03:48 +03:00
|
|
|
if (NeedMoreVideo()) {
|
|
|
|
if (aData.is<MediaData::Type>()) {
|
2016-07-01 12:55:41 +03:00
|
|
|
RequestVideoData();
|
2016-07-07 07:03:48 +03:00
|
|
|
} else {
|
|
|
|
// Reject if we can't finish video seeking.
|
2016-07-01 11:09:32 +03:00
|
|
|
CancelCallbacks();
|
2016-09-10 17:45:34 +03:00
|
|
|
RejectIfExist(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2016-07-07 07:03:48 +03:00
|
|
|
}
|
|
|
|
return;
|
2016-06-14 06:22:45 +03:00
|
|
|
}
|
2016-07-07 07:03:48 +03:00
|
|
|
MaybeFinishSeek();
|
2016-06-14 06:22:45 +03:00
|
|
|
});
|
2016-05-24 06:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-30 09:16:18 +03:00
|
|
|
NextFrameSeekTask::CancelCallbacks()
|
2016-05-24 06:03:12 +03:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-07-01 11:09:32 +03:00
|
|
|
mAudioCallback.DisconnectIfExists();
|
|
|
|
mVideoCallback.DisconnectIfExists();
|
|
|
|
mAudioWaitCallback.DisconnectIfExists();
|
|
|
|
mVideoWaitCallback.DisconnectIfExists();
|
2016-05-24 06:03:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::UpdateSeekTargetTime()
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
|
|
|
|
RefPtr<MediaData> data = mVideoQueue.PeekFront();
|
|
|
|
if (data) {
|
2016-06-30 08:37:20 +03:00
|
|
|
mTarget.SetTime(TimeUnit::FromMicroseconds(data->mTime));
|
2016-05-24 06:03:12 +03:00
|
|
|
} else if (mSeekedVideoData) {
|
2016-06-30 08:37:20 +03:00
|
|
|
mTarget.SetTime(TimeUnit::FromMicroseconds(mSeekedVideoData->mTime));
|
2016-05-24 06:03:12 +03:00
|
|
|
} else if (mIsVideoQueueFinished || mVideoQueue.AtEndOfStream()) {
|
2016-06-30 08:37:20 +03:00
|
|
|
mTarget.SetTime(mDuration);
|
2016-05-24 06:03:12 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "No data!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace media
|
|
|
|
} // namespace mozilla
|