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" |
terelius | c4b9b94 | 2016-10-28 06:51:59 -0700 | [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: |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame^] | 33 | enum LogState { |
| 34 | LOG_START = 0, |
| 35 | LOG_STOP, |
| 36 | LOG_ACTIVE, |
| 37 | }; |
| 38 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 39 | AudioDeviceBuffer(); |
| 40 | virtual ~AudioDeviceBuffer(); |
henrike@webrtc.org | 82f014a | 2013-09-10 18:24:07 +0000 | [diff] [blame] | 41 | |
terelius | c4b9b94 | 2016-10-28 06:51:59 -0700 | [diff] [blame] | 42 | void SetId(uint32_t id) {}; |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 43 | int32_t RegisterAudioCallback(AudioTransport* audio_callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame^] | 45 | void StartPlayout(); |
| 46 | void StartRecording(); |
| 47 | void StopPlayout(); |
| 48 | void StopRecording(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 50 | int32_t SetRecordingSampleRate(uint32_t fsHz); |
| 51 | int32_t SetPlayoutSampleRate(uint32_t fsHz); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 52 | int32_t RecordingSampleRate() const; |
| 53 | int32_t PlayoutSampleRate() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 55 | int32_t SetRecordingChannels(size_t channels); |
| 56 | int32_t SetPlayoutChannels(size_t channels); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 57 | size_t RecordingChannels() const; |
| 58 | size_t PlayoutChannels() const; |
| 59 | int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel); |
| 60 | int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 62 | virtual int32_t SetRecordedBuffer(const void* audio_buffer, |
| 63 | size_t num_samples); |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 64 | int32_t SetCurrentMicLevel(uint32_t level); |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 65 | 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] | 66 | virtual int32_t DeliverRecordedData(); |
| 67 | uint32_t NewMicLevel() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 69 | virtual int32_t RequestPlayoutData(size_t num_samples); |
| 70 | virtual int32_t GetPlayoutData(void* audio_buffer); |
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 | // TODO(henrika): these methods should not be used and does not contain any |
| 73 | // valid implementation. Investigate the possibility to either remove them |
| 74 | // or add a proper implementation if needed. |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 75 | int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 76 | int32_t StopInputFileRecording(); |
| 77 | int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 78 | int32_t StopOutputFileRecording(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 80 | int32_t SetTypingStatus(bool typing_status); |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 81 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 82 | private: |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame^] | 83 | // Starts/stops periodic logging of audio stats. |
| 84 | void StartPeriodicLogging(); |
| 85 | void StopPeriodicLogging(); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 86 | |
| 87 | // Called periodically on the internal thread created by the TaskQueue. |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame^] | 88 | // Updates some stats but dooes it on the task queue to ensure that access of |
| 89 | // members is serialized hence avoiding usage of locks. |
| 90 | // state = LOG_START => members are initialized and the timer starts. |
| 91 | // state = LOG_STOP => no logs are printed and the timer stops. |
| 92 | // state = LOG_ACTIVE => logs are printed and the timer is kept alive. |
| 93 | void LogStats(LogState state); |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 94 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 95 | // Updates counters in each play/record callback but does it on the task |
| 96 | // queue to ensure that they can be read by LogStats() without any locks since |
| 97 | // each task is serialized by the task queue. |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 98 | void UpdateRecStats(int16_t max_abs, size_t num_samples); |
| 99 | void UpdatePlayStats(int16_t max_abs, size_t num_samples); |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 100 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame^] | 101 | // Clears all members tracking stats for recording and playout. |
| 102 | // These methods both run on the task queue. |
| 103 | void ResetRecStats(); |
| 104 | void ResetPlayStats(); |
| 105 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 106 | // Ensures that methods are called on the same thread as the thread that |
| 107 | // creates this object. |
| 108 | rtc::ThreadChecker thread_checker_; |
| 109 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 110 | // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback() |
| 111 | // and it must outlive this object. |
| 112 | AudioTransport* audio_transport_cb_; |
| 113 | |
| 114 | // TODO(henrika): given usage of thread checker, it should be possible to |
| 115 | // remove all locks in this class. |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 116 | rtc::CriticalSection lock_; |
| 117 | rtc::CriticalSection lock_cb_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 119 | // 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 |
| 121 | // each task. |
| 122 | rtc::TaskQueue task_queue_; |
| 123 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame^] | 124 | // 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. |
| 126 | // Only used on the creating thread and not used to control any media flow. |
| 127 | bool playing_; |
| 128 | bool recording_; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 129 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 130 | // Sample rate in Hertz. |
| 131 | uint32_t rec_sample_rate_; |
| 132 | uint32_t play_sample_rate_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 134 | // Number of audio channels. |
| 135 | size_t rec_channels_; |
| 136 | size_t play_channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 138 | // Number of bytes per audio sample (2 or 4). |
| 139 | size_t rec_bytes_per_sample_; |
| 140 | size_t play_bytes_per_sample_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 142 | // Byte buffer used for recorded audio samples. Size can be changed |
| 143 | // dynamically. |
| 144 | rtc::Buffer rec_buffer_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | |
henrika | 5588a13 | 2016-10-18 05:14:30 -0700 | [diff] [blame] | 146 | // Buffer used for audio samples to be played out. Size can be changed |
| 147 | // dynamically. |
| 148 | rtc::Buffer play_buffer_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 149 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 150 | // AGC parameters. |
| 151 | uint32_t current_mic_level_; |
| 152 | uint32_t new_mic_level_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 154 | // Contains true of a key-press has been detected. |
| 155 | bool typing_status_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 157 | // Delay values used by the AEC. |
| 158 | int play_delay_ms_; |
| 159 | int rec_delay_ms_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | |
henrika | 4981051 | 2016-08-22 05:56:12 -0700 | [diff] [blame] | 161 | // Contains a clock-drift measurement. |
| 162 | int clock_drift_; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 163 | |
| 164 | // Counts number of times LogStats() has been called. |
| 165 | size_t num_stat_reports_; |
| 166 | |
| 167 | // Total number of recording callbacks where the source provides 10ms audio |
| 168 | // data each time. |
| 169 | uint64_t rec_callbacks_; |
| 170 | |
| 171 | // Total number of recording callbacks stored at the last timer task. |
| 172 | uint64_t last_rec_callbacks_; |
| 173 | |
| 174 | // Total number of playback callbacks where the sink asks for 10ms audio |
| 175 | // data each time. |
| 176 | uint64_t play_callbacks_; |
| 177 | |
| 178 | // Total number of playout callbacks stored at the last timer task. |
| 179 | uint64_t last_play_callbacks_; |
| 180 | |
| 181 | // Total number of recorded audio samples. |
| 182 | uint64_t rec_samples_; |
| 183 | |
| 184 | // Total number of recorded samples stored at the previous timer task. |
| 185 | uint64_t last_rec_samples_; |
| 186 | |
| 187 | // Total number of played audio samples. |
| 188 | uint64_t play_samples_; |
| 189 | |
| 190 | // Total number of played samples stored at the previous timer task. |
| 191 | uint64_t last_play_samples_; |
| 192 | |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame^] | 193 | // Time stamp of last timer task (drives logging). |
| 194 | uint64_t last_timer_task_time_; |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame] | 195 | |
| 196 | // Time stamp of last playout callback. |
| 197 | uint64_t last_playout_time_; |
| 198 | |
| 199 | // An array where the position corresponds to time differences (in |
| 200 | // milliseconds) between two successive playout callbacks, and the stored |
| 201 | // 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 |
| 203 | // destruction when no audio is running. |
| 204 | uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0}; |
henrika | f06f35a | 2016-09-09 14:23:11 +0200 | [diff] [blame] | 205 | |
| 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 | |
henrika | 3355f6d | 2016-10-21 12:45:25 +0200 | [diff] [blame] | 216 | // Counts number of audio callbacks modulo 50 to create a signal when |
| 217 | // a new storage of audio stats shall be done. |
| 218 | // Only updated on the OS-specific audio thread that drives audio. |
| 219 | int16_t rec_stat_count_; |
| 220 | int16_t play_stat_count_; |
henrika | ba156cf | 2016-10-31 08:18:50 -0700 | [diff] [blame^] | 221 | |
| 222 | // Time stamps of when playout and recording starts. |
| 223 | uint64_t play_start_time_; |
| 224 | uint64_t rec_start_time_; |
| 225 | |
| 226 | // Set to true at construction and modified to false as soon as one audio- |
| 227 | // level estimate larger than zero is detected. |
| 228 | bool only_silence_recorded_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | } // namespace webrtc |
| 232 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 233 | #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |