blob: e85ea043bf039560ddd29932969741c37b78297b [file] [log] [blame]
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001// 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
19namespace media {
20class AudioBus;
21}
22
23namespace content {
24
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000025class WebRtcLocalAudioRenderer;
Ben Murdochbb1529c2013-08-08 10:24:53 +010026class WebRtcLocalAudioTrack;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000027
28// This class manages the capture data flow by getting data from its
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010029// |source_|, and passing it to its |tracks_|.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000030// 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.
37class 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
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010046 // provided audio parameters. |render_view_id| specifies the render view
47 // consuming audio for capture. |session_id| is passed to the browser to
Ben Murdochbb1529c2013-08-08 10:24:53 +010048 // 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.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010050 bool Initialize(int render_view_id,
51 media::ChannelLayout channel_layout,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000052 int sample_rate,
Ben Murdochbb1529c2013-08-08 10:24:53 +010053 int session_id,
54 const std::string& device_id);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010055
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010056 // Add a audio track to the sinks of the capturer.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000057 // 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.
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010060 // Called on the main render thread or libjingle working thread.
Ben Murdochbb1529c2013-08-08 10:24:53 +010061 void AddTrack(WebRtcLocalAudioTrack* track);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000062
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010063 // Remove a audio track from the sinks of the capturer.
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010064 // Called on the main render thread or libjingle working thread.
Ben Murdochbb1529c2013-08-08 10:24:53 +010065 void RemoveTrack(WebRtcLocalAudioTrack* track);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000066
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
Ben Murdochbb1529c2013-08-08 10:24:53 +010076 // Volume APIs used by WebRtcAudioDeviceImpl.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000077 // Called on the AudioInputDevice audio thread.
Ben Murdochbb1529c2013-08-08 10:24:53 +010078 void SetVolume(int volume);
79 int Volume() const;
80 int MaxVolume() const;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000081
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
Ben Murdochbb1529c2013-08-08 10:24:53 +010096 const std::string& device_id() const { return device_id_; }
97
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010098 protected:
99 friend class base::RefCountedThreadSafe<WebRtcAudioCapturer>;
100 WebRtcAudioCapturer();
101 virtual ~WebRtcAudioCapturer();
102
103 private:
Ben Murdochbb1529c2013-08-08 10:24:53 +0100104 class TrackOwner;
105 typedef std::list<scoped_refptr<TrackOwner> > TrackList;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100106
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000107 // 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
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000114 // 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
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100118 // 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
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000129 // Used to DCHECK that we are called on the correct thread.
130 base::ThreadChecker thread_checker_;
131
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100132 // Protects |source_|, |audio_tracks_|, |running_|, |loopback_fifo_|,
133 // |params_|, |buffering_| and |agc_is_enabled_|.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000134 mutable base::Lock lock_;
135
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100136 // A list of audio tracks that the audio data is fed to.
137 TrackList tracks_;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000138
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000139 // 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
Ben Murdochbb1529c2013-08-08 10:24:53 +0100151 // The media session ID used to identify which input device to be started by
152 // the browser.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000153 int session_id_;
154
Ben Murdochbb1529c2013-08-08 10:24:53 +0100155 // 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
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000162 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioCapturer);
163};
164
165} // namespace content
166
167#endif // CONTENT_RENDERER_MEDIA_WEBRTC_AUDIO_CAPTURER_H_