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