1effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// found in the LICENSE file.
4effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
5effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#ifndef MEDIA_BASE_AUDIO_BUFFER_CONVERTER
6effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define MEDIA_BASE_AUDIO_BUFFER_CONVERTER
7effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
8effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include <deque>
9effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
10effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "base/memory/ref_counted.h"
11effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "base/memory/scoped_ptr.h"
12effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "base/time/time.h"
13effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "media/audio/audio_parameters.h"
14effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "media/base/audio_converter.h"
15effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "media/base/audio_timestamp_helper.h"
16effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "media/base/media_export.h"
17effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
18effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochnamespace media {
19effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
20effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochclass AudioBuffer;
21effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochclass AudioBus;
22effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
23effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Takes AudioBuffers in any format and uses an AudioConverter to convert them
24effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// to a common format (usually the hardware output format).
25effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochclass MEDIA_EXPORT AudioBufferConverter : public AudioConverter::InputCallback {
26effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch public:
27effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  explicit AudioBufferConverter(const AudioParameters& output_params);
28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual ~AudioBufferConverter();
29effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
30effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void AddInput(const scoped_refptr<AudioBuffer>& buffer);
31effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
32effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Is an output buffer available via GetNextBuffer()?
33effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  bool HasNextBuffer();
34effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
35effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // This should only be called this is HasNextBuffer() returns true.
36effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  scoped_refptr<AudioBuffer> GetNextBuffer();
37effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
38effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Reset internal state.
39effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void Reset();
40effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
41effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Reset internal timestamp state. Upon the next AddInput() call, our base
42effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // timestamp will be set to match the input buffer.
43effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void ResetTimestampState();
44effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  int input_buffer_size_for_testing() const {
460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return input_params_.frames_per_buffer();
470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  int input_frames_left_for_testing() const {
490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return input_frames_;
500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  }
510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
52effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch private:
53effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Callback to provide data to the AudioConverter
54effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual double ProvideInput(AudioBus* audio_bus,
55effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                              base::TimeDelta buffer_delay) OVERRIDE;
56effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
57effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Reset the converter in response to a configuration change.
58effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void ResetConverter(const scoped_refptr<AudioBuffer>& input_buffer);
59effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Perform conversion if we have enough data.
61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void ConvertIfPossible();
62effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Flush remaining output
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void Flush();
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // The output parameters.
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  AudioParameters output_params_;
68effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // The current input parameters (we cache these to detect configuration
70effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // changes, so we know when to reset the AudioConverter).
71effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  AudioParameters input_params_;
72effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
73effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  typedef std::deque<scoped_refptr<AudioBuffer> > BufferQueue;
74effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
75effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Queued up inputs (there will never be all that much data stored here, as
76effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // soon as there's enough here to produce an output buffer we will do so).
77effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  BufferQueue queued_inputs_;
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
79effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Offset into the front element of |queued_inputs_|. A ProvideInput() call
80effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // doesn't necessarily always consume an entire buffer.
81effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  int last_input_buffer_offset_;
82effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
83effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Buffer of output frames, to be returned by GetNextBuffer().
84effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  BufferQueue queued_outputs_;
85effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
86effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // How many frames of input we have in |queued_inputs_|.
87effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  int input_frames_;
88effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
89effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Input frames in the AudioConverter's internal buffers.
90effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  double buffered_input_frames_;
91effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
92effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Ratio of sample rates, in/out.
93effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  double io_sample_rate_ratio_;
94effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
95effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Computes timestamps in terms of the output sample rate.
96effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  AudioTimestampHelper timestamp_helper_;
97effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
98effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Are we flushing everything, without regard for providing AudioConverter
99effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // full AudioBuses in ProvideInput()?
100effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  bool is_flushing_;
101effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
102effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // The AudioConverter which does the real work here.
103effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  scoped_ptr<AudioConverter> audio_converter_;
104effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch};
105effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
106effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}  // namespace media
107effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
108effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif  // MEDIA_BASE_AUDIO_BUFFER_CONVERTER
109