| 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 "url/origin.h" | 26 #include "url/origin.h" |
| 27 | 27 |
| 28 namespace media { | 28 namespace media { |
| 29 | 29 |
| 30 class AudioRendererMixerPool; |
| 30 class AudioRendererMixer; | 31 class AudioRendererMixer; |
| 31 | 32 |
| 32 class MEDIA_EXPORT AudioRendererMixerInput | 33 class MEDIA_EXPORT AudioRendererMixerInput |
| 33 : NON_EXPORTED_BASE(public SwitchableAudioRendererSink), | 34 : NON_EXPORTED_BASE(public SwitchableAudioRendererSink), |
| 34 public AudioConverter::InputCallback { | 35 public AudioConverter::InputCallback { |
| 35 public: | 36 public: |
| 36 typedef base::Callback<AudioRendererMixer*(const AudioParameters& params, | 37 AudioRendererMixerInput(AudioRendererMixerPool* mixer_pool, |
| 37 const std::string& device_id, | 38 int owner_id, |
| 38 const url::Origin& security_origin, | |
| 39 OutputDeviceStatus* device_status)> | |
| 40 GetMixerCB; | |
| 41 typedef base::Callback<void(const AudioParameters& params, | |
| 42 const std::string& device_id, | |
| 43 const url::Origin& security_origin)> | |
| 44 RemoveMixerCB; | |
| 45 | |
| 46 AudioRendererMixerInput(const GetMixerCB& get_mixer_cb, | |
| 47 const RemoveMixerCB& remove_mixer_cb, | |
| 48 const std::string& device_id, | 39 const std::string& device_id, |
| 49 const url::Origin& security_origin); | 40 const url::Origin& security_origin); |
| 50 | 41 |
| 51 // SwitchableAudioRendererSink implementation. | 42 // SwitchableAudioRendererSink implementation. |
| 52 void Start() override; | 43 void Start() override; |
| 53 void Stop() override; | 44 void Stop() override; |
| 54 void Play() override; | 45 void Play() override; |
| 55 void Pause() override; | 46 void Pause() override; |
| 56 bool SetVolume(double volume) override; | 47 bool SetVolume(double volume) override; |
| 57 OutputDeviceInfo GetOutputDeviceInfo() override; | 48 OutputDeviceInfo GetOutputDeviceInfo() override; |
| 58 void Initialize(const AudioParameters& params, | 49 void Initialize(const AudioParameters& params, |
| 59 AudioRendererSink::RenderCallback* renderer) override; | 50 AudioRendererSink::RenderCallback* renderer) override; |
| 60 void SwitchOutputDevice(const std::string& device_id, | 51 void SwitchOutputDevice(const std::string& device_id, |
| 61 const url::Origin& security_origin, | 52 const url::Origin& security_origin, |
| 62 const OutputDeviceStatusCB& callback) override; | 53 const OutputDeviceStatusCB& callback) override; |
| 63 | 54 |
| 64 // Called by AudioRendererMixer when an error occurs. | 55 // Called by AudioRendererMixer when an error occurs. |
| 65 void OnRenderError(); | 56 void OnRenderError(); |
| 66 | 57 |
| 67 protected: | 58 protected: |
| 68 ~AudioRendererMixerInput() override; | 59 ~AudioRendererMixerInput() override; |
| 69 | 60 |
| 70 private: | 61 private: |
| 71 friend class AudioRendererMixerInputTest; | 62 friend class AudioRendererMixerInputTest; |
| 72 | 63 |
| 64 // Pool to obtain mixers from / return them to. |
| 65 AudioRendererMixerPool* const mixer_pool_; |
| 66 |
| 73 // Protect |volume_|, accessed by separate threads in ProvideInput() and | 67 // Protect |volume_|, accessed by separate threads in ProvideInput() and |
| 74 // SetVolume(). | 68 // SetVolume(). |
| 75 base::Lock volume_lock_; | 69 base::Lock volume_lock_; |
| 76 | 70 |
| 77 bool started_; | 71 bool started_; |
| 78 bool playing_; | 72 bool playing_; |
| 79 double volume_; | 73 double volume_; |
| 80 | 74 |
| 81 // AudioConverter::InputCallback implementation. | 75 // AudioConverter::InputCallback implementation. |
| 82 double ProvideInput(AudioBus* audio_bus, | 76 double ProvideInput(AudioBus* audio_bus, |
| 83 base::TimeDelta buffer_delay) override; | 77 base::TimeDelta buffer_delay) override; |
| 84 | 78 |
| 85 // Callbacks provided during construction which allow AudioRendererMixerInput | |
| 86 // to retrieve a mixer during Initialize() and notify when it's done with it. | |
| 87 const GetMixerCB get_mixer_cb_; | |
| 88 const RemoveMixerCB remove_mixer_cb_; | |
| 89 | |
| 90 // AudioParameters received during Initialize(). | 79 // AudioParameters received during Initialize(). |
| 91 AudioParameters params_; | 80 AudioParameters params_; |
| 92 | 81 |
| 93 // ID of hardware device to use | 82 const int owner_id_; |
| 94 std::string device_id_; | 83 std::string device_id_; // ID of hardware device to use |
| 95 url::Origin security_origin_; | 84 url::Origin security_origin_; |
| 96 | 85 |
| 97 // AudioRendererMixer provided through |get_mixer_cb_| during Initialize(), | 86 // AudioRendererMixer obtained from mixer pool during Initialize(), |
| 98 // guaranteed to live (at least) until |remove_mixer_cb_| is called. | 87 // guaranteed to live (at least) until it is returned to the pool. |
| 99 AudioRendererMixer* mixer_; | 88 AudioRendererMixer* mixer_; |
| 100 | 89 |
| 101 // Source of audio data which is provided to the mixer. | 90 // Source of audio data which is provided to the mixer. |
| 102 AudioRendererSink::RenderCallback* callback_; | 91 AudioRendererSink::RenderCallback* callback_; |
| 103 | 92 |
| 104 // Error callback for handing to AudioRendererMixer. | 93 // Error callback for handing to AudioRendererMixer. |
| 105 const base::Closure error_cb_; | 94 const base::Closure error_cb_; |
| 106 | 95 |
| 107 // Pending switch-device callback, in case SwitchOutputDevice() is invoked | 96 // Pending switch-device callback, in case SwitchOutputDevice() is invoked |
| 108 // before Start() | 97 // before Start() |
| 109 OutputDeviceStatusCB pending_switch_callback_; | 98 OutputDeviceStatusCB pending_switch_callback_; |
| 110 std::string pending_switch_device_id_; | 99 std::string pending_switch_device_id_; |
| 111 url::Origin pending_switch_security_origin_; | 100 url::Origin pending_switch_security_origin_; |
| 112 | 101 |
| 113 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerInput); | 102 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerInput); |
| 114 }; | 103 }; |
| 115 | 104 |
| 116 } // namespace media | 105 } // namespace media |
| 117 | 106 |
| 118 #endif // MEDIA_BASE_AUDIO_RENDERER_MIXER_INPUT_H_ | 107 #endif // MEDIA_BASE_AUDIO_RENDERER_MIXER_INPUT_H_ |
| OLD | NEW |