mirror of
https://github.com/roytam1/palemoon27.git
synced 2026-05-26 14:18:48 +00:00
c5b776d07a
This commit will effectively revert following changes (in chronological order): - Bug 1128380: Make AmpleVideoFrames calculation dynamic. r=cpearce (e1e11d289) - Bug 1161901 - Sprinkle more assertions and fix some formatting. r=jww (1a008614e) - Bug 1152218 - Have a larger tolerance to decide if video data is low or not. r=cpearce. (72dc49d9b) - indent fix (b6fe75862) - Bug 1161901 - Relax assertion. r=jww (f20d88804) - fix indentation (c4b2356bd) - Bug 1162381. Part 2 - refactor DecodedStreamData::mInitialTime to be consistent with AudioSink when computing stream position. r=roc. (8574a32a1) - Bug 1172778 - Update readyState when audio samples are popped by AudioSink. r=cpearce. (3f64e7151) - Bug 1172392 - Align update of audio end time of decoded stream with that of AudioSink. r=roc. (01e7ba014) - Bug 1173001 - Initialize reader task queue in the constructor. r=jww (efecb4a62) - Bug 1173001 - Enable tail dispatch for the decode task queue. r=jww (11e8feaad) - Bug 1173289 - Remove WakeDecoderRunnable from MDSM. r=roc. (9782c6dd6) - Bug 1173248 - Remove the workaround introduced to MDSM by bug 951278. r=cpearce. (43ecb0e42) - Bug 996238 - Add test for asymmetric media isolation across PC. r=ekr (1a41248b4) - Bug 1153314 - Test IdP login flow, r=jib (1b143d80a) - Bug 1155898 - Use fetch instead of XHR for IdP. r=jib (ac9bc2d01) - missing part of Bug 1171311: P4. Don't compute start time for MSE. (d5e48284d) - Bug 1163223 - Remove MediaDecoderStateMachine::mStartTime. r=jww CLOSED TREE (333b0e904) - Bug 1172826 - remove unnecessary checks for MediaDecoderStateMachine::mPlaybackRate. r=kinetik. (98c32f91d) - Bug 1143575. Rename AdvanceFrame to UpdateRenderedVideoFrames. r=cpearce (987617a9d) - Bug 1172264 - Track the MDSM's duration as a TimeUnit and eliminate the separate concept of 'end time'. r=jww (49f8f2442) - Bug 1172264 - Require Manual disconnection for all mirrors. r=jww (845e57496) - Bug 1172264 - Switch MediaDecoder's mDuration represenation to a double. r=jww (dfde6482d) - Bug 1172264 - Mirror duration from the MDSM to the MediaDecoder. r=jww (a744fd08f) - Bug 1164463 - Clean up MediaManager shutdown to be reliable and avoid holding locks while Joining a thread. r=jib (636e2e5dc) - missing part of Bug 1154389 - Stop leaking DeviceSuccessCallbackRunnable objects (2f8906119) - Bug 1169665 - Have enumerateDevices return empty array on zero devices instead of fail. r=jesup (f83fcb269) - Bug 1162720 - enumerateDevices visits main thread for profileDir. r=jesup (20687dcb7) - Bug 1173255 - Cleanup MediaManager e10s code in prep for deviceId constraint. r=jesup (43496fe28) - 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 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) - CLOSED TREE. Bug 1174077 - send silence instead of nothing to the decoded stream in CORS case. r=roc. (cf576aac9) - Bug 1177466 - Clean up places calling MaybeStartPlayback() in MediaDecoderStateMachine. r=cpearce (a2ef0f47b) - Remaining of Bug 1168008 - Replace 'Consumer' with 'Request' in MediaPromise (4e77e4f42) - bug 1137076 remove declaration of undefined OmxDecoder::ProcessCachedData() r=edwin (90ef83ee9) - bug 1137076 mark some methods as private r=edwin (e9f4dd0a1) - Bug 1173001 - Fix up some task queue naming to make MediaDecoderReader consistent with MDSM. r=jww (879b186ea) - Bug 1173641 - Hoist shutdown promise resolution into a helper. r=jww (1a02bd90a) - Bug 1173641 - Null out the thread pool when resolving shutdown. r=jww (ab3f723d5) - Bug 1173641 - Remove now-unnecessary null-out in MediaDecoderReader::BreakCycles. r=jww (3330778c6) - Bug 1173656 - Disallow TrackID reuse in TrackUnionStream. r=roc (7f4da1ea2) - Bug 1175768 - Implement SilentReadAt. r=jya (ece3c2ffa) - Bug 1178437 - Assert OnTaskQueue for most of the remaining MDSM methods. r=jww (da13ec549) - Bug 1178437 - Remove ReadOnWrongThread. r=jww (f9cf8946d) - Bug 1178437 - Dispatch SetFragmentEndTime. r=jww (740ce9882) - Bug 1178437 - Make mRealTime const and allow it to be accessed on any thread. r=jww (a65c22f1f)
386 lines
11 KiB
C++
386 lines
11 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>
|
|
|
|
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)
|
|
: mAudioCompactor(mAudioQueue)
|
|
, mDecoder(aDecoder)
|
|
, mIgnoreAudioOutputFormat(false)
|
|
, mStartTime(-1)
|
|
, mHitAudioDecodeError(false)
|
|
, mShutdown(false)
|
|
, mTaskQueueIsBorrowed(false)
|
|
, mAudioDiscontinuity(false)
|
|
, mVideoDiscontinuity(false)
|
|
{
|
|
MOZ_COUNT_CTOR(MediaDecoderReader);
|
|
}
|
|
|
|
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());
|
|
int64_t durationUs = 0;
|
|
{
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
durationUs = mDecoder->GetMediaDuration();
|
|
}
|
|
return GetEstimatedBufferedTimeRanges(stream, durationUs);
|
|
}
|
|
|
|
int64_t
|
|
MediaDecoderReader::ComputeStartTime(const VideoData* aVideo, const AudioData* aAudio)
|
|
{
|
|
int64_t startTime = std::min<int64_t>(aAudio ? aAudio->mTime : INT64_MAX,
|
|
aVideo ? aVideo->mTime : INT64_MAX);
|
|
if (startTime == INT64_MAX) {
|
|
startTime = 0;
|
|
}
|
|
DECODER_LOG("ComputeStartTime first video frame start %lld", aVideo ? aVideo->mTime : -1);
|
|
DECODER_LOG("ComputeStartTime first audio frame start %lld", aAudio ? aAudio->mTime : -1);
|
|
NS_ASSERTION(startTime >= 0, "Start time is negative");
|
|
return startTime;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
MediaTaskQueue*
|
|
MediaDecoderReader::EnsureTaskQueue()
|
|
{
|
|
if (!mTaskQueue) {
|
|
MOZ_ASSERT(!mTaskQueueIsBorrowed);
|
|
RefPtr<SharedThreadPool> pool(GetMediaThreadPool(MediaThreadType::PLAYBACK));
|
|
MOZ_DIAGNOSTIC_ASSERT(pool);
|
|
mTaskQueue = new MediaTaskQueue(pool.forget());
|
|
}
|
|
|
|
return mTaskQueue;
|
|
}
|
|
|
|
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();
|
|
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
|