2014-02-03 17:49:21 -08: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: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2013-09-05 10:29:38 -07:00
|
|
|
#include "MediaDecoderReader.h"
|
2012-11-19 07:11:21 -08:00
|
|
|
#include "AbstractMediaDecoder.h"
|
2014-11-11 20:50:21 -08:00
|
|
|
#include "MediaResource.h"
|
2010-04-27 01:53:44 -07:00
|
|
|
#include "VideoUtils.h"
|
2012-08-20 21:06:46 -07:00
|
|
|
#include "ImageContainer.h"
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2012-01-11 00:23:07 -08:00
|
|
|
#include "mozilla/mozalloc.h"
|
2013-07-30 07:25:31 -07:00
|
|
|
#include <stdint.h>
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2012-01-11 00:23:07 -08:00
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
namespace mozilla {
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
// Un-comment to enable logging of seek bisections.
|
|
|
|
//#define SEEK_LOGGING
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-11-14 11:46:40 -08:00
|
|
|
extern PRLogModuleInfo* gMediaDecoderLog;
|
2014-10-28 13:30:36 -07:00
|
|
|
#define DECODER_LOG(x, ...) \
|
|
|
|
PR_LOG(gMediaDecoderLog, PR_LOG_DEBUG, ("Decoder=%p " x, mDecoder, ##__VA_ARGS__))
|
2010-04-01 20:03:07 -07:00
|
|
|
#else
|
2014-10-28 13:30:36 -07:00
|
|
|
#define DECODER_LOG(x, ...)
|
2010-04-01 20:03:07 -07:00
|
|
|
#endif
|
|
|
|
|
2014-12-08 14:45:37 -08:00
|
|
|
PRLogModuleInfo* gMediaPromiseLog;
|
|
|
|
|
|
|
|
void
|
|
|
|
EnsureMediaPromiseLog()
|
|
|
|
{
|
|
|
|
if (!gMediaPromiseLog) {
|
|
|
|
gMediaPromiseLog = PR_NewLogModule("MediaPromise");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-19 14:33:12 -07:00
|
|
|
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);
|
2012-08-20 21:06:46 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-19 14:33:12 -07:00
|
|
|
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;
|
2013-07-07 13:33:56 -07:00
|
|
|
}
|
2014-03-19 14:33:12 -07:00
|
|
|
|
|
|
|
size_t mSize;
|
|
|
|
};
|
2012-08-20 21:06:46 -07:00
|
|
|
|
2012-11-19 07:11:21 -08:00
|
|
|
MediaDecoderReader::MediaDecoderReader(AbstractMediaDecoder* aDecoder)
|
2014-06-17 22:07:02 -07:00
|
|
|
: mAudioCompactor(mAudioQueue)
|
|
|
|
, mDecoder(aDecoder)
|
|
|
|
, mIgnoreAudioOutputFormat(false)
|
2014-11-11 20:50:20 -08:00
|
|
|
, mStartTime(-1)
|
2014-12-10 14:03:56 -08:00
|
|
|
, mHitAudioDecodeError(false)
|
2014-12-01 21:51:03 -08:00
|
|
|
, mTaskQueueIsBorrowed(false)
|
2014-06-17 22:07:02 -07:00
|
|
|
, mAudioDiscontinuity(false)
|
|
|
|
, mVideoDiscontinuity(false)
|
2014-12-01 21:51:02 -08:00
|
|
|
, mShutdown(false)
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_CTOR(MediaDecoderReader);
|
2014-12-08 14:45:37 -08:00
|
|
|
EnsureMediaPromiseLog();
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
MediaDecoderReader::~MediaDecoderReader()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
2014-12-01 21:51:02 -08:00
|
|
|
MOZ_ASSERT(mShutdown);
|
2010-04-01 20:03:07 -07:00
|
|
|
ResetDecode();
|
2012-11-14 11:46:40 -08:00
|
|
|
MOZ_COUNT_DTOR(MediaDecoderReader);
|
2010-04-01 20:03:07 -07:00
|
|
|
}
|
|
|
|
|
2014-03-19 14:33:12 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-14 11:46:40 -08:00
|
|
|
nsresult MediaDecoderReader::ResetDecode()
|
2010-04-01 20:03:07 -07:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
2012-12-06 15:27:08 -08:00
|
|
|
VideoQueue().Reset();
|
|
|
|
AudioQueue().Reset();
|
2010-04-01 20:03:07 -07:00
|
|
|
|
2014-06-17 22:07:02 -07:00
|
|
|
mAudioDiscontinuity = true;
|
|
|
|
mVideoDiscontinuity = true;
|
|
|
|
|
2010-04-01 20:03:07 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-08-21 20:11:58 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-11-11 20:50:20 -08:00
|
|
|
void
|
|
|
|
MediaDecoderReader::SetStartTime(int64_t aStartTime)
|
|
|
|
{
|
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
mStartTime = aStartTime;
|
|
|
|
}
|
|
|
|
|
2014-06-17 22:07:02 -07:00
|
|
|
nsresult
|
2014-11-11 20:50:21 -08:00
|
|
|
MediaDecoderReader::GetBuffered(mozilla::dom::TimeRanges* aBuffered)
|
2014-06-10 00:31:09 -07:00
|
|
|
{
|
2014-11-11 20:50:21 -08:00
|
|
|
AutoPinned<MediaResource> stream(mDecoder->GetResource());
|
2014-06-17 22:07:02 -07:00
|
|
|
int64_t durationUs = 0;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
durationUs = mDecoder->GetMediaDuration();
|
|
|
|
}
|
|
|
|
GetEstimatedBufferedTimeRanges(stream, durationUs, aBuffered);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-08-12 19:28:15 -07:00
|
|
|
|
2014-10-28 13:30:36 -07:00
|
|
|
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);
|
|
|
|
MOZ_ASSERT(startTime >= 0);
|
|
|
|
return startTime;
|
|
|
|
}
|
|
|
|
|
2014-06-17 22:07:02 -07:00
|
|
|
class RequestVideoWithSkipTask : public nsRunnable {
|
|
|
|
public:
|
|
|
|
RequestVideoWithSkipTask(MediaDecoderReader* aReader,
|
|
|
|
int64_t aTimeThreshold)
|
|
|
|
: mReader(aReader)
|
|
|
|
, mTimeThreshold(aTimeThreshold)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
NS_METHOD Run() {
|
|
|
|
bool skip = true;
|
|
|
|
mReader->RequestVideoData(skip, mTimeThreshold);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<MediaDecoderReader> mReader;
|
|
|
|
int64_t mTimeThreshold;
|
|
|
|
};
|
|
|
|
|
2014-12-10 14:03:56 -08:00
|
|
|
nsRefPtr<MediaDecoderReader::VideoDataPromise>
|
2014-06-17 22:07:02 -07:00
|
|
|
MediaDecoderReader::RequestVideoData(bool aSkipToNextKeyframe,
|
|
|
|
int64_t aTimeThreshold)
|
|
|
|
{
|
2014-12-10 14:03:56 -08:00
|
|
|
nsRefPtr<VideoDataPromise> p = mBaseVideoPromise.Ensure(__func__);
|
2014-06-17 22:07:02 -07:00
|
|
|
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 RequestVideoWithSkipTask(this, aTimeThreshold));
|
|
|
|
mTaskQueue->Dispatch(task);
|
2014-12-10 14:03:56 -08:00
|
|
|
return p;
|
2014-06-10 00:31:09 -07:00
|
|
|
}
|
2014-06-17 22:07:02 -07:00
|
|
|
}
|
|
|
|
if (VideoQueue().GetSize() > 0) {
|
2014-11-19 13:01:10 -08:00
|
|
|
nsRefPtr<VideoData> v = VideoQueue().PopFront();
|
2014-06-17 22:07:02 -07:00
|
|
|
if (v && mVideoDiscontinuity) {
|
|
|
|
v->mDiscontinuity = true;
|
|
|
|
mVideoDiscontinuity = false;
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
2014-12-10 14:03:56 -08:00
|
|
|
mBaseVideoPromise.Resolve(v, __func__);
|
2014-06-17 22:07:02 -07:00
|
|
|
} else if (VideoQueue().IsFinished()) {
|
2014-12-10 14:03:56 -08:00
|
|
|
mBaseVideoPromise.Reject(END_OF_STREAM, __func__);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "Dropping this promise on the floor");
|
2010-08-12 19:28:15 -07:00
|
|
|
}
|
2014-12-10 14:03:56 -08:00
|
|
|
|
|
|
|
return p;
|
2014-06-17 22:07:02 -07:00
|
|
|
}
|
2013-08-12 21:49:25 -07:00
|
|
|
|
2014-12-10 14:03:56 -08:00
|
|
|
nsRefPtr<MediaDecoderReader::AudioDataPromise>
|
2014-06-17 22:07:02 -07:00
|
|
|
MediaDecoderReader::RequestAudioData()
|
|
|
|
{
|
2014-12-10 14:03:56 -08:00
|
|
|
nsRefPtr<AudioDataPromise> p = mBaseAudioPromise.Ensure(__func__);
|
2014-06-17 22:07:02 -07:00
|
|
|
while (AudioQueue().GetSize() == 0 &&
|
|
|
|
!AudioQueue().IsFinished()) {
|
|
|
|
if (!DecodeAudioData()) {
|
|
|
|
AudioQueue().Finish();
|
2014-07-30 11:58:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// AudioQueue size is still zero, post a task to try again. Don't spin
|
|
|
|
// waiting in this while loop since it somehow prevents audio EOS from
|
|
|
|
// coming in gstreamer 1.x when there is still video buffer waiting to be
|
|
|
|
// consumed. (|mVideoSinkBufferCount| > 0)
|
|
|
|
if (AudioQueue().GetSize() == 0 && mTaskQueue) {
|
|
|
|
RefPtr<nsIRunnable> task(NS_NewRunnableMethod(
|
|
|
|
this, &MediaDecoderReader::RequestAudioData));
|
|
|
|
mTaskQueue->Dispatch(task.forget());
|
2014-12-10 14:03:56 -08:00
|
|
|
return p;
|
2014-06-17 22:07:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (AudioQueue().GetSize() > 0) {
|
2014-11-19 13:01:10 -08:00
|
|
|
nsRefPtr<AudioData> a = AudioQueue().PopFront();
|
2014-06-17 22:07:02 -07:00
|
|
|
if (mAudioDiscontinuity) {
|
|
|
|
a->mDiscontinuity = true;
|
|
|
|
mAudioDiscontinuity = false;
|
2014-06-13 13:20:37 -07:00
|
|
|
}
|
2014-12-10 14:03:56 -08:00
|
|
|
mBaseAudioPromise.Resolve(a, __func__);
|
2014-06-17 22:07:02 -07:00
|
|
|
} else if (AudioQueue().IsFinished()) {
|
2014-12-10 14:03:56 -08:00
|
|
|
mBaseAudioPromise.Reject(mHitAudioDecodeError ? DECODE_ERROR : END_OF_STREAM, __func__);
|
|
|
|
mHitAudioDecodeError = false;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false, "Dropping this promise on the floor");
|
2014-06-10 00:31:09 -07:00
|
|
|
}
|
2014-12-10 14:03:56 -08:00
|
|
|
|
|
|
|
return p;
|
2014-06-17 22:07:02 -07:00
|
|
|
}
|
2014-06-10 00:31:09 -07:00
|
|
|
|
2014-06-17 22:07:02 -07:00
|
|
|
void
|
|
|
|
MediaDecoderReader::SetCallback(RequestSampleCallback* aCallback)
|
|
|
|
{
|
|
|
|
mSampleDecodedCallback = aCallback;
|
|
|
|
}
|
2014-06-10 00:31:09 -07:00
|
|
|
|
2014-12-01 21:51:03 -08:00
|
|
|
MediaTaskQueue*
|
|
|
|
MediaDecoderReader::EnsureTaskQueue()
|
2014-06-17 22:07:02 -07:00
|
|
|
{
|
2014-12-01 21:51:03 -08:00
|
|
|
if (!mTaskQueue) {
|
|
|
|
MOZ_ASSERT(!mTaskQueueIsBorrowed);
|
|
|
|
RefPtr<SharedThreadPool> decodePool(GetMediaDecodeThreadPool());
|
|
|
|
NS_ENSURE_TRUE(decodePool, nullptr);
|
|
|
|
|
|
|
|
mTaskQueue = new MediaTaskQueue(decodePool.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
return mTaskQueue;
|
2014-06-17 22:07:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderReader::BreakCycles()
|
|
|
|
{
|
|
|
|
if (mSampleDecodedCallback) {
|
|
|
|
mSampleDecodedCallback->BreakCycles();
|
|
|
|
mSampleDecodedCallback = nullptr;
|
|
|
|
}
|
|
|
|
mTaskQueue = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:43:21 -08:00
|
|
|
nsRefPtr<ShutdownPromise>
|
2014-06-17 22:07:02 -07:00
|
|
|
MediaDecoderReader::Shutdown()
|
|
|
|
{
|
2014-12-01 21:51:03 -08:00
|
|
|
MOZ_ASSERT(OnDecodeThread());
|
2014-12-01 21:51:02 -08:00
|
|
|
mShutdown = true;
|
2014-12-10 14:03:56 -08:00
|
|
|
|
|
|
|
mBaseAudioPromise.RejectIfExists(END_OF_STREAM, __func__);
|
|
|
|
mBaseVideoPromise.RejectIfExists(END_OF_STREAM, __func__);
|
|
|
|
|
2014-06-17 22:07:02 -07:00
|
|
|
ReleaseMediaResources();
|
2014-12-09 11:43:21 -08:00
|
|
|
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().
|
2014-12-01 21:51:03 -08:00
|
|
|
if (mTaskQueue && !mTaskQueueIsBorrowed) {
|
2014-12-09 11:43:21 -08:00
|
|
|
// 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 = new ShutdownPromise(__func__);
|
|
|
|
p->Resolve(true, __func__);
|
2014-12-01 21:51:03 -08:00
|
|
|
}
|
2014-12-09 11:43:21 -08:00
|
|
|
|
|
|
|
return p;
|
2014-06-17 22:07:02 -07:00
|
|
|
}
|
|
|
|
|
2012-11-14 11:45:33 -08:00
|
|
|
} // namespace mozilla
|