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 | |
pbos@webrtc.org | 2550988 | 2013-04-09 10:30:35 +0000 | [diff] [blame] | 24 | const uint32_t kPulsePeriodMs = 1000; |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 25 | const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame^] | 26 | // Delta times between two successive playout callbacks are limited to this |
| 27 | // value before added to an internal array. |
| 28 | const size_t kMaxDeltaTimeInMs = 500; |
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 | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 38 | int32_t RegisterAudioCallback(AudioTransport* audioCallback); |
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 | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 43 | virtual int32_t SetRecordingSampleRate(uint32_t fsHz); |
| 44 | virtual int32_t SetPlayoutSampleRate(uint32_t fsHz); |
| 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 | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 48 | virtual int32_t SetRecordingChannels(size_t channels); |
| 49 | virtual int32_t SetPlayoutChannels(size_t channels); |
| 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 | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 55 | virtual int32_t SetRecordedBuffer(const void* audioBuffer, size_t nSamples); |
| 56 | int32_t SetCurrentMicLevel(uint32_t level); |
| 57 | virtual void SetVQEData(int playDelayMS, int recDelayMS, int clockDrift); |
| 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 | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 61 | virtual int32_t RequestPlayoutData(size_t nSamples); |
| 62 | virtual int32_t GetPlayoutData(void* audioBuffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 64 | int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 65 | int32_t StopInputFileRecording(); |
| 66 | int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 67 | int32_t StopOutputFileRecording(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 69 | int32_t SetTypingStatus(bool typingStatus); |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 70 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 71 | private: |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 72 | // Posts the first delayed task in the task queue and starts the periodic |
| 73 | // timer. |
| 74 | void StartTimer(); |
| 75 | |
| 76 | // Called periodically on the internal thread created by the TaskQueue. |
| 77 | void LogStats(); |
| 78 | |
| 79 | // Updates counters in each play/record callback but does it on the task |
| 80 | // queue to ensure that they can be read by LogStats() without any locks since |
| 81 | // each task is serialized by the task queue. |
| 82 | void UpdateRecStats(size_t num_samples); |
| 83 | void UpdatePlayStats(size_t num_samples); |
| 84 | |
| 85 | // Ensures that methods are called on the same thread as the thread that |
| 86 | // creates this object. |
| 87 | rtc::ThreadChecker thread_checker_; |
| 88 | |
| 89 | rtc::CriticalSection _critSect; |
| 90 | rtc::CriticalSection _critSectCb; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 91 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 92 | AudioTransport* _ptrCbAudioTransport; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 94 | // Task queue used to invoke LogStats() periodically. Tasks are executed on a |
| 95 | // worker thread but it does not necessarily have to be the same thread for |
| 96 | // each task. |
| 97 | rtc::TaskQueue task_queue_; |
| 98 | |
| 99 | // Ensures that the timer is only started once. |
| 100 | bool timer_has_started_; |
| 101 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 102 | uint32_t _recSampleRate; |
| 103 | uint32_t _playSampleRate; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 105 | size_t _recChannels; |
| 106 | size_t _playChannels; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 108 | // selected recording channel (left/right/both) |
| 109 | AudioDeviceModule::ChannelType _recChannel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 111 | // 2 or 4 depending on mono or stereo |
| 112 | size_t _recBytesPerSample; |
| 113 | size_t _playBytesPerSample; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 115 | // 10ms in stereo @ 96kHz |
| 116 | int8_t _recBuffer[kMaxBufferSizeBytes]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 118 | // one sample <=> 2 or 4 bytes |
| 119 | size_t _recSamples; |
| 120 | size_t _recSize; // in bytes |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 122 | // 10ms in stereo @ 96kHz |
| 123 | int8_t _playBuffer[kMaxBufferSizeBytes]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 125 | // one sample <=> 2 or 4 bytes |
| 126 | size_t _playSamples; |
| 127 | size_t _playSize; // in bytes |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 129 | FileWrapper& _recFile; |
| 130 | FileWrapper& _playFile; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 131 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 132 | uint32_t _currentMicLevel; |
| 133 | uint32_t _newMicLevel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 135 | bool _typingStatus; |
niklas.enbom@webrtc.org | 3be565b | 2013-05-07 21:04:24 +0000 | [diff] [blame] | 136 | |
henrika | 0fd6801 | 2016-07-04 13:01:19 +0200 | [diff] [blame] | 137 | int _playDelayMS; |
| 138 | int _recDelayMS; |
| 139 | int _clockDrift; |
| 140 | int high_delay_counter_; |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 141 | |
| 142 | // Counts number of times LogStats() has been called. |
| 143 | size_t num_stat_reports_; |
| 144 | |
| 145 | // Total number of recording callbacks where the source provides 10ms audio |
| 146 | // data each time. |
| 147 | uint64_t rec_callbacks_; |
| 148 | |
| 149 | // Total number of recording callbacks stored at the last timer task. |
| 150 | uint64_t last_rec_callbacks_; |
| 151 | |
| 152 | // Total number of playback callbacks where the sink asks for 10ms audio |
| 153 | // data each time. |
| 154 | uint64_t play_callbacks_; |
| 155 | |
| 156 | // Total number of playout callbacks stored at the last timer task. |
| 157 | uint64_t last_play_callbacks_; |
| 158 | |
| 159 | // Total number of recorded audio samples. |
| 160 | uint64_t rec_samples_; |
| 161 | |
| 162 | // Total number of recorded samples stored at the previous timer task. |
| 163 | uint64_t last_rec_samples_; |
| 164 | |
| 165 | // Total number of played audio samples. |
| 166 | uint64_t play_samples_; |
| 167 | |
| 168 | // Total number of played samples stored at the previous timer task. |
| 169 | uint64_t last_play_samples_; |
| 170 | |
| 171 | // Time stamp of last stat report. |
| 172 | uint64_t last_log_stat_time_; |
henrika | 3d7346f | 2016-07-29 16:20:47 +0200 | [diff] [blame^] | 173 | |
| 174 | // Time stamp of last playout callback. |
| 175 | uint64_t last_playout_time_; |
| 176 | |
| 177 | // An array where the position corresponds to time differences (in |
| 178 | // milliseconds) between two successive playout callbacks, and the stored |
| 179 | // value is the number of times a given time difference was found. |
| 180 | // Writing to the array is done without a lock since it is only read once at |
| 181 | // destruction when no audio is running. |
| 182 | uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0}; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | } // namespace webrtc |
| 186 | |
henrika | 6c4d0f0 | 2016-07-14 05:54:19 -0700 | [diff] [blame] | 187 | #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |