1 // Copyright 2013 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.
5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_PROCESSOR_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_PROCESSOR_H_
8 #include "base/atomicops.h"
9 #include "base/platform_file.h"
10 #include "base/synchronization/lock.h"
11 #include "base/threading/thread_checker.h"
12 #include "base/time/time.h"
13 #include "content/common/content_export.h"
14 #include "content/public/common/media_stream_request.h"
15 #include "content/renderer/media/webrtc_audio_device_impl.h"
16 #include "media/base/audio_converter.h"
17 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
18 #include "third_party/webrtc/modules/audio_processing/include/audio_processing.h"
19 #include "third_party/webrtc/modules/interface/module_common_types.h"
22 class WebMediaConstraints;
28 class AudioParameters;
33 class TypingDetection;
38 class RTCMediaConstraints;
40 using webrtc::AudioProcessorInterface;
42 // This class owns an object of webrtc::AudioProcessing which contains signal
43 // processing components like AGC, AEC and NS. It enables the components based
44 // on the getUserMedia constraints, processes the data and outputs it in a unit
45 // of 10 ms data chunk.
46 class CONTENT_EXPORT MediaStreamAudioProcessor :
47 NON_EXPORTED_BASE(public WebRtcPlayoutDataSource::Sink),
48 NON_EXPORTED_BASE(public AudioProcessorInterface) {
50 // Returns true if |kEnableAudioTrackProcessing| is on or if the
51 // |MediaStreamAudioTrackProcessing| finch experiment is enabled.
52 static bool IsAudioTrackProcessingEnabled();
54 // |playout_data_source| is used to register this class as a sink to the
55 // WebRtc playout data for processing AEC. If clients do not enable AEC,
56 // |playout_data_source| won't be used.
57 MediaStreamAudioProcessor(const blink::WebMediaConstraints& constraints,
60 WebRtcPlayoutDataSource* playout_data_source);
62 // Called when format of the capture data has changed.
63 // Called on the main render thread. The caller is responsible for stopping
64 // the capture thread before calling this method.
65 // After this method, the capture thread will be changed to a new capture
67 void OnCaptureFormatChanged(const media::AudioParameters& source_params);
69 // Pushes capture data in |audio_source| to the internal FIFO.
70 // Called on the capture audio thread.
71 void PushCaptureData(media::AudioBus* audio_source);
73 // Processes a block of 10 ms data from the internal FIFO and outputs it via
74 // |out|. |out| is the address of the pointer that will be pointed to
75 // the post-processed data if the method is returning a true. The lifetime
76 // of the data represeted by |out| is guaranteed to outlive the method call.
77 // That also says *|out| won't change until this method is called again.
78 // |new_volume| receives the new microphone volume from the AGC.
79 // The new microphoen volume range is [0, 255], and the value will be 0 if
80 // the microphone volume should not be adjusted.
81 // Returns true if the internal FIFO has at least 10 ms data for processing,
83 // |capture_delay|, |volume| and |key_pressed| will be passed to
84 // webrtc::AudioProcessing to help processing the data.
85 // Called on the capture audio thread.
86 bool ProcessAndConsumeData(base::TimeDelta capture_delay,
92 // The audio format of the input to the processor.
93 const media::AudioParameters& InputFormat() const;
95 // The audio format of the output from the processor.
96 const media::AudioParameters& OutputFormat() const;
98 // Accessor to check if the audio processing is enabled or not.
99 bool has_audio_processing() const { return audio_processing_ != NULL; }
101 // Starts/Stops the Aec dump on the |audio_processing_|.
102 // Called on the main render thread.
103 // This method takes the ownership of |aec_dump_file|.
104 void StartAecDump(const base::PlatformFile& aec_dump_file);
108 friend class base::RefCountedThreadSafe<MediaStreamAudioProcessor>;
109 virtual ~MediaStreamAudioProcessor();
112 friend class MediaStreamAudioProcessorTest;
114 class MediaStreamAudioConverter;
116 // WebRtcPlayoutDataSource::Sink implementation.
117 virtual void OnPlayoutData(media::AudioBus* audio_bus,
119 int audio_delay_milliseconds) OVERRIDE;
120 virtual void OnPlayoutDataSourceChanged() OVERRIDE;
122 // webrtc::AudioProcessorInterface implementation.
123 // This method is called on the libjingle thread.
124 virtual void GetStats(AudioProcessorStats* stats) OVERRIDE;
126 // Helper to initialize the WebRtc AudioProcessing.
127 void InitializeAudioProcessingModule(
128 const blink::WebMediaConstraints& constraints, int effects,
129 MediaStreamType type);
131 // Helper to initialize the capture converter.
132 void InitializeCaptureConverter(const media::AudioParameters& source_params);
134 // Helper to initialize the render converter.
135 void InitializeRenderConverterIfNeeded(int sample_rate,
136 int number_of_channels,
137 int frames_per_buffer);
139 // Called by ProcessAndConsumeData().
140 // Returns the new microphone volume in the range of |0, 255].
141 // When the volume does not need to be updated, it returns 0.
142 int ProcessData(webrtc::AudioFrame* audio_frame,
143 base::TimeDelta capture_delay,
147 // Called when the processor is going away.
148 void StopAudioProcessing();
150 // Cached value for the render delay latency. This member is accessed by
151 // both the capture audio thread and the render audio thread.
152 base::subtle::Atomic32 render_delay_ms_;
154 // webrtc::AudioProcessing module which does AEC, AGC, NS, HighPass filter,
156 scoped_ptr<webrtc::AudioProcessing> audio_processing_;
158 // Converter used for the down-mixing and resampling of the capture data.
159 scoped_ptr<MediaStreamAudioConverter> capture_converter_;
161 // AudioFrame used to hold the output of |capture_converter_|.
162 webrtc::AudioFrame capture_frame_;
164 // Converter used for the down-mixing and resampling of the render data when
165 // the AEC is enabled.
166 scoped_ptr<MediaStreamAudioConverter> render_converter_;
168 // AudioFrame used to hold the output of |render_converter_|.
169 webrtc::AudioFrame render_frame_;
171 // Data bus to help converting interleaved data to an AudioBus.
172 scoped_ptr<media::AudioBus> render_data_bus_;
174 // Raw pointer to the WebRtcPlayoutDataSource, which is valid for the
175 // lifetime of RenderThread.
176 WebRtcPlayoutDataSource* const playout_data_source_;
178 // Used to DCHECK that the destructor is called on the main render thread.
179 base::ThreadChecker main_thread_checker_;
181 // Used to DCHECK that some methods are called on the capture audio thread.
182 base::ThreadChecker capture_thread_checker_;
184 // Used to DCHECK that PushRenderData() is called on the render audio thread.
185 base::ThreadChecker render_thread_checker_;
187 // Flag to enable the stereo channels mirroring.
188 bool audio_mirroring_;
190 // Used by the typing detection.
191 scoped_ptr<webrtc::TypingDetection> typing_detector_;
193 // This flag is used to show the result of typing detection.
194 // It can be accessed by the capture audio thread and by the libjingle thread
195 // which calls GetStats().
196 base::subtle::Atomic32 typing_detected_;
199 } // namespace content
201 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_PROCESSOR_H_