blob: 6967ebd757147762b5291abd25d8c1a7940e0b0e [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
henrika5588a132016-10-18 05:14:30 -070014#include "webrtc/base/buffer.h"
henrika6c4d0f02016-07-14 05:54:19 -070015#include "webrtc/base/criticalsection.h"
16#include "webrtc/base/task_queue.h"
17#include "webrtc/base/thread_checker.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000018#include "webrtc/modules/audio_device/include/audio_device.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010019#include "webrtc/system_wrappers/include/file_wrapper.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000020#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
henrika3d7346f2016-07-29 16:20:47 +020023// Delta times between two successive playout callbacks are limited to this
24// value before added to an internal array.
25const size_t kMaxDeltaTimeInMs = 500;
henrika49810512016-08-22 05:56:12 -070026// TODO(henrika): remove when no longer used by external client.
27const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz
niklase@google.com470e71d2011-07-07 08:21:25 +000028
29class AudioDeviceObserver;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
henrika0fd68012016-07-04 13:01:19 +020031class AudioDeviceBuffer {
32 public:
33 AudioDeviceBuffer();
34 virtual ~AudioDeviceBuffer();
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000035
henrika3f33e2a2016-07-06 00:33:57 -070036 void SetId(uint32_t id) {};
henrika49810512016-08-22 05:56:12 -070037 int32_t RegisterAudioCallback(AudioTransport* audio_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000038
henrika0fd68012016-07-04 13:01:19 +020039 int32_t InitPlayout();
40 int32_t InitRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000041
henrika49810512016-08-22 05:56:12 -070042 int32_t SetRecordingSampleRate(uint32_t fsHz);
43 int32_t SetPlayoutSampleRate(uint32_t fsHz);
henrika0fd68012016-07-04 13:01:19 +020044 int32_t RecordingSampleRate() const;
45 int32_t PlayoutSampleRate() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000046
henrika49810512016-08-22 05:56:12 -070047 int32_t SetRecordingChannels(size_t channels);
48 int32_t SetPlayoutChannels(size_t channels);
henrika0fd68012016-07-04 13:01:19 +020049 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.com470e71d2011-07-07 08:21:25 +000053
henrika49810512016-08-22 05:56:12 -070054 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
55 size_t num_samples);
henrika0fd68012016-07-04 13:01:19 +020056 int32_t SetCurrentMicLevel(uint32_t level);
henrika49810512016-08-22 05:56:12 -070057 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms, int clock_drift);
henrika0fd68012016-07-04 13:01:19 +020058 virtual int32_t DeliverRecordedData();
59 uint32_t NewMicLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000060
henrika49810512016-08-22 05:56:12 -070061 virtual int32_t RequestPlayoutData(size_t num_samples);
62 virtual int32_t GetPlayoutData(void* audio_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +000063
henrika49810512016-08-22 05:56:12 -070064 // 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.
henrika0fd68012016-07-04 13:01:19 +020067 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.com470e71d2011-07-07 08:21:25 +000071
henrika49810512016-08-22 05:56:12 -070072 int32_t SetTypingStatus(bool typing_status);
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +000073
henrika0fd68012016-07-04 13:01:19 +020074 private:
henrika6c4d0f02016-07-14 05:54:19 -070075 // 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
henrikaf06f35a2016-09-09 14:23:11 +020082 // Clears all members tracking stats for recording and playout.
83 void ResetRecStats();
84 void ResetPlayStats();
85
henrika6c4d0f02016-07-14 05:54:19 -070086 // 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.
henrika3355f6d2016-10-21 12:45:25 +020089 void UpdateRecStats(int16_t max_abs, size_t num_samples);
90 void UpdatePlayStats(int16_t max_abs, size_t num_samples);
henrika6c4d0f02016-07-14 05:54:19 -070091
92 // Ensures that methods are called on the same thread as the thread that
93 // creates this object.
94 rtc::ThreadChecker thread_checker_;
95
henrika49810512016-08-22 05:56:12 -070096 // 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.
henrika5588a132016-10-18 05:14:30 -0700102 rtc::CriticalSection lock_;
103 rtc::CriticalSection lock_cb_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
henrika6c4d0f02016-07-14 05:54:19 -0700105 // 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
henrika49810512016-08-22 05:56:12 -0700113 // Sample rate in Hertz.
114 uint32_t rec_sample_rate_;
115 uint32_t play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
henrika49810512016-08-22 05:56:12 -0700117 // Number of audio channels.
118 size_t rec_channels_;
119 size_t play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
henrika49810512016-08-22 05:56:12 -0700121 // 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.com470e71d2011-07-07 08:21:25 +0000124
henrika5588a132016-10-18 05:14:30 -0700125 // Byte buffer used for recorded audio samples. Size can be changed
126 // dynamically.
127 rtc::Buffer rec_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
henrika5588a132016-10-18 05:14:30 -0700129 // Buffer used for audio samples to be played out. Size can be changed
130 // dynamically.
131 rtc::Buffer play_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
henrika49810512016-08-22 05:56:12 -0700133 // AGC parameters.
134 uint32_t current_mic_level_;
135 uint32_t new_mic_level_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
henrika49810512016-08-22 05:56:12 -0700137 // Contains true of a key-press has been detected.
138 bool typing_status_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000139
henrika49810512016-08-22 05:56:12 -0700140 // Delay values used by the AEC.
141 int play_delay_ms_;
142 int rec_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
henrika49810512016-08-22 05:56:12 -0700144 // Contains a clock-drift measurement.
145 int clock_drift_;
henrika6c4d0f02016-07-14 05:54:19 -0700146
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_;
henrika3d7346f2016-07-29 16:20:47 +0200178
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};
henrikaf06f35a2016-09-09 14:23:11 +0200188
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_;
henrika3355f6d2016-10-21 12:45:25 +0200205
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.com470e71d2011-07-07 08:21:25 +0000211};
212
213} // namespace webrtc
214
henrika6c4d0f02016-07-14 05:54:19 -0700215#endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_