Bug 1271508. Part 1 - refactor FFmpegAudioDecoder code to be similar to FFmpegVideoDecoder::Input() so it would be easier to extract common code to the parent class. r=jya.

MozReview-Commit-ID: 4IXZz50qrWm
This commit is contained in:
JW Wang 2016-05-09 23:03:54 +08:00
Родитель 5c9bb43e9d
Коммит 9a04622671
2 изменённых файлов: 25 добавлений и 12 удалений

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

@ -96,8 +96,8 @@ CopyAndPackAudio(AVFrame* aFrame, uint32_t aNumChannels, uint32_t aNumAFrames)
return audio;
}
void
FFmpegAudioDecoder<LIBAV_VER>::DecodePacket(MediaRawData* aSample)
FFmpegAudioDecoder<LIBAV_VER>::DecodeResult
FFmpegAudioDecoder<LIBAV_VER>::DoDecode(MediaRawData* aSample)
{
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
AVPacket packet;
@ -109,7 +109,7 @@ FFmpegAudioDecoder<LIBAV_VER>::DecodePacket(MediaRawData* aSample)
if (!PrepareFrame()) {
NS_WARNING("FFmpeg audio decoder failed to allocate frame.");
mCallback->Error();
return;
return DecodeResult::DECODE_ERROR;
}
int64_t samplePosition = aSample->mOffset;
@ -123,7 +123,7 @@ FFmpegAudioDecoder<LIBAV_VER>::DecodePacket(MediaRawData* aSample)
if (bytesConsumed < 0) {
NS_WARNING("FFmpeg audio decoder error.");
mCallback->Error();
return;
return DecodeResult::DECODE_ERROR;
}
if (decoded) {
@ -131,7 +131,7 @@ FFmpegAudioDecoder<LIBAV_VER>::DecodePacket(MediaRawData* aSample)
AudioConfig::ChannelLayout layout(numChannels);
if (!layout.IsValid()) {
mCallback->Error();
return;
return DecodeResult::DECODE_ERROR;
}
uint32_t samplingRate = mCodecContext->sample_rate;
@ -144,7 +144,7 @@ FFmpegAudioDecoder<LIBAV_VER>::DecodePacket(MediaRawData* aSample)
if (!audio || !duration.IsValid()) {
NS_WARNING("Invalid count of accumulated audio samples");
mCallback->Error();
return;
return DecodeResult::DECODE_ERROR;
}
RefPtr<AudioData> data = new AudioData(samplePosition,
@ -159,7 +159,7 @@ FFmpegAudioDecoder<LIBAV_VER>::DecodePacket(MediaRawData* aSample)
if (!pts.IsValid()) {
NS_WARNING("Invalid count of accumulated audio samples");
mCallback->Error();
return;
return DecodeResult::DECODE_ERROR;
}
}
packet.data += bytesConsumed;
@ -167,7 +167,14 @@ FFmpegAudioDecoder<LIBAV_VER>::DecodePacket(MediaRawData* aSample)
samplePosition += bytesConsumed;
}
if (mTaskQueue->IsEmpty()) {
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();
}
}
@ -175,9 +182,8 @@ FFmpegAudioDecoder<LIBAV_VER>::DecodePacket(MediaRawData* aSample)
nsresult
FFmpegAudioDecoder<LIBAV_VER>::Input(MediaRawData* aSample)
{
nsCOMPtr<nsIRunnable> runnable(NewRunnableMethod<RefPtr<MediaRawData>>(
this, &FFmpegAudioDecoder::DecodePacket, RefPtr<MediaRawData>(aSample)));
mTaskQueue->Dispatch(runnable.forget());
mTaskQueue->Dispatch(NewRunnableMethod<RefPtr<MediaRawData>>(
this, &FFmpegAudioDecoder::ProcessDecode, aSample));
return NS_OK;
}

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

@ -20,6 +20,12 @@ 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,
@ -37,7 +43,8 @@ public:
}
private:
void DecodePacket(MediaRawData* aSample);
void ProcessDecode(MediaRawData* aSample);
DecodeResult DoDecode(MediaRawData* aSample);
};
} // namespace mozilla