2016-05-24 03:03:12 +00: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 10:10:57 +00:00
|
|
|
#define SAMPLE_LOG(x, ...) MOZ_LOG(gMediaSampleLog, LogLevel::Debug, \
|
|
|
|
("[NextFrameSeekTask] Decoder=%p " x, mDecoderID, ##__VA_ARGS__))
|
2016-05-24 03:03:12 +00:00
|
|
|
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
NextFrameSeekTask::NextFrameSeekTask(const void* aDecoderID,
|
2016-07-01 10:10:57 +00:00
|
|
|
AbstractThread* aThread,
|
|
|
|
MediaDecoderReaderWrapper* aReader,
|
2016-07-05 06:12:42 +00:00
|
|
|
const SeekTarget& aTarget,
|
2016-07-01 10:10:57 +00:00
|
|
|
const MediaInfo& aInfo,
|
|
|
|
const media::TimeUnit& aDuration,
|
|
|
|
int64_t aCurrentTime,
|
|
|
|
MediaQueue<MediaData>& aAudioQueue,
|
|
|
|
MediaQueue<MediaData>& aVideoQueue)
|
2016-07-05 06:12:42 +00:00
|
|
|
: SeekTask(aDecoderID, aThread, aReader, aTarget)
|
2016-05-24 03:03:12 +00:00
|
|
|
, mAudioQueue(aAudioQueue)
|
|
|
|
, mVideoQueue(aVideoQueue)
|
2016-07-01 07:59:56 +00:00
|
|
|
, mCurrentTime(aCurrentTime)
|
2016-05-24 03:03:12 +00:00
|
|
|
, mDuration(aDuration)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-06-30 09:36:36 +00:00
|
|
|
MOZ_ASSERT(aInfo.HasVideo());
|
2016-05-24 03:03:12 +00:00
|
|
|
|
|
|
|
// Configure MediaDecoderReaderWrapper.
|
2016-06-30 06:16:18 +00:00
|
|
|
SetCallbacks();
|
2016-05-24 03:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NextFrameSeekTask::~NextFrameSeekTask()
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(mIsDiscarded);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::Discard()
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
|
|
|
|
// Disconnect MDSM.
|
|
|
|
RejectIfExist(__func__);
|
|
|
|
|
|
|
|
// Disconnect MediaDecoderReader.
|
2016-06-30 06:16:18 +00:00
|
|
|
CancelCallbacks();
|
2016-05-24 03:03:12 +00:00
|
|
|
|
|
|
|
mIsDiscarded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
NextFrameSeekTask::NeedToResetMDSM() const
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-01 07:59:56 +00: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 03:03:12 +00:00
|
|
|
{
|
2016-07-01 07:59:56 +00:00
|
|
|
while(aQueue.GetSize() > 0) {
|
|
|
|
if (aCompare(aQueue.PeekFront()->mTime)) {
|
2016-05-24 03:03:12 +00:00
|
|
|
RefPtr<MediaData> releaseMe = aQueue.PopFront();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<NextFrameSeekTask::SeekTaskPromise>
|
|
|
|
NextFrameSeekTask::Seek(const media::TimeUnit&)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
|
2016-07-01 07:59:56 +00:00
|
|
|
auto currentTime = mCurrentTime;
|
|
|
|
DiscardFrames(mVideoQueue, [currentTime] (int64_t aSampleTime) {
|
|
|
|
return aSampleTime <= currentTime;
|
|
|
|
});
|
2016-05-24 03:03:12 +00:00
|
|
|
|
2016-07-01 07:59:56 +00:00
|
|
|
RefPtr<SeekTaskPromise> promise = mSeekTaskPromise.Ensure(__func__);
|
2016-07-01 09:49:25 +00:00
|
|
|
if (!IsVideoRequestPending() && NeedMoreVideo()) {
|
2016-07-01 09:55:41 +00:00
|
|
|
RequestVideoData();
|
2016-06-30 09:03:12 +00:00
|
|
|
}
|
2016-07-01 07:59:56 +00:00
|
|
|
MaybeFinishSeek(); // Might resolve mSeekTaskPromise and modify audio queue.
|
|
|
|
return promise;
|
2016-05-24 03:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::RequestVideoData()
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
mReader->RequestVideoData(false, media::TimeUnit());
|
|
|
|
}
|
|
|
|
|
2016-07-07 03:23:11 +00: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 03:03:12 +00:00
|
|
|
bool
|
2016-07-01 03:43:06 +00:00
|
|
|
NextFrameSeekTask::IsAudioSeekComplete() const
|
2016-05-24 03:03:12 +00:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-07-01 03:43:06 +00: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 09:42:57 +00:00
|
|
|
return !mReader->IsRequestingAudioData() && !mReader->IsWaitingAudioData();
|
2016-05-24 03:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-07-07 03:23:11 +00:00
|
|
|
NextFrameSeekTask::IsVideoSeekComplete() const
|
2016-05-24 03:03:12 +00:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-07-07 03:23:11 +00: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 03:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-07-07 03:39:56 +00:00
|
|
|
NextFrameSeekTask::MaybeFinishSeek()
|
2016-05-24 03:03:12 +00:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-07-07 03:45:57 +00:00
|
|
|
if (IsAudioSeekComplete() && IsVideoSeekComplete()) {
|
2016-05-24 03:03:12 +00:00
|
|
|
UpdateSeekTargetTime();
|
2016-07-01 07:59:56 +00:00
|
|
|
|
2016-06-30 05:37:20 +00:00
|
|
|
auto time = mTarget.GetTime().ToMicroseconds();
|
2016-07-01 07:59:56 +00:00
|
|
|
DiscardFrames(mAudioQueue, [time] (int64_t aSampleTime) {
|
|
|
|
return aSampleTime < time;
|
|
|
|
});
|
|
|
|
|
2016-05-24 03:03:12 +00:00
|
|
|
Resolve(__func__); // Call to MDSM::SeekCompleted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::OnAudioDecoded(MediaData* aAudioSample)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(aAudioSample);
|
2016-06-30 09:27:55 +00:00
|
|
|
MOZ_ASSERT(!mSeekTaskPromise.IsEmpty(), "Seek shouldn't be finished");
|
2016-05-24 03:03:12 +00:00
|
|
|
|
|
|
|
// The MDSM::mDecodedAudioEndTime will be updated once the whole SeekTask is
|
|
|
|
// resolved.
|
|
|
|
|
|
|
|
SAMPLE_LOG("OnAudioDecoded [%lld,%lld] disc=%d",
|
2016-06-30 06:18:37 +00:00
|
|
|
aAudioSample->mTime,
|
|
|
|
aAudioSample->GetEndTime(),
|
|
|
|
aAudioSample->mDiscontinuity);
|
2016-05-24 03:03:12 +00:00
|
|
|
|
|
|
|
// We accept any audio data here.
|
|
|
|
mSeekedAudioData = aAudioSample;
|
|
|
|
|
2016-07-07 03:39:56 +00:00
|
|
|
MaybeFinishSeek();
|
2016-05-24 03:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::OnAudioNotDecoded(MediaDecoderReader::NotDecodedReason aReason)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-06-30 09:27:55 +00:00
|
|
|
MOZ_ASSERT(!mSeekTaskPromise.IsEmpty(), "Seek shouldn't be finished");
|
2016-05-24 03:03:12 +00:00
|
|
|
|
2016-06-30 09:27:55 +00:00
|
|
|
SAMPLE_LOG("OnAudioNotDecoded (aReason=%u)", aReason);
|
2016-05-24 03:03:12 +00: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 03:39:56 +00:00
|
|
|
MaybeFinishSeek();
|
2016-05-24 03:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::OnVideoDecoded(MediaData* aVideoSample)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
MOZ_ASSERT(aVideoSample);
|
2016-06-30 09:27:55 +00:00
|
|
|
MOZ_ASSERT(!mSeekTaskPromise.IsEmpty(), "Seek shouldn't be finished");
|
2016-05-24 03:03:12 +00:00
|
|
|
|
|
|
|
// The MDSM::mDecodedVideoEndTime will be updated once the whole SeekTask is
|
|
|
|
// resolved.
|
|
|
|
|
|
|
|
SAMPLE_LOG("OnVideoDecoded [%lld,%lld] disc=%d",
|
2016-06-30 06:18:37 +00:00
|
|
|
aVideoSample->mTime,
|
|
|
|
aVideoSample->GetEndTime(),
|
|
|
|
aVideoSample->mDiscontinuity);
|
2016-05-24 03:03:12 +00:00
|
|
|
|
2016-07-01 07:59:56 +00:00
|
|
|
if (aVideoSample->mTime > mCurrentTime) {
|
2016-05-24 03:03:12 +00:00
|
|
|
mSeekedVideoData = aVideoSample;
|
|
|
|
}
|
|
|
|
|
2016-07-07 03:45:57 +00:00
|
|
|
if (NeedMoreVideo()) {
|
2016-07-01 09:55:41 +00:00
|
|
|
RequestVideoData();
|
2016-07-07 03:45:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-07 03:39:56 +00:00
|
|
|
MaybeFinishSeek();
|
2016-05-24 03:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::OnVideoNotDecoded(MediaDecoderReader::NotDecodedReason aReason)
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-06-30 09:27:55 +00:00
|
|
|
MOZ_ASSERT(!mSeekTaskPromise.IsEmpty(), "Seek shouldn't be finished");
|
2016-05-24 03:03:12 +00:00
|
|
|
|
2016-06-30 09:27:55 +00:00
|
|
|
SAMPLE_LOG("OnVideoNotDecoded (aReason=%u)", aReason);
|
2016-05-24 03:03:12 +00:00
|
|
|
|
2016-07-07 04:03:48 +00:00
|
|
|
if (aReason == MediaDecoderReader::END_OF_STREAM) {
|
|
|
|
mIsVideoQueueFinished = true;
|
2016-05-24 03:03:12 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 04:03:48 +00:00
|
|
|
// Video seek not finished.
|
|
|
|
if (NeedMoreVideo()) {
|
|
|
|
switch (aReason) {
|
|
|
|
case MediaDecoderReader::DECODE_ERROR:
|
2016-07-01 08:09:32 +00: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 04:03:48 +00:00
|
|
|
// Reject the promise since we can't finish video seek anyway.
|
|
|
|
RejectIfExist(__func__);
|
|
|
|
break;
|
|
|
|
case MediaDecoderReader::WAITING_FOR_DATA:
|
|
|
|
mReader->WaitForData(MediaData::VIDEO_DATA);
|
|
|
|
break;
|
|
|
|
case MediaDecoderReader::CANCELED:
|
2016-07-01 09:55:41 +00:00
|
|
|
RequestVideoData();
|
2016-07-07 04:03:48 +00:00
|
|
|
break;
|
|
|
|
case MediaDecoderReader::END_OF_STREAM:
|
|
|
|
MOZ_ASSERT(false, "Shouldn't want more data for ended video.");
|
|
|
|
break;
|
|
|
|
}
|
2016-05-24 03:03:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-07 04:03:48 +00:00
|
|
|
MaybeFinishSeek();
|
2016-05-24 03:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-30 06:16:18 +00:00
|
|
|
NextFrameSeekTask::SetCallbacks()
|
2016-05-24 03:03:12 +00:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
|
|
|
|
// Register dummy callbcak for audio decoding since we don't need to handle
|
|
|
|
// the decoded audio samples.
|
2016-06-14 03:22:45 +00:00
|
|
|
mAudioCallback = mReader->AudioCallback().Connect(
|
|
|
|
OwnerThread(), [this] (AudioCallbackData aData) {
|
|
|
|
if (aData.is<MediaData*>()) {
|
|
|
|
OnAudioDecoded(aData.as<MediaData*>());
|
|
|
|
} else {
|
|
|
|
OnAudioNotDecoded(aData.as<MediaDecoderReader::NotDecodedReason>());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mVideoCallback = mReader->VideoCallback().Connect(
|
|
|
|
OwnerThread(), [this] (VideoCallbackData aData) {
|
|
|
|
typedef Tuple<MediaData*, TimeStamp> Type;
|
|
|
|
if (aData.is<Type>()) {
|
|
|
|
OnVideoDecoded(Get<0>(aData.as<Type>()));
|
|
|
|
} else {
|
|
|
|
OnVideoNotDecoded(aData.as<MediaDecoderReader::NotDecodedReason>());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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 03:39:56 +00:00
|
|
|
MaybeFinishSeek();
|
2016-06-14 03:22:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
mVideoWaitCallback = mReader->VideoWaitCallback().Connect(
|
|
|
|
OwnerThread(), [this] (WaitCallbackData aData) {
|
2016-07-07 04:03:48 +00:00
|
|
|
if (NeedMoreVideo()) {
|
|
|
|
if (aData.is<MediaData::Type>()) {
|
2016-07-01 09:55:41 +00:00
|
|
|
RequestVideoData();
|
2016-07-07 04:03:48 +00:00
|
|
|
} else {
|
|
|
|
// Reject if we can't finish video seeking.
|
2016-07-01 08:09:32 +00:00
|
|
|
CancelCallbacks();
|
2016-07-07 04:03:48 +00:00
|
|
|
RejectIfExist(__func__);
|
|
|
|
}
|
|
|
|
return;
|
2016-06-14 03:22:45 +00:00
|
|
|
}
|
2016-07-07 04:03:48 +00:00
|
|
|
MaybeFinishSeek();
|
2016-06-14 03:22:45 +00:00
|
|
|
});
|
2016-05-24 03:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-30 06:16:18 +00:00
|
|
|
NextFrameSeekTask::CancelCallbacks()
|
2016-05-24 03:03:12 +00:00
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
2016-07-01 08:09:32 +00:00
|
|
|
mAudioCallback.DisconnectIfExists();
|
|
|
|
mVideoCallback.DisconnectIfExists();
|
|
|
|
mAudioWaitCallback.DisconnectIfExists();
|
|
|
|
mVideoWaitCallback.DisconnectIfExists();
|
2016-05-24 03:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NextFrameSeekTask::UpdateSeekTargetTime()
|
|
|
|
{
|
|
|
|
AssertOwnerThread();
|
|
|
|
|
|
|
|
RefPtr<MediaData> data = mVideoQueue.PeekFront();
|
|
|
|
if (data) {
|
2016-06-30 05:37:20 +00:00
|
|
|
mTarget.SetTime(TimeUnit::FromMicroseconds(data->mTime));
|
2016-05-24 03:03:12 +00:00
|
|
|
} else if (mSeekedVideoData) {
|
2016-06-30 05:37:20 +00:00
|
|
|
mTarget.SetTime(TimeUnit::FromMicroseconds(mSeekedVideoData->mTime));
|
2016-05-24 03:03:12 +00:00
|
|
|
} else if (mIsVideoQueueFinished || mVideoQueue.AtEndOfStream()) {
|
2016-06-30 05:37:20 +00:00
|
|
|
mTarget.SetTime(mDuration);
|
2016-05-24 03:03:12 +00:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "No data!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace media
|
|
|
|
} // namespace mozilla
|