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/synchronization/lock.h"
10 #include "base/threading/thread_checker.h"
11 #include "base/time/time.h"
12 #include "content/common/content_export.h"
13 #include "media/base/audio_converter.h"
14 #include "third_party/webrtc/modules/audio_processing/include/audio_processing.h"
15 #include "third_party/webrtc/modules/interface/module_common_types.h"
18 class WebMediaConstraints;
24 class AudioParameters;
33 class RTCMediaConstraints;
35 // This class owns an object of webrtc::AudioProcessing which contains signal
36 // processing components like AGC, AEC and NS. It enables the components based
37 // on the getUserMedia constraints, processes the data and outputs it in a unit
38 // of 10 ms data chunk.
39 class CONTENT_EXPORT MediaStreamAudioProcessor :
40 public base::RefCountedThreadSafe<MediaStreamAudioProcessor> {
42 MediaStreamAudioProcessor(const media::AudioParameters& source_params,
43 const blink::WebMediaConstraints& constraints,
46 // Pushes capture data in |audio_source| to the internal FIFO.
47 // Called on the capture audio thread.
48 void PushCaptureData(media::AudioBus* audio_source);
50 // Push the render audio to webrtc::AudioProcessing for analysis. This is
51 // needed iff echo processing is enabled.
52 // |render_audio| is the pointer to the render audio data, its format
53 // is specified by |sample_rate|, |number_of_channels| and |number_of_frames|.
54 // Called on the render audio thread.
55 void PushRenderData(const int16* render_audio,
57 int number_of_channels,
59 base::TimeDelta render_delay);
61 // Processes a block of 10 ms data from the internal FIFO and outputs it via
62 // |out|. |out| is the address of the pointer that will be pointed to
63 // the post-processed data if the method is returning a true. The lifetime
64 // of the data represeted by |out| is guaranteed to outlive the method call.
65 // That also says *|out| won't change until this method is called again.
66 // |new_volume| receives the new microphone volume from the AGC.
67 // The new microphoen volume range is [0, 255], and the value will be 0 if
68 // the microphone volume should not be adjusted.
69 // Returns true if the internal FIFO has at least 10 ms data for processing,
71 // |capture_delay|, |volume| and |key_pressed| will be passed to
72 // webrtc::AudioProcessing to help processing the data.
73 // Called on the capture audio thread.
74 bool ProcessAndConsumeData(base::TimeDelta capture_delay,
81 // The audio format of the input to the processor.
82 const media::AudioParameters& InputFormat() const;
84 // The audio format of the output from the processor.
85 const media::AudioParameters& OutputFormat() const;
87 // Accessor to check if the audio processing is enabled or not.
88 bool has_audio_processing() const { return audio_processing_ != NULL; }
91 friend class base::RefCountedThreadSafe<MediaStreamAudioProcessor>;
92 virtual ~MediaStreamAudioProcessor();
95 friend class MediaStreamAudioProcessorTest;
97 class MediaStreamAudioConverter;
99 // Helper to initialize the WebRtc AudioProcessing.
100 void InitializeAudioProcessingModule(
101 const blink::WebMediaConstraints& constraints, int effects);
103 // Helper to initialize the capture converter.
104 void InitializeCaptureConverter(const media::AudioParameters& source_params);
106 // Helper to initialize the render converter.
107 void InitializeRenderConverterIfNeeded(int sample_rate,
108 int number_of_channels,
109 int frames_per_buffer);
111 // Called by ProcessAndConsumeData().
112 // Returns the new microphone volume in the range of |0, 255].
113 // When the volume does not need to be updated, it returns 0.
114 int ProcessData(webrtc::AudioFrame* audio_frame,
115 base::TimeDelta capture_delay,
119 // Called when the processor is going away.
120 void StopAudioProcessing();
122 // Cached value for the render delay latency. This member is accessed by
123 // both the capture audio thread and the render audio thread.
124 base::subtle::Atomic32 render_delay_ms_;
126 // webrtc::AudioProcessing module which does AEC, AGC, NS, HighPass filter,
128 scoped_ptr<webrtc::AudioProcessing> audio_processing_;
130 // Converter used for the down-mixing and resampling of the capture data.
131 scoped_ptr<MediaStreamAudioConverter> capture_converter_;
133 // AudioFrame used to hold the output of |capture_converter_|.
134 webrtc::AudioFrame capture_frame_;
136 // Converter used for the down-mixing and resampling of the render data when
137 // the AEC is enabled.
138 scoped_ptr<MediaStreamAudioConverter> render_converter_;
140 // AudioFrame used to hold the output of |render_converter_|.
141 webrtc::AudioFrame render_frame_;
143 // Data bus to help converting interleaved data to an AudioBus.
144 scoped_ptr<media::AudioBus> render_data_bus_;
146 // Used to DCHECK that some methods are called on the main render thread.
147 base::ThreadChecker main_thread_checker_;
149 // Used to DCHECK that some methods are called on the capture audio thread.
150 base::ThreadChecker capture_thread_checker_;
152 // Used to DCHECK that PushRenderData() is called on the render audio thread.
153 base::ThreadChecker render_thread_checker_;
155 // Flag to enable the stereo channels mirroring.
156 bool audio_mirroring_;
159 } // namespace content
161 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_PROCESSOR_H_