blob: 5c4a0638b321fbd9f804a311134dfdaa352b8071 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org28f39132012-03-01 18:01:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
henrika6c4d0f02016-07-14 05:54:19 -070011#ifndef WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
henrika6c4d0f02016-07-14 05:54:19 -070014#include "webrtc/base/criticalsection.h"
15#include "webrtc/base/task_queue.h"
16#include "webrtc/base/thread_checker.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000017#include "webrtc/modules/audio_device/include/audio_device.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010018#include "webrtc/system_wrappers/include/file_wrapper.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000019#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
22class CriticalSectionWrapper;
23
henrika3d7346f2016-07-29 16:20:47 +020024// Delta times between two successive playout callbacks are limited to this
25// value before added to an internal array.
26const size_t kMaxDeltaTimeInMs = 500;
henrika49810512016-08-22 05:56:12 -070027// TODO(henrika): remove when no longer used by external client.
28const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz
niklase@google.com470e71d2011-07-07 08:21:25 +000029
30class AudioDeviceObserver;
niklase@google.com470e71d2011-07-07 08:21:25 +000031
henrika0fd68012016-07-04 13:01:19 +020032class AudioDeviceBuffer {
33 public:
34 AudioDeviceBuffer();
35 virtual ~AudioDeviceBuffer();
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000036
henrika3f33e2a2016-07-06 00:33:57 -070037 void SetId(uint32_t id) {};
henrika49810512016-08-22 05:56:12 -070038 int32_t RegisterAudioCallback(AudioTransport* audio_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000039
henrika0fd68012016-07-04 13:01:19 +020040 int32_t InitPlayout();
41 int32_t InitRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000042
henrika49810512016-08-22 05:56:12 -070043 int32_t SetRecordingSampleRate(uint32_t fsHz);
44 int32_t SetPlayoutSampleRate(uint32_t fsHz);
henrika0fd68012016-07-04 13:01:19 +020045 int32_t RecordingSampleRate() const;
46 int32_t PlayoutSampleRate() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000047
henrika49810512016-08-22 05:56:12 -070048 int32_t SetRecordingChannels(size_t channels);
49 int32_t SetPlayoutChannels(size_t channels);
henrika0fd68012016-07-04 13:01:19 +020050 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.com470e71d2011-07-07 08:21:25 +000054
henrika49810512016-08-22 05:56:12 -070055 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
56 size_t num_samples);
henrika0fd68012016-07-04 13:01:19 +020057 int32_t SetCurrentMicLevel(uint32_t level);
henrika49810512016-08-22 05:56:12 -070058 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms, int clock_drift);
henrika0fd68012016-07-04 13:01:19 +020059 virtual int32_t DeliverRecordedData();
60 uint32_t NewMicLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000061
henrika49810512016-08-22 05:56:12 -070062 virtual int32_t RequestPlayoutData(size_t num_samples);
63 virtual int32_t GetPlayoutData(void* audio_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +000064
henrika49810512016-08-22 05:56:12 -070065 // 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.
henrika0fd68012016-07-04 13:01:19 +020068 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.com470e71d2011-07-07 08:21:25 +000072
henrika49810512016-08-22 05:56:12 -070073 int32_t SetTypingStatus(bool typing_status);
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +000074
henrika0fd68012016-07-04 13:01:19 +020075 private:
henrika073378e2016-09-09 13:15:37 +020076 // 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();
henrika49810512016-08-22 05:56:12 -070081
henrika6c4d0f02016-07-14 05:54:19 -070082 // 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
henrikaf06f35a2016-09-09 14:23:11 +020089 // Clears all members tracking stats for recording and playout.
90 void ResetRecStats();
91 void ResetPlayStats();
92
henrika6c4d0f02016-07-14 05:54:19 -070093 // Updates counters in each play/record callback but does it on the task
94 // queue to ensure that they can be read by LogStats() without any locks since
95 // each task is serialized by the task queue.
henrikaf06f35a2016-09-09 14:23:11 +020096 void UpdateRecStats(const void* audio_buffer, size_t num_samples);
97 void UpdatePlayStats(const void* audio_buffer, size_t num_samples);
henrika6c4d0f02016-07-14 05:54:19 -070098
99 // Ensures that methods are called on the same thread as the thread that
100 // creates this object.
101 rtc::ThreadChecker thread_checker_;
102
henrika49810512016-08-22 05:56:12 -0700103 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback()
104 // and it must outlive this object.
105 AudioTransport* audio_transport_cb_;
106
107 // TODO(henrika): given usage of thread checker, it should be possible to
108 // remove all locks in this class.
henrika6c4d0f02016-07-14 05:54:19 -0700109 rtc::CriticalSection _critSect;
110 rtc::CriticalSection _critSectCb;
niklase@google.com470e71d2011-07-07 08:21:25 +0000111
henrika6c4d0f02016-07-14 05:54:19 -0700112 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
113 // worker thread but it does not necessarily have to be the same thread for
114 // each task.
115 rtc::TaskQueue task_queue_;
116
117 // Ensures that the timer is only started once.
118 bool timer_has_started_;
119
henrika49810512016-08-22 05:56:12 -0700120 // Sample rate in Hertz.
121 uint32_t rec_sample_rate_;
122 uint32_t play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
henrika49810512016-08-22 05:56:12 -0700124 // Number of audio channels.
125 size_t rec_channels_;
126 size_t play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
henrika0fd68012016-07-04 13:01:19 +0200128 // selected recording channel (left/right/both)
henrika49810512016-08-22 05:56:12 -0700129 AudioDeviceModule::ChannelType rec_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
henrika49810512016-08-22 05:56:12 -0700131 // Number of bytes per audio sample (2 or 4).
132 size_t rec_bytes_per_sample_;
133 size_t play_bytes_per_sample_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
henrika49810512016-08-22 05:56:12 -0700135 // Number of audio samples/bytes per 10ms.
136 size_t rec_samples_per_10ms_;
137 size_t rec_bytes_per_10ms_;
138 size_t play_samples_per_10ms_;
139 size_t play_bytes_per_10ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000140
henrika073378e2016-09-09 13:15:37 +0200141 // Buffer used for recorded audio samples. Size is currently fixed
142 // but it should be changed to be dynamic and correspond to
143 // |play_bytes_per_10ms_|. TODO(henrika): avoid using fixed (max) size.
henrika49810512016-08-22 05:56:12 -0700144 std::unique_ptr<int8_t[]> rec_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
henrika073378e2016-09-09 13:15:37 +0200146 // Buffer used for audio samples to be played out. Size is currently fixed
147 // but it should be changed to be dynamic and correspond to
148 // |play_bytes_per_10ms_|. TODO(henrika): avoid using fixed (max) size.
henrika49810512016-08-22 05:56:12 -0700149 std::unique_ptr<int8_t[]> play_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
henrika49810512016-08-22 05:56:12 -0700151 // AGC parameters.
152 uint32_t current_mic_level_;
153 uint32_t new_mic_level_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
henrika49810512016-08-22 05:56:12 -0700155 // Contains true of a key-press has been detected.
156 bool typing_status_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
henrika49810512016-08-22 05:56:12 -0700158 // Delay values used by the AEC.
159 int play_delay_ms_;
160 int rec_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
henrika49810512016-08-22 05:56:12 -0700162 // Contains a clock-drift measurement.
163 int clock_drift_;
henrika6c4d0f02016-07-14 05:54:19 -0700164
165 // Counts number of times LogStats() has been called.
166 size_t num_stat_reports_;
167
168 // Total number of recording callbacks where the source provides 10ms audio
169 // data each time.
170 uint64_t rec_callbacks_;
171
172 // Total number of recording callbacks stored at the last timer task.
173 uint64_t last_rec_callbacks_;
174
175 // Total number of playback callbacks where the sink asks for 10ms audio
176 // data each time.
177 uint64_t play_callbacks_;
178
179 // Total number of playout callbacks stored at the last timer task.
180 uint64_t last_play_callbacks_;
181
182 // Total number of recorded audio samples.
183 uint64_t rec_samples_;
184
185 // Total number of recorded samples stored at the previous timer task.
186 uint64_t last_rec_samples_;
187
188 // Total number of played audio samples.
189 uint64_t play_samples_;
190
191 // Total number of played samples stored at the previous timer task.
192 uint64_t last_play_samples_;
193
194 // Time stamp of last stat report.
195 uint64_t last_log_stat_time_;
henrika3d7346f2016-07-29 16:20:47 +0200196
197 // Time stamp of last playout callback.
198 uint64_t last_playout_time_;
199
200 // An array where the position corresponds to time differences (in
201 // milliseconds) between two successive playout callbacks, and the stored
202 // value is the number of times a given time difference was found.
203 // Writing to the array is done without a lock since it is only read once at
204 // destruction when no audio is running.
205 uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0};
henrikaf06f35a2016-09-09 14:23:11 +0200206
207 // Contains max level (max(abs(x))) of recorded audio packets over the last
208 // 10 seconds where a new measurement is done twice per second. The level
209 // is reset to zero at each call to LogStats(). Only modified on the task
210 // queue thread.
211 int16_t max_rec_level_;
212
213 // Contains max level of recorded audio packets over the last 10 seconds
214 // where a new measurement is done twice per second.
215 int16_t max_play_level_;
216
217 // Counts number of times we detect "no audio" corresponding to a case where
218 // all level measurements since the last log has been exactly zero.
219 // In other words: this counter is incremented only if 20 measurements
220 // (two per second) in a row equals zero. The member is only incremented on
221 // the task queue and max once every 10th second.
222 size_t num_rec_level_is_zero_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000223};
224
225} // namespace webrtc
226
henrika6c4d0f02016-07-14 05:54:19 -0700227#endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_