OLD | NEW |
| (Empty) |
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_PROCESSED_LOCAL_AUDIO_SOURCE_H_ | |
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_PROCESSED_LOCAL_AUDIO_SOURCE_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/synchronization/lock.h" | |
11 #include "content/common/media/media_stream_options.h" | |
12 #include "content/renderer/media/media_stream_audio_level_calculator.h" | |
13 #include "content/renderer/media/media_stream_audio_processor.h" | |
14 #include "content/renderer/media/media_stream_audio_source.h" | |
15 #include "media/base/audio_capturer_source.h" | |
16 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | |
17 | |
18 namespace media { | |
19 class AudioBus; | |
20 } | |
21 | |
22 namespace webrtc { | |
23 class AudioSourceInterface; | |
24 } | |
25 | |
26 namespace content { | |
27 | |
28 class PeerConnectionDependencyFactory; | |
29 | |
30 // Represents a local source of audio data that is routed through the WebRTC | |
31 // audio pipeline for post-processing (e.g., for echo cancellation during a | |
32 // video conferencing call). Owns a media::AudioCapturerSource and the | |
33 // MediaStreamProcessor that modifies its audio. Modified audio is delivered to | |
34 // one or more MediaStreamAudioTracks. | |
35 class CONTENT_EXPORT ProcessedLocalAudioSource final | |
36 : NON_EXPORTED_BASE(public MediaStreamAudioSource), | |
37 NON_EXPORTED_BASE(public media::AudioCapturerSource::CaptureCallback) { | |
38 public: | |
39 // |consumer_render_frame_id| references the RenderFrame that will consume the | |
40 // audio data. Audio parameters and (optionally) a pre-existing audio session | |
41 // ID are derived from |device_info|. |factory| must outlive this instance. | |
42 ProcessedLocalAudioSource(int consumer_render_frame_id, | |
43 const StreamDeviceInfo& device_info, | |
44 PeerConnectionDependencyFactory* factory); | |
45 | |
46 ~ProcessedLocalAudioSource() final; | |
47 | |
48 // If |source| is an instance of ProcessedLocalAudioSource, return a | |
49 // type-casted pointer to it. Otherwise, return null. | |
50 static ProcessedLocalAudioSource* From(MediaStreamAudioSource* source); | |
51 | |
52 // Non-browser unit tests cannot provide RenderFrame implementations at | |
53 // run-time. This is used to skip the otherwise mandatory check for a valid | |
54 // render frame ID when the source is started. | |
55 void SetAllowInvalidRenderFrameIdForTesting(bool allowed) { | |
56 allow_invalid_render_frame_id_for_testing_ = allowed; | |
57 } | |
58 | |
59 // Gets/Sets source constraints. Using this is optional, but must be done | |
60 // before the first call to ConnectToTrack(). | |
61 const blink::WebMediaConstraints& source_constraints() const { | |
62 return constraints_; | |
63 } | |
64 void SetSourceConstraints(const blink::WebMediaConstraints& constraints); | |
65 | |
66 // The following accessors are not valid until after the source is started | |
67 // (when the first track is connected). | |
68 webrtc::AudioSourceInterface* rtc_source() const { return rtc_source_.get(); } | |
69 const scoped_refptr<MediaStreamAudioProcessor>& audio_processor() const { | |
70 return audio_processor_; | |
71 } | |
72 const scoped_refptr<MediaStreamAudioLevelCalculator::Level>& audio_level() | |
73 const { | |
74 return level_calculator_.level(); | |
75 } | |
76 | |
77 // Thread-safe volume accessors used by WebRtcAudioDeviceImpl. | |
78 void SetVolume(int volume); | |
79 int Volume() const; | |
80 int MaxVolume() const; | |
81 | |
82 // Audio parameters utilized by the source of the audio capturer. | |
83 // TODO(phoglund): Think over the implications of this accessor and if we can | |
84 // remove it. | |
85 media::AudioParameters GetInputFormat() const; | |
86 | |
87 protected: | |
88 // MediaStreamAudioSource implementation. | |
89 void* GetClassIdentifier() const final; | |
90 bool EnsureSourceIsStarted() final; | |
91 void EnsureSourceIsStopped() final; | |
92 | |
93 // AudioCapturerSource::CaptureCallback implementation. | |
94 // Called on the AudioCapturerSource audio thread. | |
95 void Capture(const media::AudioBus* audio_source, | |
96 int audio_delay_milliseconds, | |
97 double volume, | |
98 bool key_pressed) override; | |
99 void OnCaptureError(const std::string& message) override; | |
100 | |
101 private: | |
102 // Helper function to get the source buffer size based on whether audio | |
103 // processing will take place. | |
104 int GetBufferSize(int sample_rate) const; | |
105 | |
106 // The RenderFrame that will consume the audio data. Used when creating | |
107 // AudioCapturerSources. | |
108 const int consumer_render_frame_id_; | |
109 | |
110 PeerConnectionDependencyFactory* const pc_factory_; | |
111 | |
112 // In debug builds, check that all methods that could cause object graph | |
113 // or data flow changes are being called on the main thread. | |
114 base::ThreadChecker thread_checker_; | |
115 | |
116 // Cached audio constraints for the capturer. | |
117 blink::WebMediaConstraints constraints_; | |
118 | |
119 // Audio processor doing processing like FIFO, AGC, AEC and NS. Its output | |
120 // data is in a unit of 10 ms data chunk. | |
121 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; | |
122 | |
123 // The device created by the AudioDeviceFactory in EnsureSourceIsStarted(). | |
124 scoped_refptr<media::AudioCapturerSource> source_; | |
125 | |
126 // Holder for WebRTC audio pipeline objects. Created in | |
127 // EnsureSourceIsStarted(). | |
128 scoped_refptr<webrtc::AudioSourceInterface> rtc_source_; | |
129 | |
130 // Protects data elements from concurrent access when using the volume | |
131 // methods. | |
132 mutable base::Lock volume_lock_; | |
133 | |
134 // Stores latest microphone volume received in a CaptureData() callback. | |
135 // Range is [0, 255]. | |
136 int volume_; | |
137 | |
138 // Used to calculate the signal level that shows in the UI. | |
139 MediaStreamAudioLevelCalculator level_calculator_; | |
140 | |
141 bool allow_invalid_render_frame_id_for_testing_; | |
142 | |
143 DISALLOW_COPY_AND_ASSIGN(ProcessedLocalAudioSource); | |
144 }; | |
145 | |
146 } // namespace content | |
147 | |
148 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_PROCESSED_LOCAL_AUDIO_SOURCE_H_ | |
OLD | NEW |