blob: 9754187c9bb668acf6c5ec22cb6704bbb755f1ec [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:
henrika49810512016-08-22 05:56:12 -070076 void AllocatePlayoutBufferIfNeeded();
77 void AllocateRecordingBufferIfNeeded();
78
henrika6c4d0f02016-07-14 05:54:19 -070079 // Posts the first delayed task in the task queue and starts the periodic
80 // timer.
81 void StartTimer();
82
83 // Called periodically on the internal thread created by the TaskQueue.
84 void LogStats();
85
86 // 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.
89 void UpdateRecStats(size_t num_samples);
90 void UpdatePlayStats(size_t num_samples);
91
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.
henrika6c4d0f02016-07-14 05:54:19 -0700102 rtc::CriticalSection _critSect;
103 rtc::CriticalSection _critSectCb;
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
henrika0fd68012016-07-04 13:01:19 +0200121 // selected recording channel (left/right/both)
henrika49810512016-08-22 05:56:12 -0700122 AudioDeviceModule::ChannelType rec_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
henrika49810512016-08-22 05:56:12 -0700124 // Number of bytes per audio sample (2 or 4).
125 size_t rec_bytes_per_sample_;
126 size_t play_bytes_per_sample_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000127
henrika49810512016-08-22 05:56:12 -0700128 // Number of audio samples/bytes per 10ms.
129 size_t rec_samples_per_10ms_;
130 size_t rec_bytes_per_10ms_;
131 size_t play_samples_per_10ms_;
132 size_t play_bytes_per_10ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
henrika49810512016-08-22 05:56:12 -0700134 // Buffer used for recorded audio samples. Size is given by
135 // |rec_bytes_per_10ms_| and the buffer is allocated in InitRecording() on the
136 // main/creating thread.
137 std::unique_ptr<int8_t[]> rec_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000138
henrika49810512016-08-22 05:56:12 -0700139 // Buffer used for audio samples to be played out. Size is given by
140 // |play_bytes_per_10ms_| and the buffer is allocated in InitPlayout() on the
141 // main/creating thread.
142 std::unique_ptr<int8_t[]> play_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000143
henrika49810512016-08-22 05:56:12 -0700144 // AGC parameters.
145 uint32_t current_mic_level_;
146 uint32_t new_mic_level_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
henrika49810512016-08-22 05:56:12 -0700148 // Contains true of a key-press has been detected.
149 bool typing_status_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
henrika49810512016-08-22 05:56:12 -0700151 // Delay values used by the AEC.
152 int play_delay_ms_;
153 int rec_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
henrika49810512016-08-22 05:56:12 -0700155 // Contains a clock-drift measurement.
156 int clock_drift_;
henrika6c4d0f02016-07-14 05:54:19 -0700157
158 // Counts number of times LogStats() has been called.
159 size_t num_stat_reports_;
160
161 // Total number of recording callbacks where the source provides 10ms audio
162 // data each time.
163 uint64_t rec_callbacks_;
164
165 // Total number of recording callbacks stored at the last timer task.
166 uint64_t last_rec_callbacks_;
167
168 // Total number of playback callbacks where the sink asks for 10ms audio
169 // data each time.
170 uint64_t play_callbacks_;
171
172 // Total number of playout callbacks stored at the last timer task.
173 uint64_t last_play_callbacks_;
174
175 // Total number of recorded audio samples.
176 uint64_t rec_samples_;
177
178 // Total number of recorded samples stored at the previous timer task.
179 uint64_t last_rec_samples_;
180
181 // Total number of played audio samples.
182 uint64_t play_samples_;
183
184 // Total number of played samples stored at the previous timer task.
185 uint64_t last_play_samples_;
186
187 // Time stamp of last stat report.
188 uint64_t last_log_stat_time_;
henrika3d7346f2016-07-29 16:20:47 +0200189
190 // Time stamp of last playout callback.
191 uint64_t last_playout_time_;
192
193 // An array where the position corresponds to time differences (in
194 // milliseconds) between two successive playout callbacks, and the stored
195 // value is the number of times a given time difference was found.
196 // Writing to the array is done without a lock since it is only read once at
197 // destruction when no audio is running.
198 uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000199};
200
201} // namespace webrtc
202
henrika6c4d0f02016-07-14 05:54:19 -0700203#endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_