1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_
6 #define MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "media/base/audio_decoder.h"
14 #include "media/base/demuxer_stream.h"
15 #include "media/base/media_log.h"
16 #include "media/base/sample_format.h"
17 #include "media/ffmpeg/ffmpeg_deleters.h"
19 struct AVCodecContext
;
23 class SingleThreadTaskRunner
;
28 class AudioDiscardHelper
;
31 class MEDIA_EXPORT FFmpegAudioDecoder
: public AudioDecoder
{
34 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
35 const scoped_refptr
<MediaLog
>& media_log
);
36 ~FFmpegAudioDecoder() override
;
38 // AudioDecoder implementation.
39 std::string
GetDisplayName() const override
;
40 void Initialize(const AudioDecoderConfig
& config
,
41 const InitCB
& init_cb
,
42 const OutputCB
& output_cb
) override
;
43 void Decode(const scoped_refptr
<DecoderBuffer
>& buffer
,
44 const DecodeCB
& decode_cb
) override
;
45 void Reset(const base::Closure
& closure
) override
;
48 // There are four states the decoder can be in:
50 // - kUninitialized: The decoder is not initialized.
51 // - kNormal: This is the normal state. The decoder is idle and ready to
52 // decode input buffers, or is decoding an input buffer.
53 // - kDecodeFinished: EOS buffer received, codec flushed and decode finished.
54 // No further Decode() call should be made.
55 // - kError: Unexpected error happened.
57 // These are the possible state transitions.
59 // kUninitialized -> kNormal:
60 // The decoder is successfully initialized and is ready to decode buffers.
61 // kNormal -> kDecodeFinished:
62 // When buffer->end_of_stream() is true and avcodec_decode_audio4()
65 // A decoding error occurs and decoding needs to stop.
66 // (any state) -> kNormal:
67 // Any time Reset() is called.
75 // Reset decoder and call |reset_cb_|.
78 // Handles decoding an unencrypted encoded buffer.
79 void DecodeBuffer(const scoped_refptr
<DecoderBuffer
>& buffer
,
80 const DecodeCB
& decode_cb
);
81 bool FFmpegDecode(const scoped_refptr
<DecoderBuffer
>& buffer
,
82 bool* has_produced_frame
);
84 // Handles (re-)initializing the decoder with a (new) config.
85 // Returns true if initialization was successful.
86 bool ConfigureDecoder();
88 // Releases resources associated with |codec_context_| and |av_frame_|
89 // and resets them to NULL.
90 void ReleaseFFmpegResources();
91 void ResetTimestampState();
93 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
99 // FFmpeg structures owned by this object.
100 scoped_ptr
<AVCodecContext
, ScopedPtrAVFreeContext
> codec_context_
;
101 scoped_ptr
<AVFrame
, ScopedPtrAVFreeFrame
> av_frame_
;
103 AudioDecoderConfig config_
;
105 // AVSampleFormat initially requested; not Chrome's SampleFormat.
106 int av_sample_format_
;
108 scoped_ptr
<AudioDiscardHelper
> discard_helper_
;
110 scoped_refptr
<MediaLog
> media_log_
;
112 DISALLOW_IMPLICIT_CONSTRUCTORS(FFmpegAudioDecoder
);
117 #endif // MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_