blob: 3658be04e442e6bf7148f1ef4f335ee6ad61dad9 [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
henrikacfbd26d2018-09-05 11:36:22 +020014#include <atomic>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_device/include/audio_device.h"
17#include "rtc_base/buffer.h"
18#include "rtc_base/criticalsection.h"
19#include "rtc_base/task_queue.h"
20#include "rtc_base/thread_annotations.h"
21#include "rtc_base/thread_checker.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
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);
henrikacfbd26d2018-09-05 11:36:22 +020088 uint32_t RecordingSampleRate() const;
89 uint32_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
henrika883d00f2018-03-16 10:09:49 +0100107 // Called on iOS and Android where the native audio layer can be interrupted
108 // by other audio applications. These methods can then be used to reset
109 // internal states and detach thread checkers to allow for new audio sessions
110 // where native callbacks may come from a new set of I/O threads.
111 void NativeAudioPlayoutInterrupted();
112 void NativeAudioRecordingInterrupted();
henrika09a76192017-08-23 15:04:40 +0200113
henrika0fd68012016-07-04 13:01:19 +0200114 private:
henrikaba156cf2016-10-31 08:18:50 -0700115 // Starts/stops periodic logging of audio stats.
116 void StartPeriodicLogging();
117 void StopPeriodicLogging();
henrika6c4d0f02016-07-14 05:54:19 -0700118
119 // Called periodically on the internal thread created by the TaskQueue.
henrikaba156cf2016-10-31 08:18:50 -0700120 // Updates some stats but dooes it on the task queue to ensure that access of
121 // members is serialized hence avoiding usage of locks.
122 // state = LOG_START => members are initialized and the timer starts.
123 // state = LOG_STOP => no logs are printed and the timer stops.
124 // state = LOG_ACTIVE => logs are printed and the timer is kept alive.
125 void LogStats(LogState state);
henrikaf06f35a2016-09-09 14:23:11 +0200126
henrika87d11cd2017-02-08 07:16:56 -0800127 // Updates counters in each play/record callback. These counters are later
128 // (periodically) read by LogStats() using a lock.
henrika51e96082016-11-10 00:40:37 -0800129 void UpdateRecStats(int16_t max_abs, size_t samples_per_channel);
130 void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel);
henrika6c4d0f02016-07-14 05:54:19 -0700131
henrikaba156cf2016-10-31 08:18:50 -0700132 // Clears all members tracking stats for recording and playout.
133 // These methods both run on the task queue.
134 void ResetRecStats();
135 void ResetPlayStats();
136
henrikaf5022222016-11-07 15:56:59 +0100137 // This object lives on the main (creating) thread and most methods are
138 // called on that same thread. When audio has started some methods will be
139 // called on either a native audio thread for playout or a native thread for
140 // recording. Some members are not annotated since they are "protected by
henrikacfbd26d2018-09-05 11:36:22 +0200141 // design" and adding e.g. a race checker can cause failures for very few
henrikaf5022222016-11-07 15:56:59 +0100142 // edge cases and it is IMHO not worth the risk to use them in this class.
143 // TODO(henrika): see if it is possible to refactor and annotate all members.
henrika6c4d0f02016-07-14 05:54:19 -0700144
henrikaf5022222016-11-07 15:56:59 +0100145 // Main thread on which this object is created.
146 rtc::ThreadChecker main_thread_checker_;
henrika49810512016-08-22 05:56:12 -0700147
henrikaf5022222016-11-07 15:56:59 +0100148 // Native (platform specific) audio thread driving the playout side.
149 rtc::ThreadChecker playout_thread_checker_;
150
151 // Native (platform specific) audio thread driving the recording side.
152 rtc::ThreadChecker recording_thread_checker_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
henrika87d11cd2017-02-08 07:16:56 -0800154 rtc::CriticalSection lock_;
155
henrika6c4d0f02016-07-14 05:54:19 -0700156 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
157 // worker thread but it does not necessarily have to be the same thread for
158 // each task.
159 rtc::TaskQueue task_queue_;
160
henrikaf5022222016-11-07 15:56:59 +0100161 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback()
162 // and it must outlive this object. It is not possible to change this member
163 // while any media is active. It is possible to start media without calling
164 // RegisterAudioCallback() but that will lead to ignored audio callbacks in
henrikacfbd26d2018-09-05 11:36:22 +0200165 // both directions where native audio will be active but no audio samples will
henrikaf5022222016-11-07 15:56:59 +0100166 // be transported.
167 AudioTransport* audio_transport_cb_;
168
henrikacfbd26d2018-09-05 11:36:22 +0200169 // Sample rate in Hertz. Accessed atomically.
170 std::atomic<uint32_t> rec_sample_rate_;
171 std::atomic<uint32_t> play_sample_rate_;
henrika6c4d0f02016-07-14 05:54:19 -0700172
henrikacfbd26d2018-09-05 11:36:22 +0200173 // Number of audio channels. Accessed atomically.
174 std::atomic<size_t> rec_channels_;
175 std::atomic<size_t> play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
henrikaf5022222016-11-07 15:56:59 +0100177 // Keeps track of if playout/recording are active or not. A combination
178 // of these states are used to determine when to start and stop the timer.
179 // Only used on the creating thread and not used to control any media flow.
Niels Möller1e062892018-02-07 10:18:32 +0100180 bool playing_ RTC_GUARDED_BY(main_thread_checker_);
181 bool recording_ RTC_GUARDED_BY(main_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
henrika5588a132016-10-18 05:14:30 -0700183 // Buffer used for audio samples to be played out. Size can be changed
henrika51e96082016-11-10 00:40:37 -0800184 // dynamically. The 16-bit samples are interleaved, hence the size is
185 // proportional to the number of channels.
Niels Möller1e062892018-02-07 10:18:32 +0100186 rtc::BufferT<int16_t> play_buffer_ RTC_GUARDED_BY(playout_thread_checker_);
henrikaf5022222016-11-07 15:56:59 +0100187
188 // Byte buffer used for recorded audio samples. Size can be changed
189 // dynamically.
Niels Möller1e062892018-02-07 10:18:32 +0100190 rtc::BufferT<int16_t> rec_buffer_ RTC_GUARDED_BY(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000191
henrika49810512016-08-22 05:56:12 -0700192 // Contains true of a key-press has been detected.
Niels Möller1e062892018-02-07 10:18:32 +0100193 bool typing_status_ RTC_GUARDED_BY(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000194
henrika49810512016-08-22 05:56:12 -0700195 // Delay values used by the AEC.
Niels Möller1e062892018-02-07 10:18:32 +0100196 int play_delay_ms_ RTC_GUARDED_BY(recording_thread_checker_);
197 int rec_delay_ms_ RTC_GUARDED_BY(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
henrika6c4d0f02016-07-14 05:54:19 -0700199 // Counts number of times LogStats() has been called.
Niels Möller1e062892018-02-07 10:18:32 +0100200 size_t num_stat_reports_ RTC_GUARDED_BY(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700201
henrikaf5022222016-11-07 15:56:59 +0100202 // Time stamp of last timer task (drives logging).
Niels Möller1e062892018-02-07 10:18:32 +0100203 int64_t last_timer_task_time_ RTC_GUARDED_BY(task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200204
henrika3355f6d2016-10-21 12:45:25 +0200205 // Counts number of audio callbacks modulo 50 to create a signal when
206 // a new storage of audio stats shall be done.
Niels Möller1e062892018-02-07 10:18:32 +0100207 int16_t rec_stat_count_ RTC_GUARDED_BY(recording_thread_checker_);
208 int16_t play_stat_count_ RTC_GUARDED_BY(playout_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700209
210 // Time stamps of when playout and recording starts.
Niels Möller1e062892018-02-07 10:18:32 +0100211 int64_t play_start_time_ RTC_GUARDED_BY(main_thread_checker_);
212 int64_t rec_start_time_ RTC_GUARDED_BY(main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700213
henrika87d11cd2017-02-08 07:16:56 -0800214 // Contains counters for playout and recording statistics.
danilchap56359be2017-09-07 07:53:45 -0700215 Stats stats_ RTC_GUARDED_BY(lock_);
henrika87d11cd2017-02-08 07:16:56 -0800216
217 // Stores current stats at each timer task. Used to calculate differences
218 // between two successive timer events.
Niels Möller1e062892018-02-07 10:18:32 +0100219 Stats last_stats_ RTC_GUARDED_BY(task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800220
henrikaba156cf2016-10-31 08:18:50 -0700221 // Set to true at construction and modified to false as soon as one audio-
222 // level estimate larger than zero is detected.
223 bool only_silence_recorded_;
henrika0b3a6382016-11-11 02:28:50 -0800224
225 // Set to true when logging of audio stats is enabled for the first time in
226 // StartPeriodicLogging() and set to false by StopPeriodicLogging().
227 // Setting this member to false prevents (possiby invalid) log messages from
228 // being printed in the LogStats() task.
Niels Möller1e062892018-02-07 10:18:32 +0100229 bool log_stats_ RTC_GUARDED_BY(task_queue_);
henrika7be78832017-06-13 17:34:16 +0200230
231// Should *never* be defined in production builds. Only used for testing.
232// When defined, the output signal will be replaced by a sinus tone at 440Hz.
233#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
Niels Möller1e062892018-02-07 10:18:32 +0100234 double phase_ RTC_GUARDED_BY(playout_thread_checker_);
henrika7be78832017-06-13 17:34:16 +0200235#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000236};
237
238} // namespace webrtc
239
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200240#endif // MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_