mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:18:48 +00:00
2f3df18559
- Bug 1172264 - Mirror duration from the MDSM to the MediaDecoderReader and remove MDSM::GetDuration. r=jww (369a3d1b4) - Bug 1172264 - Route mExplicitDuration directly from the mediasource code to MediaDecoder, and stop passing an argument to DurationChanged. r=pending=jww (b429dfe41) - Bug 1172264 - Watch mStateMachineDuration, and stop manually firing DurationChanged. r=jww (54091368c) - bug 1126065 - Make JS callers of ios.newChannel call ios.newChannel2 in dom/browser-element. r=sicking (8c38534ed) - Bug 1144015 - (Browser API) mozbrowseropentab support. r=kchen (8b1eecb4e) - Bug 1143650: Update webref failure links r=karlt (b3c94f173) - Bug 1172264 - Mark WPT as succeeding. r=jya (664350c56) - Bug 1141029 - Disabling mochitests on Mulet with parity to B2G Desktop for taskcluster. r=ahal (73bb186cb) - Bug 1144080 - Disable mochitests on Mulet for TaskCluster. r=ahal (0b71b6a05) - Bug 1145407: Add mochitests that cause multiple tracks of the same type to be placed in the same remote stream. r=mt (263770e16) - Bug 1148649: Reenable video multistream mochitests on debug e10s. r=drno (f7674fe4a) - part of Bug 1094764 - Implement AudioContext.suspend and friends. @ (baa450713) - Bug 1166803 - Add an `msg` tag to mochitest.ini in dom/media/*. r=jesup (5284df8b2) - Bug 1166659 - Add mochitest tags for webaudio and webrtc. r=jesup, r=padenot (f5424f26f) - Bug 1087551: updated tests around addIceCandidate(). r=jib (f28cde40b) - Bug 1169338 - Part 1: Re-enable a subset of the webrtc mochitests on B2G emulator and Mulet. r=mt (9c0f8c2da) - Bug 1143827 - remove default stun server. r=abr,bsmedberg (f1e306a95) - Bug 1169338 - Part 2: Extend ICE timeouts since mochitests are frequently run on systems that are performance constrained. r=mt (da6147576) - Bug 1155493 - Part 1: Add CaretStateChangedEvent and corresponding utility function. r=roc, sr=smaug (9d710ad21) - Bug 995394: Removed parts of BrowserElementPanning.js that are only used when APZ is disabled and added that to a separte file BrowserElementPanningAPZDisabled.js r=botond, a=RyanVM (8b76bca9f) - Bug 1138252 - Load BrowserElementPanning.js only if touch events are enabled. r=botond (30f5f3197) - Bug 1155493 - Part 2: Event hook for mozbrowser element. r=kanru (6f6db8248) - Bug 1162844 - Add meta name="viewmode" to have configurable VR experiences. r=fabrice (07d6d0736) - Bug 1163961 - Browser API: Page search. r=kchen, r=ehsan (df0c37dfa) - Bug 1179718 - Convert BrowserElement.webidl to use CheckAllPermissions. r=bz (4a92b2c7d) - Bug 1147819 - Any media element should be stopped by the AudioChannelService when the window is destroyed, r=ehsan (e949db77f) - Bug 1153915 - Null check the window in AudioChannelService::WindowDestroyedEnumerator(); r=baku (b38261d9d) - Bug 1089526 - Change speaker state. r=baku (8dbf54b04) - Bug 1157121 - Add speaker status checking. r=baku (ec5416680) - Bug 1037389 - add support for deviceId in gUM constraints (merged 11 patches). r=smaug, r=jesup (bc6f9640d) - Bug 1180748 - Unbreak building with --disable-webrtc. r=jesup (b5d53b666)
381 lines
10 KiB
C++
381 lines
10 KiB
C++
/* -*- 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 "MediaDecoderReader.h"
|
|
#include "AbstractMediaDecoder.h"
|
|
#include "MediaResource.h"
|
|
#include "VideoUtils.h"
|
|
#include "ImageContainer.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
#include "mozilla/mozalloc.h"
|
|
#include <stdint.h>
|
|
#include <algorithm>
|
|
|
|
using namespace mozilla::media;
|
|
|
|
namespace mozilla {
|
|
|
|
// Un-comment to enable logging of seek bisections.
|
|
//#define SEEK_LOGGING
|
|
|
|
extern PRLogModuleInfo* gMediaDecoderLog;
|
|
#define DECODER_LOG(x, ...) \
|
|
MOZ_LOG(gMediaDecoderLog, LogLevel::Debug, ("Decoder=%p " x, mDecoder, ##__VA_ARGS__))
|
|
|
|
// Same workaround as MediaDecoderStateMachine.cpp.
|
|
#define DECODER_WARN_HELPER(a, b) NS_WARNING b
|
|
#define DECODER_WARN(x, ...) \
|
|
DECODER_WARN_HELPER(0, (nsPrintfCString("Decoder=%p " x, mDecoder, ##__VA_ARGS__).get()))
|
|
|
|
class VideoQueueMemoryFunctor : public nsDequeFunctor {
|
|
public:
|
|
VideoQueueMemoryFunctor() : mSize(0) {}
|
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf);
|
|
|
|
virtual void* operator()(void* aObject) {
|
|
const VideoData* v = static_cast<const VideoData*>(aObject);
|
|
mSize += v->SizeOfIncludingThis(MallocSizeOf);
|
|
return nullptr;
|
|
}
|
|
|
|
size_t mSize;
|
|
};
|
|
|
|
|
|
class AudioQueueMemoryFunctor : public nsDequeFunctor {
|
|
public:
|
|
AudioQueueMemoryFunctor() : mSize(0) {}
|
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf);
|
|
|
|
virtual void* operator()(void* aObject) {
|
|
const AudioData* audioData = static_cast<const AudioData*>(aObject);
|
|
mSize += audioData->SizeOfIncludingThis(MallocSizeOf);
|
|
return nullptr;
|
|
}
|
|
|
|
size_t mSize;
|
|
};
|
|
|
|
MediaDecoderReader::MediaDecoderReader(AbstractMediaDecoder* aDecoder,
|
|
MediaTaskQueue* aBorrowedTaskQueue)
|
|
: mAudioCompactor(mAudioQueue)
|
|
, mDecoder(aDecoder)
|
|
, mTaskQueue(aBorrowedTaskQueue ? aBorrowedTaskQueue
|
|
: new MediaTaskQueue(GetMediaThreadPool(MediaThreadType::PLAYBACK),
|
|
/* aSupportsTailDispatch = */ true))
|
|
, mDuration(mTaskQueue, NullableTimeUnit(), "MediaDecoderReader::mDuration (Mirror)")
|
|
, mIgnoreAudioOutputFormat(false)
|
|
, mStartTime(-1)
|
|
, mHitAudioDecodeError(false)
|
|
, mShutdown(false)
|
|
, mTaskQueueIsBorrowed(!!aBorrowedTaskQueue)
|
|
, mAudioDiscontinuity(false)
|
|
, mVideoDiscontinuity(false)
|
|
{
|
|
MOZ_COUNT_CTOR(MediaDecoderReader);
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
// Dispatch initialization that needs to happen on that task queue.
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableMethod(this, &MediaDecoderReader::InitializationTask);
|
|
mTaskQueue->Dispatch(r.forget());
|
|
}
|
|
|
|
void
|
|
MediaDecoderReader::InitializationTask()
|
|
{
|
|
if (mDecoder->CanonicalDurationOrNull()) {
|
|
mDuration.Connect(mDecoder->CanonicalDurationOrNull());
|
|
}
|
|
}
|
|
|
|
MediaDecoderReader::~MediaDecoderReader()
|
|
{
|
|
MOZ_ASSERT(mShutdown);
|
|
MOZ_ASSERT(!mDecoder);
|
|
ResetDecode();
|
|
MOZ_COUNT_DTOR(MediaDecoderReader);
|
|
}
|
|
|
|
size_t MediaDecoderReader::SizeOfVideoQueueInBytes() const
|
|
{
|
|
VideoQueueMemoryFunctor functor;
|
|
mVideoQueue.LockedForEach(functor);
|
|
return functor.mSize;
|
|
}
|
|
|
|
size_t MediaDecoderReader::SizeOfAudioQueueInBytes() const
|
|
{
|
|
AudioQueueMemoryFunctor functor;
|
|
mAudioQueue.LockedForEach(functor);
|
|
return functor.mSize;
|
|
}
|
|
|
|
size_t MediaDecoderReader::SizeOfVideoQueueInFrames()
|
|
{
|
|
return mVideoQueue.GetSize();
|
|
}
|
|
|
|
size_t MediaDecoderReader::SizeOfAudioQueueInFrames()
|
|
{
|
|
return mAudioQueue.GetSize();
|
|
}
|
|
|
|
nsresult MediaDecoderReader::ResetDecode()
|
|
{
|
|
VideoQueue().Reset();
|
|
AudioQueue().Reset();
|
|
|
|
mAudioDiscontinuity = true;
|
|
mVideoDiscontinuity = true;
|
|
|
|
mBaseAudioPromise.RejectIfExists(CANCELED, __func__);
|
|
mBaseVideoPromise.RejectIfExists(CANCELED, __func__);
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
VideoData* MediaDecoderReader::DecodeToFirstVideoData()
|
|
{
|
|
bool eof = false;
|
|
while (!eof && VideoQueue().GetSize() == 0) {
|
|
{
|
|
ReentrantMonitorAutoEnter decoderMon(mDecoder->GetReentrantMonitor());
|
|
if (mDecoder->IsShutdown()) {
|
|
return nullptr;
|
|
}
|
|
}
|
|
bool keyframeSkip = false;
|
|
eof = !DecodeVideoFrame(keyframeSkip, 0);
|
|
}
|
|
if (eof) {
|
|
VideoQueue().Finish();
|
|
}
|
|
VideoData* d = nullptr;
|
|
return (d = VideoQueue().PeekFront()) ? d : nullptr;
|
|
}
|
|
|
|
void
|
|
MediaDecoderReader::SetStartTime(int64_t aStartTime)
|
|
{
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
MOZ_ASSERT(mStartTime == -1);
|
|
mStartTime = aStartTime;
|
|
}
|
|
|
|
media::TimeIntervals
|
|
MediaDecoderReader::GetBuffered()
|
|
{
|
|
NS_ENSURE_TRUE(mStartTime >= 0, media::TimeIntervals());
|
|
AutoPinned<MediaResource> stream(mDecoder->GetResource());
|
|
|
|
if (!mDuration.ReadOnWrongThread().isSome()) {
|
|
return TimeIntervals();
|
|
}
|
|
|
|
return GetEstimatedBufferedTimeRanges(stream, mDuration.ReadOnWrongThread().ref().ToMicroseconds());
|
|
}
|
|
|
|
nsRefPtr<MediaDecoderReader::MetadataPromise>
|
|
MediaDecoderReader::AsyncReadMetadata()
|
|
{
|
|
typedef ReadMetadataFailureReason Reason;
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
mDecoder->GetReentrantMonitor().AssertNotCurrentThreadIn();
|
|
DECODER_LOG("MediaDecoderReader::AsyncReadMetadata");
|
|
|
|
if (IsWaitingMediaResources()) {
|
|
return MetadataPromise::CreateAndReject(Reason::WAITING_FOR_RESOURCES, __func__);
|
|
}
|
|
|
|
// Attempt to read the metadata.
|
|
nsRefPtr<MetadataHolder> metadata = new MetadataHolder();
|
|
nsresult rv = ReadMetadata(&metadata->mInfo, getter_Transfers(metadata->mTags));
|
|
|
|
// Reading metadata can cause us to discover that we need resources (a hardware
|
|
// resource initialized but not yet ready for use).
|
|
if (IsWaitingMediaResources()) {
|
|
return MetadataPromise::CreateAndReject(Reason::WAITING_FOR_RESOURCES, __func__);
|
|
}
|
|
|
|
// We're not waiting for anything. If we didn't get the metadata, that's an error.
|
|
if (NS_FAILED(rv) || !metadata->mInfo.HasValidMedia()) {
|
|
DECODER_WARN("ReadMetadata failed, rv=%x HasValidMedia=%d", rv, metadata->mInfo.HasValidMedia());
|
|
return MetadataPromise::CreateAndReject(Reason::METADATA_ERROR, __func__);
|
|
}
|
|
|
|
// Success!
|
|
return MetadataPromise::CreateAndResolve(metadata, __func__);
|
|
}
|
|
|
|
class ReRequestVideoWithSkipTask : public nsRunnable
|
|
{
|
|
public:
|
|
ReRequestVideoWithSkipTask(MediaDecoderReader* aReader,
|
|
int64_t aTimeThreshold)
|
|
: mReader(aReader)
|
|
, mTimeThreshold(aTimeThreshold)
|
|
{
|
|
}
|
|
|
|
NS_METHOD Run()
|
|
{
|
|
MOZ_ASSERT(mReader->GetTaskQueue()->IsCurrentThreadIn());
|
|
|
|
// Make sure ResetDecode hasn't been called in the mean time.
|
|
if (!mReader->mBaseVideoPromise.IsEmpty()) {
|
|
mReader->RequestVideoData(/* aSkip = */ true, mTimeThreshold);
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
private:
|
|
nsRefPtr<MediaDecoderReader> mReader;
|
|
const int64_t mTimeThreshold;
|
|
};
|
|
|
|
class ReRequestAudioTask : public nsRunnable
|
|
{
|
|
public:
|
|
explicit ReRequestAudioTask(MediaDecoderReader* aReader)
|
|
: mReader(aReader)
|
|
{
|
|
}
|
|
|
|
NS_METHOD Run()
|
|
{
|
|
MOZ_ASSERT(mReader->GetTaskQueue()->IsCurrentThreadIn());
|
|
|
|
// Make sure ResetDecode hasn't been called in the mean time.
|
|
if (!mReader->mBaseAudioPromise.IsEmpty()) {
|
|
mReader->RequestAudioData();
|
|
}
|
|
|
|
return NS_OK;
|
|
}
|
|
|
|
private:
|
|
nsRefPtr<MediaDecoderReader> mReader;
|
|
};
|
|
|
|
nsRefPtr<MediaDecoderReader::VideoDataPromise>
|
|
MediaDecoderReader::RequestVideoData(bool aSkipToNextKeyframe,
|
|
int64_t aTimeThreshold)
|
|
{
|
|
nsRefPtr<VideoDataPromise> p = mBaseVideoPromise.Ensure(__func__);
|
|
bool skip = aSkipToNextKeyframe;
|
|
while (VideoQueue().GetSize() == 0 &&
|
|
!VideoQueue().IsFinished()) {
|
|
if (!DecodeVideoFrame(skip, aTimeThreshold)) {
|
|
VideoQueue().Finish();
|
|
} else if (skip) {
|
|
// We still need to decode more data in order to skip to the next
|
|
// keyframe. Post another task to the decode task queue to decode
|
|
// again. We don't just decode straight in a loop here, as that
|
|
// would hog the decode task queue.
|
|
RefPtr<nsIRunnable> task(new ReRequestVideoWithSkipTask(this, aTimeThreshold));
|
|
mTaskQueue->Dispatch(task.forget());
|
|
return p;
|
|
}
|
|
}
|
|
if (VideoQueue().GetSize() > 0) {
|
|
nsRefPtr<VideoData> v = VideoQueue().PopFront();
|
|
if (v && mVideoDiscontinuity) {
|
|
v->mDiscontinuity = true;
|
|
mVideoDiscontinuity = false;
|
|
}
|
|
mBaseVideoPromise.Resolve(v, __func__);
|
|
} else if (VideoQueue().IsFinished()) {
|
|
mBaseVideoPromise.Reject(END_OF_STREAM, __func__);
|
|
} else {
|
|
MOZ_ASSERT(false, "Dropping this promise on the floor");
|
|
}
|
|
|
|
return p;
|
|
}
|
|
|
|
nsRefPtr<MediaDecoderReader::AudioDataPromise>
|
|
MediaDecoderReader::RequestAudioData()
|
|
{
|
|
nsRefPtr<AudioDataPromise> p = mBaseAudioPromise.Ensure(__func__);
|
|
while (AudioQueue().GetSize() == 0 &&
|
|
!AudioQueue().IsFinished()) {
|
|
if (!DecodeAudioData()) {
|
|
AudioQueue().Finish();
|
|
break;
|
|
}
|
|
// AudioQueue size is still zero, post a task to try again.
|
|
// (|mVideoSinkBufferCount| > 0)
|
|
if (AudioQueue().GetSize() == 0 && mTaskQueue) {
|
|
RefPtr<nsIRunnable> task(new ReRequestAudioTask(this));
|
|
mTaskQueue->Dispatch(task.forget());
|
|
return p;
|
|
}
|
|
}
|
|
if (AudioQueue().GetSize() > 0) {
|
|
nsRefPtr<AudioData> a = AudioQueue().PopFront();
|
|
if (mAudioDiscontinuity) {
|
|
a->mDiscontinuity = true;
|
|
mAudioDiscontinuity = false;
|
|
}
|
|
mBaseAudioPromise.Resolve(a, __func__);
|
|
} else if (AudioQueue().IsFinished()) {
|
|
mBaseAudioPromise.Reject(mHitAudioDecodeError ? DECODE_ERROR : END_OF_STREAM, __func__);
|
|
mHitAudioDecodeError = false;
|
|
} else {
|
|
MOZ_ASSERT(false, "Dropping this promise on the floor");
|
|
}
|
|
|
|
return p;
|
|
}
|
|
|
|
void
|
|
MediaDecoderReader::BreakCycles()
|
|
{
|
|
mTaskQueue = nullptr;
|
|
}
|
|
|
|
nsRefPtr<ShutdownPromise>
|
|
MediaDecoderReader::Shutdown()
|
|
{
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
mShutdown = true;
|
|
|
|
mBaseAudioPromise.RejectIfExists(END_OF_STREAM, __func__);
|
|
mBaseVideoPromise.RejectIfExists(END_OF_STREAM, __func__);
|
|
|
|
ReleaseMediaResources();
|
|
mDuration.DisconnectIfConnected();
|
|
|
|
nsRefPtr<ShutdownPromise> p;
|
|
|
|
// Spin down the task queue if necessary. We wait until BreakCycles to null
|
|
// out mTaskQueue, since otherwise any remaining tasks could crash when they
|
|
// invoke GetTaskQueue()->IsCurrentThreadIn().
|
|
if (mTaskQueue && !mTaskQueueIsBorrowed) {
|
|
// If we own our task queue, shutdown ends when the task queue is done.
|
|
p = mTaskQueue->BeginShutdown();
|
|
} else {
|
|
// If we don't own our task queue, we resolve immediately (though
|
|
// asynchronously).
|
|
p = ShutdownPromise::CreateAndResolve(true, __func__);
|
|
}
|
|
|
|
mDecoder = nullptr;
|
|
|
|
return p;
|
|
}
|
|
|
|
} // namespace mozilla
|
|
|
|
#undef DECODER_LOG
|
|
#undef DECODER_WARN
|
|
#undef DECODER_WARN_HELPER
|