Skip to content

Commit

Permalink
Bug 1271508. Part 3 - extract code to the parent class and remove use…
Browse files Browse the repository at this point in the history
… of mTaskQueue from sub-classes. r=jya.
  • Loading branch information
jwwang committed May 9, 2016
1 parent 2b64cf0 commit 684561e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 76 deletions.
24 changes: 0 additions & 24 deletions dom/media/platforms/ffmpeg/FFmpegAudioDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ CopyAndPackAudio(AVFrame* aFrame, uint32_t aNumChannels, uint32_t aNumAFrames)
FFmpegAudioDecoder<LIBAV_VER>::DecodeResult
FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
AVPacket packet;
mLib->av_init_packet(&packet);

Expand All @@ -108,7 +107,6 @@ FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)

if (!PrepareFrame()) {
NS_WARNING("FFmpeg audio decoder failed to allocate frame.");
mCallback->Error();
return DecodeResult::DECODE_ERROR;
}

Expand All @@ -122,15 +120,13 @@ FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)

if (bytesConsumed < 0) {
NS_WARNING("FFmpeg audio decoder error.");
mCallback->Error();
return DecodeResult::DECODE_ERROR;
}

if (decoded) {
uint32_t numChannels = mCodecContext->channels;
AudioConfig::ChannelLayout layout(numChannels);
if (!layout.IsValid()) {
mCallback->Error();
return DecodeResult::DECODE_ERROR;
}

Expand All @@ -143,7 +139,6 @@ FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
FramesToTimeUnit(mFrame->nb_samples, samplingRate);
if (!audio || !duration.IsValid()) {
NS_WARNING("Invalid count of accumulated audio samples");
mCallback->Error();
return DecodeResult::DECODE_ERROR;
}

Expand All @@ -158,7 +153,6 @@ FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
pts += duration;
if (!pts.IsValid()) {
NS_WARNING("Invalid count of accumulated audio samples");
mCallback->Error();
return DecodeResult::DECODE_ERROR;
}
}
Expand All @@ -170,27 +164,9 @@ FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
return DecodeResult::DECODE_FRAME;
}

void
FFmpegAudioDecoder<LIBAV_VER>::ProcessDecode(MediaRawData* aSample)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
if (DoDecode(aSample) != DecodeResult::DECODE_ERROR && mTaskQueue->IsEmpty()) {
mCallback->InputExhausted();
}
}

nsresult
FFmpegAudioDecoder<LIBAV_VER>::Input(MediaRawData* aSample)
{
mTaskQueue->Dispatch(NewRunnableMethod<RefPtr<MediaRawData>>(
this, &FFmpegAudioDecoder::ProcessDecode, aSample));
return NS_OK;
}

void
FFmpegAudioDecoder<LIBAV_VER>::ProcessDrain()
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
ProcessFlush();
mCallback->DrainComplete();
}
Expand Down
12 changes: 2 additions & 10 deletions dom/media/platforms/ffmpeg/FFmpegAudioDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@ template <int V> class FFmpegAudioDecoder
template <>
class FFmpegAudioDecoder<LIBAV_VER> : public FFmpegDataDecoder<LIBAV_VER>
{
enum DecodeResult {
DECODE_FRAME,
DECODE_NO_FRAME,
DECODE_ERROR
};

public:
FFmpegAudioDecoder(FFmpegLibWrapper* aLib, FlushableTaskQueue* aTaskQueue,
MediaDataDecoderCallback* aCallback,
const AudioInfo& aConfig);
virtual ~FFmpegAudioDecoder();

RefPtr<InitPromise> Init() override;
nsresult Input(MediaRawData* aSample) override;
void ProcessDrain() override;
void InitCodecContext() override;
static AVCodecID GetCodecId(const nsACString& aMimeType);
const char* GetDescriptionName() const override
Expand All @@ -43,8 +35,8 @@ class FFmpegAudioDecoder<LIBAV_VER> : public FFmpegDataDecoder<LIBAV_VER>
}

private:
void ProcessDecode(MediaRawData* aSample);
DecodeResult DoDecode(MediaRawData* aSample);
DecodeResult DoDecode(MediaRawData* aSample) override;
void ProcessDrain() override;
};

} // namespace mozilla
Expand Down
25 changes: 24 additions & 1 deletion dom/media/platforms/ffmpeg/FFmpegDataDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ StaticMutex FFmpegDataDecoder<LIBAV_VER>::sMonitor;
MediaDataDecoderCallback* aCallback,
AVCodecID aCodecID)
: mLib(aLib)
, mTaskQueue(aTaskQueue)
, mCallback(aCallback)
, mCodecContext(nullptr)
, mFrame(NULL)
, mExtraData(nullptr)
, mCodecID(aCodecID)
, mTaskQueue(aTaskQueue)
{
MOZ_ASSERT(aLib);
MOZ_COUNT_CTOR(FFmpegDataDecoder);
Expand Down Expand Up @@ -111,6 +111,29 @@ FFmpegDataDecoder<LIBAV_VER>::Shutdown()
return NS_OK;
}

void
FFmpegDataDecoder<LIBAV_VER>::ProcessDecode(MediaRawData* aSample)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
switch (DoDecode(aSample)) {
case DecodeResult::DECODE_ERROR:
mCallback->Error();
break;
default:
if (mTaskQueue->IsEmpty()) {
mCallback->InputExhausted();
}
}
}

nsresult
FFmpegDataDecoder<LIBAV_VER>::Input(MediaRawData* aSample)
{
mTaskQueue->Dispatch(NewRunnableMethod<RefPtr<MediaRawData>>(
this, &FFmpegDataDecoder::ProcessDecode, aSample));
return NS_OK;
}

nsresult
FFmpegDataDecoder<LIBAV_VER>::Flush()
{
Expand Down
15 changes: 12 additions & 3 deletions dom/media/platforms/ffmpeg/FFmpegDataDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,28 @@ class FFmpegDataDecoder<LIBAV_VER> : public MediaDataDecoder
static bool Link();

RefPtr<InitPromise> Init() override = 0;
nsresult Input(MediaRawData* aSample) override = 0;
nsresult Input(MediaRawData* aSample) override;
nsresult Flush() override;
nsresult Drain() override;
nsresult Shutdown() override;

static AVCodec* FindAVCodec(FFmpegLibWrapper* aLib, AVCodecID aCodec);

protected:
enum DecodeResult {
DECODE_FRAME,
DECODE_NO_FRAME,
DECODE_ERROR
};

// Flush and Drain operation, always run
virtual void ProcessFlush();
virtual void ProcessDrain() = 0;
virtual void ProcessShutdown();
virtual void InitCodecContext() {}
AVFrame* PrepareFrame();
nsresult InitDecoder();

FFmpegLibWrapper* mLib;
RefPtr<FlushableTaskQueue> mTaskQueue;
MediaDataDecoderCallback* mCallback;

AVCodecContext* mCodecContext;
Expand All @@ -58,7 +62,12 @@ class FFmpegDataDecoder<LIBAV_VER> : public MediaDataDecoder
AVCodecID mCodecID;

private:
void ProcessDecode(MediaRawData* aSample);
virtual DecodeResult DoDecode(MediaRawData* aSample) = 0;
virtual void ProcessDrain() = 0;

static StaticMutex sMonitor;
const RefPtr<FlushableTaskQueue> mTaskQueue;
};

} // namespace mozilla
Expand Down
26 changes: 0 additions & 26 deletions dom/media/platforms/ffmpeg/FFmpegVideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ FFmpegVideoDecoder<LIBAV_VER>::InitCodecContext()
FFmpegVideoDecoder<LIBAV_VER>::DecodeResult
FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());

uint8_t* inputData = const_cast<uint8_t*>(aSample->Data());
size_t inputSize = aSample->Size();

Expand All @@ -181,7 +179,6 @@ FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
aSample->mTime, aSample->mTimecode,
aSample->mOffset);
if (size_t(len) > inputSize) {
mCallback->Error();
return DecodeResult::DECODE_ERROR;
}
inputData += len;
Expand All @@ -208,8 +205,6 @@ FFmpegVideoDecoder<LIBAV_VER>::DecodeResult
FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample,
uint8_t* aData, int aSize)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());

AVPacket packet;
mLib->av_init_packet(&packet);

Expand All @@ -229,7 +224,6 @@ FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample,

if (!PrepareFrame()) {
NS_WARNING("FFmpeg h264 decoder failed to allocate frame.");
mCallback->Error();
return DecodeResult::DECODE_ERROR;
}

Expand All @@ -248,7 +242,6 @@ FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample,

if (bytesConsumed < 0) {
NS_WARNING("FFmpeg video decoder error.");
mCallback->Error();
return DecodeResult::DECODE_ERROR;
}

Expand Down Expand Up @@ -307,7 +300,6 @@ FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample,

if (!v) {
NS_WARNING("image allocation error.");
mCallback->Error();
return DecodeResult::DECODE_ERROR;
}
mCallback->Output(v);
Expand All @@ -316,27 +308,9 @@ FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample,
return DecodeResult::DECODE_NO_FRAME;
}

void
FFmpegVideoDecoder<LIBAV_VER>::ProcessDecode(MediaRawData* aSample)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
if (DoDecode(aSample) != DecodeResult::DECODE_ERROR && mTaskQueue->IsEmpty()) {
mCallback->InputExhausted();
}
}

nsresult
FFmpegVideoDecoder<LIBAV_VER>::Input(MediaRawData* aSample)
{
mTaskQueue->Dispatch(NewRunnableMethod<RefPtr<MediaRawData>>(
this, &FFmpegVideoDecoder::ProcessDecode, aSample));
return NS_OK;
}

void
FFmpegVideoDecoder<LIBAV_VER>::ProcessDrain()
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
RefPtr<MediaRawData> empty(new MediaRawData());
empty->mTimecode = mPtsContext.LastDts();
while (DoDecode(empty) == DecodeResult::DECODE_FRAME) {
Expand Down
15 changes: 3 additions & 12 deletions dom/media/platforms/ffmpeg/FFmpegVideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ class FFmpegVideoDecoder<LIBAV_VER> : public FFmpegDataDecoder<LIBAV_VER>
typedef mozilla::layers::Image Image;
typedef mozilla::layers::ImageContainer ImageContainer;

enum DecodeResult {
DECODE_FRAME,
DECODE_NO_FRAME,
DECODE_ERROR
};

public:
FFmpegVideoDecoder(FFmpegLibWrapper* aLib, FlushableTaskQueue* aTaskQueue,
MediaDataDecoderCallback* aCallback,
Expand All @@ -40,9 +34,6 @@ class FFmpegVideoDecoder<LIBAV_VER> : public FFmpegDataDecoder<LIBAV_VER>
virtual ~FFmpegVideoDecoder();

RefPtr<InitPromise> Init() override;
nsresult Input(MediaRawData* aSample) override;
void ProcessDrain() override;
void ProcessFlush() override;
void InitCodecContext() override;
const char* GetDescriptionName() const override
{
Expand All @@ -55,10 +46,10 @@ class FFmpegVideoDecoder<LIBAV_VER> : public FFmpegDataDecoder<LIBAV_VER>
static AVCodecID GetCodecId(const nsACString& aMimeType);

private:
void ProcessDecode(MediaRawData* aSample);
DecodeResult DoDecode(MediaRawData* aSample);
DecodeResult DoDecode(MediaRawData* aSample) override;
DecodeResult DoDecode(MediaRawData* aSample, uint8_t* aData, int aSize);
void DoDrain();
void ProcessDrain() override;
void ProcessFlush() override;
void OutputDelayedFrames();

/**
Expand Down

0 comments on commit 684561e

Please sign in to comment.