blob: 8dd95501947971c1a5dcd62581ff99ef584a1148 [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"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
henrika7be78832017-06-13 17:34:16 +020022
henrika3d7346f2016-07-29 16:20:47 +020023// Delta times between two successive playout callbacks are limited to this
24// value before added to an internal array.
25const size_t kMaxDeltaTimeInMs = 500;
henrika49810512016-08-22 05:56:12 -070026// TODO(henrika): remove when no longer used by external client.
27const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz
niklase@google.com470e71d2011-07-07 08:21:25 +000028
henrika0fd68012016-07-04 13:01:19 +020029class AudioDeviceBuffer {
30 public:
henrikaba156cf2016-10-31 08:18:50 -070031 enum LogState {
32 LOG_START = 0,
33 LOG_STOP,
34 LOG_ACTIVE,
35 };
36
henrika87d11cd2017-02-08 07:16:56 -080037 struct Stats {
38 void ResetRecStats() {
39 rec_callbacks = 0;
40 rec_samples = 0;
41 max_rec_level = 0;
42 }
43
44 void ResetPlayStats() {
45 play_callbacks = 0;
46 play_samples = 0;
47 max_play_level = 0;
48 }
49
50 // Total number of recording callbacks where the source provides 10ms audio
51 // data each time.
52 uint64_t rec_callbacks = 0;
53
54 // Total number of playback callbacks where the sink asks for 10ms audio
55 // data each time.
56 uint64_t play_callbacks = 0;
57
58 // Total number of recorded audio samples.
59 uint64_t rec_samples = 0;
60
61 // Total number of played audio samples.
62 uint64_t play_samples = 0;
63
64 // Contains max level (max(abs(x))) of recorded audio packets over the last
65 // 10 seconds where a new measurement is done twice per second. The level
66 // is reset to zero at each call to LogStats().
67 int16_t max_rec_level = 0;
68
69 // Contains max level of recorded audio packets over the last 10 seconds
70 // where a new measurement is done twice per second.
71 int16_t max_play_level = 0;
72 };
73
henrika0fd68012016-07-04 13:01:19 +020074 AudioDeviceBuffer();
75 virtual ~AudioDeviceBuffer();
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000076
henrika49810512016-08-22 05:56:12 -070077 int32_t RegisterAudioCallback(AudioTransport* audio_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000078
henrikaba156cf2016-10-31 08:18:50 -070079 void StartPlayout();
80 void StartRecording();
81 void StopPlayout();
82 void StopRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000083
henrika49810512016-08-22 05:56:12 -070084 int32_t SetRecordingSampleRate(uint32_t fsHz);
85 int32_t SetPlayoutSampleRate(uint32_t fsHz);
Patrik Höglunde2924d52018-09-05 08:52:40 +000086 int32_t RecordingSampleRate() const;
87 int32_t PlayoutSampleRate() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000088
henrika49810512016-08-22 05:56:12 -070089 int32_t SetRecordingChannels(size_t channels);
90 int32_t SetPlayoutChannels(size_t channels);
henrika0fd68012016-07-04 13:01:19 +020091 size_t RecordingChannels() const;
92 size_t PlayoutChannels() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000093
henrika49810512016-08-22 05:56:12 -070094 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -080095 size_t samples_per_channel);
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +010096 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms);
henrika0fd68012016-07-04 13:01:19 +020097 virtual int32_t DeliverRecordedData();
98 uint32_t NewMicLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000099
henrika51e96082016-11-10 00:40:37 -0800100 virtual int32_t RequestPlayoutData(size_t samples_per_channel);
henrika49810512016-08-22 05:56:12 -0700101 virtual int32_t GetPlayoutData(void* audio_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
henrika49810512016-08-22 05:56:12 -0700103 int32_t SetTypingStatus(bool typing_status);
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000104
henrika883d00f2018-03-16 10:09:49 +0100105 // Called on iOS and Android where the native audio layer can be interrupted
106 // by other audio applications. These methods can then be used to reset
107 // internal states and detach thread checkers to allow for new audio sessions
108 // where native callbacks may come from a new set of I/O threads.
109 void NativeAudioPlayoutInterrupted();
110 void NativeAudioRecordingInterrupted();
henrika09a76192017-08-23 15:04:40 +0200111
henrika0fd68012016-07-04 13:01:19 +0200112 private:
henrikaba156cf2016-10-31 08:18:50 -0700113 // Starts/stops periodic logging of audio stats.
114 void StartPeriodicLogging();
115 void StopPeriodicLogging();
henrika6c4d0f02016-07-14 05:54:19 -0700116
117 // Called periodically on the internal thread created by the TaskQueue.
henrikaba156cf2016-10-31 08:18:50 -0700118 // Updates some stats but dooes it on the task queue to ensure that access of
119 // members is serialized hence avoiding usage of locks.
120 // state = LOG_START => members are initialized and the timer starts.
121 // state = LOG_STOP => no logs are printed and the timer stops.
122 // state = LOG_ACTIVE => logs are printed and the timer is kept alive.
123 void LogStats(LogState state);
henrikaf06f35a2016-09-09 14:23:11 +0200124
henrika87d11cd2017-02-08 07:16:56 -0800125 // Updates counters in each play/record callback. These counters are later
126 // (periodically) read by LogStats() using a lock.
henrika51e96082016-11-10 00:40:37 -0800127 void UpdateRecStats(int16_t max_abs, size_t samples_per_channel);
128 void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel);
henrika6c4d0f02016-07-14 05:54:19 -0700129
henrikaba156cf2016-10-31 08:18:50 -0700130 // Clears all members tracking stats for recording and playout.
131 // These methods both run on the task queue.
132 void ResetRecStats();
133 void ResetPlayStats();
134
henrikaf5022222016-11-07 15:56:59 +0100135 // This object lives on the main (creating) thread and most methods are
136 // called on that same thread. When audio has started some methods will be
137 // called on either a native audio thread for playout or a native thread for
138 // recording. Some members are not annotated since they are "protected by
Patrik Höglunde2924d52018-09-05 08:52:40 +0000139 // design" and adding e.g. a race checker can cause failuries for very few
henrikaf5022222016-11-07 15:56:59 +0100140 // edge cases and it is IMHO not worth the risk to use them in this class.
141 // TODO(henrika): see if it is possible to refactor and annotate all members.
henrika6c4d0f02016-07-14 05:54:19 -0700142
henrikaf5022222016-11-07 15:56:59 +0100143 // Main thread on which this object is created.
144 rtc::ThreadChecker main_thread_checker_;
henrika49810512016-08-22 05:56:12 -0700145
henrikaf5022222016-11-07 15:56:59 +0100146 // Native (platform specific) audio thread driving the playout side.
147 rtc::ThreadChecker playout_thread_checker_;
148
149 // Native (platform specific) audio thread driving the recording side.
150 rtc::ThreadChecker recording_thread_checker_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000151
henrika87d11cd2017-02-08 07:16:56 -0800152 rtc::CriticalSection lock_;
153
henrika6c4d0f02016-07-14 05:54:19 -0700154 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
155 // worker thread but it does not necessarily have to be the same thread for
156 // each task.
157 rtc::TaskQueue task_queue_;
158
henrikaf5022222016-11-07 15:56:59 +0100159 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback()
160 // and it must outlive this object. It is not possible to change this member
161 // while any media is active. It is possible to start media without calling
162 // RegisterAudioCallback() but that will lead to ignored audio callbacks in
Patrik Höglunde2924d52018-09-05 08:52:40 +0000163 // both directions where native audio will be acive but no audio samples will
henrikaf5022222016-11-07 15:56:59 +0100164 // be transported.
165 AudioTransport* audio_transport_cb_;
166
Patrik Höglunde2924d52018-09-05 08:52:40 +0000167 // The members below that are not annotated are protected by design. They are
168 // all set on the main thread (verified by |main_thread_checker_|) and then
169 // read on either the playout or recording audio thread. But, media will never
170 // be active when the member is set; hence no conflict exists. It is too
171 // complex to ensure and verify that this is actually the case.
henrika6c4d0f02016-07-14 05:54:19 -0700172
Patrik Höglunde2924d52018-09-05 08:52:40 +0000173 // Sample rate in Hertz.
174 uint32_t rec_sample_rate_;
175 uint32_t play_sample_rate_;
176
177 // Number of audio channels.
178 size_t rec_channels_;
179 size_t play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
henrikaf5022222016-11-07 15:56:59 +0100181 // Keeps track of if playout/recording are active or not. A combination
182 // of these states are used to determine when to start and stop the timer.
183 // Only used on the creating thread and not used to control any media flow.
Niels Möller1e062892018-02-07 10:18:32 +0100184 bool playing_ RTC_GUARDED_BY(main_thread_checker_);
185 bool recording_ RTC_GUARDED_BY(main_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000186
henrika5588a132016-10-18 05:14:30 -0700187 // Buffer used for audio samples to be played out. Size can be changed
henrika51e96082016-11-10 00:40:37 -0800188 // dynamically. The 16-bit samples are interleaved, hence the size is
189 // proportional to the number of channels.
Niels Möller1e062892018-02-07 10:18:32 +0100190 rtc::BufferT<int16_t> play_buffer_ RTC_GUARDED_BY(playout_thread_checker_);
henrikaf5022222016-11-07 15:56:59 +0100191
192 // Byte buffer used for recorded audio samples. Size can be changed
193 // dynamically.
Niels Möller1e062892018-02-07 10:18:32 +0100194 rtc::BufferT<int16_t> rec_buffer_ RTC_GUARDED_BY(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
henrika49810512016-08-22 05:56:12 -0700196 // Contains true of a key-press has been detected.
Niels Möller1e062892018-02-07 10:18:32 +0100197 bool typing_status_ RTC_GUARDED_BY(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
henrika49810512016-08-22 05:56:12 -0700199 // Delay values used by the AEC.
Niels Möller1e062892018-02-07 10:18:32 +0100200 int play_delay_ms_ RTC_GUARDED_BY(recording_thread_checker_);
201 int rec_delay_ms_ RTC_GUARDED_BY(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
henrika6c4d0f02016-07-14 05:54:19 -0700203 // Counts number of times LogStats() has been called.
Niels Möller1e062892018-02-07 10:18:32 +0100204 size_t num_stat_reports_ RTC_GUARDED_BY(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700205
henrikaf5022222016-11-07 15:56:59 +0100206 // Time stamp of last timer task (drives logging).
Niels Möller1e062892018-02-07 10:18:32 +0100207 int64_t last_timer_task_time_ RTC_GUARDED_BY(task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200208
henrika3355f6d2016-10-21 12:45:25 +0200209 // Counts number of audio callbacks modulo 50 to create a signal when
210 // a new storage of audio stats shall be done.
Niels Möller1e062892018-02-07 10:18:32 +0100211 int16_t rec_stat_count_ RTC_GUARDED_BY(recording_thread_checker_);
212 int16_t play_stat_count_ RTC_GUARDED_BY(playout_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700213
214 // Time stamps of when playout and recording starts.
Niels Möller1e062892018-02-07 10:18:32 +0100215 int64_t play_start_time_ RTC_GUARDED_BY(main_thread_checker_);
216 int64_t rec_start_time_ RTC_GUARDED_BY(main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700217
henrika87d11cd2017-02-08 07:16:56 -0800218 // Contains counters for playout and recording statistics.
danilchap56359be2017-09-07 07:53:45 -0700219 Stats stats_ RTC_GUARDED_BY(lock_);
henrika87d11cd2017-02-08 07:16:56 -0800220
221 // Stores current stats at each timer task. Used to calculate differences
222 // between two successive timer events.
Niels Möller1e062892018-02-07 10:18:32 +0100223 Stats last_stats_ RTC_GUARDED_BY(task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800224
henrikaba156cf2016-10-31 08:18:50 -0700225 // Set to true at construction and modified to false as soon as one audio-
226 // level estimate larger than zero is detected.
227 bool only_silence_recorded_;
henrika0b3a6382016-11-11 02:28:50 -0800228
229 // Set to true when logging of audio stats is enabled for the first time in
230 // StartPeriodicLogging() and set to false by StopPeriodicLogging().
231 // Setting this member to false prevents (possiby invalid) log messages from
232 // being printed in the LogStats() task.
Niels Möller1e062892018-02-07 10:18:32 +0100233 bool log_stats_ RTC_GUARDED_BY(task_queue_);
henrika7be78832017-06-13 17:34:16 +0200234
235// Should *never* be defined in production builds. Only used for testing.
236// When defined, the output signal will be replaced by a sinus tone at 440Hz.
237#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
Niels Möller1e062892018-02-07 10:18:32 +0100238 double phase_ RTC_GUARDED_BY(playout_thread_checker_);
henrika7be78832017-06-13 17:34:16 +0200239#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000240};
241
242} // namespace webrtc
243
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200244#endif // MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_