| 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 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 14 #include "base/threading/thread_checker.h" |
| 14 #include "content/renderer/media/webrtc_audio_device_impl.h" | 15 #include "content/renderer/media/webrtc_audio_device_impl.h" |
| 15 #include "content/renderer/media/webrtc_local_audio_renderer.h" | 16 #include "content/renderer/media/webrtc_local_audio_renderer.h" |
| 16 #include "media/audio/audio_input_device.h" | 17 #include "media/audio/audio_input_device.h" |
| 17 #include "media/base/audio_capturer_source.h" | 18 #include "media/base/audio_capturer_source.h" |
| 18 #include "media/base/audio_fifo.h" | 19 #include "media/base/audio_fifo.h" |
| 19 | 20 |
| 20 namespace media { | 21 namespace media { |
| 21 class AudioBus; | 22 class AudioBus; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace content { | 25 namespace content { |
| 25 | 26 |
| 26 class WebRtcAudioCapturerSink; | 27 class WebRtcAudioCapturerSink; |
| 27 class WebRtcLocalAudioRenderer; | 28 class WebRtcLocalAudioRenderer; |
| 28 | 29 |
| 29 // This class manages the capture data flow by getting data from its | 30 // This class manages the capture data flow by getting data from its |
| 30 // |source_|, and passing it to its |sink_|. | 31 // |source_|, and passing it to its |sink_|. |
| 31 // It allows clients to inject their own capture data source by calling | 32 // It allows clients to inject their own capture data source by calling |
| 32 // SetCapturerSource(). It is also possible to enable a local sink and | 33 // SetCapturerSource(). It is also possible to enable a local sink and |
| 33 // register a callback which the sink can call when it wants to read captured | 34 // register a callback which the sink can call when it wants to read captured |
| 34 // data cached in a FIFO for local loopback rendering. | 35 // data cached in a FIFO for local loopback rendering. |
| 35 // The threading model for this class is rather messy since it will be | 36 // The threading model for this class is rather complex since it will be |
| 36 // created on a Libjingle thread, captured data is provided on a dedicated | 37 // created on the main render thread, captured data is provided on a dedicated |
| 37 // AudioInputDevice thread, and methods can be called either on the Libjingle | 38 // AudioInputDevice thread, and methods can be called either on the Libjingle |
| 38 // thread or on the main render thread but also other client threads | 39 // thread or on the main render thread but also other client threads |
| 39 // if an alternative AudioCapturerSource has been set. In addition, the | 40 // if an alternative AudioCapturerSource has been set. In addition, the |
| 40 // AudioCapturerSource::CaptureEventHandler methods are called on the IO thread | 41 // AudioCapturerSource::CaptureEventHandler methods are called on the IO thread |
| 41 // and requests for data to render is done on the AudioOutputDevice thread. | 42 // and requests for data to render is done on the AudioOutputDevice thread. |
| 42 class WebRtcAudioCapturer | 43 class CONTENT_EXPORT WebRtcAudioCapturer |
| 43 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, | 44 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, |
| 44 public media::AudioCapturerSource::CaptureCallback, | 45 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback), |
| 45 public media::AudioCapturerSource::CaptureEventHandler, | 46 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureEventHandler), |
| 46 public content::WebRtcLocalAudioRenderer::LocalRenderCallback { | 47 NON_EXPORTED_BASE( |
| 48 public content::WebRtcLocalAudioRenderer::LocalRenderCallback) { |
| 47 public: | 49 public: |
| 48 // Use to construct the audio capturer. | 50 // Use to construct the audio capturer. |
| 51 // Called on the main render thread. |
| 49 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); | 52 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); |
| 50 | 53 |
| 54 // Creates and configures the default audio capturing source using the |
| 55 // provided audio parameters. |
| 56 // Called on the main render thread. |
| 57 bool Initialize(media::ChannelLayout channel_layout, int sample_rate); |
| 58 |
| 51 // Called by the client on the sink side to add a sink. | 59 // Called by the client on the sink side to add a sink. |
| 60 // WebRtcAudioDeviceImpl calls this method on the main render thread but |
| 61 // other clients may call it from other threads. The current implementation |
| 62 // does not support multi-thread calling. |
| 63 // TODO(henrika): add lock if we extend number of supported sinks. |
| 64 // Called on the main render thread. |
| 52 void AddCapturerSink(WebRtcAudioCapturerSink* sink); | 65 void AddCapturerSink(WebRtcAudioCapturerSink* sink); |
| 53 | 66 |
| 54 // Called by the client on the sink side to remove a sink. | 67 // Called by the client on the sink side to remove a sink. |
| 68 // Called on the main render thread. |
| 69 // TODO(henrika): add lock if we extend number of supported sinks. |
| 70 // Called on the main render thread. |
| 55 void RemoveCapturerSink(WebRtcAudioCapturerSink* sink); | 71 void RemoveCapturerSink(WebRtcAudioCapturerSink* sink); |
| 56 | 72 |
| 57 // SetCapturerSource() is called if the client on the source side desires to | 73 // SetCapturerSource() is called if the client on the source side desires to |
| 58 // provide their own captured audio data. Client is responsible for calling | 74 // provide their own captured audio data. Client is responsible for calling |
| 59 // Start() on its own source to have the ball rolling. | 75 // Start() on its own source to have the ball rolling. |
| 76 // Called on the main render thread. |
| 60 void SetCapturerSource( | 77 void SetCapturerSource( |
| 61 const scoped_refptr<media::AudioCapturerSource>& source, | 78 const scoped_refptr<media::AudioCapturerSource>& source, |
| 62 media::ChannelLayout channel_layout, | 79 media::ChannelLayout channel_layout, |
| 63 float sample_rate); | 80 float sample_rate); |
| 64 | 81 |
| 65 // The |on_device_stopped_cb| callback will be called in OnDeviceStopped(). | 82 // The |on_device_stopped_cb| callback will be called in OnDeviceStopped(). |
| 83 // Called on the main render thread. |
| 66 void SetStopCallback(const base::Closure& on_device_stopped_cb); | 84 void SetStopCallback(const base::Closure& on_device_stopped_cb); |
| 67 | 85 |
| 68 // Informs this class that a local sink shall be used in addition to the | 86 // Informs this class that a local sink shall be used in addition to the |
| 69 // registered WebRtcAudioCapturerSink sink(s). The capturer will enter a | 87 // registered WebRtcAudioCapturerSink sink(s). The capturer will enter a |
| 70 // buffering mode and store all incoming audio frames in a local FIFO. | 88 // buffering mode and store all incoming audio frames in a local FIFO. |
| 71 // The renderer will read data from this buffer using the ProvideInput() | 89 // The renderer will read data from this buffer using the ProvideInput() |
| 72 // method. Called on the main render thread. | 90 // method. |
| 91 // Called on the main render thread. |
| 73 void PrepareLoopback(); | 92 void PrepareLoopback(); |
| 74 | 93 |
| 75 // Cancels loopback mode and stops buffering local copies of captured | 94 // Cancels loopback mode and stops buffering local copies of captured |
| 76 // data in the FIFO. | 95 // data in the FIFO. |
| 77 // Called on the main render thread. | 96 // Called on the main render thread. |
| 78 void CancelLoopback(); | 97 void CancelLoopback(); |
| 79 | 98 |
| 80 // Pauses buffering of captured data. Does only have an effect if a local | 99 // Pauses buffering of captured data. Does only have an effect if a local |
| 81 // sink is used. | 100 // sink is used. |
| 82 // Called on the main render thread. | 101 // Called on the main render thread. |
| 83 void PauseBuffering(); | 102 void PauseBuffering(); |
| 84 | 103 |
| 85 // Resumes buffering of captured data. Does only have an effect if a local | 104 // Resumes buffering of captured data. Does only have an effect if a local |
| 86 // sink is used. | 105 // sink is used. |
| 87 // Called on the main render thread. | 106 // Called on the main render thread. |
| 88 void ResumeBuffering(); | 107 void ResumeBuffering(); |
| 89 | 108 |
| 90 // Starts recording audio. | 109 // Starts recording audio. |
| 110 // Called on the main render thread or a Libjingle working thread. |
| 91 void Start(); | 111 void Start(); |
| 92 | 112 |
| 93 // Stops recording audio. | 113 // Stops recording audio. |
| 114 // Called on the main render thread or a Libjingle working thread. |
| 94 void Stop(); | 115 void Stop(); |
| 95 | 116 |
| 96 // Sets the microphone volume. | 117 // Sets the microphone volume. |
| 118 // Called on the AudioInputDevice audio thread. |
| 97 void SetVolume(double volume); | 119 void SetVolume(double volume); |
| 98 | 120 |
| 99 // Specifies the |session_id| to query which device to use. | 121 // Specifies the |session_id| to query which device to use. |
| 122 // Called on the main render thread. |
| 100 void SetDevice(int session_id); | 123 void SetDevice(int session_id); |
| 101 | 124 |
| 102 // Enables or disables the WebRtc AGC control. | 125 // Enables or disables the WebRtc AGC control. |
| 126 // Called from a Libjingle working thread. |
| 103 void SetAutomaticGainControl(bool enable); | 127 void SetAutomaticGainControl(bool enable); |
| 104 | 128 |
| 105 bool is_recording() const { return running_; } | 129 bool is_recording() const { return running_; } |
| 106 | 130 |
| 107 // Returns true if a local renderer has called PrepareLoopback() and it can | 131 // Returns true if a local renderer has called PrepareLoopback() and it can |
| 108 // be utilized to prevent more than one local renderer. | 132 // be utilized to prevent more than one local renderer. |
| 133 // Called on the main render thread. |
| 109 bool IsInLoopbackMode(); | 134 bool IsInLoopbackMode(); |
| 110 | 135 |
| 111 // Audio parameters utilized by the audio capturer. Can be utilized by | 136 // Audio parameters utilized by the audio capturer. Can be utilized by |
| 112 // a local renderer to set up a renderer using identical parameters as the | 137 // a local renderer to set up a renderer using identical parameters as the |
| 113 // capturer. | 138 // capturer. |
| 114 const media::AudioParameters& audio_parameter() const { return params_; } | 139 const media::AudioParameters& audio_parameter() const { return params_; } |
| 115 | 140 |
| 116 // AudioCapturerSource::CaptureCallback implementation. | 141 // AudioCapturerSource::CaptureCallback implementation. |
| 117 // Called on the AudioInputDevice audio thread. | 142 // Called on the AudioInputDevice audio thread. |
| 118 virtual void Capture(media::AudioBus* audio_source, | 143 virtual void Capture(media::AudioBus* audio_source, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 133 | 158 |
| 134 protected: | 159 protected: |
| 135 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; | 160 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; |
| 136 virtual ~WebRtcAudioCapturer(); | 161 virtual ~WebRtcAudioCapturer(); |
| 137 | 162 |
| 138 private: | 163 private: |
| 139 typedef std::list<WebRtcAudioCapturerSink*> SinkList; | 164 typedef std::list<WebRtcAudioCapturerSink*> SinkList; |
| 140 | 165 |
| 141 WebRtcAudioCapturer(); | 166 WebRtcAudioCapturer(); |
| 142 | 167 |
| 143 // Initializes the capturer, called right after the object is created. | 168 // Used to DCHECK that we are called on the correct thread. |
| 144 // Returns false if the initialization fails. | 169 base::ThreadChecker thread_checker_; |
| 145 bool Initialize(); | |
| 146 | 170 |
| 147 // Protects |source_|, |sinks_|, |running_|, |on_device_stopped_cb_|, | 171 // Protects |source_|, |sinks_|, |running_|, |on_device_stopped_cb_|, |
| 148 // |loopback_fifo_| and |buffering_|. | 172 // |loopback_fifo_| and |buffering_|. |
| 149 base::Lock lock_; | 173 base::Lock lock_; |
| 150 | 174 |
| 151 // A list of sinks that the audio data is fed to. | 175 // A list of sinks that the audio data is fed to. |
| 152 SinkList sinks_; | 176 SinkList sinks_; |
| 153 | 177 |
| 154 // The audio data source from the browser process. | 178 // The audio data source from the browser process. |
| 155 scoped_refptr<media::AudioCapturerSource> source_; | 179 scoped_refptr<media::AudioCapturerSource> source_; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 173 | 197 |
| 174 // True when FIFO is utilized, false otherwise. | 198 // True when FIFO is utilized, false otherwise. |
| 175 bool buffering_; | 199 bool buffering_; |
| 176 | 200 |
| 177 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); | 201 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); |
| 178 }; | 202 }; |
| 179 | 203 |
| 180 } // namespace content | 204 } // namespace content |
| 181 | 205 |
| 182 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ | 206 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ |
| OLD | NEW |