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 | |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 14 | #include "webrtc/base/buffer.h" |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 15 | #include "webrtc/base/criticalsection.h" |
| 16 | #include "webrtc/base/task_queue.h" |
| 17 | #include "webrtc/base/thread_checker.h" |
pbos@webrtc.org | 811269d | 2013-07-11 13:24:38 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/audio_device/include/audio_device.h" |
pbos@webrtc.org | 811269d | 2013-07-11 13:24:38 +0000 | [diff] [blame] | 19 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame] | 22 | // Delta times between two successive playout callbacks are limited to this |
| 23 | // value before added to an internal array. |
| 24 | const size_t kMaxDeltaTimeInMs = 500; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 25 | // TODO(henrika): remove when no longer used by external client. |
| 26 | const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
| 28 | class AudioDeviceObserver; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 30 | class AudioDeviceBuffer { |
| 31 | public: |
| 32 | AudioDeviceBuffer(); |
| 33 | virtual ~AudioDeviceBuffer(); |
henrike@webrtc.org | 82f014a | 2013-09-10 18:24:07 +0000 | [diff] [blame] | 34 | |
palmkvist | 04055e9 | 2016-10-28 02:08:23 -0700 | [diff] [blame] | 35 | void SetId(uint32_t id) {} |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 36 | int32_t RegisterAudioCallback(AudioTransport* audio_callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 38 | int32_t InitPlayout(); |
| 39 | int32_t InitRecording(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 41 | int32_t SetRecordingSampleRate(uint32_t fsHz); |
| 42 | int32_t SetPlayoutSampleRate(uint32_t fsHz); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 43 | int32_t RecordingSampleRate() const; |
| 44 | int32_t PlayoutSampleRate() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 46 | int32_t SetRecordingChannels(size_t channels); |
| 47 | int32_t SetPlayoutChannels(size_t channels); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 48 | size_t RecordingChannels() const; |
| 49 | size_t PlayoutChannels() const; |
| 50 | int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel); |
| 51 | int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 53 | virtual int32_t SetRecordedBuffer(const void* audio_buffer, |
| 54 | size_t num_samples); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 55 | int32_t SetCurrentMicLevel(uint32_t level); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 56 | 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] | 57 | virtual int32_t DeliverRecordedData(); |
| 58 | uint32_t NewMicLevel() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 60 | virtual int32_t RequestPlayoutData(size_t num_samples); |
| 61 | virtual int32_t GetPlayoutData(void* audio_buffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 63 | // TODO(henrika): these methods should not be used and does not contain any |
| 64 | // valid implementation. Investigate the possibility to either remove them |
| 65 | // or add a proper implementation if needed. |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 66 | int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 67 | int32_t StopInputFileRecording(); |
| 68 | int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 69 | int32_t StopOutputFileRecording(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 71 | int32_t SetTypingStatus(bool typing_status); |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 72 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 73 | private: |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 74 | // Posts the first delayed task in the task queue and starts the periodic |
| 75 | // timer. |
| 76 | void StartTimer(); |
| 77 | |
| 78 | // Called periodically on the internal thread created by the TaskQueue. |
| 79 | void LogStats(); |
| 80 | |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 81 | // Clears all members tracking stats for recording and playout. |
| 82 | void ResetRecStats(); |
| 83 | void ResetPlayStats(); |
| 84 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 85 | // Updates counters in each play/record callback but does it on the task |
| 86 | // queue to ensure that they can be read by LogStats() without any locks since |
| 87 | // each task is serialized by the task queue. |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 88 | void UpdateRecStats(int16_t max_abs, size_t num_samples); |
| 89 | void UpdatePlayStats(int16_t max_abs, size_t num_samples); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 90 | |
| 91 | // Ensures that methods are called on the same thread as the thread that |
| 92 | // creates this object. |
| 93 | rtc::ThreadChecker thread_checker_; |
| 94 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 95 | // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback() |
| 96 | // and it must outlive this object. |
| 97 | AudioTransport* audio_transport_cb_; |
| 98 | |
| 99 | // TODO(henrika): given usage of thread checker, it should be possible to |
| 100 | // remove all locks in this class. |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 101 | rtc::CriticalSection lock_; |
| 102 | rtc::CriticalSection lock_cb_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 103 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 104 | // Task queue used to invoke LogStats() periodically. Tasks are executed on a |
| 105 | // worker thread but it does not necessarily have to be the same thread for |
| 106 | // each task. |
| 107 | rtc::TaskQueue task_queue_; |
| 108 | |
| 109 | // Ensures that the timer is only started once. |
| 110 | bool timer_has_started_; |
| 111 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 112 | // Sample rate in Hertz. |
| 113 | uint32_t rec_sample_rate_; |
| 114 | uint32_t play_sample_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 116 | // Number of audio channels. |
| 117 | size_t rec_channels_; |
| 118 | size_t play_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 120 | // Number of bytes per audio sample (2 or 4). |
| 121 | size_t rec_bytes_per_sample_; |
| 122 | size_t play_bytes_per_sample_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 124 | // Byte buffer used for recorded audio samples. Size can be changed |
| 125 | // dynamically. |
| 126 | rtc::Buffer rec_buffer_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 128 | // Buffer used for audio samples to be played out. Size can be changed |
| 129 | // dynamically. |
| 130 | rtc::Buffer play_buffer_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 131 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 132 | // AGC parameters. |
| 133 | uint32_t current_mic_level_; |
| 134 | uint32_t new_mic_level_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 135 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 136 | // Contains true of a key-press has been detected. |
| 137 | bool typing_status_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 139 | // Delay values used by the AEC. |
| 140 | int play_delay_ms_; |
| 141 | int rec_delay_ms_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 143 | // Contains a clock-drift measurement. |
| 144 | int clock_drift_; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 145 | |
| 146 | // Counts number of times LogStats() has been called. |
| 147 | size_t num_stat_reports_; |
| 148 | |
| 149 | // Total number of recording callbacks where the source provides 10ms audio |
| 150 | // data each time. |
| 151 | uint64_t rec_callbacks_; |
| 152 | |
| 153 | // Total number of recording callbacks stored at the last timer task. |
| 154 | uint64_t last_rec_callbacks_; |
| 155 | |
| 156 | // Total number of playback callbacks where the sink asks for 10ms audio |
| 157 | // data each time. |
| 158 | uint64_t play_callbacks_; |
| 159 | |
| 160 | // Total number of playout callbacks stored at the last timer task. |
| 161 | uint64_t last_play_callbacks_; |
| 162 | |
| 163 | // Total number of recorded audio samples. |
| 164 | uint64_t rec_samples_; |
| 165 | |
| 166 | // Total number of recorded samples stored at the previous timer task. |
| 167 | uint64_t last_rec_samples_; |
| 168 | |
| 169 | // Total number of played audio samples. |
| 170 | uint64_t play_samples_; |
| 171 | |
| 172 | // Total number of played samples stored at the previous timer task. |
| 173 | uint64_t last_play_samples_; |
| 174 | |
| 175 | // Time stamp of last stat report. |
| 176 | uint64_t last_log_stat_time_; |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame] | 177 | |
| 178 | // Time stamp of last playout callback. |
| 179 | uint64_t last_playout_time_; |
| 180 | |
| 181 | // An array where the position corresponds to time differences (in |
| 182 | // milliseconds) between two successive playout callbacks, and the stored |
| 183 | // value is the number of times a given time difference was found. |
| 184 | // Writing to the array is done without a lock since it is only read once at |
| 185 | // destruction when no audio is running. |
| 186 | uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0}; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 187 | |
| 188 | // Contains max level (max(abs(x))) of recorded audio packets over the last |
| 189 | // 10 seconds where a new measurement is done twice per second. The level |
| 190 | // is reset to zero at each call to LogStats(). Only modified on the task |
| 191 | // queue thread. |
| 192 | int16_t max_rec_level_; |
| 193 | |
| 194 | // Contains max level of recorded audio packets over the last 10 seconds |
| 195 | // where a new measurement is done twice per second. |
| 196 | int16_t max_play_level_; |
| 197 | |
| 198 | // Counts number of times we detect "no audio" corresponding to a case where |
| 199 | // all level measurements since the last log has been exactly zero. |
| 200 | // In other words: this counter is incremented only if 20 measurements |
| 201 | // (two per second) in a row equals zero. The member is only incremented on |
| 202 | // the task queue and max once every 10th second. |
| 203 | size_t num_rec_level_is_zero_; |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 204 | |
| 205 | // Counts number of audio callbacks modulo 50 to create a signal when |
| 206 | // a new storage of audio stats shall be done. |
| 207 | // Only updated on the OS-specific audio thread that drives audio. |
| 208 | int16_t rec_stat_count_; |
| 209 | int16_t play_stat_count_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | } // namespace webrtc |
| 213 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 214 | #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |