2014-08-13 05:22:00 -07: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 "MediaSourceReader.h"
|
|
|
|
|
2014-08-21 19:14:36 -07:00
|
|
|
#include "prlog.h"
|
2014-08-13 05:22:00 -07:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
|
|
|
#include "DecoderTraits.h"
|
|
|
|
#include "MediaDecoderOwner.h"
|
|
|
|
#include "MediaSourceDecoder.h"
|
2014-08-20 01:07:00 -07:00
|
|
|
#include "MediaSourceUtils.h"
|
2014-08-21 19:14:36 -07:00
|
|
|
#include "SourceBufferDecoder.h"
|
2014-09-03 18:57:06 -07:00
|
|
|
#include "TrackBuffer.h"
|
2014-08-13 05:22:00 -07:00
|
|
|
|
|
|
|
#ifdef MOZ_FMP4
|
2014-11-20 19:48:22 -08:00
|
|
|
#include "SharedDecoderManager.h"
|
2014-08-13 05:22:00 -07:00
|
|
|
#include "MP4Decoder.h"
|
|
|
|
#include "MP4Reader.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
extern PRLogModuleInfo* GetMediaSourceLog();
|
|
|
|
extern PRLogModuleInfo* GetMediaSourceAPILog();
|
|
|
|
|
|
|
|
#define MSE_DEBUG(...) PR_LOG(GetMediaSourceLog(), PR_LOG_DEBUG, (__VA_ARGS__))
|
|
|
|
#define MSE_DEBUGV(...) PR_LOG(GetMediaSourceLog(), PR_LOG_DEBUG+1, (__VA_ARGS__))
|
|
|
|
#define MSE_API(...) PR_LOG(GetMediaSourceAPILog(), PR_LOG_DEBUG, (__VA_ARGS__))
|
|
|
|
#else
|
|
|
|
#define MSE_DEBUG(...)
|
|
|
|
#define MSE_DEBUGV(...)
|
|
|
|
#define MSE_API(...)
|
|
|
|
#endif
|
|
|
|
|
2014-11-11 15:12:21 -08:00
|
|
|
// When a stream hits EOS it needs to decide what other stream to switch to. Due
|
|
|
|
// to inaccuracies is determining buffer end frames (Bug 1065207) and rounding
|
|
|
|
// issues we use a fuzz factor to determine the end time of this stream for
|
|
|
|
// switching to the new stream. This value is based on the end of frame
|
|
|
|
// default value used in Blink, kDefaultBufferDurationInMs.
|
|
|
|
#define EOS_FUZZ_US 125000
|
|
|
|
|
2014-11-11 20:50:21 -08:00
|
|
|
using mozilla::dom::TimeRanges;
|
|
|
|
|
2014-08-13 05:22:00 -07:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-08-24 21:09:44 -07:00
|
|
|
MediaSourceReader::MediaSourceReader(MediaSourceDecoder* aDecoder)
|
2014-08-13 05:22:00 -07:00
|
|
|
: MediaDecoderReader(aDecoder)
|
2015-01-09 17:20:58 -08:00
|
|
|
, mLastAudioTime(0)
|
|
|
|
, mLastVideoTime(0)
|
2014-11-04 16:32:26 -08:00
|
|
|
, mPendingSeekTime(-1)
|
|
|
|
, mWaitingForSeekData(false)
|
2015-01-16 10:58:00 -08:00
|
|
|
, mAudioIsSeeking(false)
|
|
|
|
, mVideoIsSeeking(false)
|
2014-08-13 05:22:00 -07:00
|
|
|
, mTimeThreshold(-1)
|
2014-08-20 20:14:48 -07:00
|
|
|
, mDropAudioBeforeThreshold(false)
|
2014-08-13 05:22:00 -07:00
|
|
|
, mDropVideoBeforeThreshold(false)
|
2014-08-26 00:25:09 -07:00
|
|
|
, mEnded(false)
|
2014-09-04 17:04:54 -07:00
|
|
|
, mHasEssentialTrackBuffers(false)
|
2014-11-20 19:48:22 -08:00
|
|
|
#ifdef MOZ_FMP4
|
2014-11-04 05:14:00 -08:00
|
|
|
, mSharedDecoderManager(new SharedDecoderManager())
|
2014-11-20 19:48:22 -08:00
|
|
|
#endif
|
2014-08-13 05:22:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-04 17:04:54 -07:00
|
|
|
void
|
|
|
|
MediaSourceReader::PrepareInitialization()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::PrepareInitialization trackBuffers=%u",
|
|
|
|
this, mTrackBuffers.Length());
|
|
|
|
mEssentialTrackBuffers.AppendElements(mTrackBuffers);
|
|
|
|
mHasEssentialTrackBuffers = true;
|
|
|
|
mDecoder->NotifyWaitingForResourcesStatusChanged();
|
|
|
|
}
|
|
|
|
|
2014-08-13 05:22:00 -07:00
|
|
|
bool
|
|
|
|
MediaSourceReader::IsWaitingMediaResources()
|
|
|
|
{
|
2014-09-03 18:57:06 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2014-09-04 17:04:54 -07:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mEssentialTrackBuffers.Length(); ++i) {
|
|
|
|
if (!mEssentialTrackBuffers[i]->IsReady()) {
|
2014-09-03 18:57:06 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-09-04 17:04:54 -07:00
|
|
|
|
|
|
|
return !mHasEssentialTrackBuffers;
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
2014-12-17 15:41:19 -08:00
|
|
|
size_t
|
|
|
|
MediaSourceReader::SizeOfVideoQueueInFrames()
|
|
|
|
{
|
|
|
|
if (!mVideoReader) {
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::SizeOfVideoQueue called with no video reader", this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mVideoReader->SizeOfVideoQueueInFrames();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
MediaSourceReader::SizeOfAudioQueueInFrames()
|
|
|
|
{
|
|
|
|
if (!mAudioReader) {
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::SizeOfAudioQueue called with no audio reader", this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mAudioReader->SizeOfAudioQueueInFrames();
|
|
|
|
}
|
|
|
|
|
2014-12-10 14:03:56 -08:00
|
|
|
nsRefPtr<MediaDecoderReader::AudioDataPromise>
|
2014-08-13 05:22:00 -07:00
|
|
|
MediaSourceReader::RequestAudioData()
|
|
|
|
{
|
2014-12-10 14:03:56 -08:00
|
|
|
nsRefPtr<AudioDataPromise> p = mAudioPromise.Ensure(__func__);
|
2014-09-03 18:57:06 -07:00
|
|
|
MSE_DEBUGV("MediaSourceReader(%p)::RequestAudioData", this);
|
2014-08-20 20:14:48 -07:00
|
|
|
if (!mAudioReader) {
|
2014-08-13 05:22:00 -07:00
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::RequestAudioData called with no audio reader", this);
|
2014-12-10 14:03:56 -08:00
|
|
|
mAudioPromise.Reject(DECODE_ERROR, __func__);
|
|
|
|
return p;
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
2015-01-16 10:58:00 -08:00
|
|
|
MOZ_ASSERT(!mAudioIsSeeking);
|
2015-01-11 18:10:15 -08:00
|
|
|
if (SwitchAudioReader(mLastAudioTime)) {
|
2015-01-16 10:57:59 -08:00
|
|
|
mAudioReader->Seek(mLastAudioTime, 0)
|
2015-01-11 18:10:15 -08:00
|
|
|
->Then(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::RequestAudioDataComplete,
|
|
|
|
&MediaSourceReader::RequestAudioDataFailed);
|
|
|
|
} else {
|
|
|
|
RequestAudioDataComplete(0);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::RequestAudioDataComplete(int64_t aTime)
|
|
|
|
{
|
2014-12-10 14:03:56 -08:00
|
|
|
mAudioReader->RequestAudioData()->Then(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::OnAudioDecoded,
|
|
|
|
&MediaSourceReader::OnAudioNotDecoded);
|
2015-01-11 18:10:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::RequestAudioDataFailed(nsresult aResult)
|
|
|
|
{
|
|
|
|
OnAudioNotDecoded(DECODE_ERROR);
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::OnAudioDecoded(AudioData* aSample)
|
|
|
|
{
|
2014-09-07 21:54:52 -07:00
|
|
|
MSE_DEBUGV("MediaSourceReader(%p)::OnAudioDecoded [mTime=%lld mDuration=%lld mDiscontinuity=%d]",
|
2014-09-03 18:57:06 -07:00
|
|
|
this, aSample->mTime, aSample->mDuration, aSample->mDiscontinuity);
|
2014-08-20 20:14:48 -07:00
|
|
|
if (mDropAudioBeforeThreshold) {
|
|
|
|
if (aSample->mTime < mTimeThreshold) {
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::OnAudioDecoded mTime=%lld < mTimeThreshold=%lld",
|
|
|
|
this, aSample->mTime, mTimeThreshold);
|
2014-12-10 14:03:56 -08:00
|
|
|
mAudioReader->RequestAudioData()->Then(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::OnAudioDecoded,
|
|
|
|
&MediaSourceReader::OnAudioNotDecoded);
|
2014-08-20 20:14:48 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDropAudioBeforeThreshold = false;
|
|
|
|
}
|
2014-09-03 02:56:22 -07:00
|
|
|
|
2014-09-07 21:55:20 -07:00
|
|
|
// Any OnAudioDecoded callbacks received while mAudioIsSeeking must be not
|
|
|
|
// update our last used timestamp, as these are emitted by the reader we're
|
|
|
|
// switching away from.
|
|
|
|
if (!mAudioIsSeeking) {
|
|
|
|
mLastAudioTime = aSample->mTime + aSample->mDuration;
|
2014-09-03 02:56:22 -07:00
|
|
|
}
|
2014-12-10 14:03:56 -08:00
|
|
|
|
|
|
|
mAudioPromise.Resolve(aSample, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the closest approximation to the end time for this stream.
|
|
|
|
// mLast{Audio,Video}Time differs from the actual end time because of
|
|
|
|
// Bug 1065207 - the duration of a WebM fragment is an estimate not the
|
|
|
|
// actual duration. In the case of audio time an example of where they
|
|
|
|
// differ would be the actual sample duration being small but the
|
|
|
|
// previous sample being large. The buffered end time uses that last
|
|
|
|
// sample duration as an estimate of the end time duration giving an end
|
|
|
|
// time that is greater than mLastAudioTime, which is the actual sample
|
|
|
|
// end time.
|
|
|
|
// Reader switching is based on the buffered end time though so they can be
|
|
|
|
// quite different. By using the EOS_FUZZ_US and the buffered end time we
|
|
|
|
// attempt to account for this difference.
|
|
|
|
static void
|
|
|
|
AdjustEndTime(int64_t* aEndTime, MediaDecoderReader* aReader)
|
|
|
|
{
|
|
|
|
if (aReader) {
|
|
|
|
nsRefPtr<dom::TimeRanges> ranges = new dom::TimeRanges();
|
|
|
|
aReader->GetBuffered(ranges);
|
|
|
|
if (ranges->Length() > 0) {
|
|
|
|
// End time is a double so we convert to nearest by adding 0.5.
|
|
|
|
int64_t end = ranges->GetEndTime() * USECS_PER_S + 0.5;
|
|
|
|
*aEndTime = std::max(*aEndTime, end);
|
|
|
|
}
|
|
|
|
}
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-10 14:03:56 -08:00
|
|
|
MediaSourceReader::OnAudioNotDecoded(NotDecodedReason aReason)
|
|
|
|
{
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::OnAudioNotDecoded aReason=%u IsEnded: %d", this, aReason, IsEnded());
|
|
|
|
if (aReason == DECODE_ERROR || aReason == CANCELED) {
|
|
|
|
mAudioPromise.Reject(aReason, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of stream. Force switching past this stream to another reader by
|
|
|
|
// switching to the end of the buffered range.
|
|
|
|
MOZ_ASSERT(aReason == END_OF_STREAM);
|
|
|
|
if (mAudioReader) {
|
|
|
|
AdjustEndTime(&mLastAudioTime, mAudioReader);
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we can find a different reader that can pick up where we left off. We use the
|
|
|
|
// EOS_FUZZ_US to allow for the fact that our end time can be inaccurate due to bug
|
|
|
|
// 1065207.
|
2015-01-11 12:47:56 -08:00
|
|
|
if (SwitchAudioReader(mLastAudioTime, EOS_FUZZ_US)) {
|
2015-01-16 10:57:59 -08:00
|
|
|
mAudioReader->Seek(mLastAudioTime, 0)
|
2015-01-11 18:10:15 -08:00
|
|
|
->Then(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::RequestAudioDataComplete,
|
|
|
|
&MediaSourceReader::RequestAudioDataFailed);
|
2014-12-10 14:03:56 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the entire MediaSource is done, generate an EndOfStream.
|
|
|
|
if (IsEnded()) {
|
|
|
|
mAudioPromise.Reject(END_OF_STREAM, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have the data the caller wants. Tell that we're waiting for JS to
|
|
|
|
// give us more data.
|
|
|
|
mAudioPromise.Reject(WAITING_FOR_DATA, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsRefPtr<MediaDecoderReader::VideoDataPromise>
|
2014-08-13 05:22:00 -07:00
|
|
|
MediaSourceReader::RequestVideoData(bool aSkipToNextKeyframe, int64_t aTimeThreshold)
|
|
|
|
{
|
2014-12-10 14:03:56 -08:00
|
|
|
nsRefPtr<VideoDataPromise> p = mVideoPromise.Ensure(__func__);
|
2014-09-03 18:57:06 -07:00
|
|
|
MSE_DEBUGV("MediaSourceReader(%p)::RequestVideoData(%d, %lld)",
|
|
|
|
this, aSkipToNextKeyframe, aTimeThreshold);
|
2014-08-20 20:14:48 -07:00
|
|
|
if (!mVideoReader) {
|
2014-08-13 05:22:00 -07:00
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::RequestVideoData called with no video reader", this);
|
2014-12-10 14:03:56 -08:00
|
|
|
mVideoPromise.Reject(DECODE_ERROR, __func__);
|
|
|
|
return p;
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
2014-09-03 18:57:06 -07:00
|
|
|
if (aSkipToNextKeyframe) {
|
|
|
|
mTimeThreshold = aTimeThreshold;
|
|
|
|
mDropAudioBeforeThreshold = true;
|
|
|
|
mDropVideoBeforeThreshold = true;
|
|
|
|
}
|
2015-01-16 10:58:00 -08:00
|
|
|
MOZ_ASSERT(!mVideoIsSeeking);
|
2015-01-11 18:10:15 -08:00
|
|
|
if (SwitchVideoReader(mLastVideoTime)) {
|
2015-01-16 10:57:59 -08:00
|
|
|
mVideoReader->Seek(mLastVideoTime, 0)
|
2015-01-11 18:10:15 -08:00
|
|
|
->Then(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::RequestVideoDataComplete,
|
|
|
|
&MediaSourceReader::RequestVideoDataFailed);
|
|
|
|
} else {
|
|
|
|
mVideoReader->RequestVideoData(aSkipToNextKeyframe, aTimeThreshold)
|
|
|
|
->Then(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::OnVideoDecoded, &MediaSourceReader::OnVideoNotDecoded);
|
|
|
|
}
|
2014-12-10 14:03:56 -08:00
|
|
|
|
2015-01-11 18:10:15 -08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::RequestVideoDataComplete(int64_t aTime)
|
|
|
|
{
|
|
|
|
mVideoReader->RequestVideoData(false, 0)
|
2014-12-10 14:03:56 -08:00
|
|
|
->Then(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::OnVideoDecoded, &MediaSourceReader::OnVideoNotDecoded);
|
2015-01-11 18:10:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::RequestVideoDataFailed(nsresult aResult)
|
|
|
|
{
|
|
|
|
OnVideoNotDecoded(DECODE_ERROR);
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::OnVideoDecoded(VideoData* aSample)
|
|
|
|
{
|
2014-09-07 21:54:52 -07:00
|
|
|
MSE_DEBUGV("MediaSourceReader(%p)::OnVideoDecoded [mTime=%lld mDuration=%lld mDiscontinuity=%d]",
|
2014-09-03 18:57:06 -07:00
|
|
|
this, aSample->mTime, aSample->mDuration, aSample->mDiscontinuity);
|
2014-08-13 05:22:00 -07:00
|
|
|
if (mDropVideoBeforeThreshold) {
|
|
|
|
if (aSample->mTime < mTimeThreshold) {
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::OnVideoDecoded mTime=%lld < mTimeThreshold=%lld",
|
|
|
|
this, aSample->mTime, mTimeThreshold);
|
2014-12-10 14:03:56 -08:00
|
|
|
mVideoReader->RequestVideoData(false, 0)->Then(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::OnVideoDecoded,
|
|
|
|
&MediaSourceReader::OnVideoNotDecoded);
|
2014-08-13 05:22:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDropVideoBeforeThreshold = false;
|
|
|
|
}
|
2014-09-03 02:56:22 -07:00
|
|
|
|
2014-09-07 21:55:20 -07:00
|
|
|
// Any OnVideoDecoded callbacks received while mVideoIsSeeking must be not
|
|
|
|
// update our last used timestamp, as these are emitted by the reader we're
|
|
|
|
// switching away from.
|
|
|
|
if (!mVideoIsSeeking) {
|
|
|
|
mLastVideoTime = aSample->mTime + aSample->mDuration;
|
2014-09-03 02:56:22 -07:00
|
|
|
}
|
2014-12-10 14:03:56 -08:00
|
|
|
|
|
|
|
mVideoPromise.Resolve(aSample, __func__);
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-10 14:03:56 -08:00
|
|
|
MediaSourceReader::OnVideoNotDecoded(NotDecodedReason aReason)
|
2014-08-13 05:22:00 -07:00
|
|
|
{
|
2014-12-10 14:03:56 -08:00
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::OnVideoNotDecoded aReason=%u IsEnded: %d", this, aReason, IsEnded());
|
2014-12-08 14:45:36 -08:00
|
|
|
if (aReason == DECODE_ERROR || aReason == CANCELED) {
|
2014-12-10 14:03:56 -08:00
|
|
|
mVideoPromise.Reject(aReason, __func__);
|
2014-11-03 00:20:14 -08:00
|
|
|
return;
|
|
|
|
}
|
2014-12-10 14:03:56 -08:00
|
|
|
|
2014-11-11 15:12:21 -08:00
|
|
|
// End of stream. Force switching past this stream to another reader by
|
|
|
|
// switching to the end of the buffered range.
|
2014-12-08 14:45:36 -08:00
|
|
|
MOZ_ASSERT(aReason == END_OF_STREAM);
|
2014-12-10 14:03:56 -08:00
|
|
|
if (mVideoReader) {
|
2014-12-11 20:11:51 -08:00
|
|
|
AdjustEndTime(&mLastVideoTime, mVideoReader);
|
2014-11-11 15:12:21 -08:00
|
|
|
}
|
2014-11-03 00:20:14 -08:00
|
|
|
|
2014-11-11 15:12:21 -08:00
|
|
|
// See if we can find a different reader that can pick up where we left off. We use the
|
|
|
|
// EOS_FUZZ_US to allow for the fact that our end time can be inaccurate due to bug
|
2014-12-10 14:03:56 -08:00
|
|
|
// 1065207.
|
2015-01-11 12:47:56 -08:00
|
|
|
if (SwitchVideoReader(mLastVideoTime, EOS_FUZZ_US)) {
|
2015-01-16 10:57:59 -08:00
|
|
|
mVideoReader->Seek(mLastVideoTime, 0)
|
2014-12-10 14:03:56 -08:00
|
|
|
->Then(GetTaskQueue(), __func__, this,
|
2015-01-11 18:10:15 -08:00
|
|
|
&MediaSourceReader::RequestVideoDataComplete,
|
|
|
|
&MediaSourceReader::RequestVideoDataFailed);
|
2014-11-03 00:20:14 -08:00
|
|
|
return;
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
2014-11-03 00:20:15 -08:00
|
|
|
// If the entire MediaSource is done, generate an EndOfStream.
|
2014-11-03 00:20:14 -08:00
|
|
|
if (IsEnded()) {
|
2014-12-10 14:03:56 -08:00
|
|
|
mVideoPromise.Reject(END_OF_STREAM, __func__);
|
2014-11-03 00:20:15 -08:00
|
|
|
return;
|
2014-11-03 00:20:14 -08:00
|
|
|
}
|
|
|
|
|
2014-11-03 00:20:15 -08:00
|
|
|
// We don't have the data the caller wants. Tell that we're waiting for JS to
|
|
|
|
// give us more data.
|
2014-12-10 14:03:56 -08:00
|
|
|
mVideoPromise.Reject(WAITING_FOR_DATA, __func__);
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
2014-12-09 11:43:21 -08:00
|
|
|
nsRefPtr<ShutdownPromise>
|
2014-12-09 12:49:38 -08:00
|
|
|
MediaSourceReader::Shutdown()
|
2014-12-09 11:43:21 -08:00
|
|
|
{
|
2014-12-16 01:52:57 -08:00
|
|
|
mSeekPromise.RejectIfExists(NS_ERROR_FAILURE, __func__);
|
|
|
|
|
2014-12-09 11:43:21 -08:00
|
|
|
MOZ_ASSERT(mMediaSourceShutdownPromise.IsEmpty());
|
|
|
|
nsRefPtr<ShutdownPromise> p = mMediaSourceShutdownPromise.Ensure(__func__);
|
|
|
|
|
2014-12-12 14:22:23 -08:00
|
|
|
ContinueShutdown();
|
2014-12-09 11:43:21 -08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 14:22:23 -08:00
|
|
|
MediaSourceReader::ContinueShutdown()
|
2014-12-09 11:43:21 -08:00
|
|
|
{
|
2014-12-18 04:59:00 -08:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2014-12-09 11:43:21 -08:00
|
|
|
if (mTrackBuffers.Length()) {
|
|
|
|
mTrackBuffers[0]->Shutdown()->Then(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::ContinueShutdown,
|
|
|
|
&MediaSourceReader::ContinueShutdown);
|
|
|
|
mShutdownTrackBuffers.AppendElement(mTrackBuffers[0]);
|
|
|
|
mTrackBuffers.RemoveElementAt(0);
|
|
|
|
return;
|
2014-10-09 22:26:12 -07:00
|
|
|
}
|
2014-12-09 11:43:21 -08:00
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
mAudioTrack = nullptr;
|
|
|
|
mAudioReader = nullptr;
|
|
|
|
mVideoTrack = nullptr;
|
|
|
|
mVideoReader = nullptr;
|
2014-12-09 11:43:21 -08:00
|
|
|
|
2014-12-10 14:03:56 -08:00
|
|
|
MOZ_ASSERT(mAudioPromise.IsEmpty());
|
|
|
|
MOZ_ASSERT(mVideoPromise.IsEmpty());
|
|
|
|
|
2014-12-22 00:20:31 -08:00
|
|
|
mAudioWaitPromise.RejectIfExists(WaitForDataRejectValue(MediaData::AUDIO_DATA, WaitForDataRejectValue::SHUTDOWN), __func__);
|
|
|
|
mVideoWaitPromise.RejectIfExists(WaitForDataRejectValue(MediaData::VIDEO_DATA, WaitForDataRejectValue::SHUTDOWN), __func__);
|
|
|
|
|
2014-12-09 11:43:21 -08:00
|
|
|
MediaDecoderReader::Shutdown()->ChainTo(mMediaSourceShutdownPromise.Steal(), __func__);
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::BreakCycles()
|
|
|
|
{
|
|
|
|
MediaDecoderReader::BreakCycles();
|
2014-10-09 22:26:12 -07:00
|
|
|
|
|
|
|
// These were cleared in Shutdown().
|
|
|
|
MOZ_ASSERT(!mAudioTrack);
|
|
|
|
MOZ_ASSERT(!mAudioReader);
|
|
|
|
MOZ_ASSERT(!mVideoTrack);
|
|
|
|
MOZ_ASSERT(!mVideoReader);
|
2014-12-09 11:43:21 -08:00
|
|
|
MOZ_ASSERT(!mTrackBuffers.Length());
|
2014-10-09 22:26:12 -07:00
|
|
|
|
2014-12-09 11:43:21 -08:00
|
|
|
for (uint32_t i = 0; i < mShutdownTrackBuffers.Length(); ++i) {
|
|
|
|
mShutdownTrackBuffers[i]->BreakCycles();
|
2014-09-07 17:59:36 -07:00
|
|
|
}
|
2014-12-09 11:43:21 -08:00
|
|
|
mShutdownTrackBuffers.Clear();
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
2014-09-07 17:07:56 -07:00
|
|
|
already_AddRefed<MediaDecoderReader>
|
2014-09-09 19:27:51 -07:00
|
|
|
MediaSourceReader::SelectReader(int64_t aTarget,
|
2015-01-11 12:47:56 -08:00
|
|
|
int64_t aError,
|
2014-09-07 17:07:56 -07:00
|
|
|
const nsTArray<nsRefPtr<SourceBufferDecoder>>& aTrackDecoders)
|
|
|
|
{
|
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
|
2014-09-07 17:13:50 -07:00
|
|
|
// Consider decoders in order of newest to oldest, as a newer decoder
|
|
|
|
// providing a given buffered range is expected to replace an older one.
|
|
|
|
for (int32_t i = aTrackDecoders.Length() - 1; i >= 0; --i) {
|
2014-09-07 17:07:56 -07:00
|
|
|
nsRefPtr<MediaDecoderReader> newReader = aTrackDecoders[i]->GetReader();
|
|
|
|
|
|
|
|
nsRefPtr<dom::TimeRanges> ranges = new dom::TimeRanges();
|
|
|
|
aTrackDecoders[i]->GetBuffered(ranges);
|
2015-01-11 12:47:56 -08:00
|
|
|
if (ranges->Find(double(aTarget) / USECS_PER_S,
|
|
|
|
double(aError) / USECS_PER_S) == dom::TimeRanges::NoIndex) {
|
2014-09-09 19:27:51 -07:00
|
|
|
MSE_DEBUGV("MediaSourceReader(%p)::SelectReader(%lld) newReader=%p target not in ranges=%s",
|
2014-09-07 17:07:56 -07:00
|
|
|
this, aTarget, newReader.get(), DumpTimeRanges(ranges).get());
|
|
|
|
continue;
|
2014-09-03 07:14:25 -07:00
|
|
|
}
|
2014-09-07 17:07:56 -07:00
|
|
|
|
|
|
|
return newReader.forget();
|
2014-09-03 07:14:25 -07:00
|
|
|
}
|
2014-08-28 01:18:01 -07:00
|
|
|
|
2014-09-07 17:07:56 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-22 00:20:31 -08:00
|
|
|
bool
|
|
|
|
MediaSourceReader::HaveData(int64_t aTarget, MediaData::Type aType)
|
|
|
|
{
|
|
|
|
TrackBuffer* trackBuffer = aType == MediaData::AUDIO_DATA ? mAudioTrack : mVideoTrack;
|
|
|
|
MOZ_ASSERT(trackBuffer);
|
2015-01-11 12:47:56 -08:00
|
|
|
nsRefPtr<MediaDecoderReader> reader = SelectReader(aTarget, EOS_FUZZ_US, trackBuffer->Decoders());
|
2014-12-22 00:20:31 -08:00
|
|
|
return !!reader;
|
|
|
|
}
|
|
|
|
|
2014-09-07 17:07:56 -07:00
|
|
|
bool
|
2015-01-11 12:47:56 -08:00
|
|
|
MediaSourceReader::SwitchAudioReader(int64_t aTarget, int64_t aError)
|
2014-09-07 17:07:56 -07:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
// XXX: Can't handle adding an audio track after ReadMetadata.
|
|
|
|
if (!mAudioTrack) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-11 12:47:56 -08:00
|
|
|
nsRefPtr<MediaDecoderReader> newReader = SelectReader(aTarget, aError, mAudioTrack->Decoders());
|
2014-09-07 21:54:52 -07:00
|
|
|
if (newReader && newReader != mAudioReader) {
|
2014-09-07 17:07:56 -07:00
|
|
|
mAudioReader->SetIdle();
|
|
|
|
mAudioReader = newReader;
|
2014-09-07 21:54:52 -07:00
|
|
|
MSE_DEBUGV("MediaSourceReader(%p)::SwitchAudioReader switched reader to %p", this, mAudioReader.get());
|
|
|
|
return true;
|
2014-09-07 17:07:56 -07:00
|
|
|
}
|
2014-09-07 21:54:52 -07:00
|
|
|
return false;
|
2014-09-03 10:55:55 -07:00
|
|
|
}
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
bool
|
2015-01-11 12:47:56 -08:00
|
|
|
MediaSourceReader::SwitchVideoReader(int64_t aTarget, int64_t aError)
|
2014-09-03 10:55:55 -07:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2014-09-07 17:07:56 -07:00
|
|
|
// XXX: Can't handle adding a video track after ReadMetadata.
|
2014-09-03 18:57:06 -07:00
|
|
|
if (!mVideoTrack) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-11 12:47:56 -08:00
|
|
|
nsRefPtr<MediaDecoderReader> newReader = SelectReader(aTarget, aError, mVideoTrack->Decoders());
|
2014-09-07 21:54:52 -07:00
|
|
|
if (newReader && newReader != mVideoReader) {
|
2014-09-07 17:07:56 -07:00
|
|
|
mVideoReader->SetIdle();
|
|
|
|
mVideoReader = newReader;
|
2014-09-07 21:54:52 -07:00
|
|
|
MSE_DEBUGV("MediaSourceReader(%p)::SwitchVideoReader switched reader to %p", this, mVideoReader.get());
|
|
|
|
return true;
|
2014-09-03 10:55:55 -07:00
|
|
|
}
|
2014-09-07 21:54:52 -07:00
|
|
|
return false;
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
2015-01-13 16:27:00 -08:00
|
|
|
bool
|
|
|
|
MediaSourceReader::IsDormantNeeded()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
if (mVideoReader) {
|
|
|
|
return mVideoReader->IsDormantNeeded();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::ReleaseMediaResources()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
if (mVideoReader) {
|
|
|
|
mVideoReader->ReleaseMediaResources();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-13 05:22:00 -07:00
|
|
|
MediaDecoderReader*
|
|
|
|
CreateReaderForType(const nsACString& aType, AbstractMediaDecoder* aDecoder)
|
|
|
|
{
|
|
|
|
#ifdef MOZ_FMP4
|
|
|
|
// The MP4Reader that supports fragmented MP4 and uses
|
|
|
|
// PlatformDecoderModules is hidden behind prefs for regular video
|
|
|
|
// elements, but we always want to use it for MSE, so instantiate it
|
|
|
|
// directly here.
|
|
|
|
if ((aType.LowerCaseEqualsLiteral("video/mp4") ||
|
|
|
|
aType.LowerCaseEqualsLiteral("audio/mp4")) &&
|
|
|
|
MP4Decoder::IsEnabled()) {
|
|
|
|
return new MP4Reader(aDecoder);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return DecoderTraits::CreateReader(aType, aDecoder);
|
|
|
|
}
|
|
|
|
|
2014-08-21 19:14:36 -07:00
|
|
|
already_AddRefed<SourceBufferDecoder>
|
2015-01-07 15:58:55 -08:00
|
|
|
MediaSourceReader::CreateSubDecoder(const nsACString& aType, int64_t aTimestampOffset)
|
2014-08-13 05:22:00 -07:00
|
|
|
{
|
2014-08-26 17:45:05 -07:00
|
|
|
if (IsShutdown()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-08-20 01:07:00 -07:00
|
|
|
MOZ_ASSERT(GetTaskQueue());
|
2014-08-21 19:14:36 -07:00
|
|
|
nsRefPtr<SourceBufferDecoder> decoder =
|
2015-01-07 15:58:55 -08:00
|
|
|
new SourceBufferDecoder(new SourceBufferResource(aType), mDecoder, aTimestampOffset);
|
2014-08-13 05:22:00 -07:00
|
|
|
nsRefPtr<MediaDecoderReader> reader(CreateReaderForType(aType, decoder));
|
|
|
|
if (!reader) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-11-11 20:50:20 -08:00
|
|
|
|
|
|
|
// MSE uses a start time of 0 everywhere. Set that immediately on the
|
|
|
|
// subreader to make sure that it's always in a state where we can invoke
|
|
|
|
// GetBuffered on it.
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(decoder->GetReentrantMonitor());
|
|
|
|
reader->SetStartTime(0);
|
|
|
|
}
|
|
|
|
|
2014-12-01 21:51:03 -08:00
|
|
|
// This part is icky. It would be nicer to just give each subreader its own
|
|
|
|
// task queue. Unfortunately though, Request{Audio,Video}Data implementations
|
|
|
|
// currently assert that they're on "the decode thread", and so having
|
|
|
|
// separate task queues makes MediaSource stuff unnecessarily cumbersome. We
|
|
|
|
// should remove the need for these assertions (which probably involves making
|
|
|
|
// all Request*Data implementations fully async), and then get rid of the
|
|
|
|
// borrowing.
|
|
|
|
reader->SetBorrowedTaskQueue(GetTaskQueue());
|
|
|
|
|
2014-11-20 19:48:22 -08:00
|
|
|
#ifdef MOZ_FMP4
|
2014-11-04 05:14:00 -08:00
|
|
|
reader->SetSharedDecoderManager(mSharedDecoderManager);
|
2014-11-20 19:48:22 -08:00
|
|
|
#endif
|
2014-08-13 05:22:00 -07:00
|
|
|
reader->Init(nullptr);
|
2014-09-03 18:57:06 -07:00
|
|
|
|
2014-08-13 05:22:00 -07:00
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::CreateSubDecoder subdecoder %p subreader %p",
|
|
|
|
this, decoder.get(), reader.get());
|
|
|
|
decoder->SetReader(reader);
|
2014-10-13 15:05:00 -07:00
|
|
|
#ifdef MOZ_EME
|
|
|
|
decoder->SetCDMProxy(mCDMProxy);
|
|
|
|
#endif
|
2014-09-03 18:57:06 -07:00
|
|
|
return decoder.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::AddTrackBuffer(TrackBuffer* aTrackBuffer)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::AddTrackBuffer %p", this, aTrackBuffer);
|
|
|
|
mTrackBuffers.AppendElement(aTrackBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::RemoveTrackBuffer(TrackBuffer* aTrackBuffer)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::RemoveTrackBuffer %p", this, aTrackBuffer);
|
|
|
|
mTrackBuffers.RemoveElement(aTrackBuffer);
|
|
|
|
if (mAudioTrack == aTrackBuffer) {
|
|
|
|
mAudioTrack = nullptr;
|
|
|
|
}
|
|
|
|
if (mVideoTrack == aTrackBuffer) {
|
|
|
|
mVideoTrack = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::OnTrackBufferConfigured(TrackBuffer* aTrackBuffer, const MediaInfo& aInfo)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2014-09-04 17:04:54 -07:00
|
|
|
MOZ_ASSERT(aTrackBuffer->IsReady());
|
2014-09-03 18:57:06 -07:00
|
|
|
MOZ_ASSERT(mTrackBuffers.Contains(aTrackBuffer));
|
|
|
|
if (aInfo.HasAudio() && !mAudioTrack) {
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::OnTrackBufferConfigured %p audio", this, aTrackBuffer);
|
|
|
|
mAudioTrack = aTrackBuffer;
|
|
|
|
}
|
|
|
|
if (aInfo.HasVideo() && !mVideoTrack) {
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::OnTrackBufferConfigured %p video", this, aTrackBuffer);
|
|
|
|
mVideoTrack = aTrackBuffer;
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
mDecoder->NotifyWaitingForResourcesStatusChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-09-09 19:27:51 -07:00
|
|
|
MediaSourceReader::TrackBuffersContainTime(int64_t aTime)
|
2014-08-13 05:22:00 -07:00
|
|
|
{
|
2014-09-03 18:57:06 -07:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
if (mAudioTrack && !mAudioTrack->ContainsTime(aTime)) {
|
|
|
|
return false;
|
2014-08-28 01:18:01 -07:00
|
|
|
}
|
2014-09-03 18:57:06 -07:00
|
|
|
if (mVideoTrack && !mVideoTrack->ContainsTime(aTime)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
2014-11-04 16:32:26 -08:00
|
|
|
void
|
|
|
|
MediaSourceReader::NotifyTimeRangesChanged()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
if (mWaitingForSeekData) {
|
|
|
|
//post a task to the state machine thread to call seek.
|
|
|
|
RefPtr<nsIRunnable> task(NS_NewRunnableMethod(
|
|
|
|
this, &MediaSourceReader::AttemptSeek));
|
|
|
|
GetTaskQueue()->Dispatch(task.forget());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 01:52:57 -08:00
|
|
|
nsRefPtr<MediaDecoderReader::SeekPromise>
|
2015-01-16 10:58:00 -08:00
|
|
|
MediaSourceReader::Seek(int64_t aTime, int64_t aIgnored /* Used only for ogg which is non-MSE */)
|
2014-08-13 05:22:00 -07:00
|
|
|
{
|
2015-01-16 10:58:00 -08:00
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::Seek(aTime=%lld, aEnd=%lld, aCurrent=%lld)",
|
|
|
|
this, aTime);
|
2014-09-03 18:57:06 -07:00
|
|
|
|
2015-01-16 10:58:00 -08:00
|
|
|
MOZ_ASSERT(mSeekPromise.IsEmpty());
|
2014-12-16 01:52:57 -08:00
|
|
|
nsRefPtr<SeekPromise> p = mSeekPromise.Ensure(__func__);
|
|
|
|
|
2014-11-04 16:32:26 -08:00
|
|
|
if (IsShutdown()) {
|
2014-12-16 01:52:57 -08:00
|
|
|
mSeekPromise.Reject(NS_ERROR_FAILURE, __func__);
|
|
|
|
return p;
|
2014-11-04 16:32:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store pending seek target in case the track buffers don't contain
|
|
|
|
// the desired time and we delay doing the seek.
|
|
|
|
mPendingSeekTime = aTime;
|
|
|
|
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-01-11 14:05:08 -08:00
|
|
|
mWaitingForSeekData = true;
|
2014-11-04 16:32:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
AttemptSeek();
|
2014-12-16 01:52:57 -08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-01-16 10:58:00 -08:00
|
|
|
void
|
|
|
|
MediaSourceReader::CancelSeek()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnDecodeThread());
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
if (mWaitingForSeekData) {
|
|
|
|
mSeekPromise.Reject(NS_OK, __func__);
|
|
|
|
mWaitingForSeekData = false;
|
|
|
|
mPendingSeekTime = -1;
|
|
|
|
} else if (mVideoIsSeeking) {
|
|
|
|
// NB: Currently all readers have sync Seeks(), so this is a no-op.
|
|
|
|
mVideoReader->CancelSeek();
|
|
|
|
} else if (mAudioIsSeeking) {
|
|
|
|
// NB: Currently all readers have sync Seeks(), so this is a no-op.
|
|
|
|
mAudioReader->CancelSeek();
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mSeekPromise.IsEmpty());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 01:52:57 -08:00
|
|
|
void
|
2015-01-11 14:05:08 -08:00
|
|
|
MediaSourceReader::OnVideoSeekCompleted(int64_t aTime)
|
2014-12-16 01:52:57 -08:00
|
|
|
{
|
2015-01-11 14:05:08 -08:00
|
|
|
mPendingSeekTime = aTime;
|
2015-01-16 10:58:00 -08:00
|
|
|
MOZ_ASSERT(mVideoIsSeeking);
|
|
|
|
MOZ_ASSERT(!mAudioIsSeeking);
|
|
|
|
mVideoIsSeeking = false;
|
|
|
|
|
2015-01-11 14:05:08 -08:00
|
|
|
if (mAudioTrack) {
|
|
|
|
mAudioIsSeeking = true;
|
|
|
|
SwitchAudioReader(mPendingSeekTime);
|
2015-01-16 10:58:00 -08:00
|
|
|
mAudioReader->Seek(mPendingSeekTime, 0)
|
2015-01-11 14:05:08 -08:00
|
|
|
->Then(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::OnAudioSeekCompleted,
|
|
|
|
&MediaSourceReader::OnSeekFailed);
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::Seek audio reader=%p", this, mAudioReader.get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mSeekPromise.Resolve(mPendingSeekTime, __func__);
|
2014-11-04 16:32:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-11 14:05:08 -08:00
|
|
|
MediaSourceReader::OnAudioSeekCompleted(int64_t aTime)
|
2014-11-04 16:32:26 -08:00
|
|
|
{
|
2015-01-16 10:58:00 -08:00
|
|
|
mPendingSeekTime = aTime;
|
|
|
|
MOZ_ASSERT(mAudioIsSeeking);
|
|
|
|
MOZ_ASSERT(!mVideoIsSeeking);
|
|
|
|
mAudioIsSeeking = false;
|
|
|
|
|
2015-01-11 14:05:08 -08:00
|
|
|
mSeekPromise.Resolve(mPendingSeekTime, __func__);
|
2015-01-16 10:58:00 -08:00
|
|
|
mPendingSeekTime = -1;
|
2014-12-16 01:52:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-11 14:05:08 -08:00
|
|
|
MediaSourceReader::OnSeekFailed(nsresult aResult)
|
|
|
|
{
|
2015-01-16 10:58:00 -08:00
|
|
|
MOZ_ASSERT(mVideoIsSeeking || mAudioIsSeeking);
|
|
|
|
|
|
|
|
if (mVideoIsSeeking) {
|
|
|
|
MOZ_ASSERT(!mAudioIsSeeking);
|
|
|
|
mVideoIsSeeking = false;
|
2014-11-04 16:32:26 -08:00
|
|
|
}
|
2015-01-16 10:58:00 -08:00
|
|
|
|
|
|
|
if (mAudioIsSeeking) {
|
|
|
|
MOZ_ASSERT(!mVideoIsSeeking);
|
|
|
|
mAudioIsSeeking = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPendingSeekTime = -1;
|
|
|
|
mSeekPromise.Reject(aResult, __func__);
|
2014-11-04 16:32:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::AttemptSeek()
|
|
|
|
{
|
|
|
|
// Make sure we don't hold the monitor while calling into the reader
|
|
|
|
// Seek methods since it can deadlock.
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
if (!mWaitingForSeekData || !TrackBuffersContainTime(mPendingSeekTime)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-16 10:58:00 -08:00
|
|
|
mWaitingForSeekData = false;
|
2014-11-04 16:32:26 -08:00
|
|
|
}
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
ResetDecode();
|
|
|
|
for (uint32_t i = 0; i < mTrackBuffers.Length(); ++i) {
|
|
|
|
mTrackBuffers[i]->ResetDecode();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decoding discontinuity upon seek, reset last times to seek target.
|
2014-11-04 16:32:26 -08:00
|
|
|
mLastAudioTime = mPendingSeekTime;
|
|
|
|
mLastVideoTime = mPendingSeekTime;
|
2014-08-13 05:22:00 -07:00
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
if (mVideoTrack) {
|
2014-09-03 02:56:22 -07:00
|
|
|
mVideoIsSeeking = true;
|
2014-11-04 16:32:26 -08:00
|
|
|
SwitchVideoReader(mPendingSeekTime);
|
2015-01-16 10:58:00 -08:00
|
|
|
mVideoReader->Seek(mPendingSeekTime, 0)
|
2014-12-16 01:52:57 -08:00
|
|
|
->Then(GetTaskQueue(), __func__, this,
|
2015-01-11 14:05:08 -08:00
|
|
|
&MediaSourceReader::OnVideoSeekCompleted,
|
2014-12-16 01:52:57 -08:00
|
|
|
&MediaSourceReader::OnSeekFailed);
|
2014-11-04 16:32:26 -08:00
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::Seek video reader=%p", this, mVideoReader.get());
|
2015-01-11 14:05:08 -08:00
|
|
|
} else {
|
|
|
|
OnVideoSeekCompleted(mPendingSeekTime);
|
2014-11-04 16:32:26 -08:00
|
|
|
}
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
2014-11-11 20:50:21 -08:00
|
|
|
nsresult
|
|
|
|
MediaSourceReader::GetBuffered(dom::TimeRanges* aBuffered)
|
|
|
|
{
|
2014-11-11 20:50:21 -08:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
MOZ_ASSERT(aBuffered->Length() == 0);
|
|
|
|
if (mTrackBuffers.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
double highestEndTime = 0;
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<TimeRanges>> activeRanges;
|
|
|
|
for (uint32_t i = 0; i < mTrackBuffers.Length(); ++i) {
|
|
|
|
nsRefPtr<TimeRanges> r = new TimeRanges();
|
|
|
|
mTrackBuffers[i]->Buffered(r);
|
|
|
|
activeRanges.AppendElement(r);
|
|
|
|
highestEndTime = std::max(highestEndTime, activeRanges.LastElement()->GetEndTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeRanges* intersectionRanges = aBuffered;
|
|
|
|
intersectionRanges->Add(0, highestEndTime);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < activeRanges.Length(); ++i) {
|
|
|
|
TimeRanges* sourceRanges = activeRanges[i];
|
|
|
|
|
|
|
|
if (IsEnded()) {
|
|
|
|
// Set the end time on the last range to highestEndTime by adding a
|
|
|
|
// new range spanning the current end time to highestEndTime, which
|
|
|
|
// Normalize() will then merge with the old last range.
|
|
|
|
sourceRanges->Add(sourceRanges->GetEndTime(), highestEndTime);
|
|
|
|
sourceRanges->Normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
intersectionRanges->Intersection(sourceRanges);
|
|
|
|
}
|
|
|
|
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::GetBuffered ranges=%s", this, DumpTimeRanges(intersectionRanges).get());
|
2014-11-11 20:50:21 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-22 00:20:31 -08:00
|
|
|
nsRefPtr<MediaDecoderReader::WaitForDataPromise>
|
|
|
|
MediaSourceReader::WaitForData(MediaData::Type aType)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
nsRefPtr<WaitForDataPromise> p = WaitPromise(aType).Ensure(__func__);
|
|
|
|
MaybeNotifyHaveData();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::MaybeNotifyHaveData()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
bool haveAudio = false, haveVideo = false;
|
|
|
|
if (!mAudioIsSeeking && mAudioTrack && HaveData(mLastAudioTime, MediaData::AUDIO_DATA)) {
|
|
|
|
haveAudio = true;
|
|
|
|
WaitPromise(MediaData::AUDIO_DATA).ResolveIfExists(MediaData::AUDIO_DATA, __func__);
|
|
|
|
}
|
|
|
|
if (!mVideoIsSeeking && mVideoTrack && HaveData(mLastVideoTime, MediaData::VIDEO_DATA)) {
|
|
|
|
haveVideo = true;
|
|
|
|
WaitPromise(MediaData::VIDEO_DATA).ResolveIfExists(MediaData::VIDEO_DATA, __func__);
|
|
|
|
}
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::MaybeNotifyHaveData haveAudio=%d, haveVideo=%d", this,
|
|
|
|
haveAudio, haveVideo);
|
|
|
|
}
|
|
|
|
|
2014-08-13 05:22:00 -07:00
|
|
|
nsresult
|
|
|
|
MediaSourceReader::ReadMetadata(MediaInfo* aInfo, MetadataTags** aTags)
|
|
|
|
{
|
2014-12-21 23:14:59 -08:00
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
|
2014-09-15 20:15:58 -07:00
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::ReadMetadata tracks=%u/%u audio=%p video=%p",
|
|
|
|
this, mEssentialTrackBuffers.Length(), mTrackBuffers.Length(),
|
2014-09-04 17:04:54 -07:00
|
|
|
mAudioTrack.get(), mVideoTrack.get());
|
2014-09-15 20:15:58 -07:00
|
|
|
|
2014-12-21 23:14:59 -08:00
|
|
|
mEssentialTrackBuffers.Clear();
|
2014-09-03 18:57:06 -07:00
|
|
|
if (!mAudioTrack && !mVideoTrack) {
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::ReadMetadata missing track: mAudioTrack=%p mVideoTrack=%p",
|
|
|
|
this, mAudioTrack.get(), mVideoTrack.get());
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-08-28 01:18:01 -07:00
|
|
|
|
2014-09-03 10:55:55 -07:00
|
|
|
int64_t maxDuration = -1;
|
2014-09-03 18:57:06 -07:00
|
|
|
|
|
|
|
if (mAudioTrack) {
|
|
|
|
MOZ_ASSERT(mAudioTrack->IsReady());
|
|
|
|
mAudioReader = mAudioTrack->Decoders()[0]->GetReader();
|
|
|
|
|
|
|
|
const MediaInfo& info = mAudioReader->GetMediaInfo();
|
|
|
|
MOZ_ASSERT(info.HasAudio());
|
|
|
|
mInfo.mAudio = info.mAudio;
|
|
|
|
maxDuration = std::max(maxDuration, mAudioReader->GetDecoder()->GetMediaDuration());
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::ReadMetadata audio reader=%p maxDuration=%lld",
|
|
|
|
this, mAudioReader.get(), maxDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVideoTrack) {
|
|
|
|
MOZ_ASSERT(mVideoTrack->IsReady());
|
|
|
|
mVideoReader = mVideoTrack->Decoders()[0]->GetReader();
|
|
|
|
|
|
|
|
const MediaInfo& info = mVideoReader->GetMediaInfo();
|
|
|
|
MOZ_ASSERT(info.HasVideo());
|
|
|
|
mInfo.mVideo = info.mVideo;
|
|
|
|
maxDuration = std::max(maxDuration, mVideoReader->GetDecoder()->GetMediaDuration());
|
|
|
|
MSE_DEBUG("MediaSourceReader(%p)::ReadMetadata video reader=%p maxDuration=%lld",
|
|
|
|
this, mVideoReader.get(), maxDuration);
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
2015-01-16 04:49:01 -08:00
|
|
|
if (!maxDuration) {
|
|
|
|
// Treat a duration of 0 as infinity
|
|
|
|
maxDuration = -1;
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
2015-01-16 04:49:01 -08:00
|
|
|
static_cast<MediaSourceDecoder*>(mDecoder)->SetDecodedDuration(maxDuration);
|
2014-08-13 05:22:00 -07:00
|
|
|
|
|
|
|
*aInfo = mInfo;
|
|
|
|
*aTags = nullptr; // TODO: Handle metadata.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-11-18 04:23:00 -08:00
|
|
|
void
|
|
|
|
MediaSourceReader::ReadUpdatedMetadata(MediaInfo* aInfo)
|
|
|
|
{
|
|
|
|
if (mAudioTrack) {
|
|
|
|
MOZ_ASSERT(mAudioTrack->IsReady());
|
|
|
|
mAudioReader = mAudioTrack->Decoders()[0]->GetReader();
|
|
|
|
|
|
|
|
const MediaInfo& info = mAudioReader->GetMediaInfo();
|
|
|
|
MOZ_ASSERT(info.HasAudio());
|
|
|
|
mInfo.mAudio = info.mAudio;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVideoTrack) {
|
|
|
|
MOZ_ASSERT(mVideoTrack->IsReady());
|
|
|
|
mVideoReader = mVideoTrack->Decoders()[0]->GetReader();
|
|
|
|
|
|
|
|
const MediaInfo& info = mVideoReader->GetMediaInfo();
|
|
|
|
MOZ_ASSERT(info.HasVideo());
|
|
|
|
mInfo.mVideo = info.mVideo;
|
|
|
|
}
|
|
|
|
*aInfo = mInfo;
|
|
|
|
}
|
|
|
|
|
2014-08-26 00:25:09 -07:00
|
|
|
void
|
|
|
|
MediaSourceReader::Ended()
|
|
|
|
{
|
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
mEnded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaSourceReader::IsEnded()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
return mEnded;
|
|
|
|
}
|
|
|
|
|
2014-10-13 15:05:00 -07:00
|
|
|
#ifdef MOZ_EME
|
|
|
|
nsresult
|
|
|
|
MediaSourceReader::SetCDMProxy(CDMProxy* aProxy)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
|
|
|
|
mCDMProxy = aProxy;
|
|
|
|
for (size_t i = 0; i < mTrackBuffers.Length(); i++) {
|
|
|
|
nsresult rv = mTrackBuffers[i]->SetCDMProxy(aProxy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-15 19:14:56 -08:00
|
|
|
bool
|
|
|
|
MediaSourceReader::IsActiveReader(MediaDecoderReader* aReader)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
return aReader == mVideoReader.get() || aReader == mAudioReader.get();
|
|
|
|
}
|
|
|
|
|
2014-08-13 05:22:00 -07:00
|
|
|
} // namespace mozilla
|