blob: 9a6a88a1be4ae992d02d48f1240b0fbfb2e41fcb [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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
henrikacfbd26d2018-09-05 11:36:22 +020017#include <atomic>
18
Artem Titovd15a5752021-02-10 14:31:24 +010019#include "api/sequence_checker.h"
Danil Chapovalov1c41be62019-04-01 09:16:12 +020020#include "api/task_queue/task_queue_factory.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "modules/audio_device/include/audio_device_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/buffer.h"
Markus Handell5f612822020-07-08 10:13:20 +020023#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/task_queue.h"
25#include "rtc_base/thread_annotations.h"
Olov Brändström092d7762022-02-07 12:21:32 +010026#include "rtc_base/timestamp_aligner.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
28namespace webrtc {
henrika7be78832017-06-13 17:34:16 +020029
henrika3d7346f2016-07-29 16:20:47 +020030// Delta times between two successive playout callbacks are limited to this
31// value before added to an internal array.
32const size_t kMaxDeltaTimeInMs = 500;
henrika49810512016-08-22 05:56:12 -070033// TODO(henrika): remove when no longer used by external client.
34const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz
niklase@google.com470e71d2011-07-07 08:21:25 +000035
henrika0fd68012016-07-04 13:01:19 +020036class AudioDeviceBuffer {
37 public:
henrikaba156cf2016-10-31 08:18:50 -070038 enum LogState {
39 LOG_START = 0,
40 LOG_STOP,
41 LOG_ACTIVE,
42 };
43
henrika87d11cd2017-02-08 07:16:56 -080044 struct Stats {
45 void ResetRecStats() {
46 rec_callbacks = 0;
47 rec_samples = 0;
48 max_rec_level = 0;
49 }
50
51 void ResetPlayStats() {
52 play_callbacks = 0;
53 play_samples = 0;
54 max_play_level = 0;
55 }
56
57 // Total number of recording callbacks where the source provides 10ms audio
58 // data each time.
59 uint64_t rec_callbacks = 0;
60
61 // Total number of playback callbacks where the sink asks for 10ms audio
62 // data each time.
63 uint64_t play_callbacks = 0;
64
65 // Total number of recorded audio samples.
66 uint64_t rec_samples = 0;
67
68 // Total number of played audio samples.
69 uint64_t play_samples = 0;
70
71 // Contains max level (max(abs(x))) of recorded audio packets over the last
72 // 10 seconds where a new measurement is done twice per second. The level
73 // is reset to zero at each call to LogStats().
74 int16_t max_rec_level = 0;
75
76 // Contains max level of recorded audio packets over the last 10 seconds
77 // where a new measurement is done twice per second.
78 int16_t max_play_level = 0;
79 };
80
Danil Chapovalov1c41be62019-04-01 09:16:12 +020081 explicit AudioDeviceBuffer(TaskQueueFactory* task_queue_factory);
henrika0fd68012016-07-04 13:01:19 +020082 virtual ~AudioDeviceBuffer();
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000083
henrika49810512016-08-22 05:56:12 -070084 int32_t RegisterAudioCallback(AudioTransport* audio_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000085
henrikaba156cf2016-10-31 08:18:50 -070086 void StartPlayout();
87 void StartRecording();
88 void StopPlayout();
89 void StopRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000090
henrika49810512016-08-22 05:56:12 -070091 int32_t SetRecordingSampleRate(uint32_t fsHz);
92 int32_t SetPlayoutSampleRate(uint32_t fsHz);
henrikacfbd26d2018-09-05 11:36:22 +020093 uint32_t RecordingSampleRate() const;
94 uint32_t PlayoutSampleRate() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000095
henrika49810512016-08-22 05:56:12 -070096 int32_t SetRecordingChannels(size_t channels);
97 int32_t SetPlayoutChannels(size_t channels);
henrika0fd68012016-07-04 13:01:19 +020098 size_t RecordingChannels() const;
99 size_t PlayoutChannels() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
Olov Brändströmb732bd52022-01-28 15:07:39 +0100101 // TODO(bugs.webrtc.org/13621) Deprecate this function
henrika49810512016-08-22 05:56:12 -0700102 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -0800103 size_t samples_per_channel);
Olov Brändströmb732bd52022-01-28 15:07:39 +0100104
105 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
106 size_t samples_per_channel,
107 int64_t capture_timestamp_ns);
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +0100108 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms);
henrika0fd68012016-07-04 13:01:19 +0200109 virtual int32_t DeliverRecordedData();
110 uint32_t NewMicLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000111
henrika51e96082016-11-10 00:40:37 -0800112 virtual int32_t RequestPlayoutData(size_t samples_per_channel);
henrika49810512016-08-22 05:56:12 -0700113 virtual int32_t GetPlayoutData(void* audio_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
henrika49810512016-08-22 05:56:12 -0700115 int32_t SetTypingStatus(bool typing_status);
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000116
henrika0fd68012016-07-04 13:01:19 +0200117 private:
henrikaba156cf2016-10-31 08:18:50 -0700118 // Starts/stops periodic logging of audio stats.
119 void StartPeriodicLogging();
120 void StopPeriodicLogging();
henrika6c4d0f02016-07-14 05:54:19 -0700121
122 // Called periodically on the internal thread created by the TaskQueue.
henrikaba156cf2016-10-31 08:18:50 -0700123 // Updates some stats but dooes it on the task queue to ensure that access of
124 // members is serialized hence avoiding usage of locks.
125 // state = LOG_START => members are initialized and the timer starts.
126 // state = LOG_STOP => no logs are printed and the timer stops.
127 // state = LOG_ACTIVE => logs are printed and the timer is kept alive.
128 void LogStats(LogState state);
henrikaf06f35a2016-09-09 14:23:11 +0200129
henrika87d11cd2017-02-08 07:16:56 -0800130 // Updates counters in each play/record callback. These counters are later
131 // (periodically) read by LogStats() using a lock.
henrika51e96082016-11-10 00:40:37 -0800132 void UpdateRecStats(int16_t max_abs, size_t samples_per_channel);
133 void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel);
henrika6c4d0f02016-07-14 05:54:19 -0700134
henrikaba156cf2016-10-31 08:18:50 -0700135 // Clears all members tracking stats for recording and playout.
136 // These methods both run on the task queue.
137 void ResetRecStats();
138 void ResetPlayStats();
139
henrikaf5022222016-11-07 15:56:59 +0100140 // This object lives on the main (creating) thread and most methods are
141 // called on that same thread. When audio has started some methods will be
142 // called on either a native audio thread for playout or a native thread for
143 // recording. Some members are not annotated since they are "protected by
henrikacfbd26d2018-09-05 11:36:22 +0200144 // design" and adding e.g. a race checker can cause failures for very few
henrikaf5022222016-11-07 15:56:59 +0100145 // edge cases and it is IMHO not worth the risk to use them in this class.
146 // TODO(henrika): see if it is possible to refactor and annotate all members.
henrika6c4d0f02016-07-14 05:54:19 -0700147
henrikaf5022222016-11-07 15:56:59 +0100148 // Main thread on which this object is created.
Artem Titovc8421c42021-02-02 10:57:19 +0100149 SequenceChecker main_thread_checker_;
henrika49810512016-08-22 05:56:12 -0700150
Markus Handell5f612822020-07-08 10:13:20 +0200151 Mutex lock_;
henrika87d11cd2017-02-08 07:16:56 -0800152
henrika6c4d0f02016-07-14 05:54:19 -0700153 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
154 // worker thread but it does not necessarily have to be the same thread for
155 // each task.
156 rtc::TaskQueue task_queue_;
157
henrikaf5022222016-11-07 15:56:59 +0100158 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback()
159 // and it must outlive this object. It is not possible to change this member
160 // while any media is active. It is possible to start media without calling
161 // RegisterAudioCallback() but that will lead to ignored audio callbacks in
henrikacfbd26d2018-09-05 11:36:22 +0200162 // both directions where native audio will be active but no audio samples will
henrikaf5022222016-11-07 15:56:59 +0100163 // be transported.
164 AudioTransport* audio_transport_cb_;
165
henrikacfbd26d2018-09-05 11:36:22 +0200166 // Sample rate in Hertz. Accessed atomically.
167 std::atomic<uint32_t> rec_sample_rate_;
168 std::atomic<uint32_t> play_sample_rate_;
henrika6c4d0f02016-07-14 05:54:19 -0700169
henrikacfbd26d2018-09-05 11:36:22 +0200170 // Number of audio channels. Accessed atomically.
171 std::atomic<size_t> rec_channels_;
172 std::atomic<size_t> play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000173
henrikaf5022222016-11-07 15:56:59 +0100174 // Keeps track of if playout/recording are active or not. A combination
175 // of these states are used to determine when to start and stop the timer.
176 // Only used on the creating thread and not used to control any media flow.
Niels Möller1e062892018-02-07 10:18:32 +0100177 bool playing_ RTC_GUARDED_BY(main_thread_checker_);
178 bool recording_ RTC_GUARDED_BY(main_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
henrika5588a132016-10-18 05:14:30 -0700180 // Buffer used for audio samples to be played out. Size can be changed
henrika51e96082016-11-10 00:40:37 -0800181 // dynamically. The 16-bit samples are interleaved, hence the size is
182 // proportional to the number of channels.
henrika36b31792018-09-13 13:01:14 +0200183 rtc::BufferT<int16_t> play_buffer_;
henrikaf5022222016-11-07 15:56:59 +0100184
185 // Byte buffer used for recorded audio samples. Size can be changed
186 // dynamically.
henrika36b31792018-09-13 13:01:14 +0200187 rtc::BufferT<int16_t> rec_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
henrika49810512016-08-22 05:56:12 -0700189 // Contains true of a key-press has been detected.
henrika36b31792018-09-13 13:01:14 +0200190 bool typing_status_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000191
henrika49810512016-08-22 05:56:12 -0700192 // Delay values used by the AEC.
henrika36b31792018-09-13 13:01:14 +0200193 int play_delay_ms_;
194 int rec_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
Olov Brändströmb732bd52022-01-28 15:07:39 +0100196 // Capture timestamp.
197 int64_t capture_timestamp_ns_;
198
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.
henrika36b31792018-09-13 13:01:14 +0200207 int16_t rec_stat_count_;
208 int16_t play_stat_count_;
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
Niels Möllerbe74b802022-03-18 14:10:15 +0100231 // Used for converting capture timestaps (received from AudioRecordThread
Olov Brändström092d7762022-02-07 12:21:32 +0100232 // via AudioRecordJni::DataIsRecorded) to RTC clock.
233 rtc::TimestampAligner timestamp_aligner_;
234
henrika7be78832017-06-13 17:34:16 +0200235// 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
henrika36b31792018-09-13 13:01:14 +0200238 double phase_;
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_