blob: 7111f74e770a55e1adb865a9f5649c01eaa31c0c [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"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000019#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
henrika3d7346f2016-07-29 16:20:47 +020022// Delta times between two successive playout callbacks are limited to this
23// value before added to an internal array.
24const size_t kMaxDeltaTimeInMs = 500;
henrika49810512016-08-22 05:56:12 -070025// TODO(henrika): remove when no longer used by external client.
26const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz
niklase@google.com470e71d2011-07-07 08:21:25 +000027
28class AudioDeviceObserver;
niklase@google.com470e71d2011-07-07 08:21:25 +000029
henrika0fd68012016-07-04 13:01:19 +020030class AudioDeviceBuffer {
31 public:
32 AudioDeviceBuffer();
33 virtual ~AudioDeviceBuffer();
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000034
palmkvist04055e92016-10-28 02:08:23 -070035 void SetId(uint32_t id) {}
henrika49810512016-08-22 05:56:12 -070036 int32_t RegisterAudioCallback(AudioTransport* audio_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000037
henrika0fd68012016-07-04 13:01:19 +020038 int32_t InitPlayout();
39 int32_t InitRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000040
henrika49810512016-08-22 05:56:12 -070041 int32_t SetRecordingSampleRate(uint32_t fsHz);
42 int32_t SetPlayoutSampleRate(uint32_t fsHz);
henrika0fd68012016-07-04 13:01:19 +020043 int32_t RecordingSampleRate() const;
44 int32_t PlayoutSampleRate() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000045
henrika49810512016-08-22 05:56:12 -070046 int32_t SetRecordingChannels(size_t channels);
47 int32_t SetPlayoutChannels(size_t channels);
henrika0fd68012016-07-04 13:01:19 +020048 size_t RecordingChannels() const;
49 size_t PlayoutChannels() const;
50 int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel);
51 int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000052
henrika49810512016-08-22 05:56:12 -070053 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
54 size_t num_samples);
henrika0fd68012016-07-04 13:01:19 +020055 int32_t SetCurrentMicLevel(uint32_t level);
henrika49810512016-08-22 05:56:12 -070056 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms, int clock_drift);
henrika0fd68012016-07-04 13:01:19 +020057 virtual int32_t DeliverRecordedData();
58 uint32_t NewMicLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000059
henrika49810512016-08-22 05:56:12 -070060 virtual int32_t RequestPlayoutData(size_t num_samples);
61 virtual int32_t GetPlayoutData(void* audio_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +000062
henrika49810512016-08-22 05:56:12 -070063 // TODO(henrika): these methods should not be used and does not contain any
64 // valid implementation. Investigate the possibility to either remove them
65 // or add a proper implementation if needed.
henrika0fd68012016-07-04 13:01:19 +020066 int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]);
67 int32_t StopInputFileRecording();
68 int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]);
69 int32_t StopOutputFileRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000070
henrika49810512016-08-22 05:56:12 -070071 int32_t SetTypingStatus(bool typing_status);
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +000072
henrika0fd68012016-07-04 13:01:19 +020073 private:
henrika6c4d0f02016-07-14 05:54:19 -070074 // Posts the first delayed task in the task queue and starts the periodic
75 // timer.
76 void StartTimer();
77
78 // Called periodically on the internal thread created by the TaskQueue.
79 void LogStats();
80
henrikaf06f35a2016-09-09 14:23:11 +020081 // Clears all members tracking stats for recording and playout.
82 void ResetRecStats();
83 void ResetPlayStats();
84
henrika6c4d0f02016-07-14 05:54:19 -070085 // Updates counters in each play/record callback but does it on the task
86 // queue to ensure that they can be read by LogStats() without any locks since
87 // each task is serialized by the task queue.
henrika3355f6d2016-10-21 12:45:25 +020088 void UpdateRecStats(int16_t max_abs, size_t num_samples);
89 void UpdatePlayStats(int16_t max_abs, size_t num_samples);
henrika6c4d0f02016-07-14 05:54:19 -070090
91 // Ensures that methods are called on the same thread as the thread that
92 // creates this object.
93 rtc::ThreadChecker thread_checker_;
94
henrika49810512016-08-22 05:56:12 -070095 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback()
96 // and it must outlive this object.
97 AudioTransport* audio_transport_cb_;
98
99 // TODO(henrika): given usage of thread checker, it should be possible to
100 // remove all locks in this class.
henrika5588a132016-10-18 05:14:30 -0700101 rtc::CriticalSection lock_;
102 rtc::CriticalSection lock_cb_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
henrika6c4d0f02016-07-14 05:54:19 -0700104 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
105 // worker thread but it does not necessarily have to be the same thread for
106 // each task.
107 rtc::TaskQueue task_queue_;
108
109 // Ensures that the timer is only started once.
110 bool timer_has_started_;
111
henrika49810512016-08-22 05:56:12 -0700112 // Sample rate in Hertz.
113 uint32_t rec_sample_rate_;
114 uint32_t play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
henrika49810512016-08-22 05:56:12 -0700116 // Number of audio channels.
117 size_t rec_channels_;
118 size_t play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
henrika49810512016-08-22 05:56:12 -0700120 // Number of bytes per audio sample (2 or 4).
121 size_t rec_bytes_per_sample_;
122 size_t play_bytes_per_sample_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
henrika5588a132016-10-18 05:14:30 -0700124 // Byte buffer used for recorded audio samples. Size can be changed
125 // dynamically.
126 rtc::Buffer rec_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
henrika5588a132016-10-18 05:14:30 -0700128 // Buffer used for audio samples to be played out. Size can be changed
129 // dynamically.
130 rtc::Buffer play_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
henrika49810512016-08-22 05:56:12 -0700132 // AGC parameters.
133 uint32_t current_mic_level_;
134 uint32_t new_mic_level_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
henrika49810512016-08-22 05:56:12 -0700136 // Contains true of a key-press has been detected.
137 bool typing_status_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
henrika49810512016-08-22 05:56:12 -0700139 // Delay values used by the AEC.
140 int play_delay_ms_;
141 int rec_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000142
henrika49810512016-08-22 05:56:12 -0700143 // Contains a clock-drift measurement.
144 int clock_drift_;
henrika6c4d0f02016-07-14 05:54:19 -0700145
146 // Counts number of times LogStats() has been called.
147 size_t num_stat_reports_;
148
149 // Total number of recording callbacks where the source provides 10ms audio
150 // data each time.
151 uint64_t rec_callbacks_;
152
153 // Total number of recording callbacks stored at the last timer task.
154 uint64_t last_rec_callbacks_;
155
156 // Total number of playback callbacks where the sink asks for 10ms audio
157 // data each time.
158 uint64_t play_callbacks_;
159
160 // Total number of playout callbacks stored at the last timer task.
161 uint64_t last_play_callbacks_;
162
163 // Total number of recorded audio samples.
164 uint64_t rec_samples_;
165
166 // Total number of recorded samples stored at the previous timer task.
167 uint64_t last_rec_samples_;
168
169 // Total number of played audio samples.
170 uint64_t play_samples_;
171
172 // Total number of played samples stored at the previous timer task.
173 uint64_t last_play_samples_;
174
175 // Time stamp of last stat report.
176 uint64_t last_log_stat_time_;
henrika3d7346f2016-07-29 16:20:47 +0200177
178 // Time stamp of last playout callback.
179 uint64_t last_playout_time_;
180
181 // An array where the position corresponds to time differences (in
182 // milliseconds) between two successive playout callbacks, and the stored
183 // value is the number of times a given time difference was found.
184 // Writing to the array is done without a lock since it is only read once at
185 // destruction when no audio is running.
186 uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0};
henrikaf06f35a2016-09-09 14:23:11 +0200187
188 // Contains max level (max(abs(x))) of recorded audio packets over the last
189 // 10 seconds where a new measurement is done twice per second. The level
190 // is reset to zero at each call to LogStats(). Only modified on the task
191 // queue thread.
192 int16_t max_rec_level_;
193
194 // Contains max level of recorded audio packets over the last 10 seconds
195 // where a new measurement is done twice per second.
196 int16_t max_play_level_;
197
198 // Counts number of times we detect "no audio" corresponding to a case where
199 // all level measurements since the last log has been exactly zero.
200 // In other words: this counter is incremented only if 20 measurements
201 // (two per second) in a row equals zero. The member is only incremented on
202 // the task queue and max once every 10th second.
203 size_t num_rec_level_is_zero_;
henrika3355f6d2016-10-21 12:45:25 +0200204
205 // Counts number of audio callbacks modulo 50 to create a signal when
206 // a new storage of audio stats shall be done.
207 // Only updated on the OS-specific audio thread that drives audio.
208 int16_t rec_stat_count_;
209 int16_t play_stat_count_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210};
211
212} // namespace webrtc
213
henrika6c4d0f02016-07-14 05:54:19 -0700214#endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_