OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // THREAD SAFETY | 5 // THREAD SAFETY |
6 // | 6 // |
7 // This class is generally not thread safe. Callers should ensure thread safety. | 7 // This class is generally not thread safe. Callers should ensure thread safety. |
8 // For instance, the |sink_lock_| in WebAudioSourceProvider synchronizes access | 8 // For instance, the |sink_lock_| in WebAudioSourceProvider synchronizes access |
9 // to this object across the main thread (for WebAudio APIs) and the | 9 // to this object across the main thread (for WebAudio APIs) and the |
10 // media thread (for HTMLMediaElement APIs). | 10 // media thread (for HTMLMediaElement APIs). |
11 // | 11 // |
12 // The one exception is protection for |volume_| via |volume_lock_|. This lock | 12 // The one exception is protection for |volume_| via |volume_lock_|. This lock |
13 // prevents races between SetVolume() (called on any thread) and ProvideInput | 13 // prevents races between SetVolume() (called on any thread) and ProvideInput |
14 // (called on audio device thread). See http://crbug.com/588992. | 14 // (called on audio device thread). See http://crbug.com/588992. |
15 | 15 |
16 #ifndef MEDIA_BASE_AUDIO_RENDERER_MIXER_INPUT_H_ | 16 #ifndef MEDIA_BASE_AUDIO_RENDERER_MIXER_INPUT_H_ |
17 #define MEDIA_BASE_AUDIO_RENDERER_MIXER_INPUT_H_ | 17 #define MEDIA_BASE_AUDIO_RENDERER_MIXER_INPUT_H_ |
18 | 18 |
19 #include <string> | 19 #include <string> |
20 | 20 |
21 #include "base/callback.h" | 21 #include "base/callback.h" |
22 #include "base/macros.h" | 22 #include "base/macros.h" |
23 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
24 #include "media/base/audio_converter.h" | 24 #include "media/base/audio_converter.h" |
25 #include "media/base/audio_renderer_sink.h" | 25 #include "media/base/audio_renderer_sink.h" |
26 #include "media/base/output_device.h" | |
27 #include "url/origin.h" | 26 #include "url/origin.h" |
28 | 27 |
29 namespace media { | 28 namespace media { |
30 | 29 |
31 class AudioRendererMixer; | 30 class AudioRendererMixer; |
32 | 31 |
33 class MEDIA_EXPORT AudioRendererMixerInput | 32 class MEDIA_EXPORT AudioRendererMixerInput |
34 : NON_EXPORTED_BASE(public RestartableAudioRendererSink), | 33 : NON_EXPORTED_BASE(public SwitchableAudioRendererSink), |
35 NON_EXPORTED_BASE(public OutputDevice), | |
36 public AudioConverter::InputCallback { | 34 public AudioConverter::InputCallback { |
37 public: | 35 public: |
38 typedef base::Callback<AudioRendererMixer*(const AudioParameters& params, | 36 typedef base::Callback<AudioRendererMixer*(const AudioParameters& params, |
39 const std::string& device_id, | 37 const std::string& device_id, |
40 const url::Origin& security_origin, | 38 const url::Origin& security_origin, |
41 OutputDeviceStatus* device_status)> | 39 OutputDeviceStatus* device_status)> |
42 GetMixerCB; | 40 GetMixerCB; |
43 typedef base::Callback<void(const AudioParameters& params, | 41 typedef base::Callback<void(const AudioParameters& params, |
44 const std::string& device_id, | 42 const std::string& device_id, |
45 const url::Origin& security_origin)> | 43 const url::Origin& security_origin)> |
46 RemoveMixerCB; | 44 RemoveMixerCB; |
47 | 45 |
48 typedef base::Callback<AudioParameters(const std::string& device_id, | |
49 const url::Origin& security_origin)> | |
50 GetHardwareParamsCB; | |
51 | |
52 AudioRendererMixerInput(const GetMixerCB& get_mixer_cb, | 46 AudioRendererMixerInput(const GetMixerCB& get_mixer_cb, |
53 const RemoveMixerCB& remove_mixer_cb, | 47 const RemoveMixerCB& remove_mixer_cb, |
54 const GetHardwareParamsCB& get_hardware_params_cb, | |
55 const std::string& device_id, | 48 const std::string& device_id, |
56 const url::Origin& security_origin); | 49 const url::Origin& security_origin); |
57 | 50 |
58 // RestartableAudioRendererSink implementation. | 51 // SwitchableAudioRendererSink implementation. |
59 void Start() override; | 52 void Start() override; |
60 void Stop() override; | 53 void Stop() override; |
61 void Play() override; | 54 void Play() override; |
62 void Pause() override; | 55 void Pause() override; |
63 bool SetVolume(double volume) override; | 56 bool SetVolume(double volume) override; |
64 OutputDevice* GetOutputDevice() override; | 57 OutputDeviceInfo GetOutputDeviceInfo() override; |
65 void Initialize(const AudioParameters& params, | 58 void Initialize(const AudioParameters& params, |
66 AudioRendererSink::RenderCallback* renderer) override; | 59 AudioRendererSink::RenderCallback* renderer) override; |
67 | |
68 // OutputDevice implementation. | |
69 void SwitchOutputDevice(const std::string& device_id, | 60 void SwitchOutputDevice(const std::string& device_id, |
70 const url::Origin& security_origin, | 61 const url::Origin& security_origin, |
71 const SwitchOutputDeviceCB& callback) override; | 62 const OutputDeviceStatusCB& callback) override; |
72 AudioParameters GetOutputParameters() override; | |
73 OutputDeviceStatus GetDeviceStatus() override; | |
74 | 63 |
75 // Called by AudioRendererMixer when an error occurs. | 64 // Called by AudioRendererMixer when an error occurs. |
76 void OnRenderError(); | 65 void OnRenderError(); |
77 | 66 |
78 protected: | 67 protected: |
79 ~AudioRendererMixerInput() override; | 68 ~AudioRendererMixerInput() override; |
80 | 69 |
81 private: | 70 private: |
82 friend class AudioRendererMixerInputTest; | 71 friend class AudioRendererMixerInputTest; |
83 | 72 |
84 // Protect |volume_|, accessed by separate threads in ProvideInput() and | 73 // Protect |volume_|, accessed by separate threads in ProvideInput() and |
85 // SetVolume(). | 74 // SetVolume(). |
86 base::Lock volume_lock_; | 75 base::Lock volume_lock_; |
87 | 76 |
88 bool started_; | 77 bool started_; |
89 bool playing_; | 78 bool playing_; |
90 double volume_; | 79 double volume_; |
91 | 80 |
92 // AudioConverter::InputCallback implementation. | 81 // AudioConverter::InputCallback implementation. |
93 double ProvideInput(AudioBus* audio_bus, | 82 double ProvideInput(AudioBus* audio_bus, |
94 base::TimeDelta buffer_delay) override; | 83 base::TimeDelta buffer_delay) override; |
95 | 84 |
96 // Callbacks provided during construction which allow AudioRendererMixerInput | 85 // Callbacks provided during construction which allow AudioRendererMixerInput |
97 // to retrieve a mixer during Initialize() and notify when it's done with it. | 86 // to retrieve a mixer during Initialize() and notify when it's done with it. |
98 const GetMixerCB get_mixer_cb_; | 87 const GetMixerCB get_mixer_cb_; |
99 const RemoveMixerCB remove_mixer_cb_; | 88 const RemoveMixerCB remove_mixer_cb_; |
100 | 89 |
101 // Callbacks provided during construction which allows AudioRendererMixerInput | |
102 // to access hardware output parameters when it is detached from the mixer. | |
103 const GetHardwareParamsCB get_hardware_params_cb_; | |
104 | |
105 // AudioParameters received during Initialize(). | 90 // AudioParameters received during Initialize(). |
106 AudioParameters params_; | 91 AudioParameters params_; |
107 | 92 |
108 // ID of hardware device to use | 93 // ID of hardware device to use |
109 std::string device_id_; | 94 std::string device_id_; |
110 url::Origin security_origin_; | 95 url::Origin security_origin_; |
111 | 96 |
112 // AudioRendererMixer provided through |get_mixer_cb_| during Initialize(), | 97 // AudioRendererMixer provided through |get_mixer_cb_| during Initialize(), |
113 // guaranteed to live (at least) until |remove_mixer_cb_| is called. | 98 // guaranteed to live (at least) until |remove_mixer_cb_| is called. |
114 AudioRendererMixer* mixer_; | 99 AudioRendererMixer* mixer_; |
115 | 100 |
116 // Source of audio data which is provided to the mixer. | 101 // Source of audio data which is provided to the mixer. |
117 AudioRendererSink::RenderCallback* callback_; | 102 AudioRendererSink::RenderCallback* callback_; |
118 | 103 |
119 // Error callback for handing to AudioRendererMixer. | 104 // Error callback for handing to AudioRendererMixer. |
120 const base::Closure error_cb_; | 105 const base::Closure error_cb_; |
121 | 106 |
122 // Pending switch-device callback, in case SwitchOutputDevice() is invoked | 107 // Pending switch-device callback, in case SwitchOutputDevice() is invoked |
123 // before Start() | 108 // before Start() |
124 SwitchOutputDeviceCB pending_switch_callback_; | 109 OutputDeviceStatusCB pending_switch_callback_; |
125 std::string pending_switch_device_id_; | 110 std::string pending_switch_device_id_; |
126 url::Origin pending_switch_security_origin_; | 111 url::Origin pending_switch_security_origin_; |
127 | 112 |
128 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerInput); | 113 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerInput); |
129 }; | 114 }; |
130 | 115 |
131 } // namespace media | 116 } // namespace media |
132 | 117 |
133 #endif // MEDIA_BASE_AUDIO_RENDERER_MIXER_INPUT_H_ | 118 #endif // MEDIA_BASE_AUDIO_RENDERER_MIXER_INPUT_H_ |
OLD | NEW |