OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ | 11 #ifndef WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |
12 #define WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ | 12 #define WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |
13 | 13 |
14 #include "webrtc/base/buffer.h" | 14 #include "webrtc/base/buffer.h" |
15 #include "webrtc/base/criticalsection.h" | |
16 #include "webrtc/base/task_queue.h" | 15 #include "webrtc/base/task_queue.h" |
| 16 #include "webrtc/base/thread_annotations.h" |
17 #include "webrtc/base/thread_checker.h" | 17 #include "webrtc/base/thread_checker.h" |
18 #include "webrtc/modules/audio_device/include/audio_device.h" | 18 #include "webrtc/modules/audio_device/include/audio_device.h" |
19 #include "webrtc/system_wrappers/include/file_wrapper.h" | 19 #include "webrtc/system_wrappers/include/file_wrapper.h" |
20 #include "webrtc/typedefs.h" | 20 #include "webrtc/typedefs.h" |
21 | 21 |
22 namespace webrtc { | 22 namespace webrtc { |
23 // Delta times between two successive playout callbacks are limited to this | 23 // Delta times between two successive playout callbacks are limited to this |
24 // value before added to an internal array. | 24 // value before added to an internal array. |
25 const size_t kMaxDeltaTimeInMs = 500; | 25 const size_t kMaxDeltaTimeInMs = 500; |
26 // TODO(henrika): remove when no longer used by external client. | 26 // TODO(henrika): remove when no longer used by external client. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 // queue to ensure that they can be read by LogStats() without any locks since | 96 // queue to ensure that they can be read by LogStats() without any locks since |
97 // each task is serialized by the task queue. | 97 // each task is serialized by the task queue. |
98 void UpdateRecStats(int16_t max_abs, size_t num_samples); | 98 void UpdateRecStats(int16_t max_abs, size_t num_samples); |
99 void UpdatePlayStats(int16_t max_abs, size_t num_samples); | 99 void UpdatePlayStats(int16_t max_abs, size_t num_samples); |
100 | 100 |
101 // Clears all members tracking stats for recording and playout. | 101 // Clears all members tracking stats for recording and playout. |
102 // These methods both run on the task queue. | 102 // These methods both run on the task queue. |
103 void ResetRecStats(); | 103 void ResetRecStats(); |
104 void ResetPlayStats(); | 104 void ResetPlayStats(); |
105 | 105 |
106 // Ensures that methods are called on the same thread as the thread that | 106 // This object lives on the main (creating) thread and most methods are |
107 // creates this object. | 107 // called on that same thread. When audio has started some methods will be |
108 rtc::ThreadChecker thread_checker_; | 108 // called on either a native audio thread for playout or a native thread for |
| 109 // recording. Some members are not annotated since they are "protected by |
| 110 // design" and adding e.g. a race checker can cause failuries for very few |
| 111 // edge cases and it is IMHO not worth the risk to use them in this class. |
| 112 // TODO(henrika): see if it is possible to refactor and annotate all members. |
109 | 113 |
110 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback() | 114 // Main thread on which this object is created. |
111 // and it must outlive this object. | 115 rtc::ThreadChecker main_thread_checker_; |
112 AudioTransport* audio_transport_cb_; | |
113 | 116 |
114 // TODO(henrika): given usage of thread checker, it should be possible to | 117 // Native (platform specific) audio thread driving the playout side. |
115 // remove all locks in this class. | 118 rtc::ThreadChecker playout_thread_checker_; |
116 rtc::CriticalSection lock_; | 119 |
117 rtc::CriticalSection lock_cb_; | 120 // Native (platform specific) audio thread driving the recording side. |
| 121 rtc::ThreadChecker recording_thread_checker_; |
118 | 122 |
119 // Task queue used to invoke LogStats() periodically. Tasks are executed on a | 123 // Task queue used to invoke LogStats() periodically. Tasks are executed on a |
120 // worker thread but it does not necessarily have to be the same thread for | 124 // worker thread but it does not necessarily have to be the same thread for |
121 // each task. | 125 // each task. |
122 rtc::TaskQueue task_queue_; | 126 rtc::TaskQueue task_queue_; |
123 | 127 |
124 // Keeps track of if playout/recording are active or not. A combination | 128 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback() |
125 // of these states are used to determine when to start and stop the timer. | 129 // and it must outlive this object. It is not possible to change this member |
126 // Only used on the creating thread and not used to control any media flow. | 130 // while any media is active. It is possible to start media without calling |
127 bool playing_; | 131 // RegisterAudioCallback() but that will lead to ignored audio callbacks in |
128 bool recording_; | 132 // both directions where native audio will be acive but no audio samples will |
| 133 // be transported. |
| 134 AudioTransport* audio_transport_cb_; |
| 135 |
| 136 // The members below that are not annotated are protected by design. They are |
| 137 // all set on the main thread (verified by |main_thread_checker_|) and then |
| 138 // read on either the playout or recording audio thread. But, media will never |
| 139 // be active when the member is set; hence no conflict exists. It is too |
| 140 // complex to ensure and verify that this is actually the case. |
129 | 141 |
130 // Sample rate in Hertz. | 142 // Sample rate in Hertz. |
131 uint32_t rec_sample_rate_; | 143 uint32_t rec_sample_rate_; |
132 uint32_t play_sample_rate_; | 144 uint32_t play_sample_rate_; |
133 | 145 |
134 // Number of audio channels. | 146 // Number of audio channels. |
135 size_t rec_channels_; | 147 size_t rec_channels_; |
136 size_t play_channels_; | 148 size_t play_channels_; |
137 | 149 |
138 // Number of bytes per audio sample (2 or 4). | 150 // Keeps track of if playout/recording are active or not. A combination |
139 size_t rec_bytes_per_sample_; | 151 // of these states are used to determine when to start and stop the timer. |
140 size_t play_bytes_per_sample_; | 152 // Only used on the creating thread and not used to control any media flow. |
| 153 bool playing_ ACCESS_ON(main_thread_checker_); |
| 154 bool recording_ ACCESS_ON(main_thread_checker_); |
| 155 |
| 156 // Buffer used for audio samples to be played out. Size can be changed |
| 157 // dynamically. |
| 158 rtc::Buffer play_buffer_ ACCESS_ON(playout_thread_checker_); |
141 | 159 |
142 // Byte buffer used for recorded audio samples. Size can be changed | 160 // Byte buffer used for recorded audio samples. Size can be changed |
143 // dynamically. | 161 // dynamically. |
144 rtc::Buffer rec_buffer_; | 162 rtc::Buffer rec_buffer_ ACCESS_ON(recording_thread_checker_); |
145 | |
146 // Buffer used for audio samples to be played out. Size can be changed | |
147 // dynamically. | |
148 rtc::Buffer play_buffer_; | |
149 | 163 |
150 // AGC parameters. | 164 // AGC parameters. |
| 165 #if !defined(WEBRTC_WIN) |
| 166 uint32_t current_mic_level_ ACCESS_ON(recording_thread_checker_); |
| 167 #else |
| 168 // Windows uses a dedicated thread for volume APIs. |
151 uint32_t current_mic_level_; | 169 uint32_t current_mic_level_; |
152 uint32_t new_mic_level_; | 170 #endif |
| 171 uint32_t new_mic_level_ ACCESS_ON(recording_thread_checker_); |
153 | 172 |
154 // Contains true of a key-press has been detected. | 173 // Contains true of a key-press has been detected. |
155 bool typing_status_; | 174 bool typing_status_ ACCESS_ON(recording_thread_checker_); |
156 | 175 |
157 // Delay values used by the AEC. | 176 // Delay values used by the AEC. |
158 int play_delay_ms_; | 177 int play_delay_ms_ ACCESS_ON(recording_thread_checker_); |
159 int rec_delay_ms_; | 178 int rec_delay_ms_ ACCESS_ON(recording_thread_checker_); |
160 | 179 |
161 // Contains a clock-drift measurement. | 180 // Contains a clock-drift measurement. |
162 int clock_drift_; | 181 int clock_drift_ ACCESS_ON(recording_thread_checker_); |
163 | 182 |
164 // Counts number of times LogStats() has been called. | 183 // Counts number of times LogStats() has been called. |
165 size_t num_stat_reports_; | 184 size_t num_stat_reports_ ACCESS_ON(task_queue_); |
166 | 185 |
167 // Total number of recording callbacks where the source provides 10ms audio | 186 // Total number of recording callbacks where the source provides 10ms audio |
168 // data each time. | 187 // data each time. |
169 uint64_t rec_callbacks_; | 188 uint64_t rec_callbacks_ ACCESS_ON(task_queue_); |
170 | 189 |
171 // Total number of recording callbacks stored at the last timer task. | 190 // Total number of recording callbacks stored at the last timer task. |
172 uint64_t last_rec_callbacks_; | 191 uint64_t last_rec_callbacks_ ACCESS_ON(task_queue_); |
173 | 192 |
174 // Total number of playback callbacks where the sink asks for 10ms audio | 193 // Total number of playback callbacks where the sink asks for 10ms audio |
175 // data each time. | 194 // data each time. |
176 uint64_t play_callbacks_; | 195 uint64_t play_callbacks_ ACCESS_ON(task_queue_); |
177 | 196 |
178 // Total number of playout callbacks stored at the last timer task. | 197 // Total number of playout callbacks stored at the last timer task. |
179 uint64_t last_play_callbacks_; | 198 uint64_t last_play_callbacks_ ACCESS_ON(task_queue_); |
180 | 199 |
181 // Total number of recorded audio samples. | 200 // Total number of recorded audio samples. |
182 uint64_t rec_samples_; | 201 uint64_t rec_samples_ ACCESS_ON(task_queue_); |
183 | 202 |
184 // Total number of recorded samples stored at the previous timer task. | 203 // Total number of recorded samples stored at the previous timer task. |
185 uint64_t last_rec_samples_; | 204 uint64_t last_rec_samples_ ACCESS_ON(task_queue_); |
186 | 205 |
187 // Total number of played audio samples. | 206 // Total number of played audio samples. |
188 uint64_t play_samples_; | 207 uint64_t play_samples_ ACCESS_ON(task_queue_); |
189 | 208 |
190 // Total number of played samples stored at the previous timer task. | 209 // Total number of played samples stored at the previous timer task. |
191 uint64_t last_play_samples_; | 210 uint64_t last_play_samples_ ACCESS_ON(task_queue_); |
192 | |
193 // Time stamp of last timer task (drives logging). | |
194 uint64_t last_timer_task_time_; | |
195 | |
196 // Time stamp of last playout callback. | |
197 uint64_t last_playout_time_; | |
198 | |
199 // An array where the position corresponds to time differences (in | |
200 // milliseconds) between two successive playout callbacks, and the stored | |
201 // value is the number of times a given time difference was found. | |
202 // Writing to the array is done without a lock since it is only read once at | |
203 // destruction when no audio is running. | |
204 uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0}; | |
205 | 211 |
206 // Contains max level (max(abs(x))) of recorded audio packets over the last | 212 // Contains max level (max(abs(x))) of recorded audio packets over the last |
207 // 10 seconds where a new measurement is done twice per second. The level | 213 // 10 seconds where a new measurement is done twice per second. The level |
208 // is reset to zero at each call to LogStats(). Only modified on the task | 214 // is reset to zero at each call to LogStats(). |
209 // queue thread. | 215 int16_t max_rec_level_ ACCESS_ON(task_queue_); |
210 int16_t max_rec_level_; | |
211 | 216 |
212 // Contains max level of recorded audio packets over the last 10 seconds | 217 // Contains max level of recorded audio packets over the last 10 seconds |
213 // where a new measurement is done twice per second. | 218 // where a new measurement is done twice per second. |
214 int16_t max_play_level_; | 219 int16_t max_play_level_ ACCESS_ON(task_queue_); |
| 220 |
| 221 // Time stamp of last timer task (drives logging). |
| 222 uint64_t last_timer_task_time_ ACCESS_ON(task_queue_); |
215 | 223 |
216 // Counts number of audio callbacks modulo 50 to create a signal when | 224 // Counts number of audio callbacks modulo 50 to create a signal when |
217 // a new storage of audio stats shall be done. | 225 // a new storage of audio stats shall be done. |
218 // Only updated on the OS-specific audio thread that drives audio. | 226 int16_t rec_stat_count_ ACCESS_ON(recording_thread_checker_); |
219 int16_t rec_stat_count_; | 227 int16_t play_stat_count_ ACCESS_ON(playout_thread_checker_); |
220 int16_t play_stat_count_; | |
221 | 228 |
222 // Time stamps of when playout and recording starts. | 229 // Time stamps of when playout and recording starts. |
223 uint64_t play_start_time_; | 230 uint64_t play_start_time_ ACCESS_ON(main_thread_checker_); |
224 uint64_t rec_start_time_; | 231 uint64_t rec_start_time_ ACCESS_ON(main_thread_checker_); |
225 | 232 |
226 // Set to true at construction and modified to false as soon as one audio- | 233 // Set to true at construction and modified to false as soon as one audio- |
227 // level estimate larger than zero is detected. | 234 // level estimate larger than zero is detected. |
228 bool only_silence_recorded_; | 235 bool only_silence_recorded_; |
229 }; | 236 }; |
230 | 237 |
231 } // namespace webrtc | 238 } // namespace webrtc |
232 | 239 |
233 #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ | 240 #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |
OLD | NEW |