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