Chromium Code Reviews| 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" | 15 #include "webrtc/base/criticalsection.h" |
| 16 #include "webrtc/base/task_queue.h" | 16 #include "webrtc/base/task_queue.h" |
| 17 #include "webrtc/base/thread_annotations.h" | |
| 17 #include "webrtc/base/thread_checker.h" | 18 #include "webrtc/base/thread_checker.h" |
| 18 #include "webrtc/modules/audio_device/include/audio_device.h" | 19 #include "webrtc/modules/audio_device/include/audio_device.h" |
| 19 #include "webrtc/system_wrappers/include/file_wrapper.h" | 20 #include "webrtc/system_wrappers/include/file_wrapper.h" |
| 20 #include "webrtc/typedefs.h" | 21 #include "webrtc/typedefs.h" |
| 21 | 22 |
| 22 namespace webrtc { | 23 namespace webrtc { |
| 23 // Delta times between two successive playout callbacks are limited to this | 24 // Delta times between two successive playout callbacks are limited to this |
| 24 // value before added to an internal array. | 25 // value before added to an internal array. |
| 25 const size_t kMaxDeltaTimeInMs = 500; | 26 const size_t kMaxDeltaTimeInMs = 500; |
| 26 // TODO(henrika): remove when no longer used by external client. | 27 // TODO(henrika): remove when no longer used by external client. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 40 virtual ~AudioDeviceBuffer(); | 41 virtual ~AudioDeviceBuffer(); |
| 41 | 42 |
| 42 void SetId(uint32_t id) {}; | 43 void SetId(uint32_t id) {}; |
| 43 int32_t RegisterAudioCallback(AudioTransport* audio_callback); | 44 int32_t RegisterAudioCallback(AudioTransport* audio_callback); |
| 44 | 45 |
| 45 void StartPlayout(); | 46 void StartPlayout(); |
| 46 void StartRecording(); | 47 void StartRecording(); |
| 47 void StopPlayout(); | 48 void StopPlayout(); |
| 48 void StopRecording(); | 49 void StopRecording(); |
| 49 | 50 |
| 50 int32_t SetRecordingSampleRate(uint32_t fsHz); | 51 int32_t SetRecordingSampleRate(uint32_t fsHz) LOCKS_EXCLUDED(lock_); |
| 51 int32_t SetPlayoutSampleRate(uint32_t fsHz); | 52 int32_t SetPlayoutSampleRate(uint32_t fsHz) LOCKS_EXCLUDED(lock_); |
| 52 int32_t RecordingSampleRate() const; | 53 int32_t RecordingSampleRate() const; |
| 53 int32_t PlayoutSampleRate() const; | 54 int32_t PlayoutSampleRate() const; |
| 54 | 55 |
| 55 int32_t SetRecordingChannels(size_t channels); | 56 int32_t SetRecordingChannels(size_t channels) LOCKS_EXCLUDED(lock_); |
| 56 int32_t SetPlayoutChannels(size_t channels); | 57 int32_t SetPlayoutChannels(size_t channels) LOCKS_EXCLUDED(lock_); |
| 57 size_t RecordingChannels() const; | 58 size_t RecordingChannels() const; |
| 58 size_t PlayoutChannels() const; | 59 size_t PlayoutChannels() const; |
| 59 int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel); | 60 int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel); |
| 60 int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const; | 61 int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const; |
| 61 | 62 |
| 62 virtual int32_t SetRecordedBuffer(const void* audio_buffer, | 63 virtual int32_t SetRecordedBuffer(const void* audio_buffer, |
| 63 size_t num_samples); | 64 size_t num_samples) LOCKS_EXCLUDED(lock_); |
| 64 int32_t SetCurrentMicLevel(uint32_t level); | 65 int32_t SetCurrentMicLevel(uint32_t level); |
| 65 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms, int clock_drift); | 66 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms, int clock_drift); |
| 66 virtual int32_t DeliverRecordedData(); | 67 virtual int32_t DeliverRecordedData() LOCKS_EXCLUDED(lock_); |
| 67 uint32_t NewMicLevel() const; | 68 uint32_t NewMicLevel() const; |
| 68 | 69 |
| 69 virtual int32_t RequestPlayoutData(size_t num_samples); | 70 virtual int32_t RequestPlayoutData(size_t num_samples) LOCKS_EXCLUDED(lock_); |
| 70 virtual int32_t GetPlayoutData(void* audio_buffer); | 71 virtual int32_t GetPlayoutData(void* audio_buffer) LOCKS_EXCLUDED(lock_); |
| 71 | 72 |
| 72 // TODO(henrika): these methods should not be used and does not contain any | 73 // TODO(henrika): these methods should not be used and does not contain any |
| 73 // valid implementation. Investigate the possibility to either remove them | 74 // valid implementation. Investigate the possibility to either remove them |
| 74 // or add a proper implementation if needed. | 75 // or add a proper implementation if needed. |
| 75 int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]); | 76 int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 76 int32_t StopInputFileRecording(); | 77 int32_t StopInputFileRecording(); |
| 77 int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]); | 78 int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 78 int32_t StopOutputFileRecording(); | 79 int32_t StopOutputFileRecording(); |
| 79 | 80 |
| 80 int32_t SetTypingStatus(bool typing_status); | 81 int32_t SetTypingStatus(bool typing_status); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 96 // queue to ensure that they can be read by LogStats() without any locks since | 97 // queue to ensure that they can be read by LogStats() without any locks since |
| 97 // each task is serialized by the task queue. | 98 // each task is serialized by the task queue. |
| 98 void UpdateRecStats(int16_t max_abs, size_t num_samples); | 99 void UpdateRecStats(int16_t max_abs, size_t num_samples); |
| 99 void UpdatePlayStats(int16_t max_abs, size_t num_samples); | 100 void UpdatePlayStats(int16_t max_abs, size_t num_samples); |
| 100 | 101 |
| 101 // Clears all members tracking stats for recording and playout. | 102 // Clears all members tracking stats for recording and playout. |
| 102 // These methods both run on the task queue. | 103 // These methods both run on the task queue. |
| 103 void ResetRecStats(); | 104 void ResetRecStats(); |
| 104 void ResetPlayStats(); | 105 void ResetPlayStats(); |
| 105 | 106 |
| 106 // Ensures that methods are called on the same thread as the thread that | |
| 107 // creates this object. | |
| 108 rtc::ThreadChecker thread_checker_; | 107 rtc::ThreadChecker thread_checker_; |
|
kwiberg-webrtc
2016/11/02 14:14:57
Could this one get a descriptive name too?
henrika_webrtc
2016/11/02 16:23:24
Done.
| |
| 108 rtc::ThreadChecker playout_thread_checker_; | |
| 109 rtc::ThreadChecker recording_thread_checker_; | |
| 110 | |
| 111 rtc::CriticalSection lock_; | |
|
kwiberg-webrtc
2016/11/02 14:14:57
OK, *three* thread checkers... *and* a lock. I'd l
henrika_webrtc
2016/11/02 16:23:24
It is actually not that complicated. Let me try to
| |
| 109 | 112 |
| 110 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback() | 113 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback() |
| 111 // and it must outlive this object. | 114 // and it must outlive this object. It is not possible to change this member |
| 115 // while any media is active. It is possible to start media without calling | |
| 116 // RegisterAudioCallback() but that will lead to ignored audio callbacks in | |
| 117 // both directions where native audio will be acive but no audio samples will | |
| 118 // be transported. | |
| 112 AudioTransport* audio_transport_cb_; | 119 AudioTransport* audio_transport_cb_; |
| 113 | 120 |
| 114 // TODO(henrika): given usage of thread checker, it should be possible to | |
| 115 // remove all locks in this class. | |
| 116 rtc::CriticalSection lock_; | |
| 117 rtc::CriticalSection lock_cb_; | |
| 118 | |
| 119 // Task queue used to invoke LogStats() periodically. Tasks are executed on a | 121 // 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 | 122 // worker thread but it does not necessarily have to be the same thread for |
| 121 // each task. | 123 // each task. |
| 122 rtc::TaskQueue task_queue_; | 124 rtc::TaskQueue task_queue_; |
| 123 | 125 |
| 124 // Keeps track of if playout/recording are active or not. A combination | 126 // Keeps track of if playout/recording are active or not. A combination |
| 125 // of these states are used to determine when to start and stop the timer. | 127 // of these states are used to determine when to start and stop the timer. |
| 126 // Only used on the creating thread and not used to control any media flow. | 128 // Only used on the creating thread and not used to control any media flow. |
| 127 bool playing_; | 129 bool playing_ ACCESS_ON(thread_checker_); |
| 128 bool recording_; | 130 bool recording_ ACCESS_ON(thread_checker_); |
| 129 | 131 |
| 130 // Sample rate in Hertz. | 132 // Sample rate in Hertz. |
| 131 uint32_t rec_sample_rate_; | 133 uint32_t rec_sample_rate_ GUARDED_BY(lock_); |
| 132 uint32_t play_sample_rate_; | 134 uint32_t play_sample_rate_ GUARDED_BY(lock_); |
| 133 | 135 |
| 134 // Number of audio channels. | 136 // Number of audio channels. |
| 135 size_t rec_channels_; | 137 size_t rec_channels_ GUARDED_BY(lock_); |
| 136 size_t play_channels_; | 138 size_t play_channels_ GUARDED_BY(lock_); |
| 137 | |
| 138 // Number of bytes per audio sample (2 or 4). | |
| 139 size_t rec_bytes_per_sample_; | |
| 140 size_t play_bytes_per_sample_; | |
| 141 | 139 |
| 142 // Byte buffer used for recorded audio samples. Size can be changed | 140 // Byte buffer used for recorded audio samples. Size can be changed |
| 143 // dynamically. | 141 // dynamically. |
| 144 rtc::Buffer rec_buffer_; | 142 rtc::Buffer rec_buffer_ ACCESS_ON(recording_thread_checker_); |
| 145 | 143 |
| 146 // Buffer used for audio samples to be played out. Size can be changed | 144 // Buffer used for audio samples to be played out. Size can be changed |
| 147 // dynamically. | 145 // dynamically. |
| 148 rtc::Buffer play_buffer_; | 146 rtc::Buffer play_buffer_ ACCESS_ON(playout_thread_checker_); |
| 149 | 147 |
| 150 // AGC parameters. | 148 // AGC parameters. |
| 151 uint32_t current_mic_level_; | 149 uint32_t current_mic_level_ ACCESS_ON(recording_thread_checker_); |
| 152 uint32_t new_mic_level_; | 150 uint32_t new_mic_level_ ACCESS_ON(recording_thread_checker_); |
| 153 | 151 |
| 154 // Contains true of a key-press has been detected. | 152 // Contains true of a key-press has been detected. |
| 155 bool typing_status_; | 153 bool typing_status_ ACCESS_ON(recording_thread_checker_); |
| 156 | 154 |
| 157 // Delay values used by the AEC. | 155 // Delay values used by the AEC. |
| 158 int play_delay_ms_; | 156 int play_delay_ms_ ACCESS_ON(recording_thread_checker_); |
| 159 int rec_delay_ms_; | 157 int rec_delay_ms_ ACCESS_ON(recording_thread_checker_); |
| 160 | 158 |
| 161 // Contains a clock-drift measurement. | 159 // Contains a clock-drift measurement. |
| 162 int clock_drift_; | 160 int clock_drift_ ACCESS_ON(recording_thread_checker_); |
| 163 | 161 |
| 164 // Counts number of times LogStats() has been called. | 162 // Counts number of times LogStats() has been called. |
| 165 size_t num_stat_reports_; | 163 size_t num_stat_reports_ ACCESS_ON(task_queue_); |
|
kwiberg-webrtc
2016/11/02 14:14:57
Aha, a task queue annotation. Excellent!
henrika_webrtc
2016/11/02 16:23:24
;-)
| |
| 166 | 164 |
| 167 // Total number of recording callbacks where the source provides 10ms audio | 165 // Total number of recording callbacks where the source provides 10ms audio |
| 168 // data each time. | 166 // data each time. |
| 169 uint64_t rec_callbacks_; | 167 uint64_t rec_callbacks_ ACCESS_ON(task_queue_); |
| 170 | 168 |
| 171 // Total number of recording callbacks stored at the last timer task. | 169 // Total number of recording callbacks stored at the last timer task. |
| 172 uint64_t last_rec_callbacks_; | 170 uint64_t last_rec_callbacks_ ACCESS_ON(task_queue_); |
| 173 | 171 |
| 174 // Total number of playback callbacks where the sink asks for 10ms audio | 172 // Total number of playback callbacks where the sink asks for 10ms audio |
| 175 // data each time. | 173 // data each time. |
| 176 uint64_t play_callbacks_; | 174 uint64_t play_callbacks_ ACCESS_ON(task_queue_); |
| 177 | 175 |
| 178 // Total number of playout callbacks stored at the last timer task. | 176 // Total number of playout callbacks stored at the last timer task. |
| 179 uint64_t last_play_callbacks_; | 177 uint64_t last_play_callbacks_ ACCESS_ON(task_queue_); |
| 180 | 178 |
| 181 // Total number of recorded audio samples. | 179 // Total number of recorded audio samples. |
| 182 uint64_t rec_samples_; | 180 uint64_t rec_samples_ ACCESS_ON(task_queue_); |
| 183 | 181 |
| 184 // Total number of recorded samples stored at the previous timer task. | 182 // Total number of recorded samples stored at the previous timer task. |
| 185 uint64_t last_rec_samples_; | 183 uint64_t last_rec_samples_ ACCESS_ON(task_queue_); |
| 186 | 184 |
| 187 // Total number of played audio samples. | 185 // Total number of played audio samples. |
| 188 uint64_t play_samples_; | 186 uint64_t play_samples_ ACCESS_ON(task_queue_); |
| 189 | 187 |
| 190 // Total number of played samples stored at the previous timer task. | 188 // Total number of played samples stored at the previous timer task. |
| 191 uint64_t last_play_samples_; | 189 uint64_t last_play_samples_ ACCESS_ON(task_queue_); |
| 190 | |
| 191 // Contains max level (max(abs(x))) of recorded audio packets over the last | |
| 192 // 10 seconds where a new measurement is done twice per second. The level | |
| 193 // is reset to zero at each call to LogStats(). | |
| 194 int16_t max_rec_level_ ACCESS_ON(task_queue_); | |
| 195 | |
| 196 // Contains max level of recorded audio packets over the last 10 seconds | |
| 197 // where a new measurement is done twice per second. | |
| 198 int16_t max_play_level_ ACCESS_ON(task_queue_); | |
| 192 | 199 |
| 193 // Time stamp of last timer task (drives logging). | 200 // Time stamp of last timer task (drives logging). |
| 194 uint64_t last_timer_task_time_; | 201 uint64_t last_timer_task_time_ ACCESS_ON(task_queue_); |
| 195 | 202 |
| 196 // Time stamp of last playout callback. | 203 // Time stamp of last playout callback. |
| 197 uint64_t last_playout_time_; | 204 uint64_t last_playout_time_; |
| 198 | 205 |
| 199 // An array where the position corresponds to time differences (in | 206 // An array where the position corresponds to time differences (in |
| 200 // milliseconds) between two successive playout callbacks, and the stored | 207 // milliseconds) between two successive playout callbacks, and the stored |
| 201 // value is the number of times a given time difference was found. | 208 // 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 | 209 // Writing to the array is done without a lock since it is only read once at |
| 203 // destruction when no audio is running. | 210 // destruction when no audio is running. |
| 204 uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0}; | 211 uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0}; |
| 205 | 212 |
| 206 // 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 | |
| 208 // is reset to zero at each call to LogStats(). Only modified on the task | |
| 209 // queue thread. | |
| 210 int16_t max_rec_level_; | |
| 211 | |
| 212 // Contains max level of recorded audio packets over the last 10 seconds | |
| 213 // where a new measurement is done twice per second. | |
| 214 int16_t max_play_level_; | |
| 215 | |
| 216 // Counts number of audio callbacks modulo 50 to create a signal when | 213 // Counts number of audio callbacks modulo 50 to create a signal when |
| 217 // a new storage of audio stats shall be done. | 214 // a new storage of audio stats shall be done. |
| 218 // Only updated on the OS-specific audio thread that drives audio. | 215 int16_t rec_stat_count_ ACCESS_ON(recording_thread_checker_); |
| 219 int16_t rec_stat_count_; | 216 int16_t play_stat_count_ ACCESS_ON(playout_thread_checker_); |
| 220 int16_t play_stat_count_; | |
| 221 | 217 |
| 222 // Time stamps of when playout and recording starts. | 218 // Time stamps of when playout and recording starts. |
| 223 uint64_t play_start_time_; | 219 uint64_t play_start_time_ ACCESS_ON(thread_checker_); |
| 224 uint64_t rec_start_time_; | 220 uint64_t rec_start_time_ ACCESS_ON(thread_checker_);; |
| 225 | 221 |
| 226 // Set to true at construction and modified to false as soon as one audio- | 222 // Set to true at construction and modified to false as soon as one audio- |
| 227 // level estimate larger than zero is detected. | 223 // level estimate larger than zero is detected. |
| 228 bool only_silence_recorded_; | 224 bool only_silence_recorded_; |
| 229 }; | 225 }; |
| 230 | 226 |
| 231 } // namespace webrtc | 227 } // namespace webrtc |
| 232 | 228 |
| 233 #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ | 229 #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |
| OLD | NEW |