niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
leozwang@webrtc.org | 28f3913 | 2012-03-01 18:01:48 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
pbos@webrtc.org | 811269d | 2013-07-11 13:24:38 +0000 | [diff] [blame] | 14 | #include "webrtc/modules/audio_device/include/audio_device.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 15 | #include "webrtc/rtc_base/buffer.h" |
| 16 | #include "webrtc/rtc_base/criticalsection.h" |
| 17 | #include "webrtc/rtc_base/task_queue.h" |
| 18 | #include "webrtc/rtc_base/thread_annotations.h" |
| 19 | #include "webrtc/rtc_base/thread_checker.h" |
terelius | c4b9b94 | 2016-10-28 06:51:59 -0700 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/include/file_wrapper.h" |
pbos@webrtc.org | 811269d | 2013-07-11 13:24:38 +0000 | [diff] [blame] | 21 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 24 | |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame] | 25 | // Delta times between two successive playout callbacks are limited to this |
| 26 | // value before added to an internal array. |
| 27 | const size_t kMaxDeltaTimeInMs = 500; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 28 | // TODO(henrika): remove when no longer used by external client. |
| 29 | const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
| 31 | class AudioDeviceObserver; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 33 | class AudioDeviceBuffer { |
| 34 | public: |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 35 | enum LogState { |
| 36 | LOG_START = 0, |
| 37 | LOG_STOP, |
| 38 | LOG_ACTIVE, |
| 39 | }; |
| 40 | |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 41 | struct Stats { |
| 42 | void ResetRecStats() { |
| 43 | rec_callbacks = 0; |
| 44 | rec_samples = 0; |
| 45 | max_rec_level = 0; |
| 46 | } |
| 47 | |
| 48 | void ResetPlayStats() { |
| 49 | play_callbacks = 0; |
| 50 | play_samples = 0; |
| 51 | max_play_level = 0; |
| 52 | } |
| 53 | |
| 54 | // Total number of recording callbacks where the source provides 10ms audio |
| 55 | // data each time. |
| 56 | uint64_t rec_callbacks = 0; |
| 57 | |
| 58 | // Total number of playback callbacks where the sink asks for 10ms audio |
| 59 | // data each time. |
| 60 | uint64_t play_callbacks = 0; |
| 61 | |
| 62 | // Total number of recorded audio samples. |
| 63 | uint64_t rec_samples = 0; |
| 64 | |
| 65 | // Total number of played audio samples. |
| 66 | uint64_t play_samples = 0; |
| 67 | |
| 68 | // Contains max level (max(abs(x))) of recorded audio packets over the last |
| 69 | // 10 seconds where a new measurement is done twice per second. The level |
| 70 | // is reset to zero at each call to LogStats(). |
| 71 | int16_t max_rec_level = 0; |
| 72 | |
| 73 | // Contains max level of recorded audio packets over the last 10 seconds |
| 74 | // where a new measurement is done twice per second. |
| 75 | int16_t max_play_level = 0; |
| 76 | }; |
| 77 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 78 | AudioDeviceBuffer(); |
| 79 | virtual ~AudioDeviceBuffer(); |
henrike@webrtc.org | 82f014a | 2013-09-10 18:24:07 +0000 | [diff] [blame] | 80 | |
terelius | c4b9b94 | 2016-10-28 06:51:59 -0700 | [diff] [blame] | 81 | void SetId(uint32_t id) {}; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 82 | int32_t RegisterAudioCallback(AudioTransport* audio_callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 84 | void StartPlayout(); |
| 85 | void StartRecording(); |
| 86 | void StopPlayout(); |
| 87 | void StopRecording(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 89 | int32_t SetRecordingSampleRate(uint32_t fsHz); |
| 90 | int32_t SetPlayoutSampleRate(uint32_t fsHz); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 91 | int32_t RecordingSampleRate() const; |
| 92 | int32_t PlayoutSampleRate() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 94 | int32_t SetRecordingChannels(size_t channels); |
| 95 | int32_t SetPlayoutChannels(size_t channels); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 96 | size_t RecordingChannels() const; |
| 97 | size_t PlayoutChannels() const; |
| 98 | int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel); |
| 99 | int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 101 | virtual int32_t SetRecordedBuffer(const void* audio_buffer, |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 102 | size_t samples_per_channel); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 103 | int32_t SetCurrentMicLevel(uint32_t level); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 104 | virtual void SetVQEData(int play_delay_ms, int rec_delay_ms, int clock_drift); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 105 | virtual int32_t DeliverRecordedData(); |
| 106 | uint32_t NewMicLevel() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 108 | virtual int32_t RequestPlayoutData(size_t samples_per_channel); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 109 | virtual int32_t GetPlayoutData(void* audio_buffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 111 | // TODO(henrika): these methods should not be used and does not contain any |
| 112 | // valid implementation. Investigate the possibility to either remove them |
| 113 | // or add a proper implementation if needed. |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 114 | int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 115 | int32_t StopInputFileRecording(); |
| 116 | int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 117 | int32_t StopOutputFileRecording(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 119 | int32_t SetTypingStatus(bool typing_status); |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 120 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 121 | private: |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 122 | // Starts/stops periodic logging of audio stats. |
| 123 | void StartPeriodicLogging(); |
| 124 | void StopPeriodicLogging(); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 125 | |
| 126 | // Called periodically on the internal thread created by the TaskQueue. |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 127 | // Updates some stats but dooes it on the task queue to ensure that access of |
| 128 | // members is serialized hence avoiding usage of locks. |
| 129 | // state = LOG_START => members are initialized and the timer starts. |
| 130 | // state = LOG_STOP => no logs are printed and the timer stops. |
| 131 | // state = LOG_ACTIVE => logs are printed and the timer is kept alive. |
| 132 | void LogStats(LogState state); |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 133 | |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 134 | // Updates counters in each play/record callback. These counters are later |
| 135 | // (periodically) read by LogStats() using a lock. |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 136 | void UpdateRecStats(int16_t max_abs, size_t samples_per_channel); |
| 137 | void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 138 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 139 | // Clears all members tracking stats for recording and playout. |
| 140 | // These methods both run on the task queue. |
| 141 | void ResetRecStats(); |
| 142 | void ResetPlayStats(); |
| 143 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 144 | // This object lives on the main (creating) thread and most methods are |
| 145 | // called on that same thread. When audio has started some methods will be |
| 146 | // called on either a native audio thread for playout or a native thread for |
| 147 | // recording. Some members are not annotated since they are "protected by |
| 148 | // design" and adding e.g. a race checker can cause failuries for very few |
| 149 | // edge cases and it is IMHO not worth the risk to use them in this class. |
| 150 | // TODO(henrika): see if it is possible to refactor and annotate all members. |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 151 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 152 | // Main thread on which this object is created. |
| 153 | rtc::ThreadChecker main_thread_checker_; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 154 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 155 | // Native (platform specific) audio thread driving the playout side. |
| 156 | rtc::ThreadChecker playout_thread_checker_; |
| 157 | |
| 158 | // Native (platform specific) audio thread driving the recording side. |
| 159 | rtc::ThreadChecker recording_thread_checker_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 161 | rtc::CriticalSection lock_; |
| 162 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 163 | // Task queue used to invoke LogStats() periodically. Tasks are executed on a |
| 164 | // worker thread but it does not necessarily have to be the same thread for |
| 165 | // each task. |
| 166 | rtc::TaskQueue task_queue_; |
| 167 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 168 | // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback() |
| 169 | // and it must outlive this object. It is not possible to change this member |
| 170 | // while any media is active. It is possible to start media without calling |
| 171 | // RegisterAudioCallback() but that will lead to ignored audio callbacks in |
| 172 | // both directions where native audio will be acive but no audio samples will |
| 173 | // be transported. |
| 174 | AudioTransport* audio_transport_cb_; |
| 175 | |
| 176 | // The members below that are not annotated are protected by design. They are |
| 177 | // all set on the main thread (verified by |main_thread_checker_|) and then |
| 178 | // read on either the playout or recording audio thread. But, media will never |
| 179 | // be active when the member is set; hence no conflict exists. It is too |
| 180 | // complex to ensure and verify that this is actually the case. |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 181 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 182 | // Sample rate in Hertz. |
| 183 | uint32_t rec_sample_rate_; |
| 184 | uint32_t play_sample_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 186 | // Number of audio channels. |
| 187 | size_t rec_channels_; |
| 188 | size_t play_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 189 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 190 | // Keeps track of if playout/recording are active or not. A combination |
| 191 | // of these states are used to determine when to start and stop the timer. |
| 192 | // Only used on the creating thread and not used to control any media flow. |
| 193 | bool playing_ ACCESS_ON(main_thread_checker_); |
| 194 | bool recording_ ACCESS_ON(main_thread_checker_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 195 | |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 196 | // Buffer used for audio samples to be played out. Size can be changed |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 197 | // dynamically. The 16-bit samples are interleaved, hence the size is |
| 198 | // proportional to the number of channels. |
| 199 | rtc::BufferT<int16_t> play_buffer_ ACCESS_ON(playout_thread_checker_); |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 200 | |
| 201 | // Byte buffer used for recorded audio samples. Size can be changed |
| 202 | // dynamically. |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 203 | rtc::BufferT<int16_t> rec_buffer_ ACCESS_ON(recording_thread_checker_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 205 | // AGC parameters. |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 206 | #if !defined(WEBRTC_WIN) |
| 207 | uint32_t current_mic_level_ ACCESS_ON(recording_thread_checker_); |
| 208 | #else |
| 209 | // Windows uses a dedicated thread for volume APIs. |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 210 | uint32_t current_mic_level_; |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 211 | #endif |
| 212 | uint32_t new_mic_level_ ACCESS_ON(recording_thread_checker_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 214 | // Contains true of a key-press has been detected. |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 215 | bool typing_status_ ACCESS_ON(recording_thread_checker_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 217 | // Delay values used by the AEC. |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 218 | int play_delay_ms_ ACCESS_ON(recording_thread_checker_); |
| 219 | int rec_delay_ms_ ACCESS_ON(recording_thread_checker_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 221 | // Contains a clock-drift measurement. |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 222 | int clock_drift_ ACCESS_ON(recording_thread_checker_); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 223 | |
| 224 | // Counts number of times LogStats() has been called. |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 225 | size_t num_stat_reports_ ACCESS_ON(task_queue_); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 226 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 227 | // Time stamp of last timer task (drives logging). |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 228 | int64_t last_timer_task_time_ ACCESS_ON(task_queue_); |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 229 | |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 230 | // Counts number of audio callbacks modulo 50 to create a signal when |
| 231 | // a new storage of audio stats shall be done. |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 232 | int16_t rec_stat_count_ ACCESS_ON(recording_thread_checker_); |
| 233 | int16_t play_stat_count_ ACCESS_ON(playout_thread_checker_); |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 234 | |
| 235 | // Time stamps of when playout and recording starts. |
nisse | deb95f3 | 2016-11-28 01:54:54 -0800 | [diff] [blame] | 236 | int64_t play_start_time_ ACCESS_ON(main_thread_checker_); |
| 237 | int64_t rec_start_time_ ACCESS_ON(main_thread_checker_); |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 238 | |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 239 | // Contains counters for playout and recording statistics. |
| 240 | Stats stats_ GUARDED_BY(lock_); |
| 241 | |
| 242 | // Stores current stats at each timer task. Used to calculate differences |
| 243 | // between two successive timer events. |
| 244 | Stats last_stats_ ACCESS_ON(task_queue_); |
| 245 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 246 | // Set to true at construction and modified to false as soon as one audio- |
| 247 | // level estimate larger than zero is detected. |
| 248 | bool only_silence_recorded_; |
henrika | 0b3a638 | 2016-11-11 02:28:50 -0800 | [diff] [blame] | 249 | |
| 250 | // Set to true when logging of audio stats is enabled for the first time in |
| 251 | // StartPeriodicLogging() and set to false by StopPeriodicLogging(). |
| 252 | // Setting this member to false prevents (possiby invalid) log messages from |
| 253 | // being printed in the LogStats() task. |
| 254 | bool log_stats_ ACCESS_ON(task_queue_); |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 255 | |
| 256 | // Should *never* be defined in production builds. Only used for testing. |
| 257 | // When defined, the output signal will be replaced by a sinus tone at 440Hz. |
| 258 | #ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE |
| 259 | double phase_ ACCESS_ON(playout_thread_checker_); |
| 260 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | } // namespace webrtc |
| 264 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 265 | #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |