| 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" |
| 17 #include "webrtc/base/thread_annotations.h" | 16 #include "webrtc/base/thread_annotations.h" |
| 18 #include "webrtc/base/thread_checker.h" | 17 #include "webrtc/base/thread_checker.h" |
| 19 #include "webrtc/modules/audio_device/include/audio_device.h" | 18 #include "webrtc/modules/audio_device/include/audio_device.h" |
| 20 #include "webrtc/system_wrappers/include/file_wrapper.h" | 19 #include "webrtc/system_wrappers/include/file_wrapper.h" |
| 21 #include "webrtc/typedefs.h" | 20 #include "webrtc/typedefs.h" |
| 22 | 21 |
| 23 namespace webrtc { | 22 namespace webrtc { |
| 24 // Delta times between two successive playout callbacks are limited to this | 23 // Delta times between two successive playout callbacks are limited to this |
| 25 // value before added to an internal array. | 24 // value before added to an internal array. |
| 26 const size_t kMaxDeltaTimeInMs = 500; | 25 const size_t kMaxDeltaTimeInMs = 500; |
| 27 // TODO(henrika): remove when no longer used by external client. | 26 // TODO(henrika): remove when no longer used by external client. |
| 28 const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz | 27 const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz |
| 29 | 28 |
| 30 class AudioDeviceObserver; | 29 class AudioDeviceObserver; |
| 31 | 30 |
| 32 class AudioDeviceBuffer { | 31 class AudioDeviceBuffer { |
| 33 public: | 32 public: |
| 34 enum LogState { | 33 enum LogState { |
| 35 LOG_START = 0, | 34 LOG_START = 0, |
| 36 LOG_STOP, | 35 LOG_STOP, |
| 37 LOG_ACTIVE, | 36 LOG_ACTIVE, |
| 38 }; | 37 }; |
| 39 | 38 |
| 40 struct Stats { | |
| 41 void ResetRecStats() { | |
| 42 rec_callbacks = 0; | |
| 43 rec_samples = 0; | |
| 44 max_rec_level = 0; | |
| 45 } | |
| 46 | |
| 47 void ResetPlayStats() { | |
| 48 play_callbacks = 0; | |
| 49 play_samples = 0; | |
| 50 max_play_level = 0; | |
| 51 } | |
| 52 | |
| 53 // Total number of recording callbacks where the source provides 10ms audio | |
| 54 // data each time. | |
| 55 uint64_t rec_callbacks = 0; | |
| 56 | |
| 57 // Total number of playback callbacks where the sink asks for 10ms audio | |
| 58 // data each time. | |
| 59 uint64_t play_callbacks = 0; | |
| 60 | |
| 61 // Total number of recorded audio samples. | |
| 62 uint64_t rec_samples = 0; | |
| 63 | |
| 64 // Total number of played audio samples. | |
| 65 uint64_t play_samples = 0; | |
| 66 | |
| 67 // Contains max level (max(abs(x))) of recorded audio packets over the last | |
| 68 // 10 seconds where a new measurement is done twice per second. The level | |
| 69 // is reset to zero at each call to LogStats(). | |
| 70 int16_t max_rec_level = 0; | |
| 71 | |
| 72 // Contains max level of recorded audio packets over the last 10 seconds | |
| 73 // where a new measurement is done twice per second. | |
| 74 int16_t max_play_level = 0; | |
| 75 }; | |
| 76 | |
| 77 AudioDeviceBuffer(); | 39 AudioDeviceBuffer(); |
| 78 virtual ~AudioDeviceBuffer(); | 40 virtual ~AudioDeviceBuffer(); |
| 79 | 41 |
| 80 void SetId(uint32_t id) {}; | 42 void SetId(uint32_t id) {}; |
| 81 int32_t RegisterAudioCallback(AudioTransport* audio_callback); | 43 int32_t RegisterAudioCallback(AudioTransport* audio_callback); |
| 82 | 44 |
| 83 void StartPlayout(); | 45 void StartPlayout(); |
| 84 void StartRecording(); | 46 void StartRecording(); |
| 85 void StopPlayout(); | 47 void StopPlayout(); |
| 86 void StopRecording(); | 48 void StopRecording(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 void StopPeriodicLogging(); | 85 void StopPeriodicLogging(); |
| 124 | 86 |
| 125 // Called periodically on the internal thread created by the TaskQueue. | 87 // Called periodically on the internal thread created by the TaskQueue. |
| 126 // Updates some stats but dooes it on the task queue to ensure that access of | 88 // Updates some stats but dooes it on the task queue to ensure that access of |
| 127 // members is serialized hence avoiding usage of locks. | 89 // members is serialized hence avoiding usage of locks. |
| 128 // state = LOG_START => members are initialized and the timer starts. | 90 // state = LOG_START => members are initialized and the timer starts. |
| 129 // state = LOG_STOP => no logs are printed and the timer stops. | 91 // state = LOG_STOP => no logs are printed and the timer stops. |
| 130 // state = LOG_ACTIVE => logs are printed and the timer is kept alive. | 92 // state = LOG_ACTIVE => logs are printed and the timer is kept alive. |
| 131 void LogStats(LogState state); | 93 void LogStats(LogState state); |
| 132 | 94 |
| 133 // Updates counters in each play/record callback. These counters are later | 95 // Updates counters in each play/record callback but does it on the task |
| 134 // (periodically) read by LogStats() using a lock. | 96 // queue to ensure that they can be read by LogStats() without any locks since |
| 97 // each task is serialized by the task queue. |
| 135 void UpdateRecStats(int16_t max_abs, size_t samples_per_channel); | 98 void UpdateRecStats(int16_t max_abs, size_t samples_per_channel); |
| 136 void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel); | 99 void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel); |
| 137 | 100 |
| 138 // Clears all members tracking stats for recording and playout. | 101 // Clears all members tracking stats for recording and playout. |
| 139 // These methods both run on the task queue. | 102 // These methods both run on the task queue. |
| 140 void ResetRecStats(); | 103 void ResetRecStats(); |
| 141 void ResetPlayStats(); | 104 void ResetPlayStats(); |
| 142 | 105 |
| 143 // This object lives on the main (creating) thread and most methods are | 106 // This object lives on the main (creating) thread and most methods are |
| 144 // called on that same thread. When audio has started some methods will be | 107 // called on that same thread. When audio has started some methods will be |
| 145 // called on either a native audio thread for playout or a native thread for | 108 // called on either a native audio thread for playout or a native thread for |
| 146 // recording. Some members are not annotated since they are "protected by | 109 // recording. Some members are not annotated since they are "protected by |
| 147 // design" and adding e.g. a race checker can cause failuries for very few | 110 // design" and adding e.g. a race checker can cause failuries for very few |
| 148 // edge cases and it is IMHO not worth the risk to use them in this class. | 111 // edge cases and it is IMHO not worth the risk to use them in this class. |
| 149 // TODO(henrika): see if it is possible to refactor and annotate all members. | 112 // TODO(henrika): see if it is possible to refactor and annotate all members. |
| 150 | 113 |
| 151 // Main thread on which this object is created. | 114 // Main thread on which this object is created. |
| 152 rtc::ThreadChecker main_thread_checker_; | 115 rtc::ThreadChecker main_thread_checker_; |
| 153 | 116 |
| 154 // Native (platform specific) audio thread driving the playout side. | 117 // Native (platform specific) audio thread driving the playout side. |
| 155 rtc::ThreadChecker playout_thread_checker_; | 118 rtc::ThreadChecker playout_thread_checker_; |
| 156 | 119 |
| 157 // Native (platform specific) audio thread driving the recording side. | 120 // Native (platform specific) audio thread driving the recording side. |
| 158 rtc::ThreadChecker recording_thread_checker_; | 121 rtc::ThreadChecker recording_thread_checker_; |
| 159 | 122 |
| 160 rtc::CriticalSection lock_; | |
| 161 | |
| 162 // 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 |
| 163 // 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 |
| 164 // each task. | 125 // each task. |
| 165 rtc::TaskQueue task_queue_; | 126 rtc::TaskQueue task_queue_; |
| 166 | 127 |
| 167 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback() | 128 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback() |
| 168 // and it must outlive this object. It is not possible to change this member | 129 // and it must outlive this object. It is not possible to change this member |
| 169 // while any media is active. It is possible to start media without calling | 130 // while any media is active. It is possible to start media without calling |
| 170 // RegisterAudioCallback() but that will lead to ignored audio callbacks in | 131 // RegisterAudioCallback() but that will lead to ignored audio callbacks in |
| 171 // both directions where native audio will be acive but no audio samples will | 132 // both directions where native audio will be acive but no audio samples will |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 // Delay values used by the AEC. | 177 // Delay values used by the AEC. |
| 217 int play_delay_ms_ ACCESS_ON(recording_thread_checker_); | 178 int play_delay_ms_ ACCESS_ON(recording_thread_checker_); |
| 218 int rec_delay_ms_ ACCESS_ON(recording_thread_checker_); | 179 int rec_delay_ms_ ACCESS_ON(recording_thread_checker_); |
| 219 | 180 |
| 220 // Contains a clock-drift measurement. | 181 // Contains a clock-drift measurement. |
| 221 int clock_drift_ ACCESS_ON(recording_thread_checker_); | 182 int clock_drift_ ACCESS_ON(recording_thread_checker_); |
| 222 | 183 |
| 223 // Counts number of times LogStats() has been called. | 184 // Counts number of times LogStats() has been called. |
| 224 size_t num_stat_reports_ ACCESS_ON(task_queue_); | 185 size_t num_stat_reports_ ACCESS_ON(task_queue_); |
| 225 | 186 |
| 187 // Total number of recording callbacks where the source provides 10ms audio |
| 188 // data each time. |
| 189 uint64_t rec_callbacks_ ACCESS_ON(task_queue_); |
| 190 |
| 191 // Total number of recording callbacks stored at the last timer task. |
| 192 uint64_t last_rec_callbacks_ ACCESS_ON(task_queue_); |
| 193 |
| 194 // Total number of playback callbacks where the sink asks for 10ms audio |
| 195 // data each time. |
| 196 uint64_t play_callbacks_ ACCESS_ON(task_queue_); |
| 197 |
| 198 // Total number of playout callbacks stored at the last timer task. |
| 199 uint64_t last_play_callbacks_ ACCESS_ON(task_queue_); |
| 200 |
| 201 // Total number of recorded audio samples. |
| 202 uint64_t rec_samples_ ACCESS_ON(task_queue_); |
| 203 |
| 204 // Total number of recorded samples stored at the previous timer task. |
| 205 uint64_t last_rec_samples_ ACCESS_ON(task_queue_); |
| 206 |
| 207 // Total number of played audio samples. |
| 208 uint64_t play_samples_ ACCESS_ON(task_queue_); |
| 209 |
| 210 // Total number of played samples stored at the previous timer task. |
| 211 uint64_t last_play_samples_ ACCESS_ON(task_queue_); |
| 212 |
| 213 // Contains max level (max(abs(x))) of recorded audio packets over the last |
| 214 // 10 seconds where a new measurement is done twice per second. The level |
| 215 // is reset to zero at each call to LogStats(). |
| 216 int16_t max_rec_level_ ACCESS_ON(task_queue_); |
| 217 |
| 218 // Contains max level of recorded audio packets over the last 10 seconds |
| 219 // where a new measurement is done twice per second. |
| 220 int16_t max_play_level_ ACCESS_ON(task_queue_); |
| 221 |
| 226 // Time stamp of last timer task (drives logging). | 222 // Time stamp of last timer task (drives logging). |
| 227 int64_t last_timer_task_time_ ACCESS_ON(task_queue_); | 223 int64_t last_timer_task_time_ ACCESS_ON(task_queue_); |
| 228 | 224 |
| 229 // Counts number of audio callbacks modulo 50 to create a signal when | 225 // Counts number of audio callbacks modulo 50 to create a signal when |
| 230 // a new storage of audio stats shall be done. | 226 // a new storage of audio stats shall be done. |
| 231 int16_t rec_stat_count_ ACCESS_ON(recording_thread_checker_); | 227 int16_t rec_stat_count_ ACCESS_ON(recording_thread_checker_); |
| 232 int16_t play_stat_count_ ACCESS_ON(playout_thread_checker_); | 228 int16_t play_stat_count_ ACCESS_ON(playout_thread_checker_); |
| 233 | 229 |
| 234 // Time stamps of when playout and recording starts. | 230 // Time stamps of when playout and recording starts. |
| 235 int64_t play_start_time_ ACCESS_ON(main_thread_checker_); | 231 int64_t play_start_time_ ACCESS_ON(main_thread_checker_); |
| 236 int64_t rec_start_time_ ACCESS_ON(main_thread_checker_); | 232 int64_t rec_start_time_ ACCESS_ON(main_thread_checker_); |
| 237 | 233 |
| 238 // Contains counters for playout and recording statistics. | |
| 239 Stats stats_ GUARDED_BY(lock_); | |
| 240 | |
| 241 // Stores current stats at each timer task. Used to calculate differences | |
| 242 // between two successive timer events. | |
| 243 Stats last_stats_ ACCESS_ON(task_queue_); | |
| 244 | |
| 245 // Set to true at construction and modified to false as soon as one audio- | 234 // Set to true at construction and modified to false as soon as one audio- |
| 246 // level estimate larger than zero is detected. | 235 // level estimate larger than zero is detected. |
| 247 bool only_silence_recorded_; | 236 bool only_silence_recorded_; |
| 248 | 237 |
| 249 // Set to true when logging of audio stats is enabled for the first time in | 238 // Set to true when logging of audio stats is enabled for the first time in |
| 250 // StartPeriodicLogging() and set to false by StopPeriodicLogging(). | 239 // StartPeriodicLogging() and set to false by StopPeriodicLogging(). |
| 251 // Setting this member to false prevents (possiby invalid) log messages from | 240 // Setting this member to false prevents (possiby invalid) log messages from |
| 252 // being printed in the LogStats() task. | 241 // being printed in the LogStats() task. |
| 253 bool log_stats_ ACCESS_ON(task_queue_); | 242 bool log_stats_ ACCESS_ON(task_queue_); |
| 254 }; | 243 }; |
| 255 | 244 |
| 256 } // namespace webrtc | 245 } // namespace webrtc |
| 257 | 246 |
| 258 #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ | 247 #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |
| OLD | NEW |