blob: 04e1be8687c4c105cef9bc506cede57cad2ae3bb [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
pbos@webrtc.org811269d2013-07-11 13:24:38 +000014#include "webrtc/modules/audio_device/include/audio_device.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020015#include "webrtc/rtc_base/buffer.h"
16#include "webrtc/rtc_base/criticalsection.h"
17#include "webrtc/rtc_base/task_queue.h"
18#include "webrtc/rtc_base/thread_annotations.h"
19#include "webrtc/rtc_base/thread_checker.h"
tereliusc4b9b942016-10-28 06:51:59 -070020#include "webrtc/system_wrappers/include/file_wrapper.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000021#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23namespace webrtc {
henrika7be78832017-06-13 17:34:16 +020024
henrika3d7346f2016-07-29 16:20:47 +020025// Delta times between two successive playout callbacks are limited to this
26// value before added to an internal array.
27const size_t kMaxDeltaTimeInMs = 500;
henrika49810512016-08-22 05:56:12 -070028// TODO(henrika): remove when no longer used by external client.
29const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz
niklase@google.com470e71d2011-07-07 08:21:25 +000030
31class AudioDeviceObserver;
niklase@google.com470e71d2011-07-07 08:21:25 +000032
henrika0fd68012016-07-04 13:01:19 +020033class AudioDeviceBuffer {
34 public:
henrikaba156cf2016-10-31 08:18:50 -070035 enum LogState {
36 LOG_START = 0,
37 LOG_STOP,
38 LOG_ACTIVE,
39 };
40
henrika87d11cd2017-02-08 07:16:56 -080041 struct Stats {
42 void ResetRecStats() {
43 rec_callbacks = 0;
44 rec_samples = 0;
45 max_rec_level = 0;
46 }
47
48 void ResetPlayStats() {
49 play_callbacks = 0;
50 play_samples = 0;
51 max_play_level = 0;
52 }
53
54 // Total number of recording callbacks where the source provides 10ms audio
55 // data each time.
56 uint64_t rec_callbacks = 0;
57
58 // Total number of playback callbacks where the sink asks for 10ms audio
59 // data each time.
60 uint64_t play_callbacks = 0;
61
62 // Total number of recorded audio samples.
63 uint64_t rec_samples = 0;
64
65 // Total number of played audio samples.
66 uint64_t play_samples = 0;
67
68 // Contains max level (max(abs(x))) of recorded audio packets over the last
69 // 10 seconds where a new measurement is done twice per second. The level
70 // is reset to zero at each call to LogStats().
71 int16_t max_rec_level = 0;
72
73 // Contains max level of recorded audio packets over the last 10 seconds
74 // where a new measurement is done twice per second.
75 int16_t max_play_level = 0;
76 };
77
henrika0fd68012016-07-04 13:01:19 +020078 AudioDeviceBuffer();
79 virtual ~AudioDeviceBuffer();
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000080
tereliusc4b9b942016-10-28 06:51:59 -070081 void SetId(uint32_t id) {};
henrika49810512016-08-22 05:56:12 -070082 int32_t RegisterAudioCallback(AudioTransport* audio_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000083
henrikaba156cf2016-10-31 08:18:50 -070084 void StartPlayout();
85 void StartRecording();
86 void StopPlayout();
87 void StopRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000088
henrika49810512016-08-22 05:56:12 -070089 int32_t SetRecordingSampleRate(uint32_t fsHz);
90 int32_t SetPlayoutSampleRate(uint32_t fsHz);
henrika0fd68012016-07-04 13:01:19 +020091 int32_t RecordingSampleRate() const;
92 int32_t PlayoutSampleRate() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000093
henrika49810512016-08-22 05:56:12 -070094 int32_t SetRecordingChannels(size_t channels);
95 int32_t SetPlayoutChannels(size_t channels);
henrika0fd68012016-07-04 13:01:19 +020096 size_t RecordingChannels() const;
97 size_t PlayoutChannels() const;
98 int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel);
99 int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
henrika49810512016-08-22 05:56:12 -0700101 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -0800102 size_t samples_per_channel);
henrika0fd68012016-07-04 13:01:19 +0200103 int32_t SetCurrentMicLevel(uint32_t level);
henrika49810512016-08-22 05:56:12 -0700104 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms, int clock_drift);
henrika0fd68012016-07-04 13:01:19 +0200105 virtual int32_t DeliverRecordedData();
106 uint32_t NewMicLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
henrika51e96082016-11-10 00:40:37 -0800108 virtual int32_t RequestPlayoutData(size_t samples_per_channel);
henrika49810512016-08-22 05:56:12 -0700109 virtual int32_t GetPlayoutData(void* audio_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
henrika49810512016-08-22 05:56:12 -0700111 // TODO(henrika): these methods should not be used and does not contain any
112 // valid implementation. Investigate the possibility to either remove them
113 // or add a proper implementation if needed.
henrika0fd68012016-07-04 13:01:19 +0200114 int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]);
115 int32_t StopInputFileRecording();
116 int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]);
117 int32_t StopOutputFileRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
henrika49810512016-08-22 05:56:12 -0700119 int32_t SetTypingStatus(bool typing_status);
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000120
henrika0fd68012016-07-04 13:01:19 +0200121 private:
henrikaba156cf2016-10-31 08:18:50 -0700122 // Starts/stops periodic logging of audio stats.
123 void StartPeriodicLogging();
124 void StopPeriodicLogging();
henrika6c4d0f02016-07-14 05:54:19 -0700125
126 // Called periodically on the internal thread created by the TaskQueue.
henrikaba156cf2016-10-31 08:18:50 -0700127 // Updates some stats but dooes it on the task queue to ensure that access of
128 // members is serialized hence avoiding usage of locks.
129 // state = LOG_START => members are initialized and the timer starts.
130 // state = LOG_STOP => no logs are printed and the timer stops.
131 // state = LOG_ACTIVE => logs are printed and the timer is kept alive.
132 void LogStats(LogState state);
henrikaf06f35a2016-09-09 14:23:11 +0200133
henrika87d11cd2017-02-08 07:16:56 -0800134 // Updates counters in each play/record callback. These counters are later
135 // (periodically) read by LogStats() using a lock.
henrika51e96082016-11-10 00:40:37 -0800136 void UpdateRecStats(int16_t max_abs, size_t samples_per_channel);
137 void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel);
henrika6c4d0f02016-07-14 05:54:19 -0700138
henrikaba156cf2016-10-31 08:18:50 -0700139 // Clears all members tracking stats for recording and playout.
140 // These methods both run on the task queue.
141 void ResetRecStats();
142 void ResetPlayStats();
143
henrikaf5022222016-11-07 15:56:59 +0100144 // This object lives on the main (creating) thread and most methods are
145 // called on that same thread. When audio has started some methods will be
146 // called on either a native audio thread for playout or a native thread for
147 // recording. Some members are not annotated since they are "protected by
148 // design" and adding e.g. a race checker can cause failuries for very few
149 // edge cases and it is IMHO not worth the risk to use them in this class.
150 // TODO(henrika): see if it is possible to refactor and annotate all members.
henrika6c4d0f02016-07-14 05:54:19 -0700151
henrikaf5022222016-11-07 15:56:59 +0100152 // Main thread on which this object is created.
153 rtc::ThreadChecker main_thread_checker_;
henrika49810512016-08-22 05:56:12 -0700154
henrikaf5022222016-11-07 15:56:59 +0100155 // Native (platform specific) audio thread driving the playout side.
156 rtc::ThreadChecker playout_thread_checker_;
157
158 // Native (platform specific) audio thread driving the recording side.
159 rtc::ThreadChecker recording_thread_checker_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000160
henrika87d11cd2017-02-08 07:16:56 -0800161 rtc::CriticalSection lock_;
162
henrika6c4d0f02016-07-14 05:54:19 -0700163 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
164 // worker thread but it does not necessarily have to be the same thread for
165 // each task.
166 rtc::TaskQueue task_queue_;
167
henrikaf5022222016-11-07 15:56:59 +0100168 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback()
169 // and it must outlive this object. It is not possible to change this member
170 // while any media is active. It is possible to start media without calling
171 // RegisterAudioCallback() but that will lead to ignored audio callbacks in
172 // both directions where native audio will be acive but no audio samples will
173 // be transported.
174 AudioTransport* audio_transport_cb_;
175
176 // The members below that are not annotated are protected by design. They are
177 // all set on the main thread (verified by |main_thread_checker_|) and then
178 // read on either the playout or recording audio thread. But, media will never
179 // be active when the member is set; hence no conflict exists. It is too
180 // complex to ensure and verify that this is actually the case.
henrika6c4d0f02016-07-14 05:54:19 -0700181
henrika49810512016-08-22 05:56:12 -0700182 // Sample rate in Hertz.
183 uint32_t rec_sample_rate_;
184 uint32_t play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
henrika49810512016-08-22 05:56:12 -0700186 // Number of audio channels.
187 size_t rec_channels_;
188 size_t play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000189
henrikaf5022222016-11-07 15:56:59 +0100190 // Keeps track of if playout/recording are active or not. A combination
191 // of these states are used to determine when to start and stop the timer.
192 // Only used on the creating thread and not used to control any media flow.
193 bool playing_ ACCESS_ON(main_thread_checker_);
194 bool recording_ ACCESS_ON(main_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
henrika5588a132016-10-18 05:14:30 -0700196 // Buffer used for audio samples to be played out. Size can be changed
henrika51e96082016-11-10 00:40:37 -0800197 // dynamically. The 16-bit samples are interleaved, hence the size is
198 // proportional to the number of channels.
199 rtc::BufferT<int16_t> play_buffer_ ACCESS_ON(playout_thread_checker_);
henrikaf5022222016-11-07 15:56:59 +0100200
201 // Byte buffer used for recorded audio samples. Size can be changed
202 // dynamically.
henrika51e96082016-11-10 00:40:37 -0800203 rtc::BufferT<int16_t> rec_buffer_ ACCESS_ON(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000204
henrika49810512016-08-22 05:56:12 -0700205 // AGC parameters.
henrikaf5022222016-11-07 15:56:59 +0100206#if !defined(WEBRTC_WIN)
207 uint32_t current_mic_level_ ACCESS_ON(recording_thread_checker_);
208#else
209 // Windows uses a dedicated thread for volume APIs.
henrika49810512016-08-22 05:56:12 -0700210 uint32_t current_mic_level_;
henrikaf5022222016-11-07 15:56:59 +0100211#endif
212 uint32_t new_mic_level_ ACCESS_ON(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000213
henrika49810512016-08-22 05:56:12 -0700214 // Contains true of a key-press has been detected.
henrikaf5022222016-11-07 15:56:59 +0100215 bool typing_status_ ACCESS_ON(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000216
henrika49810512016-08-22 05:56:12 -0700217 // Delay values used by the AEC.
henrikaf5022222016-11-07 15:56:59 +0100218 int play_delay_ms_ ACCESS_ON(recording_thread_checker_);
219 int rec_delay_ms_ ACCESS_ON(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000220
henrika49810512016-08-22 05:56:12 -0700221 // Contains a clock-drift measurement.
henrikaf5022222016-11-07 15:56:59 +0100222 int clock_drift_ ACCESS_ON(recording_thread_checker_);
henrika6c4d0f02016-07-14 05:54:19 -0700223
224 // Counts number of times LogStats() has been called.
henrikaf5022222016-11-07 15:56:59 +0100225 size_t num_stat_reports_ ACCESS_ON(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700226
henrikaf5022222016-11-07 15:56:59 +0100227 // Time stamp of last timer task (drives logging).
nissedeb95f32016-11-28 01:54:54 -0800228 int64_t last_timer_task_time_ ACCESS_ON(task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200229
henrika3355f6d2016-10-21 12:45:25 +0200230 // Counts number of audio callbacks modulo 50 to create a signal when
231 // a new storage of audio stats shall be done.
henrikaf5022222016-11-07 15:56:59 +0100232 int16_t rec_stat_count_ ACCESS_ON(recording_thread_checker_);
233 int16_t play_stat_count_ ACCESS_ON(playout_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700234
235 // Time stamps of when playout and recording starts.
nissedeb95f32016-11-28 01:54:54 -0800236 int64_t play_start_time_ ACCESS_ON(main_thread_checker_);
237 int64_t rec_start_time_ ACCESS_ON(main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700238
henrika87d11cd2017-02-08 07:16:56 -0800239 // Contains counters for playout and recording statistics.
240 Stats stats_ GUARDED_BY(lock_);
241
242 // Stores current stats at each timer task. Used to calculate differences
243 // between two successive timer events.
244 Stats last_stats_ ACCESS_ON(task_queue_);
245
henrikaba156cf2016-10-31 08:18:50 -0700246 // Set to true at construction and modified to false as soon as one audio-
247 // level estimate larger than zero is detected.
248 bool only_silence_recorded_;
henrika0b3a6382016-11-11 02:28:50 -0800249
250 // Set to true when logging of audio stats is enabled for the first time in
251 // StartPeriodicLogging() and set to false by StopPeriodicLogging().
252 // Setting this member to false prevents (possiby invalid) log messages from
253 // being printed in the LogStats() task.
254 bool log_stats_ ACCESS_ON(task_queue_);
henrika7be78832017-06-13 17:34:16 +0200255
256// Should *never* be defined in production builds. Only used for testing.
257// When defined, the output signal will be replaced by a sinus tone at 440Hz.
258#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
259 double phase_ ACCESS_ON(playout_thread_checker_);
260#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000261};
262
263} // namespace webrtc
264
henrika6c4d0f02016-07-14 05:54:19 -0700265#endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_