Bug 1271508. Part 3 - extract code to the parent class and remove use of mTaskQueue from sub-classes. r=jya.

This commit is contained in:
JW Wang 2016-05-09 23:23:48 +08:00
Родитель 663cb8f7b8
Коммит 651acd2608
6 изменённых файлов: 41 добавлений и 76 удалений

Просмотреть файл

@ -99,7 +99,6 @@ CopyAndPackAudio(AVFrame* aFrame, uint32_t aNumChannels, uint32_t aNumAFrames)
FFmpegAudioDecoder<LIBAV_VER>::DecodeResult FFmpegAudioDecoder<LIBAV_VER>::DecodeResult
FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample) FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
{ {
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
AVPacket packet; AVPacket packet;
mLib->av_init_packet(&packet); mLib->av_init_packet(&packet);
@ -108,7 +107,6 @@ FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
if (!PrepareFrame()) { if (!PrepareFrame()) {
NS_WARNING("FFmpeg audio decoder failed to allocate frame."); NS_WARNING("FFmpeg audio decoder failed to allocate frame.");
mCallback->Error();
return DecodeResult::DECODE_ERROR; return DecodeResult::DECODE_ERROR;
} }
@ -122,7 +120,6 @@ FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
if (bytesConsumed < 0) { if (bytesConsumed < 0) {
NS_WARNING("FFmpeg audio decoder error."); NS_WARNING("FFmpeg audio decoder error.");
mCallback->Error();
return DecodeResult::DECODE_ERROR; return DecodeResult::DECODE_ERROR;
} }
@ -130,7 +127,6 @@ FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
uint32_t numChannels = mCodecContext->channels; uint32_t numChannels = mCodecContext->channels;
AudioConfig::ChannelLayout layout(numChannels); AudioConfig::ChannelLayout layout(numChannels);
if (!layout.IsValid()) { if (!layout.IsValid()) {
mCallback->Error();
return DecodeResult::DECODE_ERROR; return DecodeResult::DECODE_ERROR;
} }
@ -143,7 +139,6 @@ FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
FramesToTimeUnit(mFrame->nb_samples, samplingRate); FramesToTimeUnit(mFrame->nb_samples, samplingRate);
if (!audio || !duration.IsValid()) { if (!audio || !duration.IsValid()) {
NS_WARNING("Invalid count of accumulated audio samples"); NS_WARNING("Invalid count of accumulated audio samples");
mCallback->Error();
return DecodeResult::DECODE_ERROR; return DecodeResult::DECODE_ERROR;
} }
@ -158,7 +153,6 @@ FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
pts += duration; pts += duration;
if (!pts.IsValid()) { if (!pts.IsValid()) {
NS_WARNING("Invalid count of accumulated audio samples"); NS_WARNING("Invalid count of accumulated audio samples");
mCallback->Error();
return DecodeResult::DECODE_ERROR; return DecodeResult::DECODE_ERROR;
} }
} }
@ -170,27 +164,9 @@ FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
return DecodeResult::DECODE_FRAME; 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 void
FFmpegAudioDecoder<LIBAV_VER>::ProcessDrain() FFmpegAudioDecoder<LIBAV_VER>::ProcessDrain()
{ {
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
ProcessFlush(); ProcessFlush();
mCallback->DrainComplete(); mCallback->DrainComplete();
} }

Просмотреть файл

@ -20,12 +20,6 @@ template <int V> class FFmpegAudioDecoder
template <> template <>
class FFmpegAudioDecoder<LIBAV_VER> : public FFmpegDataDecoder<LIBAV_VER> class FFmpegAudioDecoder<LIBAV_VER> : public FFmpegDataDecoder<LIBAV_VER>
{ {
enum DecodeResult {
DECODE_FRAME,
DECODE_NO_FRAME,
DECODE_ERROR
};
public: public:
FFmpegAudioDecoder(FFmpegLibWrapper* aLib, FlushableTaskQueue* aTaskQueue, FFmpegAudioDecoder(FFmpegLibWrapper* aLib, FlushableTaskQueue* aTaskQueue,
MediaDataDecoderCallback* aCallback, MediaDataDecoderCallback* aCallback,
@ -33,8 +27,6 @@ public:
virtual ~FFmpegAudioDecoder(); virtual ~FFmpegAudioDecoder();
RefPtr<InitPromise> Init() override; RefPtr<InitPromise> Init() override;
nsresult Input(MediaRawData* aSample) override;
void ProcessDrain() override;
void InitCodecContext() override; void InitCodecContext() override;
static AVCodecID GetCodecId(const nsACString& aMimeType); static AVCodecID GetCodecId(const nsACString& aMimeType);
const char* GetDescriptionName() const override const char* GetDescriptionName() const override
@ -43,8 +35,8 @@ public:
} }
private: private:
void ProcessDecode(MediaRawData* aSample); DecodeResult DoDecode(MediaRawData* aSample) override;
DecodeResult DoDecode(MediaRawData* aSample); void ProcessDrain() override;
}; };
} // namespace mozilla } // namespace mozilla

Просмотреть файл

@ -26,12 +26,12 @@ StaticMutex FFmpegDataDecoder<LIBAV_VER>::sMonitor;
MediaDataDecoderCallback* aCallback, MediaDataDecoderCallback* aCallback,
AVCodecID aCodecID) AVCodecID aCodecID)
: mLib(aLib) : mLib(aLib)
, mTaskQueue(aTaskQueue)
, mCallback(aCallback) , mCallback(aCallback)
, mCodecContext(nullptr) , mCodecContext(nullptr)
, mFrame(NULL) , mFrame(NULL)
, mExtraData(nullptr) , mExtraData(nullptr)
, mCodecID(aCodecID) , mCodecID(aCodecID)
, mTaskQueue(aTaskQueue)
{ {
MOZ_ASSERT(aLib); MOZ_ASSERT(aLib);
MOZ_COUNT_CTOR(FFmpegDataDecoder); MOZ_COUNT_CTOR(FFmpegDataDecoder);
@ -111,6 +111,29 @@ FFmpegDataDecoder<LIBAV_VER>::Shutdown()
return NS_OK; 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 nsresult
FFmpegDataDecoder<LIBAV_VER>::Flush() FFmpegDataDecoder<LIBAV_VER>::Flush()
{ {

Просмотреть файл

@ -32,7 +32,7 @@ public:
static bool Link(); static bool Link();
RefPtr<InitPromise> Init() override = 0; RefPtr<InitPromise> Init() override = 0;
nsresult Input(MediaRawData* aSample) override = 0; nsresult Input(MediaRawData* aSample) override;
nsresult Flush() override; nsresult Flush() override;
nsresult Drain() override; nsresult Drain() override;
nsresult Shutdown() override; nsresult Shutdown() override;
@ -40,16 +40,20 @@ public:
static AVCodec* FindAVCodec(FFmpegLibWrapper* aLib, AVCodecID aCodec); static AVCodec* FindAVCodec(FFmpegLibWrapper* aLib, AVCodecID aCodec);
protected: protected:
enum DecodeResult {
DECODE_FRAME,
DECODE_NO_FRAME,
DECODE_ERROR
};
// Flush and Drain operation, always run // Flush and Drain operation, always run
virtual void ProcessFlush(); virtual void ProcessFlush();
virtual void ProcessDrain() = 0;
virtual void ProcessShutdown(); virtual void ProcessShutdown();
virtual void InitCodecContext() {} virtual void InitCodecContext() {}
AVFrame* PrepareFrame(); AVFrame* PrepareFrame();
nsresult InitDecoder(); nsresult InitDecoder();
FFmpegLibWrapper* mLib; FFmpegLibWrapper* mLib;
RefPtr<FlushableTaskQueue> mTaskQueue;
MediaDataDecoderCallback* mCallback; MediaDataDecoderCallback* mCallback;
AVCodecContext* mCodecContext; AVCodecContext* mCodecContext;
@ -58,7 +62,12 @@ protected:
AVCodecID mCodecID; AVCodecID mCodecID;
private: private:
void ProcessDecode(MediaRawData* aSample);
virtual DecodeResult DoDecode(MediaRawData* aSample) = 0;
virtual void ProcessDrain() = 0;
static StaticMutex sMonitor; static StaticMutex sMonitor;
const RefPtr<FlushableTaskQueue> mTaskQueue;
}; };
} // namespace mozilla } // namespace mozilla

Просмотреть файл

@ -161,8 +161,6 @@ FFmpegVideoDecoder<LIBAV_VER>::InitCodecContext()
FFmpegVideoDecoder<LIBAV_VER>::DecodeResult FFmpegVideoDecoder<LIBAV_VER>::DecodeResult
FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample) FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
{ {
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
uint8_t* inputData = const_cast<uint8_t*>(aSample->Data()); uint8_t* inputData = const_cast<uint8_t*>(aSample->Data());
size_t inputSize = aSample->Size(); size_t inputSize = aSample->Size();
@ -181,7 +179,6 @@ FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
aSample->mTime, aSample->mTimecode, aSample->mTime, aSample->mTimecode,
aSample->mOffset); aSample->mOffset);
if (size_t(len) > inputSize) { if (size_t(len) > inputSize) {
mCallback->Error();
return DecodeResult::DECODE_ERROR; return DecodeResult::DECODE_ERROR;
} }
inputData += len; inputData += len;
@ -208,8 +205,6 @@ FFmpegVideoDecoder<LIBAV_VER>::DecodeResult
FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample, FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample,
uint8_t* aData, int aSize) uint8_t* aData, int aSize)
{ {
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
AVPacket packet; AVPacket packet;
mLib->av_init_packet(&packet); mLib->av_init_packet(&packet);
@ -229,7 +224,6 @@ FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample,
if (!PrepareFrame()) { if (!PrepareFrame()) {
NS_WARNING("FFmpeg h264 decoder failed to allocate frame."); NS_WARNING("FFmpeg h264 decoder failed to allocate frame.");
mCallback->Error();
return DecodeResult::DECODE_ERROR; return DecodeResult::DECODE_ERROR;
} }
@ -248,7 +242,6 @@ FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample,
if (bytesConsumed < 0) { if (bytesConsumed < 0) {
NS_WARNING("FFmpeg video decoder error."); NS_WARNING("FFmpeg video decoder error.");
mCallback->Error();
return DecodeResult::DECODE_ERROR; return DecodeResult::DECODE_ERROR;
} }
@ -307,7 +300,6 @@ FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample,
if (!v) { if (!v) {
NS_WARNING("image allocation error."); NS_WARNING("image allocation error.");
mCallback->Error();
return DecodeResult::DECODE_ERROR; return DecodeResult::DECODE_ERROR;
} }
mCallback->Output(v); mCallback->Output(v);
@ -316,27 +308,9 @@ FFmpegVideoDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample,
return DecodeResult::DECODE_NO_FRAME; 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 void
FFmpegVideoDecoder<LIBAV_VER>::ProcessDrain() FFmpegVideoDecoder<LIBAV_VER>::ProcessDrain()
{ {
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
RefPtr<MediaRawData> empty(new MediaRawData()); RefPtr<MediaRawData> empty(new MediaRawData());
empty->mTimecode = mPtsContext.LastDts(); empty->mTimecode = mPtsContext.LastDts();
while (DoDecode(empty) == DecodeResult::DECODE_FRAME) { while (DoDecode(empty) == DecodeResult::DECODE_FRAME) {

Просмотреть файл

@ -26,12 +26,6 @@ class FFmpegVideoDecoder<LIBAV_VER> : public FFmpegDataDecoder<LIBAV_VER>
typedef mozilla::layers::Image Image; typedef mozilla::layers::Image Image;
typedef mozilla::layers::ImageContainer ImageContainer; typedef mozilla::layers::ImageContainer ImageContainer;
enum DecodeResult {
DECODE_FRAME,
DECODE_NO_FRAME,
DECODE_ERROR
};
public: public:
FFmpegVideoDecoder(FFmpegLibWrapper* aLib, FlushableTaskQueue* aTaskQueue, FFmpegVideoDecoder(FFmpegLibWrapper* aLib, FlushableTaskQueue* aTaskQueue,
MediaDataDecoderCallback* aCallback, MediaDataDecoderCallback* aCallback,
@ -40,9 +34,6 @@ public:
virtual ~FFmpegVideoDecoder(); virtual ~FFmpegVideoDecoder();
RefPtr<InitPromise> Init() override; RefPtr<InitPromise> Init() override;
nsresult Input(MediaRawData* aSample) override;
void ProcessDrain() override;
void ProcessFlush() override;
void InitCodecContext() override; void InitCodecContext() override;
const char* GetDescriptionName() const override const char* GetDescriptionName() const override
{ {
@ -55,10 +46,10 @@ public:
static AVCodecID GetCodecId(const nsACString& aMimeType); static AVCodecID GetCodecId(const nsACString& aMimeType);
private: private:
void ProcessDecode(MediaRawData* aSample); DecodeResult DoDecode(MediaRawData* aSample) override;
DecodeResult DoDecode(MediaRawData* aSample);
DecodeResult DoDecode(MediaRawData* aSample, uint8_t* aData, int aSize); DecodeResult DoDecode(MediaRawData* aSample, uint8_t* aData, int aSize);
void DoDrain(); void ProcessDrain() override;
void ProcessFlush() override;
void OutputDelayedFrames(); void OutputDelayedFrames();
/** /**