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

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

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