blob: 9f71ebe99d6fcfd90ae73b4c14c095b0b4b863c0 [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>
henrikacfbd26d2018-09-05 11:36:22 +020016#include <atomic>
17
Danil Chapovalov1c41be62019-04-01 09:16:12 +020018#include "api/task_queue/task_queue_factory.h"
Yves Gerey988cc082018-10-23 12:03:01 +020019#include "modules/audio_device/include/audio_device_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/task_queue.h"
23#include "rtc_base/thread_annotations.h"
24#include "rtc_base/thread_checker.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000025
26namespace webrtc {
henrika7be78832017-06-13 17:34:16 +020027
henrika3d7346f2016-07-29 16:20:47 +020028// Delta times between two successive playout callbacks are limited to this
29// value before added to an internal array.
30const size_t kMaxDeltaTimeInMs = 500;
henrika49810512016-08-22 05:56:12 -070031// TODO(henrika): remove when no longer used by external client.
32const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz
niklase@google.com470e71d2011-07-07 08:21:25 +000033
henrika0fd68012016-07-04 13:01:19 +020034class AudioDeviceBuffer {
35 public:
henrikaba156cf2016-10-31 08:18:50 -070036 enum LogState {
37 LOG_START = 0,
38 LOG_STOP,
39 LOG_ACTIVE,
40 };
41
henrika87d11cd2017-02-08 07:16:56 -080042 struct Stats {
43 void ResetRecStats() {
44 rec_callbacks = 0;
45 rec_samples = 0;
46 max_rec_level = 0;
47 }
48
49 void ResetPlayStats() {
50 play_callbacks = 0;
51 play_samples = 0;
52 max_play_level = 0;
53 }
54
55 // Total number of recording callbacks where the source provides 10ms audio
56 // data each time.
57 uint64_t rec_callbacks = 0;
58
59 // Total number of playback callbacks where the sink asks for 10ms audio
60 // data each time.
61 uint64_t play_callbacks = 0;
62
63 // Total number of recorded audio samples.
64 uint64_t rec_samples = 0;
65
66 // Total number of played audio samples.
67 uint64_t play_samples = 0;
68
69 // Contains max level (max(abs(x))) of recorded audio packets over the last
70 // 10 seconds where a new measurement is done twice per second. The level
71 // is reset to zero at each call to LogStats().
72 int16_t max_rec_level = 0;
73
74 // Contains max level of recorded audio packets over the last 10 seconds
75 // where a new measurement is done twice per second.
76 int16_t max_play_level = 0;
77 };
78
Danil Chapovalov1c41be62019-04-01 09:16:12 +020079 explicit AudioDeviceBuffer(TaskQueueFactory* task_queue_factory);
henrika0fd68012016-07-04 13:01:19 +020080 virtual ~AudioDeviceBuffer();
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000081
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);
henrikacfbd26d2018-09-05 11:36:22 +020091 uint32_t RecordingSampleRate() const;
92 uint32_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;
niklase@google.com470e71d2011-07-07 08:21:25 +000098
henrika49810512016-08-22 05:56:12 -070099 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -0800100 size_t samples_per_channel);
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +0100101 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms);
henrika0fd68012016-07-04 13:01:19 +0200102 virtual int32_t DeliverRecordedData();
103 uint32_t NewMicLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
henrika51e96082016-11-10 00:40:37 -0800105 virtual int32_t RequestPlayoutData(size_t samples_per_channel);
henrika49810512016-08-22 05:56:12 -0700106 virtual int32_t GetPlayoutData(void* audio_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
henrika49810512016-08-22 05:56:12 -0700108 int32_t SetTypingStatus(bool typing_status);
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000109
henrika0fd68012016-07-04 13:01:19 +0200110 private:
henrikaba156cf2016-10-31 08:18:50 -0700111 // Starts/stops periodic logging of audio stats.
112 void StartPeriodicLogging();
113 void StopPeriodicLogging();
henrika6c4d0f02016-07-14 05:54:19 -0700114
115 // Called periodically on the internal thread created by the TaskQueue.
henrikaba156cf2016-10-31 08:18:50 -0700116 // Updates some stats but dooes it on the task queue to ensure that access of
117 // members is serialized hence avoiding usage of locks.
118 // state = LOG_START => members are initialized and the timer starts.
119 // state = LOG_STOP => no logs are printed and the timer stops.
120 // state = LOG_ACTIVE => logs are printed and the timer is kept alive.
121 void LogStats(LogState state);
henrikaf06f35a2016-09-09 14:23:11 +0200122
henrika87d11cd2017-02-08 07:16:56 -0800123 // Updates counters in each play/record callback. These counters are later
124 // (periodically) read by LogStats() using a lock.
henrika51e96082016-11-10 00:40:37 -0800125 void UpdateRecStats(int16_t max_abs, size_t samples_per_channel);
126 void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel);
henrika6c4d0f02016-07-14 05:54:19 -0700127
henrikaba156cf2016-10-31 08:18:50 -0700128 // Clears all members tracking stats for recording and playout.
129 // These methods both run on the task queue.
130 void ResetRecStats();
131 void ResetPlayStats();
132
henrikaf5022222016-11-07 15:56:59 +0100133 // This object lives on the main (creating) thread and most methods are
134 // called on that same thread. When audio has started some methods will be
135 // called on either a native audio thread for playout or a native thread for
136 // recording. Some members are not annotated since they are "protected by
henrikacfbd26d2018-09-05 11:36:22 +0200137 // design" and adding e.g. a race checker can cause failures for very few
henrikaf5022222016-11-07 15:56:59 +0100138 // edge cases and it is IMHO not worth the risk to use them in this class.
139 // TODO(henrika): see if it is possible to refactor and annotate all members.
henrika6c4d0f02016-07-14 05:54:19 -0700140
henrikaf5022222016-11-07 15:56:59 +0100141 // Main thread on which this object is created.
142 rtc::ThreadChecker main_thread_checker_;
henrika49810512016-08-22 05:56:12 -0700143
henrika87d11cd2017-02-08 07:16:56 -0800144 rtc::CriticalSection lock_;
145
henrika6c4d0f02016-07-14 05:54:19 -0700146 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
147 // worker thread but it does not necessarily have to be the same thread for
148 // each task.
149 rtc::TaskQueue task_queue_;
150
henrikaf5022222016-11-07 15:56:59 +0100151 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback()
152 // and it must outlive this object. It is not possible to change this member
153 // while any media is active. It is possible to start media without calling
154 // RegisterAudioCallback() but that will lead to ignored audio callbacks in
henrikacfbd26d2018-09-05 11:36:22 +0200155 // both directions where native audio will be active but no audio samples will
henrikaf5022222016-11-07 15:56:59 +0100156 // be transported.
157 AudioTransport* audio_transport_cb_;
158
henrikacfbd26d2018-09-05 11:36:22 +0200159 // Sample rate in Hertz. Accessed atomically.
160 std::atomic<uint32_t> rec_sample_rate_;
161 std::atomic<uint32_t> play_sample_rate_;
henrika6c4d0f02016-07-14 05:54:19 -0700162
henrikacfbd26d2018-09-05 11:36:22 +0200163 // Number of audio channels. Accessed atomically.
164 std::atomic<size_t> rec_channels_;
165 std::atomic<size_t> play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
henrikaf5022222016-11-07 15:56:59 +0100167 // Keeps track of if playout/recording are active or not. A combination
168 // of these states are used to determine when to start and stop the timer.
169 // Only used on the creating thread and not used to control any media flow.
Niels Möller1e062892018-02-07 10:18:32 +0100170 bool playing_ RTC_GUARDED_BY(main_thread_checker_);
171 bool recording_ RTC_GUARDED_BY(main_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
henrika5588a132016-10-18 05:14:30 -0700173 // Buffer used for audio samples to be played out. Size can be changed
henrika51e96082016-11-10 00:40:37 -0800174 // dynamically. The 16-bit samples are interleaved, hence the size is
175 // proportional to the number of channels.
henrika36b31792018-09-13 13:01:14 +0200176 rtc::BufferT<int16_t> play_buffer_;
henrikaf5022222016-11-07 15:56:59 +0100177
178 // Byte buffer used for recorded audio samples. Size can be changed
179 // dynamically.
henrika36b31792018-09-13 13:01:14 +0200180 rtc::BufferT<int16_t> rec_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000181
henrika49810512016-08-22 05:56:12 -0700182 // Contains true of a key-press has been detected.
henrika36b31792018-09-13 13:01:14 +0200183 bool typing_status_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184
henrika49810512016-08-22 05:56:12 -0700185 // Delay values used by the AEC.
henrika36b31792018-09-13 13:01:14 +0200186 int play_delay_ms_;
187 int rec_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
henrika6c4d0f02016-07-14 05:54:19 -0700189 // Counts number of times LogStats() has been called.
Niels Möller1e062892018-02-07 10:18:32 +0100190 size_t num_stat_reports_ RTC_GUARDED_BY(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700191
henrikaf5022222016-11-07 15:56:59 +0100192 // Time stamp of last timer task (drives logging).
Niels Möller1e062892018-02-07 10:18:32 +0100193 int64_t last_timer_task_time_ RTC_GUARDED_BY(task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200194
henrika3355f6d2016-10-21 12:45:25 +0200195 // Counts number of audio callbacks modulo 50 to create a signal when
196 // a new storage of audio stats shall be done.
henrika36b31792018-09-13 13:01:14 +0200197 int16_t rec_stat_count_;
198 int16_t play_stat_count_;
henrikaba156cf2016-10-31 08:18:50 -0700199
200 // Time stamps of when playout and recording starts.
Niels Möller1e062892018-02-07 10:18:32 +0100201 int64_t play_start_time_ RTC_GUARDED_BY(main_thread_checker_);
202 int64_t rec_start_time_ RTC_GUARDED_BY(main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700203
henrika87d11cd2017-02-08 07:16:56 -0800204 // Contains counters for playout and recording statistics.
danilchap56359be2017-09-07 07:53:45 -0700205 Stats stats_ RTC_GUARDED_BY(lock_);
henrika87d11cd2017-02-08 07:16:56 -0800206
207 // Stores current stats at each timer task. Used to calculate differences
208 // between two successive timer events.
Niels Möller1e062892018-02-07 10:18:32 +0100209 Stats last_stats_ RTC_GUARDED_BY(task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800210
henrikaba156cf2016-10-31 08:18:50 -0700211 // Set to true at construction and modified to false as soon as one audio-
212 // level estimate larger than zero is detected.
213 bool only_silence_recorded_;
henrika0b3a6382016-11-11 02:28:50 -0800214
215 // Set to true when logging of audio stats is enabled for the first time in
216 // StartPeriodicLogging() and set to false by StopPeriodicLogging().
217 // Setting this member to false prevents (possiby invalid) log messages from
218 // being printed in the LogStats() task.
Niels Möller1e062892018-02-07 10:18:32 +0100219 bool log_stats_ RTC_GUARDED_BY(task_queue_);
henrika7be78832017-06-13 17:34:16 +0200220
221// Should *never* be defined in production builds. Only used for testing.
222// When defined, the output signal will be replaced by a sinus tone at 440Hz.
223#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
henrika36b31792018-09-13 13:01:14 +0200224 double phase_;
henrika7be78832017-06-13 17:34:16 +0200225#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000226};
227
228} // namespace webrtc
229
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200230#endif // MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_