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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
// 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.
//
// THREAD SAFETY
//
// This class is generally not thread safe. Callers should ensure thread safety.
// For instance, the |sink_lock_| in WebAudioSourceProvider synchronizes access
// to this object across the main thread (for WebAudio APIs) and the
// media thread (for HTMLMediaElement APIs).
//
// The one exception is protection for |volume_| via |volume_lock_|. This lock
// prevents races between SetVolume() (called on any thread) and ProvideInput
// (called on audio device thread). See http://crbug.com/588992.
#ifndef MEDIA_BASE_AUDIO_RENDERER_MIXER_INPUT_H_
#define MEDIA_BASE_AUDIO_RENDERER_MIXER_INPUT_H_
#include <string>
#include "base/callback.h"
#include "base/macros.h"
#include "base/synchronization/lock.h"
#include "media/base/audio_converter.h"
#include "media/base/audio_renderer_sink.h"
#include "media/base/output_device.h"
#include "url/origin.h"
namespace media {
class AudioRendererMixer;
class MEDIA_EXPORT AudioRendererMixerInput
: NON_EXPORTED_BASE(public RestartableAudioRendererSink),
NON_EXPORTED_BASE(public OutputDevice),
public AudioConverter::InputCallback {
public:
typedef base::Callback<AudioRendererMixer*(const AudioParameters& params,
const std::string& device_id,
const url::Origin& security_origin,
OutputDeviceStatus* device_status)>
GetMixerCB;
typedef base::Callback<void(const AudioParameters& params,
const std::string& device_id,
const url::Origin& security_origin)>
RemoveMixerCB;
typedef base::Callback<AudioParameters(const std::string& device_id,
const url::Origin& security_origin)>
GetHardwareParamsCB;
AudioRendererMixerInput(const GetMixerCB& get_mixer_cb,
const RemoveMixerCB& remove_mixer_cb,
const GetHardwareParamsCB& get_hardware_params_cb,
const std::string& device_id,
const url::Origin& security_origin);
// RestartableAudioRendererSink implementation.
void Start() override;
void Stop() override;
void Play() override;
void Pause() override;
bool SetVolume(double volume) override;
OutputDevice* GetOutputDevice() override;
void Initialize(const AudioParameters& params,
AudioRendererSink::RenderCallback* renderer) override;
// OutputDevice implementation.
void SwitchOutputDevice(const std::string& device_id,
const url::Origin& security_origin,
const SwitchOutputDeviceCB& callback) override;
AudioParameters GetOutputParameters() override;
OutputDeviceStatus GetDeviceStatus() override;
// Called by AudioRendererMixer when an error occurs.
void OnRenderError();
protected:
~AudioRendererMixerInput() override;
private:
friend class AudioRendererMixerInputTest;
// Protect |volume_|, accessed by separate threads in ProvideInput() and
// SetVolume().
base::Lock volume_lock_;
bool started_;
bool playing_;
double volume_;
// AudioConverter::InputCallback implementation.
double ProvideInput(AudioBus* audio_bus,
base::TimeDelta buffer_delay) override;
// Callbacks provided during construction which allow AudioRendererMixerInput
// to retrieve a mixer during Initialize() and notify when it's done with it.
const GetMixerCB get_mixer_cb_;
const RemoveMixerCB remove_mixer_cb_;
// Callbacks provided during construction which allows AudioRendererMixerInput
// to access hardware output parameters when it is detached from the mixer.
const GetHardwareParamsCB get_hardware_params_cb_;
// AudioParameters received during Initialize().
AudioParameters params_;
// ID of hardware device to use
std::string device_id_;
url::Origin security_origin_;
// AudioRendererMixer provided through |get_mixer_cb_| during Initialize(),
// guaranteed to live (at least) until |remove_mixer_cb_| is called.
AudioRendererMixer* mixer_;
// Source of audio data which is provided to the mixer.
AudioRendererSink::RenderCallback* callback_;
// Error callback for handing to AudioRendererMixer.
const base::Closure error_cb_;
// Pending switch-device callback, in case SwitchOutputDevice() is invoked
// before Start()
SwitchOutputDeviceCB pending_switch_callback_;
std::string pending_switch_device_id_;
url::Origin pending_switch_security_origin_;
DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerInput);
};
} // namespace media
#endif // MEDIA_BASE_AUDIO_RENDERER_MIXER_INPUT_H_
|