blob: c4ef95b3211cee7069b290f40557a0be0c6263c0 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_
12#define MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/audio_device/include/audio_device.h"
15#include "rtc_base/buffer.h"
16#include "rtc_base/criticalsection.h"
17#include "rtc_base/task_queue.h"
18#include "rtc_base/thread_annotations.h"
19#include "rtc_base/thread_checker.h"
20#include "system_wrappers/include/file_wrapper.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020021#include "typedefs.h" // NOLINT(build/include)
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
henrika0fd68012016-07-04 13:01:19 +020031class AudioDeviceBuffer {
32 public:
henrikaba156cf2016-10-31 08:18:50 -070033 enum LogState {
34 LOG_START = 0,
35 LOG_STOP,
36 LOG_ACTIVE,
37 };
38
henrika87d11cd2017-02-08 07:16:56 -080039 struct Stats {
40 void ResetRecStats() {
41 rec_callbacks = 0;
42 rec_samples = 0;
43 max_rec_level = 0;
44 }
45
46 void ResetPlayStats() {
47 play_callbacks = 0;
48 play_samples = 0;
49 max_play_level = 0;
50 }
51
52 // Total number of recording callbacks where the source provides 10ms audio
53 // data each time.
54 uint64_t rec_callbacks = 0;
55
56 // Total number of playback callbacks where the sink asks for 10ms audio
57 // data each time.
58 uint64_t play_callbacks = 0;
59
60 // Total number of recorded audio samples.
61 uint64_t rec_samples = 0;
62
63 // Total number of played audio samples.
64 uint64_t play_samples = 0;
65
66 // Contains max level (max(abs(x))) of recorded audio packets over the last
67 // 10 seconds where a new measurement is done twice per second. The level
68 // is reset to zero at each call to LogStats().
69 int16_t max_rec_level = 0;
70
71 // Contains max level of recorded audio packets over the last 10 seconds
72 // where a new measurement is done twice per second.
73 int16_t max_play_level = 0;
74 };
75
henrika0fd68012016-07-04 13:01:19 +020076 AudioDeviceBuffer();
77 virtual ~AudioDeviceBuffer();
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000078
henrika49810512016-08-22 05:56:12 -070079 int32_t RegisterAudioCallback(AudioTransport* audio_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000080
henrikaba156cf2016-10-31 08:18:50 -070081 void StartPlayout();
82 void StartRecording();
83 void StopPlayout();
84 void StopRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000085
henrika49810512016-08-22 05:56:12 -070086 int32_t SetRecordingSampleRate(uint32_t fsHz);
87 int32_t SetPlayoutSampleRate(uint32_t fsHz);
henrika0fd68012016-07-04 13:01:19 +020088 int32_t RecordingSampleRate() const;
89 int32_t PlayoutSampleRate() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000090
henrika49810512016-08-22 05:56:12 -070091 int32_t SetRecordingChannels(size_t channels);
92 int32_t SetPlayoutChannels(size_t channels);
henrika0fd68012016-07-04 13:01:19 +020093 size_t RecordingChannels() const;
94 size_t PlayoutChannels() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000095
henrika49810512016-08-22 05:56:12 -070096 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -080097 size_t samples_per_channel);
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +010098 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms);
henrika0fd68012016-07-04 13:01:19 +020099 virtual int32_t DeliverRecordedData();
100 uint32_t NewMicLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
henrika51e96082016-11-10 00:40:37 -0800102 virtual int32_t RequestPlayoutData(size_t samples_per_channel);
henrika49810512016-08-22 05:56:12 -0700103 virtual int32_t GetPlayoutData(void* audio_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
henrika49810512016-08-22 05:56:12 -0700105 int32_t SetTypingStatus(bool typing_status);
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000106
henrika09a76192017-08-23 15:04:40 +0200107 // Called on iOS where the native audio layer can be interrupted by other
108 // audio applications. This method can then be used to reset internal
109 // states and detach thread checkers to allow for a new audio session where
110 // native callbacks may come from a new set of I/O threads.
111 void NativeAudioInterrupted();
112
henrika0fd68012016-07-04 13:01:19 +0200113 private:
henrikaba156cf2016-10-31 08:18:50 -0700114 // Starts/stops periodic logging of audio stats.
115 void StartPeriodicLogging();
116 void StopPeriodicLogging();
henrika6c4d0f02016-07-14 05:54:19 -0700117
118 // Called periodically on the internal thread created by the TaskQueue.
henrikaba156cf2016-10-31 08:18:50 -0700119 // Updates some stats but dooes it on the task queue to ensure that access of
120 // members is serialized hence avoiding usage of locks.
121 // state = LOG_START => members are initialized and the timer starts.
122 // state = LOG_STOP => no logs are printed and the timer stops.
123 // state = LOG_ACTIVE => logs are printed and the timer is kept alive.
124 void LogStats(LogState state);
henrikaf06f35a2016-09-09 14:23:11 +0200125
henrika87d11cd2017-02-08 07:16:56 -0800126 // Updates counters in each play/record callback. These counters are later
127 // (periodically) read by LogStats() using a lock.
henrika51e96082016-11-10 00:40:37 -0800128 void UpdateRecStats(int16_t max_abs, size_t samples_per_channel);
129 void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel);
henrika6c4d0f02016-07-14 05:54:19 -0700130
henrikaba156cf2016-10-31 08:18:50 -0700131 // Clears all members tracking stats for recording and playout.
132 // These methods both run on the task queue.
133 void ResetRecStats();
134 void ResetPlayStats();
135
henrikaf5022222016-11-07 15:56:59 +0100136 // This object lives on the main (creating) thread and most methods are
137 // called on that same thread. When audio has started some methods will be
138 // called on either a native audio thread for playout or a native thread for
139 // recording. Some members are not annotated since they are "protected by
140 // design" and adding e.g. a race checker can cause failuries for very few
141 // edge cases and it is IMHO not worth the risk to use them in this class.
142 // TODO(henrika): see if it is possible to refactor and annotate all members.
henrika6c4d0f02016-07-14 05:54:19 -0700143
henrikaf5022222016-11-07 15:56:59 +0100144 // Main thread on which this object is created.
145 rtc::ThreadChecker main_thread_checker_;
henrika49810512016-08-22 05:56:12 -0700146
henrikaf5022222016-11-07 15:56:59 +0100147 // Native (platform specific) audio thread driving the playout side.
148 rtc::ThreadChecker playout_thread_checker_;
149
150 // Native (platform specific) audio thread driving the recording side.
151 rtc::ThreadChecker recording_thread_checker_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
henrika87d11cd2017-02-08 07:16:56 -0800153 rtc::CriticalSection lock_;
154
henrika6c4d0f02016-07-14 05:54:19 -0700155 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
156 // worker thread but it does not necessarily have to be the same thread for
157 // each task.
158 rtc::TaskQueue task_queue_;
159
henrikaf5022222016-11-07 15:56:59 +0100160 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback()
161 // and it must outlive this object. It is not possible to change this member
162 // while any media is active. It is possible to start media without calling
163 // RegisterAudioCallback() but that will lead to ignored audio callbacks in
164 // both directions where native audio will be acive but no audio samples will
165 // be transported.
166 AudioTransport* audio_transport_cb_;
167
168 // The members below that are not annotated are protected by design. They are
169 // all set on the main thread (verified by |main_thread_checker_|) and then
170 // read on either the playout or recording audio thread. But, media will never
171 // be active when the member is set; hence no conflict exists. It is too
172 // complex to ensure and verify that this is actually the case.
henrika6c4d0f02016-07-14 05:54:19 -0700173
henrika49810512016-08-22 05:56:12 -0700174 // Sample rate in Hertz.
175 uint32_t rec_sample_rate_;
176 uint32_t play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
henrika49810512016-08-22 05:56:12 -0700178 // Number of audio channels.
179 size_t rec_channels_;
180 size_t play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181
henrikaf5022222016-11-07 15:56:59 +0100182 // Keeps track of if playout/recording are active or not. A combination
183 // of these states are used to determine when to start and stop the timer.
184 // Only used on the creating thread and not used to control any media flow.
danilchap56359be2017-09-07 07:53:45 -0700185 bool playing_ RTC_ACCESS_ON(main_thread_checker_);
186 bool recording_ RTC_ACCESS_ON(main_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000187
henrika5588a132016-10-18 05:14:30 -0700188 // Buffer used for audio samples to be played out. Size can be changed
henrika51e96082016-11-10 00:40:37 -0800189 // dynamically. The 16-bit samples are interleaved, hence the size is
190 // proportional to the number of channels.
danilchap56359be2017-09-07 07:53:45 -0700191 rtc::BufferT<int16_t> play_buffer_ RTC_ACCESS_ON(playout_thread_checker_);
henrikaf5022222016-11-07 15:56:59 +0100192
193 // Byte buffer used for recorded audio samples. Size can be changed
194 // dynamically.
danilchap56359be2017-09-07 07:53:45 -0700195 rtc::BufferT<int16_t> rec_buffer_ RTC_ACCESS_ON(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000196
henrika49810512016-08-22 05:56:12 -0700197 // Contains true of a key-press has been detected.
danilchap56359be2017-09-07 07:53:45 -0700198 bool typing_status_ RTC_ACCESS_ON(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
henrika49810512016-08-22 05:56:12 -0700200 // Delay values used by the AEC.
danilchap56359be2017-09-07 07:53:45 -0700201 int play_delay_ms_ RTC_ACCESS_ON(recording_thread_checker_);
202 int rec_delay_ms_ RTC_ACCESS_ON(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000203
henrika6c4d0f02016-07-14 05:54:19 -0700204 // Counts number of times LogStats() has been called.
danilchap56359be2017-09-07 07:53:45 -0700205 size_t num_stat_reports_ RTC_ACCESS_ON(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700206
henrikaf5022222016-11-07 15:56:59 +0100207 // Time stamp of last timer task (drives logging).
danilchap56359be2017-09-07 07:53:45 -0700208 int64_t last_timer_task_time_ RTC_ACCESS_ON(task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200209
henrika3355f6d2016-10-21 12:45:25 +0200210 // Counts number of audio callbacks modulo 50 to create a signal when
211 // a new storage of audio stats shall be done.
danilchap56359be2017-09-07 07:53:45 -0700212 int16_t rec_stat_count_ RTC_ACCESS_ON(recording_thread_checker_);
213 int16_t play_stat_count_ RTC_ACCESS_ON(playout_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700214
215 // Time stamps of when playout and recording starts.
danilchap56359be2017-09-07 07:53:45 -0700216 int64_t play_start_time_ RTC_ACCESS_ON(main_thread_checker_);
217 int64_t rec_start_time_ RTC_ACCESS_ON(main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700218
henrika87d11cd2017-02-08 07:16:56 -0800219 // Contains counters for playout and recording statistics.
danilchap56359be2017-09-07 07:53:45 -0700220 Stats stats_ RTC_GUARDED_BY(lock_);
henrika87d11cd2017-02-08 07:16:56 -0800221
222 // Stores current stats at each timer task. Used to calculate differences
223 // between two successive timer events.
danilchap56359be2017-09-07 07:53:45 -0700224 Stats last_stats_ RTC_ACCESS_ON(task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800225
henrikaba156cf2016-10-31 08:18:50 -0700226 // Set to true at construction and modified to false as soon as one audio-
227 // level estimate larger than zero is detected.
228 bool only_silence_recorded_;
henrika0b3a6382016-11-11 02:28:50 -0800229
230 // Set to true when logging of audio stats is enabled for the first time in
231 // StartPeriodicLogging() and set to false by StopPeriodicLogging().
232 // Setting this member to false prevents (possiby invalid) log messages from
233 // being printed in the LogStats() task.
danilchap56359be2017-09-07 07:53:45 -0700234 bool log_stats_ RTC_ACCESS_ON(task_queue_);
henrika7be78832017-06-13 17:34:16 +0200235
236// Should *never* be defined in production builds. Only used for testing.
237// When defined, the output signal will be replaced by a sinus tone at 440Hz.
238#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
danilchap56359be2017-09-07 07:53:45 -0700239 double phase_ RTC_ACCESS_ON(playout_thread_checker_);
henrika7be78832017-06-13 17:34:16 +0200240#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000241};
242
243} // namespace webrtc
244
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200245#endif // MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_