Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / filters / ffmpeg_audio_decoder.h
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.
4
5 #ifndef MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_
6 #define MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_
7
8 #include <list>
9
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"
18
19 struct AVCodecContext;
20 struct AVFrame;
21
22 namespace base {
23 class SingleThreadTaskRunner;
24 }
25
26 namespace media {
27
28 class AudioDiscardHelper;
29 class DecoderBuffer;
30
31 class MEDIA_EXPORT FFmpegAudioDecoder : public AudioDecoder {
32  public:
33   FFmpegAudioDecoder(
34       const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
35       const LogCB& log_cb);
36   virtual ~FFmpegAudioDecoder();
37
38   // AudioDecoder implementation.
39   virtual void Initialize(const AudioDecoderConfig& config,
40                           const PipelineStatusCB& status_cb) OVERRIDE;
41   virtual void Decode(const scoped_refptr<DecoderBuffer>& buffer,
42                       const DecodeCB& decode_cb) OVERRIDE;
43   virtual scoped_refptr<AudioBuffer> GetDecodeOutput() OVERRIDE;
44   virtual void Reset(const base::Closure& closure) OVERRIDE;
45   virtual void Stop() OVERRIDE;
46
47  private:
48   enum DecoderState {
49     kUninitialized,
50     kNormal,
51     kFlushCodec,
52     kDecodeFinished,
53     kError
54   };
55
56   // Reset decoder and call |reset_cb_|.
57   void DoReset();
58
59   // Handles decoding an unencrypted encoded buffer.
60   void DecodeBuffer(const scoped_refptr<DecoderBuffer>& buffer,
61                     const DecodeCB& decode_cb);
62   bool FFmpegDecode(const scoped_refptr<DecoderBuffer>& buffer);
63
64   // Handles (re-)initializing the decoder with a (new) config.
65   // Returns true if initialization was successful.
66   bool ConfigureDecoder();
67
68   // Releases resources associated with |codec_context_| and |av_frame_|
69   // and resets them to NULL.
70   void ReleaseFFmpegResources();
71   void ResetTimestampState();
72
73   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
74
75   DecoderState state_;
76
77   // FFmpeg structures owned by this object.
78   scoped_ptr<AVCodecContext, ScopedPtrAVFreeContext> codec_context_;
79   scoped_ptr<AVFrame, ScopedPtrAVFreeFrame> av_frame_;
80
81   AudioDecoderConfig config_;
82
83   // AVSampleFormat initially requested; not Chrome's SampleFormat.
84   int av_sample_format_;
85
86   scoped_ptr<AudioDiscardHelper> discard_helper_;
87
88   // Since multiple frames may be decoded from the same packet we need to queue
89   // them up.
90   std::list<scoped_refptr<AudioBuffer> > queued_audio_;
91
92   LogCB log_cb_;
93
94   DISALLOW_IMPLICIT_CONSTRUCTORS(FFmpegAudioDecoder);
95 };
96
97 }  // namespace media
98
99 #endif  // MEDIA_FILTERS_FFMPEG_AUDIO_DECODER_H_