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"
|
|
|
|
|
2015-02-16 19:22:51 -08:00
|
|
|
#include <cmath>
|
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"
|
2015-01-28 18:35:58 -08:00
|
|
|
#include "nsPrintfCString.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
|
2015-02-27 14:37:52 -08:00
|
|
|
extern PRLogModuleInfo* GetMediaSourceLog();
|
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
#define MSE_DEBUG(arg, ...) PR_LOG(GetMediaSourceLog(), PR_LOG_DEBUG, ("MediaSourceReader(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))
|
|
|
|
#define MSE_DEBUGV(arg, ...) PR_LOG(GetMediaSourceLog(), PR_LOG_DEBUG + 1, ("MediaSourceReader(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))
|
2014-08-13 05:22:00 -07:00
|
|
|
#else
|
|
|
|
#define MSE_DEBUG(...)
|
|
|
|
#define MSE_DEBUGV(...)
|
|
|
|
#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-29 22:11:11 -08:00
|
|
|
, mTimeThreshold(0)
|
2014-08-20 20:14:48 -07:00
|
|
|
, mDropAudioBeforeThreshold(false)
|
2014-08-13 05:22:00 -07:00
|
|
|
, mDropVideoBeforeThreshold(false)
|
2015-02-09 04:29:01 -08:00
|
|
|
, mAudioDiscontinuity(false)
|
|
|
|
, mVideoDiscontinuity(false)
|
2014-08-26 00:25:09 -07:00
|
|
|
, mEnded(false)
|
2015-01-24 02:46:21 -08:00
|
|
|
, mMediaSourceDuration(0)
|
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());
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("trackBuffers=%u", mTrackBuffers.Length());
|
2014-09-04 17:04:54 -07:00
|
|
|
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()
|
|
|
|
{
|
2015-02-09 04:28:59 -08:00
|
|
|
if (!GetVideoReader()) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("called with no video reader");
|
2014-12-17 15:41:19 -08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-02-09 04:28:59 -08:00
|
|
|
return GetVideoReader()->SizeOfVideoQueueInFrames();
|
2014-12-17 15:41:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
MediaSourceReader::SizeOfAudioQueueInFrames()
|
|
|
|
{
|
2015-02-09 04:28:59 -08:00
|
|
|
if (!GetAudioReader()) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("called with no audio reader");
|
2014-12-17 15:41:19 -08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-02-09 04:28:59 -08:00
|
|
|
return GetAudioReader()->SizeOfAudioQueueInFrames();
|
2014-12-17 15:41:19 -08:00
|
|
|
}
|
|
|
|
|
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__);
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUGV("");
|
2015-02-23 21:38:41 -08:00
|
|
|
if (!mAudioTrack) {
|
|
|
|
MSE_DEBUG("called with no audio track");
|
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-29 22:11:11 -08:00
|
|
|
if (IsSeeking()) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("called mid-seek. Rejecting.");
|
2015-01-16 10:58:00 -08:00
|
|
|
mAudioPromise.Reject(CANCELED, __func__);
|
|
|
|
return p;
|
|
|
|
}
|
2015-02-04 13:13:59 -08:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mAudioSeekRequest.Exists());
|
2015-01-29 22:11:12 -08:00
|
|
|
|
2015-02-06 04:51:46 -08:00
|
|
|
SwitchSourceResult ret = SwitchAudioSource(&mLastAudioTime);
|
2015-01-24 02:45:58 -08:00
|
|
|
switch (ret) {
|
2015-02-09 04:28:59 -08:00
|
|
|
case SOURCE_NEW:
|
|
|
|
mAudioSeekRequest.Begin(GetAudioReader()->Seek(GetReaderAudioTime(mLastAudioTime), 0)
|
2015-01-29 22:11:12 -08:00
|
|
|
->RefableThen(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::CompleteAudioSeekAndDoRequest,
|
|
|
|
&MediaSourceReader::CompleteAudioSeekAndRejectPromise));
|
2015-01-24 02:45:58 -08:00
|
|
|
break;
|
2015-02-23 21:38:41 -08:00
|
|
|
case SOURCE_NONE:
|
2015-01-24 02:45:58 -08:00
|
|
|
if (mLastAudioTime) {
|
2015-01-24 02:46:21 -08:00
|
|
|
CheckForWaitOrEndOfStream(MediaData::AUDIO_DATA, mLastAudioTime);
|
2015-01-24 02:45:58 -08:00
|
|
|
break;
|
|
|
|
}
|
2015-02-23 21:38:41 -08:00
|
|
|
// Fallback to using first reader
|
|
|
|
mAudioSourceDecoder = mAudioTrack->Decoders()[0];
|
2015-01-24 02:45:58 -08:00
|
|
|
default:
|
2015-01-29 22:11:11 -08:00
|
|
|
DoAudioRequest();
|
2015-01-24 02:45:58 -08:00
|
|
|
break;
|
2015-01-11 18:10:15 -08:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-01-29 22:11:11 -08:00
|
|
|
void MediaSourceReader::DoAudioRequest()
|
|
|
|
{
|
2015-02-09 04:28:59 -08:00
|
|
|
mAudioRequest.Begin(GetAudioReader()->RequestAudioData()
|
2015-01-29 22:11:11 -08:00
|
|
|
->RefableThen(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::OnAudioDecoded,
|
|
|
|
&MediaSourceReader::OnAudioNotDecoded));
|
2015-01-11 18:10:15 -08:00
|
|
|
}
|
|
|
|
|
2015-01-28 19:54:34 -08:00
|
|
|
void
|
|
|
|
MediaSourceReader::OnAudioDecoded(AudioData* aSample)
|
|
|
|
{
|
2015-02-04 13:13:59 -08:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsSeeking());
|
2015-01-29 22:11:11 -08:00
|
|
|
mAudioRequest.Complete();
|
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
int64_t ourTime = aSample->mTime + mAudioSourceDecoder->GetTimestampOffset();
|
2015-02-09 04:29:01 -08:00
|
|
|
if (aSample->mDiscontinuity) {
|
|
|
|
mAudioDiscontinuity = true;
|
|
|
|
}
|
2015-02-09 04:28:59 -08:00
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUGV("[mTime=%lld mDuration=%lld mDiscontinuity=%d]",
|
|
|
|
ourTime, aSample->mDuration, aSample->mDiscontinuity);
|
2014-08-20 20:14:48 -07:00
|
|
|
if (mDropAudioBeforeThreshold) {
|
2015-02-09 04:28:59 -08:00
|
|
|
if (ourTime < mTimeThreshold) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("mTime=%lld < mTimeThreshold=%lld",
|
|
|
|
ourTime, mTimeThreshold);
|
2015-02-09 04:28:59 -08:00
|
|
|
mAudioRequest.Begin(GetAudioReader()->RequestAudioData()
|
2015-01-29 22:11:11 -08:00
|
|
|
->RefableThen(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
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
// Adjust the sample time into our reference.
|
|
|
|
nsRefPtr<AudioData> newSample =
|
|
|
|
AudioData::TransferAndUpdateTimestampAndDuration(aSample,
|
|
|
|
ourTime,
|
|
|
|
aSample->mDuration);
|
|
|
|
mLastAudioTime = newSample->GetEndTime();
|
2015-02-09 04:29:01 -08:00
|
|
|
if (mAudioDiscontinuity) {
|
|
|
|
newSample->mDiscontinuity = true;
|
|
|
|
mAudioDiscontinuity = false;
|
|
|
|
}
|
2014-12-10 14:03:56 -08:00
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
mAudioPromise.Resolve(newSample, __func__);
|
2014-12-10 14:03:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2015-02-09 04:28:59 -08:00
|
|
|
AdjustEndTime(int64_t* aEndTime, SourceBufferDecoder* aDecoder)
|
2014-12-10 14:03:56 -08:00
|
|
|
{
|
2015-02-09 04:29:00 -08:00
|
|
|
if (aDecoder) {
|
2014-12-10 14:03:56 -08:00
|
|
|
nsRefPtr<dom::TimeRanges> ranges = new dom::TimeRanges();
|
2015-02-09 04:29:00 -08:00
|
|
|
aDecoder->GetBuffered(ranges);
|
2014-12-10 14:03:56 -08:00
|
|
|
if (ranges->Length() > 0) {
|
2015-02-16 19:22:51 -08:00
|
|
|
int64_t end = std::ceil(ranges->GetEndTime() * USECS_PER_S);
|
2014-12-10 14:03:56 -08:00
|
|
|
*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)
|
|
|
|
{
|
2015-02-04 13:13:59 -08:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsSeeking());
|
2015-01-29 22:11:11 -08:00
|
|
|
mAudioRequest.Complete();
|
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("aReason=%u IsEnded: %d", aReason, IsEnded());
|
2014-12-10 14:03:56 -08:00
|
|
|
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);
|
2015-02-09 04:28:59 -08:00
|
|
|
if (mAudioSourceDecoder) {
|
|
|
|
AdjustEndTime(&mLastAudioTime, mAudioSourceDecoder);
|
2014-12-10 14:03:56 -08:00
|
|
|
}
|
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
// See if we can find a different source that can pick up where we left off.
|
2015-02-06 04:51:46 -08:00
|
|
|
if (SwitchAudioSource(&mLastAudioTime) == SOURCE_NEW) {
|
2015-02-09 04:28:59 -08:00
|
|
|
mAudioSeekRequest.Begin(GetAudioReader()->Seek(GetReaderAudioTime(mLastAudioTime), 0)
|
2015-01-29 22:11:12 -08:00
|
|
|
->RefableThen(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::CompleteAudioSeekAndDoRequest,
|
|
|
|
&MediaSourceReader::CompleteAudioSeekAndRejectPromise));
|
2014-12-10 14:03:56 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-24 02:46:21 -08:00
|
|
|
CheckForWaitOrEndOfStream(MediaData::AUDIO_DATA, mLastAudioTime);
|
2014-12-10 14:03:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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__);
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUGV("RequestVideoData(%d, %lld)",
|
|
|
|
aSkipToNextKeyframe, aTimeThreshold);
|
2015-02-23 21:38:41 -08:00
|
|
|
if (!mVideoTrack) {
|
|
|
|
MSE_DEBUG("called with no video track");
|
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-29 22:11:11 -08:00
|
|
|
if (IsSeeking()) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("called mid-seek. Rejecting.");
|
2015-01-16 10:58:00 -08:00
|
|
|
mVideoPromise.Reject(CANCELED, __func__);
|
|
|
|
return p;
|
|
|
|
}
|
2015-02-04 13:13:59 -08:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mVideoSeekRequest.Exists());
|
2015-01-29 22:11:12 -08:00
|
|
|
|
2015-02-06 04:51:46 -08:00
|
|
|
SwitchSourceResult ret = SwitchVideoSource(&mLastVideoTime);
|
2015-01-24 02:45:58 -08:00
|
|
|
switch (ret) {
|
2015-02-09 04:28:59 -08:00
|
|
|
case SOURCE_NEW:
|
|
|
|
mVideoSeekRequest.Begin(GetVideoReader()->Seek(GetReaderVideoTime(mLastVideoTime), 0)
|
2015-01-29 22:11:12 -08:00
|
|
|
->RefableThen(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::CompleteVideoSeekAndDoRequest,
|
|
|
|
&MediaSourceReader::CompleteVideoSeekAndRejectPromise));
|
2015-01-24 02:45:58 -08:00
|
|
|
break;
|
2015-02-23 21:38:41 -08:00
|
|
|
case SOURCE_NONE:
|
2015-01-24 02:45:58 -08:00
|
|
|
if (mLastVideoTime) {
|
2015-01-24 02:46:21 -08:00
|
|
|
CheckForWaitOrEndOfStream(MediaData::VIDEO_DATA, mLastVideoTime);
|
2015-01-24 02:45:58 -08:00
|
|
|
break;
|
|
|
|
}
|
2015-02-23 21:38:41 -08:00
|
|
|
// Fallback to using first reader.
|
|
|
|
mVideoSourceDecoder = mVideoTrack->Decoders()[0];
|
2015-01-24 02:45:58 -08:00
|
|
|
default:
|
2015-01-29 22:11:11 -08:00
|
|
|
DoVideoRequest();
|
2015-01-24 02:45:58 -08:00
|
|
|
break;
|
2015-01-11 18:10:15 -08:00
|
|
|
}
|
2014-12-10 14:03:56 -08:00
|
|
|
|
2015-01-11 18:10:15 -08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-01-29 22:11:11 -08:00
|
|
|
void
|
|
|
|
MediaSourceReader::DoVideoRequest()
|
|
|
|
{
|
2015-02-09 04:28:59 -08:00
|
|
|
mVideoRequest.Begin(GetVideoReader()->RequestVideoData(mDropVideoBeforeThreshold, GetReaderVideoTime(mTimeThreshold))
|
2015-01-29 22:11:11 -08:00
|
|
|
->RefableThen(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::OnVideoDecoded,
|
|
|
|
&MediaSourceReader::OnVideoNotDecoded));
|
2015-01-11 18:10:15 -08:00
|
|
|
}
|
|
|
|
|
2015-01-28 19:54:34 -08:00
|
|
|
void
|
|
|
|
MediaSourceReader::OnVideoDecoded(VideoData* aSample)
|
|
|
|
{
|
2015-02-04 13:13:59 -08:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsSeeking());
|
2015-01-29 22:11:11 -08:00
|
|
|
mVideoRequest.Complete();
|
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
// Adjust the sample time into our reference.
|
|
|
|
int64_t ourTime = aSample->mTime + mVideoSourceDecoder->GetTimestampOffset();
|
2015-02-09 04:29:01 -08:00
|
|
|
if (aSample->mDiscontinuity) {
|
|
|
|
mVideoDiscontinuity = true;
|
|
|
|
}
|
2015-02-09 04:28:59 -08:00
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUGV("[mTime=%lld mDuration=%lld mDiscontinuity=%d]",
|
|
|
|
ourTime, aSample->mDuration, aSample->mDiscontinuity);
|
2014-08-13 05:22:00 -07:00
|
|
|
if (mDropVideoBeforeThreshold) {
|
2015-02-09 04:28:59 -08:00
|
|
|
if (ourTime < mTimeThreshold) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("mTime=%lld < mTimeThreshold=%lld",
|
|
|
|
ourTime, mTimeThreshold);
|
2015-01-29 22:11:11 -08:00
|
|
|
DoVideoRequest();
|
2014-08-13 05:22:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDropVideoBeforeThreshold = false;
|
2015-01-29 22:11:11 -08:00
|
|
|
mTimeThreshold = 0;
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
2014-09-03 02:56:22 -07:00
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
// Adjust the sample time into our reference.
|
|
|
|
nsRefPtr<VideoData> newSample =
|
|
|
|
VideoData::ShallowCopyUpdateTimestampAndDuration(aSample,
|
|
|
|
ourTime,
|
|
|
|
aSample->mDuration);
|
2014-12-10 14:03:56 -08:00
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
mLastVideoTime = newSample->GetEndTime();
|
2015-02-09 04:29:01 -08:00
|
|
|
if (mVideoDiscontinuity) {
|
|
|
|
newSample->mDiscontinuity = true;
|
|
|
|
mVideoDiscontinuity = false;
|
|
|
|
}
|
2015-02-09 04:28:59 -08:00
|
|
|
|
|
|
|
mVideoPromise.Resolve(newSample, __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
|
|
|
{
|
2015-02-04 13:13:59 -08:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsSeeking());
|
2015-01-29 22:11:11 -08:00
|
|
|
mVideoRequest.Complete();
|
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("aReason=%u IsEnded: %d", 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);
|
2015-02-09 04:28:59 -08:00
|
|
|
if (mVideoSourceDecoder) {
|
|
|
|
AdjustEndTime(&mLastVideoTime, mVideoSourceDecoder);
|
2014-11-11 15:12:21 -08:00
|
|
|
}
|
2014-11-03 00:20:14 -08:00
|
|
|
|
2015-01-30 17:45:49 -08:00
|
|
|
// See if we can find a different reader that can pick up where we left off.
|
2015-02-06 04:51:46 -08:00
|
|
|
if (SwitchVideoSource(&mLastVideoTime) == SOURCE_NEW) {
|
2015-02-09 04:28:59 -08:00
|
|
|
mVideoSeekRequest.Begin(GetVideoReader()->Seek(GetReaderVideoTime(mLastVideoTime), 0)
|
2015-01-29 22:11:12 -08:00
|
|
|
->RefableThen(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::CompleteVideoSeekAndDoRequest,
|
|
|
|
&MediaSourceReader::CompleteVideoSeekAndRejectPromise));
|
2014-11-03 00:20:14 -08:00
|
|
|
return;
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
2015-01-24 02:46:21 -08:00
|
|
|
CheckForWaitOrEndOfStream(MediaData::VIDEO_DATA, mLastVideoTime);
|
2015-01-24 02:45:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-24 02:46:21 -08:00
|
|
|
MediaSourceReader::CheckForWaitOrEndOfStream(MediaData::Type aType, int64_t aTime)
|
2015-01-24 02:45:58 -08:00
|
|
|
{
|
2014-11-03 00:20:15 -08:00
|
|
|
// If the entire MediaSource is done, generate an EndOfStream.
|
2015-01-24 02:46:21 -08:00
|
|
|
if (IsNearEnd(aTime)) {
|
2015-01-24 02:45:58 -08:00
|
|
|
if (aType == MediaData::AUDIO_DATA) {
|
|
|
|
mAudioPromise.Reject(END_OF_STREAM, __func__);
|
|
|
|
} else {
|
|
|
|
mVideoPromise.Reject(END_OF_STREAM, __func__);
|
|
|
|
}
|
2014-11-03 00:20:15 -08:00
|
|
|
return;
|
2014-11-03 00:20:14 -08:00
|
|
|
}
|
|
|
|
|
2015-01-24 02:45:58 -08:00
|
|
|
if (aType == MediaData::AUDIO_DATA) {
|
|
|
|
// 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__);
|
|
|
|
} else {
|
|
|
|
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;
|
2015-02-09 04:28:59 -08:00
|
|
|
mAudioSourceDecoder = nullptr;
|
2014-09-03 18:57:06 -07:00
|
|
|
mVideoTrack = nullptr;
|
2015-02-09 04:28:59 -08:00
|
|
|
mVideoSourceDecoder = nullptr;
|
2014-12-09 11:43:21 -08:00
|
|
|
|
2015-01-31 02:22:38 -08:00
|
|
|
#ifdef MOZ_FMP4
|
2015-01-29 00:50:48 -08:00
|
|
|
if (mSharedDecoderManager) {
|
|
|
|
mSharedDecoderManager->Shutdown();
|
|
|
|
mSharedDecoderManager = nullptr;
|
|
|
|
}
|
2015-01-31 02:22:38 -08:00
|
|
|
#endif
|
2015-01-29 00:50:48 -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);
|
2015-02-09 04:28:59 -08:00
|
|
|
MOZ_ASSERT(!mAudioSourceDecoder);
|
2014-10-09 22:26:12 -07:00
|
|
|
MOZ_ASSERT(!mVideoTrack);
|
2015-02-09 04:28:59 -08:00
|
|
|
MOZ_ASSERT(!mVideoSourceDecoder);
|
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
|
|
|
}
|
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
already_AddRefed<SourceBufferDecoder>
|
|
|
|
MediaSourceReader::SelectDecoder(int64_t aTarget,
|
|
|
|
int64_t aTolerance,
|
|
|
|
const nsTArray<nsRefPtr<SourceBufferDecoder>>& aTrackDecoders)
|
2014-09-07 17:07:56 -07:00
|
|
|
{
|
2015-02-12 21:52:42 -08:00
|
|
|
return static_cast<MediaSourceDecoder*>(mDecoder)
|
|
|
|
->SelectDecoder(aTarget, aTolerance, aTrackDecoders);
|
2014-09-07 17:07:56 -07:00
|
|
|
}
|
|
|
|
|
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-02-09 04:28:59 -08:00
|
|
|
nsRefPtr<SourceBufferDecoder> decoder = SelectDecoder(aTarget, EOS_FUZZ_US, trackBuffer->Decoders());
|
|
|
|
return !!decoder;
|
2014-12-22 00:20:31 -08:00
|
|
|
}
|
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
MediaSourceReader::SwitchSourceResult
|
2015-02-06 04:51:46 -08:00
|
|
|
MediaSourceReader::SwitchAudioSource(int64_t* aTarget)
|
2014-09-07 17:07:56 -07:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
// XXX: Can't handle adding an audio track after ReadMetadata.
|
|
|
|
if (!mAudioTrack) {
|
2015-02-23 21:38:41 -08:00
|
|
|
return SOURCE_NONE;
|
2014-09-07 17:07:56 -07:00
|
|
|
}
|
2015-01-30 17:45:49 -08:00
|
|
|
|
|
|
|
// We first search without the tolerance and then search with it, so that, in
|
|
|
|
// the case of perfectly-aligned data, we don't prematurely jump to a new
|
|
|
|
// reader and skip the last few samples of the current one.
|
2015-02-06 04:51:46 -08:00
|
|
|
bool usedFuzz = false;
|
2015-02-09 04:28:59 -08:00
|
|
|
nsRefPtr<SourceBufferDecoder> newDecoder =
|
2015-02-06 04:51:46 -08:00
|
|
|
SelectDecoder(*aTarget, /* aTolerance = */ 0, mAudioTrack->Decoders());
|
2015-02-09 04:28:59 -08:00
|
|
|
if (!newDecoder) {
|
2015-02-06 04:51:46 -08:00
|
|
|
newDecoder = SelectDecoder(*aTarget, EOS_FUZZ_US, mAudioTrack->Decoders());
|
|
|
|
usedFuzz = true;
|
2015-02-09 04:28:59 -08:00
|
|
|
}
|
2015-02-23 21:38:41 -08:00
|
|
|
if (GetAudioReader() && mAudioSourceDecoder != newDecoder) {
|
2015-02-09 04:28:59 -08:00
|
|
|
GetAudioReader()->SetIdle();
|
2015-02-23 21:38:41 -08:00
|
|
|
}
|
|
|
|
if (!newDecoder) {
|
|
|
|
mAudioSourceDecoder = nullptr;
|
|
|
|
return SOURCE_NONE;
|
|
|
|
}
|
|
|
|
if (newDecoder == mAudioSourceDecoder) {
|
|
|
|
return SOURCE_EXISTING;
|
|
|
|
}
|
|
|
|
mAudioSourceDecoder = newDecoder;
|
|
|
|
if (usedFuzz) {
|
|
|
|
// A decoder buffered range is continuous. We would have failed the exact
|
|
|
|
// search but succeeded the fuzzy one if our target was shortly before
|
|
|
|
// start time.
|
|
|
|
nsRefPtr<dom::TimeRanges> ranges = new dom::TimeRanges();
|
|
|
|
newDecoder->GetBuffered(ranges);
|
|
|
|
int64_t startTime = ranges->GetStartTime() * USECS_PER_S;
|
|
|
|
if (*aTarget < startTime) {
|
|
|
|
*aTarget = startTime;
|
2015-02-06 04:51:46 -08:00
|
|
|
}
|
2015-02-09 04:28:59 -08:00
|
|
|
}
|
2015-02-23 21:38:41 -08:00
|
|
|
MSE_DEBUGV("switched decoder to %p (fuzz:%d)",
|
|
|
|
mAudioSourceDecoder.get(), usedFuzz);
|
|
|
|
return SOURCE_NEW;
|
2014-09-03 10:55:55 -07:00
|
|
|
}
|
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
MediaSourceReader::SwitchSourceResult
|
2015-02-06 04:51:46 -08:00
|
|
|
MediaSourceReader::SwitchVideoSource(int64_t* aTarget)
|
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) {
|
2015-02-23 21:38:41 -08:00
|
|
|
return SOURCE_NONE;
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
2015-01-30 17:45:49 -08:00
|
|
|
|
|
|
|
// We first search without the tolerance and then search with it, so that, in
|
|
|
|
// the case of perfectly-aligned data, we don't prematurely jump to a new
|
|
|
|
// reader and skip the last few samples of the current one.
|
2015-02-06 04:51:46 -08:00
|
|
|
bool usedFuzz = false;
|
2015-02-09 04:28:59 -08:00
|
|
|
nsRefPtr<SourceBufferDecoder> newDecoder =
|
2015-02-06 04:51:46 -08:00
|
|
|
SelectDecoder(*aTarget, /* aTolerance = */ 0, mVideoTrack->Decoders());
|
2015-02-09 04:28:59 -08:00
|
|
|
if (!newDecoder) {
|
2015-02-06 04:51:46 -08:00
|
|
|
newDecoder = SelectDecoder(*aTarget, EOS_FUZZ_US, mVideoTrack->Decoders());
|
|
|
|
usedFuzz = true;
|
2015-02-09 04:28:59 -08:00
|
|
|
}
|
2015-02-23 21:38:41 -08:00
|
|
|
if (GetVideoReader() && mVideoSourceDecoder != newDecoder) {
|
2015-02-09 04:28:59 -08:00
|
|
|
GetVideoReader()->SetIdle();
|
2015-02-23 21:38:41 -08:00
|
|
|
}
|
|
|
|
if (!newDecoder) {
|
|
|
|
mVideoSourceDecoder = nullptr;
|
|
|
|
return SOURCE_NONE;
|
|
|
|
}
|
|
|
|
if (newDecoder == mVideoSourceDecoder) {
|
|
|
|
return SOURCE_EXISTING;
|
|
|
|
}
|
|
|
|
mVideoSourceDecoder = newDecoder;
|
|
|
|
if (usedFuzz) {
|
|
|
|
// A decoder buffered range is continuous. We would have failed the exact
|
|
|
|
// search but succeeded the fuzzy one if our target was shortly before
|
|
|
|
// start time.
|
|
|
|
nsRefPtr<dom::TimeRanges> ranges = new dom::TimeRanges();
|
|
|
|
newDecoder->GetBuffered(ranges);
|
|
|
|
int64_t startTime = ranges->GetStartTime() * USECS_PER_S;
|
|
|
|
if (*aTarget < startTime) {
|
|
|
|
*aTarget = startTime;
|
2015-02-06 04:51:46 -08:00
|
|
|
}
|
2015-02-09 04:28:59 -08:00
|
|
|
}
|
2015-02-23 21:38:41 -08:00
|
|
|
MSE_DEBUGV("switched decoder to %p (fuzz:%d)",
|
|
|
|
mVideoSourceDecoder.get(), usedFuzz);
|
|
|
|
return SOURCE_NEW;
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
2015-01-13 16:27:00 -08:00
|
|
|
bool
|
|
|
|
MediaSourceReader::IsDormantNeeded()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-02-09 04:28:59 -08:00
|
|
|
if (GetVideoReader()) {
|
|
|
|
return GetVideoReader()->IsDormantNeeded();
|
2015-01-13 16:27:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::ReleaseMediaResources()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-02-09 04:28:59 -08:00
|
|
|
if (GetVideoReader()) {
|
|
|
|
GetVideoReader()->ReleaseMediaResources();
|
2015-01-13 16:27:00 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")) &&
|
2015-02-26 16:31:46 -08:00
|
|
|
MP4Decoder::IsEnabled() && aDecoder) {
|
2014-08-13 05:22:00 -07:00
|
|
|
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
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("subdecoder %p subreader %p",
|
|
|
|
decoder.get(), reader.get());
|
2014-08-13 05:22:00 -07:00
|
|
|
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());
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("AddTrackBuffer(%p)", aTrackBuffer);
|
2014-09-03 18:57:06 -07:00
|
|
|
mTrackBuffers.AppendElement(aTrackBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::RemoveTrackBuffer(TrackBuffer* aTrackBuffer)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("RemoveTrackBuffer(%p)", aTrackBuffer);
|
2014-09-03 18:57:06 -07:00
|
|
|
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) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("%p audio", aTrackBuffer);
|
2014-09-03 18:57:06 -07:00
|
|
|
mAudioTrack = aTrackBuffer;
|
|
|
|
}
|
|
|
|
if (aInfo.HasVideo() && !mVideoTrack) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("%p video", aTrackBuffer);
|
2014-09-03 18:57:06 -07:00
|
|
|
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());
|
2015-01-30 17:45:49 -08:00
|
|
|
if (mAudioTrack && !mAudioTrack->ContainsTime(aTime, EOS_FUZZ_US)) {
|
2014-09-03 18:57:06 -07:00
|
|
|
return false;
|
2014-08-28 01:18:01 -07:00
|
|
|
}
|
2015-01-30 17:45:49 -08:00
|
|
|
if (mVideoTrack && !mVideoTrack->ContainsTime(aTime, EOS_FUZZ_US)) {
|
2014-09-03 18:57:06 -07:00
|
|
|
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) {
|
2015-02-25 22:37:48 -08:00
|
|
|
//post a task to the state machine thread to call seek.
|
2014-11-04 16:32:26 -08:00
|
|
|
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-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("Seek(aTime=%lld, aEnd=%lld, aCurrent=%lld)",
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-01-29 22:11:11 -08:00
|
|
|
// Any previous requests we've been waiting on are now unwanted.
|
|
|
|
mAudioRequest.DisconnectIfExists();
|
|
|
|
mVideoRequest.DisconnectIfExists();
|
|
|
|
|
|
|
|
// Additionally, reject any outstanding promises _we_ made that we might have
|
|
|
|
// been waiting on the above to fulfill.
|
|
|
|
mAudioPromise.RejectIfExists(CANCELED, __func__);
|
|
|
|
mVideoPromise.RejectIfExists(CANCELED, __func__);
|
|
|
|
|
2015-02-03 18:17:56 -08:00
|
|
|
// Do the same for any data wait promises.
|
|
|
|
mAudioWaitPromise.RejectIfExists(WaitForDataRejectValue(MediaData::AUDIO_DATA, WaitForDataRejectValue::CANCELED), __func__);
|
|
|
|
mVideoWaitPromise.RejectIfExists(WaitForDataRejectValue(MediaData::VIDEO_DATA, WaitForDataRejectValue::CANCELED), __func__);
|
|
|
|
|
2015-01-29 22:11:12 -08:00
|
|
|
// Finally, if we were midway seeking a new reader to find a sample, abandon
|
|
|
|
// that too.
|
|
|
|
mAudioSeekRequest.DisconnectIfExists();
|
|
|
|
mVideoSeekRequest.DisconnectIfExists();
|
|
|
|
|
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;
|
2015-02-09 04:29:01 -08:00
|
|
|
mDropAudioBeforeThreshold = false;
|
|
|
|
mDropVideoBeforeThreshold = false;
|
|
|
|
mTimeThreshold = 0;
|
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());
|
2015-01-29 22:11:12 -08:00
|
|
|
mWaitingForSeekData = false;
|
|
|
|
mPendingSeekTime = -1;
|
2015-02-09 04:28:59 -08:00
|
|
|
if (GetAudioReader()) {
|
2015-01-29 22:11:12 -08:00
|
|
|
mAudioSeekRequest.DisconnectIfExists();
|
2015-02-09 04:28:59 -08:00
|
|
|
GetAudioReader()->CancelSeek();
|
2015-01-28 18:54:11 -08:00
|
|
|
}
|
2015-02-09 04:28:59 -08:00
|
|
|
if (GetVideoReader()) {
|
2015-01-29 22:11:12 -08:00
|
|
|
mVideoSeekRequest.DisconnectIfExists();
|
2015-02-09 04:28:59 -08:00
|
|
|
GetVideoReader()->CancelSeek();
|
2015-01-29 22:11:12 -08:00
|
|
|
}
|
|
|
|
mSeekPromise.RejectIfExists(NS_OK, __func__);
|
2015-01-16 10:58:00 -08:00
|
|
|
}
|
|
|
|
|
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-29 22:11:12 -08:00
|
|
|
mVideoSeekRequest.Complete();
|
2015-01-16 10:58:00 -08:00
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
// The aTime we receive is in the sub-reader's reference.
|
|
|
|
int64_t ourTime = aTime + mVideoSourceDecoder->GetTimestampOffset();
|
|
|
|
|
2015-01-11 14:05:08 -08:00
|
|
|
if (mAudioTrack) {
|
2015-02-09 04:28:59 -08:00
|
|
|
mPendingSeekTime = ourTime;
|
2015-01-29 22:11:11 -08:00
|
|
|
DoAudioSeek();
|
|
|
|
} else {
|
|
|
|
mPendingSeekTime = -1;
|
2015-02-09 04:28:59 -08:00
|
|
|
mSeekPromise.Resolve(ourTime, __func__);
|
2015-01-29 22:11:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 22:11:12 -08:00
|
|
|
void
|
|
|
|
MediaSourceReader::OnVideoSeekFailed(nsresult aResult)
|
|
|
|
{
|
|
|
|
mVideoSeekRequest.Complete();
|
|
|
|
mPendingSeekTime = -1;
|
|
|
|
mSeekPromise.Reject(aResult, __func__);
|
|
|
|
}
|
|
|
|
|
2015-01-29 22:11:11 -08:00
|
|
|
void
|
|
|
|
MediaSourceReader::DoAudioSeek()
|
|
|
|
{
|
2015-02-23 21:38:41 -08:00
|
|
|
if (SwitchAudioSource(&mPendingSeekTime) == SOURCE_NONE) {
|
|
|
|
// Data we need got evicted since the last time we checked for data
|
|
|
|
// availability. Abort current seek attempt.
|
|
|
|
mWaitingForSeekData = true;
|
|
|
|
return;
|
|
|
|
}
|
2015-02-09 04:28:59 -08:00
|
|
|
mAudioSeekRequest.Begin(GetAudioReader()->Seek(GetReaderAudioTime(mPendingSeekTime), 0)
|
|
|
|
->RefableThen(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::OnAudioSeekCompleted,
|
|
|
|
&MediaSourceReader::OnAudioSeekFailed));
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("reader=%p", GetAudioReader());
|
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-29 22:11:12 -08:00
|
|
|
mAudioSeekRequest.Complete();
|
2015-01-16 10:58:00 -08:00
|
|
|
mPendingSeekTime = -1;
|
2015-02-09 04:28:59 -08:00
|
|
|
// The aTime we receive is in the sub-reader's reference.
|
|
|
|
mSeekPromise.Resolve(aTime + mAudioSourceDecoder->GetTimestampOffset(),
|
|
|
|
__func__);
|
2014-12-16 01:52:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-29 22:11:12 -08:00
|
|
|
MediaSourceReader::OnAudioSeekFailed(nsresult aResult)
|
2015-01-11 14:05:08 -08:00
|
|
|
{
|
2015-01-29 22:11:12 -08:00
|
|
|
mAudioSeekRequest.Complete();
|
2015-01-16 10:58:00 -08:00
|
|
|
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());
|
2015-02-23 21:38:41 -08:00
|
|
|
if (!mWaitingForSeekData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!TrackBuffersContainTime(mPendingSeekTime)) {
|
|
|
|
mVideoSourceDecoder = nullptr;
|
|
|
|
mAudioSourceDecoder = nullptr;
|
2014-11-04 16:32:26 -08:00
|
|
|
return;
|
|
|
|
}
|
2015-01-16 10:58:00 -08:00
|
|
|
mWaitingForSeekData = false;
|
2014-11-04 16:32:26 -08:00
|
|
|
}
|
|
|
|
|
2015-02-25 22:37:48 -08:00
|
|
|
ResetDecode();
|
|
|
|
for (uint32_t i = 0; i < mTrackBuffers.Length(); ++i) {
|
|
|
|
mTrackBuffers[i]->ResetDecode();
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
// 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) {
|
2015-01-29 22:11:11 -08:00
|
|
|
DoVideoSeek();
|
|
|
|
} else if (mAudioTrack) {
|
|
|
|
DoAudioSeek();
|
2015-01-11 14:05:08 -08:00
|
|
|
} else {
|
2015-01-29 22:11:11 -08:00
|
|
|
MOZ_CRASH();
|
2014-11-04 16:32:26 -08:00
|
|
|
}
|
2014-08-13 05:22:00 -07:00
|
|
|
}
|
|
|
|
|
2015-01-29 22:11:11 -08:00
|
|
|
void
|
|
|
|
MediaSourceReader::DoVideoSeek()
|
|
|
|
{
|
2015-02-23 21:38:41 -08:00
|
|
|
if (SwitchVideoSource(&mPendingSeekTime) == SOURCE_NONE) {
|
|
|
|
// Data we need got evicted since the last time we checked for data
|
|
|
|
// availability. Abort current seek attempt.
|
|
|
|
mWaitingForSeekData = true;
|
|
|
|
return;
|
|
|
|
}
|
2015-02-09 04:28:59 -08:00
|
|
|
mVideoSeekRequest.Begin(GetVideoReader()->Seek(GetReaderVideoTime(mPendingSeekTime), 0)
|
2015-01-29 22:11:12 -08:00
|
|
|
->RefableThen(GetTaskQueue(), __func__, this,
|
|
|
|
&MediaSourceReader::OnVideoSeekCompleted,
|
|
|
|
&MediaSourceReader::OnVideoSeekFailed));
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("reader=%p", GetVideoReader());
|
2015-01-29 22:11:11 -08: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);
|
|
|
|
}
|
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("ranges=%s", 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;
|
2015-01-29 22:11:11 -08:00
|
|
|
if (!IsSeeking() && mAudioTrack && HaveData(mLastAudioTime, MediaData::AUDIO_DATA)) {
|
2014-12-22 00:20:31 -08:00
|
|
|
haveAudio = true;
|
|
|
|
WaitPromise(MediaData::AUDIO_DATA).ResolveIfExists(MediaData::AUDIO_DATA, __func__);
|
|
|
|
}
|
2015-01-29 22:11:11 -08:00
|
|
|
if (!IsSeeking() && mVideoTrack && HaveData(mLastVideoTime, MediaData::VIDEO_DATA)) {
|
2014-12-22 00:20:31 -08:00
|
|
|
haveVideo = true;
|
|
|
|
WaitPromise(MediaData::VIDEO_DATA).ResolveIfExists(MediaData::VIDEO_DATA, __func__);
|
|
|
|
}
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("isSeeking=%d haveAudio=%d, haveVideo=%d",
|
|
|
|
IsSeeking(), haveAudio, haveVideo);
|
2014-12-22 00:20:31 -08:00
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("tracks=%u/%u audio=%p video=%p",
|
|
|
|
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) {
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("missing track: mAudioTrack=%p mVideoTrack=%p",
|
|
|
|
mAudioTrack.get(), mVideoTrack.get());
|
2014-09-03 18:57:06 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-08-28 01:18:01 -07:00
|
|
|
|
2014-09-03 18:57:06 -07:00
|
|
|
if (mAudioTrack) {
|
|
|
|
MOZ_ASSERT(mAudioTrack->IsReady());
|
2015-02-09 04:28:59 -08:00
|
|
|
mAudioSourceDecoder = mAudioTrack->Decoders()[0];
|
2014-09-03 18:57:06 -07:00
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
const MediaInfo& info = GetAudioReader()->GetMediaInfo();
|
2014-09-03 18:57:06 -07:00
|
|
|
MOZ_ASSERT(info.HasAudio());
|
|
|
|
mInfo.mAudio = info.mAudio;
|
2015-01-26 22:10:11 -08:00
|
|
|
mInfo.mIsEncrypted = mInfo.mIsEncrypted || info.mIsEncrypted;
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("audio reader=%p duration=%lld",
|
|
|
|
mAudioSourceDecoder.get(),
|
2015-02-09 04:28:59 -08:00
|
|
|
mAudioSourceDecoder->GetReader()->GetDecoder()->GetMediaDuration());
|
2014-09-03 18:57:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mVideoTrack) {
|
|
|
|
MOZ_ASSERT(mVideoTrack->IsReady());
|
2015-02-09 04:28:59 -08:00
|
|
|
mVideoSourceDecoder = mVideoTrack->Decoders()[0];
|
2014-09-03 18:57:06 -07:00
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
const MediaInfo& info = GetVideoReader()->GetMediaInfo();
|
2014-09-03 18:57:06 -07:00
|
|
|
MOZ_ASSERT(info.HasVideo());
|
|
|
|
mInfo.mVideo = info.mVideo;
|
2015-01-26 22:10:11 -08:00
|
|
|
mInfo.mIsEncrypted = mInfo.mIsEncrypted || info.mIsEncrypted;
|
2015-02-11 23:52:13 -08:00
|
|
|
MSE_DEBUG("video reader=%p duration=%lld",
|
|
|
|
GetVideoReader(),
|
2015-02-09 04:28:59 -08:00
|
|
|
GetVideoReader()->GetDecoder()->GetMediaDuration());
|
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());
|
2015-02-09 04:28:59 -08:00
|
|
|
mAudioSourceDecoder = mAudioTrack->Decoders()[0];
|
2014-11-18 04:23:00 -08:00
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
const MediaInfo& info = GetAudioReader()->GetMediaInfo();
|
2014-11-18 04:23:00 -08:00
|
|
|
MOZ_ASSERT(info.HasAudio());
|
|
|
|
mInfo.mAudio = info.mAudio;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVideoTrack) {
|
|
|
|
MOZ_ASSERT(mVideoTrack->IsReady());
|
2015-02-09 04:28:59 -08:00
|
|
|
mVideoSourceDecoder = mVideoTrack->Decoders()[0];
|
2014-11-18 04:23:00 -08:00
|
|
|
|
2015-02-09 04:28:59 -08:00
|
|
|
const MediaInfo& info = GetVideoReader()->GetMediaInfo();
|
2014-11-18 04:23:00 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-24 02:46:21 -08:00
|
|
|
bool
|
|
|
|
MediaSourceReader::IsNearEnd(int64_t aTime)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
return mEnded && aTime >= (mMediaSourceDuration * USECS_PER_S - EOS_FUZZ_US);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaSourceReader::SetMediaSourceDuration(double aDuration)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
mMediaSourceDuration = aDuration;
|
|
|
|
}
|
|
|
|
|
2015-01-28 18:35:58 -08:00
|
|
|
void
|
|
|
|
MediaSourceReader::GetMozDebugReaderData(nsAString& aString)
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
nsAutoCString result;
|
|
|
|
result += nsPrintfCString("Dumping data for reader %p:\n", this);
|
|
|
|
if (mAudioTrack) {
|
|
|
|
result += nsPrintfCString("\tDumping Audio Track Decoders: - mLastAudioTime: %f\n", double(mLastAudioTime) / USECS_PER_S);
|
|
|
|
for (int32_t i = mAudioTrack->Decoders().Length() - 1; i >= 0; --i) {
|
|
|
|
nsRefPtr<MediaDecoderReader> newReader = mAudioTrack->Decoders()[i]->GetReader();
|
|
|
|
|
|
|
|
nsRefPtr<dom::TimeRanges> ranges = new dom::TimeRanges();
|
|
|
|
mAudioTrack->Decoders()[i]->GetBuffered(ranges);
|
2015-01-28 18:36:02 -08:00
|
|
|
result += nsPrintfCString("\t\tReader %d: %p ranges=%s active=%s size=%lld\n",
|
2015-01-28 18:35:58 -08:00
|
|
|
i, newReader.get(), DumpTimeRanges(ranges).get(),
|
2015-02-09 04:28:59 -08:00
|
|
|
newReader.get() == GetAudioReader() ? "true" : "false",
|
2015-01-28 18:36:02 -08:00
|
|
|
mAudioTrack->Decoders()[i]->GetResource()->GetSize());
|
2015-01-28 18:35:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVideoTrack) {
|
|
|
|
result += nsPrintfCString("\tDumping Video Track Decoders - mLastVideoTime: %f\n", double(mLastVideoTime) / USECS_PER_S);
|
|
|
|
for (int32_t i = mVideoTrack->Decoders().Length() - 1; i >= 0; --i) {
|
|
|
|
nsRefPtr<MediaDecoderReader> newReader = mVideoTrack->Decoders()[i]->GetReader();
|
|
|
|
|
|
|
|
nsRefPtr<dom::TimeRanges> ranges = new dom::TimeRanges();
|
|
|
|
mVideoTrack->Decoders()[i]->GetBuffered(ranges);
|
2015-01-28 18:36:02 -08:00
|
|
|
result += nsPrintfCString("\t\tReader %d: %p ranges=%s active=%s size=%lld\n",
|
2015-01-28 18:35:58 -08:00
|
|
|
i, newReader.get(), DumpTimeRanges(ranges).get(),
|
2015-02-09 04:28:59 -08:00
|
|
|
newReader.get() == GetVideoReader() ? "true" : "false",
|
2015-01-28 18:36:02 -08:00
|
|
|
mVideoTrack->Decoders()[i]->GetResource()->GetSize());
|
2015-01-28 18:35:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
aString += NS_ConvertUTF8toUTF16(result);
|
|
|
|
}
|
|
|
|
|
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());
|
2015-02-23 21:38:41 -08:00
|
|
|
return aReader && (aReader == GetVideoReader() || aReader == GetAudioReader());
|
2015-02-09 04:28:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaDecoderReader*
|
|
|
|
MediaSourceReader::GetAudioReader() const
|
|
|
|
{
|
|
|
|
return mAudioSourceDecoder ? mAudioSourceDecoder->GetReader() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaDecoderReader*
|
|
|
|
MediaSourceReader::GetVideoReader() const
|
|
|
|
{
|
|
|
|
return mVideoSourceDecoder ? mVideoSourceDecoder->GetReader() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
MediaSourceReader::GetReaderAudioTime(int64_t aTime) const
|
|
|
|
{
|
|
|
|
return aTime - mAudioSourceDecoder->GetTimestampOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t
|
|
|
|
MediaSourceReader::GetReaderVideoTime(int64_t aTime) const
|
|
|
|
{
|
|
|
|
return aTime - mVideoSourceDecoder->GetTimestampOffset();
|
2015-01-15 19:14:56 -08:00
|
|
|
}
|
|
|
|
|
2015-02-11 23:52:13 -08:00
|
|
|
#undef MSE_DEBUG
|
|
|
|
#undef MSE_DEBUGV
|
2014-08-13 05:22:00 -07:00
|
|
|
} // namespace mozilla
|