2014-02-04 05:49:21 +04: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 15:12:37 +04: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-02 07:03:07 +04:00
|
|
|
|
2013-09-05 21:29:38 +04:00
|
|
|
#include "MediaDecoderReader.h"
|
2012-11-19 19:11:21 +04:00
|
|
|
#include "AbstractMediaDecoder.h"
|
2010-04-27 12:53:44 +04:00
|
|
|
#include "VideoUtils.h"
|
2012-08-21 08:06:46 +04:00
|
|
|
#include "ImageContainer.h"
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2012-01-11 12:23:07 +04:00
|
|
|
#include "mozilla/mozalloc.h"
|
2013-07-30 18:25:31 +04:00
|
|
|
#include <stdint.h>
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2012-01-11 12:23:07 +04:00
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2010-04-02 07:03:07 +04:00
|
|
|
// Un-comment to enable logging of seek bisections.
|
|
|
|
//#define SEEK_LOGGING
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
2012-11-14 23:46:40 +04:00
|
|
|
extern PRLogModuleInfo* gMediaDecoderLog;
|
2014-10-28 23:30:36 +03:00
|
|
|
#define DECODER_LOG(x, ...) \
|
|
|
|
PR_LOG(gMediaDecoderLog, PR_LOG_DEBUG, ("Decoder=%p " x, mDecoder, ##__VA_ARGS__))
|
2010-04-02 07:03:07 +04:00
|
|
|
#else
|
2014-10-28 23:30:36 +03:00
|
|
|
#define DECODER_LOG(x, ...)
|
2010-04-02 07:03:07 +04:00
|
|
|
#endif
|
|
|
|
|
2014-03-20 01:33:12 +04: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-21 08:06:46 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-20 01:33:12 +04: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-08 00:33:56 +04:00
|
|
|
}
|
2014-03-20 01:33:12 +04:00
|
|
|
|
|
|
|
size_t mSize;
|
|
|
|
};
|
2012-08-21 08:06:46 +04:00
|
|
|
|
2012-11-19 19:11:21 +04:00
|
|
|
MediaDecoderReader::MediaDecoderReader(AbstractMediaDecoder* aDecoder)
|
2014-06-18 09:07:02 +04:00
|
|
|
: mAudioCompactor(mAudioQueue)
|
|
|
|
, mDecoder(aDecoder)
|
|
|
|
, mIgnoreAudioOutputFormat(false)
|
2014-11-12 07:50:20 +03:00
|
|
|
, mStartTime(-1)
|
2014-06-18 09:07:02 +04:00
|
|
|
, mAudioDiscontinuity(false)
|
|
|
|
, mVideoDiscontinuity(false)
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
2012-11-14 23:46:40 +04:00
|
|
|
MOZ_COUNT_CTOR(MediaDecoderReader);
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaDecoderReader::~MediaDecoderReader()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
|
|
|
ResetDecode();
|
2012-11-14 23:46:40 +04:00
|
|
|
MOZ_COUNT_DTOR(MediaDecoderReader);
|
2010-04-02 07:03:07 +04:00
|
|
|
}
|
|
|
|
|
2014-03-20 01:33:12 +04: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 23:46:40 +04:00
|
|
|
nsresult MediaDecoderReader::ResetDecode()
|
2010-04-02 07:03:07 +04:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
2012-12-07 03:27:08 +04:00
|
|
|
VideoQueue().Reset();
|
|
|
|
AudioQueue().Reset();
|
2010-04-02 07:03:07 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
mAudioDiscontinuity = true;
|
|
|
|
mVideoDiscontinuity = true;
|
|
|
|
|
2010-04-02 07:03:07 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-08-22 07:11:58 +04: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-12 07:50:20 +03:00
|
|
|
void
|
|
|
|
MediaDecoderReader::SetStartTime(int64_t aStartTime)
|
|
|
|
{
|
|
|
|
mDecoder->GetReentrantMonitor().AssertCurrentThreadIn();
|
|
|
|
mStartTime = aStartTime;
|
|
|
|
}
|
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
nsresult
|
|
|
|
MediaDecoderReader::GetBuffered(mozilla::dom::TimeRanges* aBuffered,
|
|
|
|
int64_t aStartTime)
|
2014-06-10 11:31:09 +04:00
|
|
|
{
|
2014-06-18 09:07:02 +04:00
|
|
|
MediaResource* stream = mDecoder->GetResource();
|
|
|
|
int64_t durationUs = 0;
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mDecoder->GetReentrantMonitor());
|
|
|
|
durationUs = mDecoder->GetMediaDuration();
|
|
|
|
}
|
|
|
|
GetEstimatedBufferedTimeRanges(stream, durationUs, aBuffered);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-08-13 06:28:15 +04:00
|
|
|
|
2014-10-28 23:30:36 +03: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-18 09:07:02 +04: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;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderReader::RequestVideoData(bool aSkipToNextKeyframe,
|
|
|
|
int64_t aTimeThreshold)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
return;
|
2014-06-10 11:31:09 +04:00
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
|
|
|
if (VideoQueue().GetSize() > 0) {
|
|
|
|
VideoData* v = VideoQueue().PopFront();
|
|
|
|
if (v && mVideoDiscontinuity) {
|
|
|
|
v->mDiscontinuity = true;
|
|
|
|
mVideoDiscontinuity = false;
|
2010-08-13 06:28:15 +04:00
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
GetCallback()->OnVideoDecoded(v);
|
|
|
|
} else if (VideoQueue().IsFinished()) {
|
2014-11-03 11:20:14 +03:00
|
|
|
GetCallback()->OnNotDecoded(MediaData::VIDEO_DATA, RequestSampleCallback::END_OF_STREAM);
|
2010-08-13 06:28:15 +04:00
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
2013-08-13 08:49:25 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
void
|
|
|
|
MediaDecoderReader::RequestAudioData()
|
|
|
|
{
|
|
|
|
while (AudioQueue().GetSize() == 0 &&
|
|
|
|
!AudioQueue().IsFinished()) {
|
|
|
|
if (!DecodeAudioData()) {
|
|
|
|
AudioQueue().Finish();
|
2014-07-30 22:58:00 +04: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());
|
|
|
|
return;
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (AudioQueue().GetSize() > 0) {
|
|
|
|
AudioData* a = AudioQueue().PopFront();
|
|
|
|
if (mAudioDiscontinuity) {
|
|
|
|
a->mDiscontinuity = true;
|
|
|
|
mAudioDiscontinuity = false;
|
2014-06-14 00:20:37 +04:00
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
GetCallback()->OnAudioDecoded(a);
|
|
|
|
return;
|
|
|
|
} else if (AudioQueue().IsFinished()) {
|
2014-11-03 11:20:14 +03:00
|
|
|
GetCallback()->OnNotDecoded(MediaData::AUDIO_DATA, RequestSampleCallback::END_OF_STREAM);
|
2014-06-18 09:07:02 +04:00
|
|
|
return;
|
2014-06-10 11:31:09 +04:00
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
}
|
2014-06-10 11:31:09 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
void
|
|
|
|
MediaDecoderReader::SetCallback(RequestSampleCallback* aCallback)
|
|
|
|
{
|
|
|
|
mSampleDecodedCallback = aCallback;
|
|
|
|
}
|
2014-06-10 11:31:09 +04:00
|
|
|
|
2014-06-18 09:07:02 +04:00
|
|
|
void
|
|
|
|
MediaDecoderReader::SetTaskQueue(MediaTaskQueue* aTaskQueue)
|
|
|
|
{
|
|
|
|
mTaskQueue = aTaskQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderReader::BreakCycles()
|
|
|
|
{
|
|
|
|
if (mSampleDecodedCallback) {
|
|
|
|
mSampleDecodedCallback->BreakCycles();
|
|
|
|
mSampleDecodedCallback = nullptr;
|
|
|
|
}
|
|
|
|
mTaskQueue = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaDecoderReader::Shutdown()
|
|
|
|
{
|
|
|
|
ReleaseMediaResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioDecodeRendezvous::AudioDecodeRendezvous()
|
|
|
|
: mMonitor("AudioDecodeRendezvous")
|
|
|
|
, mHaveResult(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioDecodeRendezvous::~AudioDecodeRendezvous()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioDecodeRendezvous::OnAudioDecoded(AudioData* aSample)
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
mSample = aSample;
|
|
|
|
mStatus = NS_OK;
|
|
|
|
mHaveResult = true;
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-03 11:20:14 +03:00
|
|
|
AudioDecodeRendezvous::OnNotDecoded(MediaData::Type aType, NotDecodedReason aReason)
|
2014-06-18 09:07:02 +04:00
|
|
|
{
|
2014-11-03 11:20:14 +03:00
|
|
|
MOZ_ASSERT(aType == MediaData::AUDIO_DATA);
|
2014-06-18 09:07:02 +04:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
mSample = nullptr;
|
2014-11-03 11:20:14 +03:00
|
|
|
mStatus = aReason == DECODE_ERROR ? NS_ERROR_FAILURE : NS_OK;
|
2014-06-18 09:07:02 +04:00
|
|
|
mHaveResult = true;
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioDecodeRendezvous::Reset()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
mHaveResult = false;
|
|
|
|
mStatus = NS_OK;
|
|
|
|
mSample = nullptr;
|
2010-08-13 06:28:15 +04:00
|
|
|
}
|
|
|
|
|
2013-10-21 07:31:05 +04:00
|
|
|
nsresult
|
2014-06-18 09:07:02 +04:00
|
|
|
AudioDecodeRendezvous::Await(nsAutoPtr<AudioData>& aSample)
|
2013-10-21 07:31:05 +04:00
|
|
|
{
|
2014-06-18 09:07:02 +04:00
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
while (!mHaveResult) {
|
|
|
|
mon.Wait();
|
2013-10-21 07:31:05 +04:00
|
|
|
}
|
2014-06-18 09:07:02 +04:00
|
|
|
mHaveResult = false;
|
|
|
|
aSample = mSample;
|
|
|
|
return mStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioDecodeRendezvous::Cancel()
|
|
|
|
{
|
|
|
|
MonitorAutoLock mon(mMonitor);
|
|
|
|
mStatus = NS_ERROR_ABORT;
|
|
|
|
mHaveResult = true;
|
|
|
|
mon.NotifyAll();
|
2013-10-21 07:31:05 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
} // namespace mozilla
|