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_BASE_AUDIO_DECODER_H_
6#define MEDIA_BASE_AUDIO_DECODER_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "base/memory/ref_counted.h"
12#include "media/base/audio_decoder_config.h"
13#include "media/base/channel_layout.h"
14#include "media/base/decoder_buffer.h"
15#include "media/base/media_export.h"
16#include "media/base/pipeline_status.h"
17
18namespace media {
19
20class AudioBuffer;
21class DemuxerStream;
22
23class MEDIA_EXPORT AudioDecoder {
24 public:
25  // Status codes for decode operations.
26  // TODO(rileya): Now that both AudioDecoder and VideoDecoder Status enums
27  // match, break them into a decoder_status.h.
28  enum Status {
29    kOk,  // We're all good.
30    kAborted,  // We aborted as a result of Reset() or destruction.
31    kDecodeError,  // A decoding error occurred.
32    kDecryptError  // Decrypting error happened.
33  };
34
35  // Callback for AudioDecoder to return a decoded frame whenever it becomes
36  // available. Only non-EOS frames should be returned via this callback.
37  typedef base::Callback<void(const scoped_refptr<AudioBuffer>&)> OutputCB;
38
39  // Callback for Decode(). Called after the decoder has completed decoding
40  // corresponding DecoderBuffer, indicating that it's ready to accept another
41  // buffer to decode.
42  typedef base::Callback<void(Status)> DecodeCB;
43
44  AudioDecoder();
45
46  // Fires any pending callbacks, stops and destroys the decoder.
47  // Note: Since this is a destructor, |this| will be destroyed after this call.
48  // Make sure the callbacks fired from this call doesn't post any task that
49  // depends on |this|.
50  virtual ~AudioDecoder();
51
52  // Returns the name of the decoder for logging purpose.
53  virtual std::string GetDisplayName() const = 0;
54
55  // Initializes an AudioDecoder with the given DemuxerStream, executing the
56  // callback upon completion.
57  //  |statistics_cb| is used to update global pipeline statistics.
58  //  |output_cb| is called for decoded audio buffers (see Decode()).
59  virtual void Initialize(const AudioDecoderConfig& config,
60                          const PipelineStatusCB& status_cb,
61                          const OutputCB& output_cb) = 0;
62
63  // Requests samples to be decoded. Only one decode may be in flight at any
64  // given time. Once the buffer is decoded the decoder calls |decode_cb|.
65  // |output_cb| specified in Initialize() is called for each decoded buffer,
66  // before or after |decode_cb|.
67  //
68  // Implementations guarantee that the callbacks will not be called from within
69  // this method.
70  //
71  // If |buffer| is an EOS buffer then the decoder must be flushed, i.e.
72  // |output_cb| must be called for each frame pending in the queue and
73  // |decode_cb| must be called after that.
74  virtual void Decode(const scoped_refptr<DecoderBuffer>& buffer,
75                      const DecodeCB& decode_cb) = 0;
76
77  // Resets decoder state. All pending Decode() requests will be finished or
78  // aborted before |closure| is called.
79  virtual void Reset(const base::Closure& closure) = 0;
80
81 private:
82  DISALLOW_COPY_AND_ASSIGN(AudioDecoder);
83};
84
85}  // namespace media
86
87#endif  // MEDIA_BASE_AUDIO_DECODER_H_
88