1 // Copyright 2015 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 CHROMECAST_MEDIA_API_CAST_AUDIO_DECODER_H_
6 #define CHROMECAST_MEDIA_API_CAST_AUDIO_DECODER_H_
7 
8 #include <memory>
9 
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "chromecast/media/api/decoder_buffer_base.h"
13 #include "chromecast/public/media/decoder_config.h"
14 
15 namespace base {
16 class SingleThreadTaskRunner;
17 }  // namespace base
18 
19 namespace chromecast {
20 namespace media {
21 
22 // Audio decoder interface.
23 class CastAudioDecoder {
24  public:
25   enum Status {
26     kDecodeOk,
27     kDecodeError,
28   };
29 
30   enum OutputFormat {
31     kOutputSigned16,     // Output signed 16-bit interleaved samples.
32     kOutputPlanarFloat,  // Output planar float samples.
33   };
34 
35   // Callback called when a buffer has been decoded. |config| is the actual
36   // config of the buffer, which may differ from the config indicated by the
37   // wrapper format.
38   typedef base::OnceCallback<void(
39       Status status,
40       const AudioConfig& config,
41       scoped_refptr<media::DecoderBufferBase> decoded)>
42       DecodeCallback;
43 
44   // Creates a CastAudioDecoder instance for the given |config|. Decoding must
45   // occur on the same thread as |task_runner|. Returns an empty unique_ptr if
46   // the decoder could not be created.
47   static std::unique_ptr<CastAudioDecoder> Create(
48       scoped_refptr<base::SingleThreadTaskRunner> task_runner,
49       const media::AudioConfig& config,
50       OutputFormat output_format);
51 
52   // Given a CastAudioDecoder::OutputFormat, return the size of each sample in
53   // that OutputFormat in bytes.
54   static int OutputFormatSizeInBytes(CastAudioDecoder::OutputFormat format);
55 
56   virtual ~CastAudioDecoder() = default;
57 
58   // Returns the expected config of the next decoded audio. Note that the config
59   // may change as more audio is decoded.
60   virtual const AudioConfig& GetOutputConfig() const = 0;
61 
62   // Converts encoded data to the |output_format|. Must be called on the same
63   // thread as |task_runner|. Decoded data will be passed to |decode_callback|.
64   // The |decode_callback| will not be called after the CastAudioDecoder
65   // instance is destroyed. It is OK to pass an end-of-stream DecoderBuffer as
66   // |data|.
67   virtual void Decode(scoped_refptr<media::DecoderBufferBase> data,
68                       DecodeCallback decode_callback) = 0;
69 };
70 
71 }  // namespace media
72 }  // namespace chromecast
73 
74 #endif  // CHROMECAST_MEDIA_API_CAST_AUDIO_DECODER_H_
75