blob: e384a31239002723ffe17369ef2064850cea94e5 [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
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
henrika49810512016-08-22 05:56:12 -070099 // 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.
henrika6c4d0f02016-07-14 05:54:19 -0700105 rtc::CriticalSection _critSect;
106 rtc::CriticalSection _critSectCb;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
henrika6c4d0f02016-07-14 05:54:19 -0700108 // 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
henrika49810512016-08-22 05:56:12 -0700116 // Sample rate in Hertz.
117 uint32_t rec_sample_rate_;
118 uint32_t play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000119
henrika49810512016-08-22 05:56:12 -0700120 // Number of audio channels.
121 size_t rec_channels_;
122 size_t play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
henrika0fd68012016-07-04 13:01:19 +0200124 // selected recording channel (left/right/both)
henrika49810512016-08-22 05:56:12 -0700125 AudioDeviceModule::ChannelType rec_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
henrika49810512016-08-22 05:56:12 -0700127 // 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.com470e71d2011-07-07 08:21:25 +0000130
henrika49810512016-08-22 05:56:12 -0700131 // 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.com470e71d2011-07-07 08:21:25 +0000136
henrika073378e2016-09-09 13:15:37 +0200137 // 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.
henrika49810512016-08-22 05:56:12 -0700140 std::unique_ptr<int8_t[]> rec_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000141
henrika073378e2016-09-09 13:15:37 +0200142 // 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.
henrika49810512016-08-22 05:56:12 -0700145 std::unique_ptr<int8_t[]> play_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
henrika49810512016-08-22 05:56:12 -0700147 // AGC parameters.
148 uint32_t current_mic_level_;
149 uint32_t new_mic_level_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
henrika49810512016-08-22 05:56:12 -0700151 // Contains true of a key-press has been detected.
152 bool typing_status_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
henrika49810512016-08-22 05:56:12 -0700154 // Delay values used by the AEC.
155 int play_delay_ms_;
156 int rec_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
henrika49810512016-08-22 05:56:12 -0700158 // Contains a clock-drift measurement.
159 int clock_drift_;
henrika6c4d0f02016-07-14 05:54:19 -0700160
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_;
henrika3d7346f2016-07-29 16:20:47 +0200192
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.com470e71d2011-07-07 08:21:25 +0000202};
203
204} // namespace webrtc
205
henrika6c4d0f02016-07-14 05:54:19 -0700206#endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_