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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ 7 8 #include <list> 9 #include <string> 10 11 #include "base/callback.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/synchronization/lock.h" 14 #include "base/threading/thread_checker.h" 15 #include "content/renderer/media/webrtc_audio_device_impl.h" 16 #include "media/audio/audio_input_device.h" 17 #include "media/base/audio_capturer_source.h" 18 19 namespace media { 20 class AudioBus; 21 } 22 23 namespace content { 24 25 class WebRtcLocalAudioRenderer; 26 class WebRtcLocalAudioTrack; 27 28 // This class manages the capture data flow by getting data from its 29 // |source_|, and passing it to its |tracks_|. 30 // It allows clients to inject their own capture data source by calling 31 // SetCapturerSource(). 32 // The threading model for this class is rather complex since it will be 33 // created on the main render thread, captured data is provided on a dedicated 34 // AudioInputDevice thread, and methods can be called either on the Libjingle 35 // thread or on the main render thread but also other client threads 36 // if an alternative AudioCapturerSource has been set. 37 class CONTENT_EXPORT WebRtcAudioCapturer 38 : public base::RefCountedThreadSafe<WebRtcAudioCapturer>, 39 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { 40 public: 41 // Use to construct the audio capturer. 42 // Called on the main render thread. 43 static scoped_refptr<WebRtcAudioCapturer> CreateCapturer(); 44 45 // Creates and configures the default audio capturing source using the 46 // provided audio parameters. |render_view_id| specifies the render view 47 // consuming audio for capture. |session_id| is passed to the browser to 48 // decide which device to use. |device_id| is used to identify which device 49 // the capturer is created for. Called on the main render thread. 50 bool Initialize(int render_view_id, 51 media::ChannelLayout channel_layout, 52 int sample_rate, 53 int session_id, 54 const std::string& device_id); 55 56 // Add a audio track to the sinks of the capturer. 57 // WebRtcAudioDeviceImpl calls this method on the main render thread but 58 // other clients may call it from other threads. The current implementation 59 // does not support multi-thread calling. 60 // Called on the main render thread or libjingle working thread. 61 void AddTrack(WebRtcLocalAudioTrack* track); 62 63 // Remove a audio track from the sinks of the capturer. 64 // Called on the main render thread or libjingle working thread. 65 void RemoveTrack(WebRtcLocalAudioTrack* track); 66 67 // SetCapturerSource() is called if the client on the source side desires to 68 // provide their own captured audio data. Client is responsible for calling 69 // Start() on its own source to have the ball rolling. 70 // Called on the main render thread. 71 void SetCapturerSource( 72 const scoped_refptr<media::AudioCapturerSource>& source, 73 media::ChannelLayout channel_layout, 74 float sample_rate); 75 76 // Volume APIs used by WebRtcAudioDeviceImpl. 77 // Called on the AudioInputDevice audio thread. 78 void SetVolume(int volume); 79 int Volume() const; 80 int MaxVolume() const; 81 82 // Enables or disables the WebRtc AGC control. 83 // Called from a Libjingle working thread. 84 void SetAutomaticGainControl(bool enable); 85 86 bool is_recording() const { return running_; } 87 88 // Audio parameters utilized by the audio capturer. Can be utilized by 89 // a local renderer to set up a renderer using identical parameters as the 90 // capturer. 91 // TODO(phoglund): This accessor is inherently unsafe since the returned 92 // parameters can become outdated at any time. Think over the implications 93 // of this accessor and if we can remove it. 94 media::AudioParameters audio_parameters() const; 95 96 const std::string& device_id() const { return device_id_; } 97 98 protected: 99 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>; 100 WebRtcAudioCapturer(); 101 virtual ~WebRtcAudioCapturer(); 102 103 private: 104 class TrackOwner; 105 typedef std::list<scoped_refptr<TrackOwner> > TrackList; 106 107 // AudioCapturerSource::CaptureCallback implementation. 108 // Called on the AudioInputDevice audio thread. 109 virtual void Capture(media::AudioBus* audio_source, 110 int audio_delay_milliseconds, 111 double volume) OVERRIDE; 112 virtual void OnCaptureError() OVERRIDE; 113 114 // Reconfigures the capturer with a new buffer size and capture parameters. 115 // Must be called without holding the lock. Returns true on success. 116 bool Reconfigure(int sample_rate, media::ChannelLayout channel_layout); 117 118 // Starts recording audio. 119 // Triggered by AddSink() on the main render thread or a Libjingle working 120 // thread. It should NOT be called under |lock_|. 121 void Start(); 122 123 // Stops recording audio. 124 // Triggered by RemoveSink() on the main render thread or a Libjingle working 125 // thread. It should NOT be called under |lock_|. 126 void Stop(); 127 128 129 // Used to DCHECK that we are called on the correct thread. 130 base::ThreadChecker thread_checker_; 131 132 // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|, 133 // |params_|, |buffering_| and |agc_is_enabled_|. 134 mutable base::Lock lock_; 135 136 // A list of audio tracks that the audio data is fed to. 137 TrackList tracks_; 138 139 // The audio data source from the browser process. 140 scoped_refptr<media::AudioCapturerSource> source_; 141 142 // Buffers used for temporary storage during capture callbacks. 143 // Allocated during initialization. 144 class ConfiguredBuffer; 145 scoped_refptr<ConfiguredBuffer> buffer_; 146 bool running_; 147 148 // True when automatic gain control is enabled, false otherwise. 149 bool agc_is_enabled_; 150 151 // The media session ID used to identify which input device to be started by 152 // the browser. 153 int session_id_; 154 155 // The device this capturer is given permission to use. 156 std::string device_id_; 157 158 // Stores latest microphone volume received in a CaptureData() callback. 159 // Range is [0, 255]. 160 int volume_; 161 162 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer); 163 }; 164 165 } // namespace content 166 167 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_ 168