summaryrefslogtreecommitdiffstats
path: root/media/base/audio_renderer_mixer.h
blob: 7bb85af449613754306114c8d7eefa8d60b069c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BASE_AUDIO_RENDERER_MIXER_H_
#define MEDIA_BASE_AUDIO_RENDERER_MIXER_H_

#include <set>

#include "base/synchronization/lock.h"
#include "media/base/audio_renderer_mixer_input.h"
#include "media/base/audio_renderer_sink.h"
#include "media/base/multi_channel_resampler.h"

namespace media {

// Mixes a set of AudioRendererMixerInputs into a single output stream which is
// funneled into a single shared AudioRendererSink; saving a bundle on renderer
// side resources.  Resampling is done post-mixing as it is the most expensive
// process.  If the input sample rate matches the audio hardware sample rate, no
// resampling is done.
class MEDIA_EXPORT AudioRendererMixer
    : NON_EXPORTED_BASE(public AudioRendererSink::RenderCallback) {
 public:
  AudioRendererMixer(const AudioParameters& input_params,
                     const AudioParameters& output_params,
                     const scoped_refptr<AudioRendererSink>& sink);
  virtual ~AudioRendererMixer();

  // Add or remove a mixer input from mixing; called by AudioRendererMixerInput.
  void AddMixerInput(const scoped_refptr<AudioRendererMixerInput>& input);
  void RemoveMixerInput(const scoped_refptr<AudioRendererMixerInput>& input);

 private:
  // AudioRendererSink::RenderCallback implementation.
  virtual int Render(AudioBus* audio_bus,
                     int audio_delay_milliseconds) OVERRIDE;
  virtual void OnRenderError() OVERRIDE;

  // Handles mixing and volume adjustment.  Fully fills |audio_bus| with mixed
  // audio data.  When resampling is necessary, ProvideInput() will be called
  // by MultiChannelResampler when more data is necessary.
  void ProvideInput(AudioBus* audio_bus);

  // Output sink for this mixer.
  scoped_refptr<AudioRendererSink> audio_sink_;

  // Set of mixer inputs to be mixed by this mixer.  Access is thread-safe
  // through |mixer_inputs_lock_|.
  typedef std::set< scoped_refptr<AudioRendererMixerInput> >
      AudioRendererMixerInputSet;
  AudioRendererMixerInputSet mixer_inputs_;
  base::Lock mixer_inputs_lock_;

  // Vector for rendering audio data from each mixer input.
  scoped_ptr<AudioBus> mixer_input_audio_bus_;

  // Handles resampling post-mixing.
  scoped_ptr<MultiChannelResampler> resampler_;

  // The audio delay in milliseconds received by the last Render() call.
  int current_audio_delay_milliseconds_;

  // Ratio of input data to output data.  Used to scale audio delay information.
  double io_ratio_;
  double input_ms_per_frame_;

  DISALLOW_COPY_AND_ASSIGN(AudioRendererMixer);
};

}  // namespace media

#endif  // MEDIA_BASE_AUDIO_RENDERER_MIXER_H_