Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / media_stream_audio_processor.h
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.
4
5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_PROCESSOR_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_PROCESSOR_H_
7
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"
16
17 namespace blink {
18 class WebMediaConstraints;
19 }
20
21 namespace media {
22 class AudioBus;
23 class AudioFifo;
24 class AudioParameters;
25 }  // namespace media
26
27 namespace webrtc {
28 class AudioFrame;
29 }
30
31 namespace content {
32
33 class RTCMediaConstraints;
34
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> {
41  public:
42   MediaStreamAudioProcessor(const media::AudioParameters& source_params,
43                             const blink::WebMediaConstraints& constraints,
44                             int effects);
45
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);
49
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,
56                       int sample_rate,
57                       int number_of_channels,
58                       int number_of_frames,
59                       base::TimeDelta render_delay);
60
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,
70   // otherwise false.
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,
75                              int volume,
76                              bool key_pressed,
77                              int* new_volume,
78                              int16** out);
79
80
81   // The audio format of the input to the processor.
82   const media::AudioParameters& InputFormat() const;
83
84   // The audio format of the output from the processor.
85   const media::AudioParameters& OutputFormat() const;
86
87   // Accessor to check if the audio processing is enabled or not.
88   bool has_audio_processing() const { return audio_processing_ != NULL; }
89
90  protected:
91   friend class base::RefCountedThreadSafe<MediaStreamAudioProcessor>;
92   virtual ~MediaStreamAudioProcessor();
93
94  private:
95   friend class MediaStreamAudioProcessorTest;
96
97   class MediaStreamAudioConverter;
98
99   // Helper to initialize the WebRtc AudioProcessing.
100   void InitializeAudioProcessingModule(
101       const blink::WebMediaConstraints& constraints, int effects);
102
103   // Helper to initialize the capture converter.
104   void InitializeCaptureConverter(const media::AudioParameters& source_params);
105
106   // Helper to initialize the render converter.
107   void InitializeRenderConverterIfNeeded(int sample_rate,
108                                          int number_of_channels,
109                                          int frames_per_buffer);
110
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,
116                   int volume,
117                   bool key_pressed);
118
119   // Called when the processor is going away.
120   void StopAudioProcessing();
121
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_;
125
126   // webrtc::AudioProcessing module which does AEC, AGC, NS, HighPass filter,
127   // ..etc.
128   scoped_ptr<webrtc::AudioProcessing> audio_processing_;
129
130   // Converter used for the down-mixing and resampling of the capture data.
131   scoped_ptr<MediaStreamAudioConverter> capture_converter_;
132
133   // AudioFrame used to hold the output of |capture_converter_|.
134   webrtc::AudioFrame capture_frame_;
135
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_;
139
140   // AudioFrame used to hold the output of |render_converter_|.
141   webrtc::AudioFrame render_frame_;
142
143   // Data bus to help converting interleaved data to an AudioBus.
144   scoped_ptr<media::AudioBus> render_data_bus_;
145
146   // Used to DCHECK that some methods are called on the main render thread.
147   base::ThreadChecker main_thread_checker_;
148
149   // Used to DCHECK that some methods are called on the capture audio thread.
150   base::ThreadChecker capture_thread_checker_;
151
152   // Used to DCHECK that PushRenderData() is called on the render audio thread.
153   base::ThreadChecker render_thread_checker_;
154
155   // Flag to enable the stereo channels mirroring.
156   bool audio_mirroring_;
157 };
158
159 }  // namespace content
160
161 #endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_PROCESSOR_H_