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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |
| 12 | #define MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 17 | #include <atomic> |
| 18 | |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 19 | #include "api/sequence_checker.h" |
Danil Chapovalov | 1c41be6 | 2019-04-01 09:16:12 +0200 | [diff] [blame] | 20 | #include "api/task_queue/task_queue_factory.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 21 | #include "modules/audio_device/include/audio_device_defines.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/buffer.h" |
Markus Handell | 5f61282 | 2020-07-08 10:13:20 +0200 | [diff] [blame] | 23 | #include "rtc_base/synchronization/mutex.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/task_queue.h" |
| 25 | #include "rtc_base/thread_annotations.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 28 | |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame] | 29 | // Delta times between two successive playout callbacks are limited to this |
| 30 | // value before added to an internal array. |
| 31 | const size_t kMaxDeltaTimeInMs = 500; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 32 | // TODO(henrika): remove when no longer used by external client. |
| 33 | const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 35 | class AudioDeviceBuffer { |
| 36 | public: |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 37 | enum LogState { |
| 38 | LOG_START = 0, |
| 39 | LOG_STOP, |
| 40 | LOG_ACTIVE, |
| 41 | }; |
| 42 | |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 43 | struct Stats { |
| 44 | void ResetRecStats() { |
| 45 | rec_callbacks = 0; |
| 46 | rec_samples = 0; |
| 47 | max_rec_level = 0; |
| 48 | } |
| 49 | |
| 50 | void ResetPlayStats() { |
| 51 | play_callbacks = 0; |
| 52 | play_samples = 0; |
| 53 | max_play_level = 0; |
| 54 | } |
| 55 | |
| 56 | // Total number of recording callbacks where the source provides 10ms audio |
| 57 | // data each time. |
| 58 | uint64_t rec_callbacks = 0; |
| 59 | |
| 60 | // Total number of playback callbacks where the sink asks for 10ms audio |
| 61 | // data each time. |
| 62 | uint64_t play_callbacks = 0; |
| 63 | |
| 64 | // Total number of recorded audio samples. |
| 65 | uint64_t rec_samples = 0; |
| 66 | |
| 67 | // Total number of played audio samples. |
| 68 | uint64_t play_samples = 0; |
| 69 | |
| 70 | // Contains max level (max(abs(x))) of recorded audio packets over the last |
| 71 | // 10 seconds where a new measurement is done twice per second. The level |
| 72 | // is reset to zero at each call to LogStats(). |
| 73 | int16_t max_rec_level = 0; |
| 74 | |
| 75 | // Contains max level of recorded audio packets over the last 10 seconds |
| 76 | // where a new measurement is done twice per second. |
| 77 | int16_t max_play_level = 0; |
| 78 | }; |
| 79 | |
Danil Chapovalov | 1c41be6 | 2019-04-01 09:16:12 +0200 | [diff] [blame] | 80 | explicit AudioDeviceBuffer(TaskQueueFactory* task_queue_factory); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 81 | virtual ~AudioDeviceBuffer(); |
henrike@webrtc.org | 82f014a | 2013-09-10 18:24:07 +0000 | [diff] [blame] | 82 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 83 | int32_t RegisterAudioCallback(AudioTransport* audio_callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 85 | void StartPlayout(); |
| 86 | void StartRecording(); |
| 87 | void StopPlayout(); |
| 88 | void StopRecording(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 90 | int32_t SetRecordingSampleRate(uint32_t fsHz); |
| 91 | int32_t SetPlayoutSampleRate(uint32_t fsHz); |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 92 | uint32_t RecordingSampleRate() const; |
| 93 | uint32_t PlayoutSampleRate() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 95 | int32_t SetRecordingChannels(size_t channels); |
| 96 | int32_t SetPlayoutChannels(size_t channels); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 97 | size_t RecordingChannels() const; |
| 98 | size_t PlayoutChannels() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 100 | virtual int32_t SetRecordedBuffer(const void* audio_buffer, |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 101 | size_t samples_per_channel); |
Fredrik Solenberg | 1a50cd5 | 2018-01-16 09:19:38 +0100 | [diff] [blame] | 102 | virtual void SetVQEData(int play_delay_ms, int rec_delay_ms); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 103 | virtual int32_t DeliverRecordedData(); |
| 104 | uint32_t NewMicLevel() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 106 | virtual int32_t RequestPlayoutData(size_t samples_per_channel); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 107 | virtual int32_t GetPlayoutData(void* audio_buffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 109 | int32_t SetTypingStatus(bool typing_status); |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 110 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 111 | private: |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 112 | // Starts/stops periodic logging of audio stats. |
| 113 | void StartPeriodicLogging(); |
| 114 | void StopPeriodicLogging(); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 115 | |
| 116 | // Called periodically on the internal thread created by the TaskQueue. |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 117 | // Updates some stats but dooes it on the task queue to ensure that access of |
| 118 | // members is serialized hence avoiding usage of locks. |
| 119 | // state = LOG_START => members are initialized and the timer starts. |
| 120 | // state = LOG_STOP => no logs are printed and the timer stops. |
| 121 | // state = LOG_ACTIVE => logs are printed and the timer is kept alive. |
| 122 | void LogStats(LogState state); |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 123 | |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 124 | // Updates counters in each play/record callback. These counters are later |
| 125 | // (periodically) read by LogStats() using a lock. |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 126 | void UpdateRecStats(int16_t max_abs, size_t samples_per_channel); |
| 127 | void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 128 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 129 | // Clears all members tracking stats for recording and playout. |
| 130 | // These methods both run on the task queue. |
| 131 | void ResetRecStats(); |
| 132 | void ResetPlayStats(); |
| 133 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 134 | // This object lives on the main (creating) thread and most methods are |
| 135 | // called on that same thread. When audio has started some methods will be |
| 136 | // called on either a native audio thread for playout or a native thread for |
| 137 | // recording. Some members are not annotated since they are "protected by |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 138 | // design" and adding e.g. a race checker can cause failures for very few |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 139 | // edge cases and it is IMHO not worth the risk to use them in this class. |
| 140 | // TODO(henrika): see if it is possible to refactor and annotate all members. |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 141 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 142 | // Main thread on which this object is created. |
Artem Titov | c8421c4 | 2021-02-02 10:57:19 +0100 | [diff] [blame] | 143 | SequenceChecker main_thread_checker_; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 144 | |
Markus Handell | 5f61282 | 2020-07-08 10:13:20 +0200 | [diff] [blame] | 145 | Mutex lock_; |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 146 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 147 | // Task queue used to invoke LogStats() periodically. Tasks are executed on a |
| 148 | // worker thread but it does not necessarily have to be the same thread for |
| 149 | // each task. |
| 150 | rtc::TaskQueue task_queue_; |
| 151 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 152 | // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback() |
| 153 | // and it must outlive this object. It is not possible to change this member |
| 154 | // while any media is active. It is possible to start media without calling |
| 155 | // RegisterAudioCallback() but that will lead to ignored audio callbacks in |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 156 | // both directions where native audio will be active but no audio samples will |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 157 | // be transported. |
| 158 | AudioTransport* audio_transport_cb_; |
| 159 | |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 160 | // Sample rate in Hertz. Accessed atomically. |
| 161 | std::atomic<uint32_t> rec_sample_rate_; |
| 162 | std::atomic<uint32_t> play_sample_rate_; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 163 | |
henrika | cfbd26d | 2018-09-05 11:36:22 +0200 | [diff] [blame] | 164 | // Number of audio channels. Accessed atomically. |
| 165 | std::atomic<size_t> rec_channels_; |
| 166 | std::atomic<size_t> play_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 168 | // Keeps track of if playout/recording are active or not. A combination |
| 169 | // of these states are used to determine when to start and stop the timer. |
| 170 | // Only used on the creating thread and not used to control any media flow. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 171 | bool playing_ RTC_GUARDED_BY(main_thread_checker_); |
| 172 | bool recording_ RTC_GUARDED_BY(main_thread_checker_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 173 | |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 174 | // Buffer used for audio samples to be played out. Size can be changed |
henrika | 51e9608 | 2016-11-10 00:40:37 -0800 | [diff] [blame] | 175 | // dynamically. The 16-bit samples are interleaved, hence the size is |
| 176 | // proportional to the number of channels. |
henrika | 36b3179 | 2018-09-13 13:01:14 +0200 | [diff] [blame] | 177 | rtc::BufferT<int16_t> play_buffer_; |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 178 | |
| 179 | // Byte buffer used for recorded audio samples. Size can be changed |
| 180 | // dynamically. |
henrika | 36b3179 | 2018-09-13 13:01:14 +0200 | [diff] [blame] | 181 | rtc::BufferT<int16_t> rec_buffer_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 183 | // Contains true of a key-press has been detected. |
henrika | 36b3179 | 2018-09-13 13:01:14 +0200 | [diff] [blame] | 184 | bool typing_status_; |
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 | // Delay values used by the AEC. |
henrika | 36b3179 | 2018-09-13 13:01:14 +0200 | [diff] [blame] | 187 | int play_delay_ms_; |
| 188 | int rec_delay_ms_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 189 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 190 | // Counts number of times LogStats() has been called. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 191 | size_t num_stat_reports_ RTC_GUARDED_BY(task_queue_); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 192 | |
henrika | f502222 | 2016-11-07 15:56:59 +0100 | [diff] [blame] | 193 | // Time stamp of last timer task (drives logging). |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 194 | int64_t last_timer_task_time_ RTC_GUARDED_BY(task_queue_); |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 195 | |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 196 | // Counts number of audio callbacks modulo 50 to create a signal when |
| 197 | // a new storage of audio stats shall be done. |
henrika | 36b3179 | 2018-09-13 13:01:14 +0200 | [diff] [blame] | 198 | int16_t rec_stat_count_; |
| 199 | int16_t play_stat_count_; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 200 | |
| 201 | // Time stamps of when playout and recording starts. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 202 | int64_t play_start_time_ RTC_GUARDED_BY(main_thread_checker_); |
| 203 | int64_t rec_start_time_ RTC_GUARDED_BY(main_thread_checker_); |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 204 | |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 205 | // Contains counters for playout and recording statistics. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 206 | Stats stats_ RTC_GUARDED_BY(lock_); |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 207 | |
| 208 | // Stores current stats at each timer task. Used to calculate differences |
| 209 | // between two successive timer events. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 210 | Stats last_stats_ RTC_GUARDED_BY(task_queue_); |
henrika | 87d11cd | 2017-02-08 07:16:56 -0800 | [diff] [blame] | 211 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame] | 212 | // Set to true at construction and modified to false as soon as one audio- |
| 213 | // level estimate larger than zero is detected. |
| 214 | bool only_silence_recorded_; |
henrika | 0b3a638 | 2016-11-11 02:28:50 -0800 | [diff] [blame] | 215 | |
| 216 | // Set to true when logging of audio stats is enabled for the first time in |
| 217 | // StartPeriodicLogging() and set to false by StopPeriodicLogging(). |
| 218 | // Setting this member to false prevents (possiby invalid) log messages from |
| 219 | // being printed in the LogStats() task. |
Niels Möller | 1e06289 | 2018-02-07 10:18:32 +0100 | [diff] [blame] | 220 | bool log_stats_ RTC_GUARDED_BY(task_queue_); |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 221 | |
| 222 | // Should *never* be defined in production builds. Only used for testing. |
| 223 | // When defined, the output signal will be replaced by a sinus tone at 440Hz. |
| 224 | #ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE |
henrika | 36b3179 | 2018-09-13 13:01:14 +0200 | [diff] [blame] | 225 | double phase_; |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 226 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | } // namespace webrtc |
| 230 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 231 | #endif // MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |