blob: bbe2969d43697558b65874e3501e7eda607990af [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
Yves Gerey988cc082018-10-23 12:03:01 +020018#include "modules/audio_device/include/audio_device_defines.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/buffer.h"
20#include "rtc_base/criticalsection.h"
21#include "rtc_base/task_queue.h"
22#include "rtc_base/thread_annotations.h"
23#include "rtc_base/thread_checker.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000024
25namespace webrtc {
henrika7be78832017-06-13 17:34:16 +020026
henrika3d7346f2016-07-29 16:20:47 +020027// Delta times between two successive playout callbacks are limited to this
28// value before added to an internal array.
29const size_t kMaxDeltaTimeInMs = 500;
henrika49810512016-08-22 05:56:12 -070030// TODO(henrika): remove when no longer used by external client.
31const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz
niklase@google.com470e71d2011-07-07 08:21:25 +000032
henrika0fd68012016-07-04 13:01:19 +020033class AudioDeviceBuffer {
34 public:
henrikaba156cf2016-10-31 08:18:50 -070035 enum LogState {
36 LOG_START = 0,
37 LOG_STOP,
38 LOG_ACTIVE,
39 };
40
henrika87d11cd2017-02-08 07:16:56 -080041 struct Stats {
42 void ResetRecStats() {
43 rec_callbacks = 0;
44 rec_samples = 0;
45 max_rec_level = 0;
46 }
47
48 void ResetPlayStats() {
49 play_callbacks = 0;
50 play_samples = 0;
51 max_play_level = 0;
52 }
53
54 // Total number of recording callbacks where the source provides 10ms audio
55 // data each time.
56 uint64_t rec_callbacks = 0;
57
58 // Total number of playback callbacks where the sink asks for 10ms audio
59 // data each time.
60 uint64_t play_callbacks = 0;
61
62 // Total number of recorded audio samples.
63 uint64_t rec_samples = 0;
64
65 // Total number of played audio samples.
66 uint64_t play_samples = 0;
67
68 // Contains max level (max(abs(x))) of recorded audio packets over the last
69 // 10 seconds where a new measurement is done twice per second. The level
70 // is reset to zero at each call to LogStats().
71 int16_t max_rec_level = 0;
72
73 // Contains max level of recorded audio packets over the last 10 seconds
74 // where a new measurement is done twice per second.
75 int16_t max_play_level = 0;
76 };
77
henrika0fd68012016-07-04 13:01:19 +020078 AudioDeviceBuffer();
79 virtual ~AudioDeviceBuffer();
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000080
henrika49810512016-08-22 05:56:12 -070081 int32_t RegisterAudioCallback(AudioTransport* audio_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000082
henrikaba156cf2016-10-31 08:18:50 -070083 void StartPlayout();
84 void StartRecording();
85 void StopPlayout();
86 void StopRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000087
henrika49810512016-08-22 05:56:12 -070088 int32_t SetRecordingSampleRate(uint32_t fsHz);
89 int32_t SetPlayoutSampleRate(uint32_t fsHz);
henrikacfbd26d2018-09-05 11:36:22 +020090 uint32_t RecordingSampleRate() const;
91 uint32_t PlayoutSampleRate() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000092
henrika49810512016-08-22 05:56:12 -070093 int32_t SetRecordingChannels(size_t channels);
94 int32_t SetPlayoutChannels(size_t channels);
henrika0fd68012016-07-04 13:01:19 +020095 size_t RecordingChannels() const;
96 size_t PlayoutChannels() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000097
henrika49810512016-08-22 05:56:12 -070098 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -080099 size_t samples_per_channel);
Fredrik Solenberg1a50cd52018-01-16 09:19:38 +0100100 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms);
henrika0fd68012016-07-04 13:01:19 +0200101 virtual int32_t DeliverRecordedData();
102 uint32_t NewMicLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
henrika51e96082016-11-10 00:40:37 -0800104 virtual int32_t RequestPlayoutData(size_t samples_per_channel);
henrika49810512016-08-22 05:56:12 -0700105 virtual int32_t GetPlayoutData(void* audio_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
henrika49810512016-08-22 05:56:12 -0700107 int32_t SetTypingStatus(bool typing_status);
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000108
henrika0fd68012016-07-04 13:01:19 +0200109 private:
henrikaba156cf2016-10-31 08:18:50 -0700110 // Starts/stops periodic logging of audio stats.
111 void StartPeriodicLogging();
112 void StopPeriodicLogging();
henrika6c4d0f02016-07-14 05:54:19 -0700113
114 // Called periodically on the internal thread created by the TaskQueue.
henrikaba156cf2016-10-31 08:18:50 -0700115 // Updates some stats but dooes it on the task queue to ensure that access of
116 // members is serialized hence avoiding usage of locks.
117 // state = LOG_START => members are initialized and the timer starts.
118 // state = LOG_STOP => no logs are printed and the timer stops.
119 // state = LOG_ACTIVE => logs are printed and the timer is kept alive.
120 void LogStats(LogState state);
henrikaf06f35a2016-09-09 14:23:11 +0200121
henrika87d11cd2017-02-08 07:16:56 -0800122 // Updates counters in each play/record callback. These counters are later
123 // (periodically) read by LogStats() using a lock.
henrika51e96082016-11-10 00:40:37 -0800124 void UpdateRecStats(int16_t max_abs, size_t samples_per_channel);
125 void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel);
henrika6c4d0f02016-07-14 05:54:19 -0700126
henrikaba156cf2016-10-31 08:18:50 -0700127 // Clears all members tracking stats for recording and playout.
128 // These methods both run on the task queue.
129 void ResetRecStats();
130 void ResetPlayStats();
131
henrikaf5022222016-11-07 15:56:59 +0100132 // This object lives on the main (creating) thread and most methods are
133 // called on that same thread. When audio has started some methods will be
134 // called on either a native audio thread for playout or a native thread for
135 // recording. Some members are not annotated since they are "protected by
henrikacfbd26d2018-09-05 11:36:22 +0200136 // design" and adding e.g. a race checker can cause failures for very few
henrikaf5022222016-11-07 15:56:59 +0100137 // edge cases and it is IMHO not worth the risk to use them in this class.
138 // TODO(henrika): see if it is possible to refactor and annotate all members.
henrika6c4d0f02016-07-14 05:54:19 -0700139
henrikaf5022222016-11-07 15:56:59 +0100140 // Main thread on which this object is created.
141 rtc::ThreadChecker main_thread_checker_;
henrika49810512016-08-22 05:56:12 -0700142
henrika87d11cd2017-02-08 07:16:56 -0800143 rtc::CriticalSection lock_;
144
henrika6c4d0f02016-07-14 05:54:19 -0700145 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
146 // worker thread but it does not necessarily have to be the same thread for
147 // each task.
148 rtc::TaskQueue task_queue_;
149
henrikaf5022222016-11-07 15:56:59 +0100150 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback()
151 // and it must outlive this object. It is not possible to change this member
152 // while any media is active. It is possible to start media without calling
153 // RegisterAudioCallback() but that will lead to ignored audio callbacks in
henrikacfbd26d2018-09-05 11:36:22 +0200154 // both directions where native audio will be active but no audio samples will
henrikaf5022222016-11-07 15:56:59 +0100155 // be transported.
156 AudioTransport* audio_transport_cb_;
157
henrikacfbd26d2018-09-05 11:36:22 +0200158 // Sample rate in Hertz. Accessed atomically.
159 std::atomic<uint32_t> rec_sample_rate_;
160 std::atomic<uint32_t> play_sample_rate_;
henrika6c4d0f02016-07-14 05:54:19 -0700161
henrikacfbd26d2018-09-05 11:36:22 +0200162 // Number of audio channels. Accessed atomically.
163 std::atomic<size_t> rec_channels_;
164 std::atomic<size_t> play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000165
henrikaf5022222016-11-07 15:56:59 +0100166 // Keeps track of if playout/recording are active or not. A combination
167 // of these states are used to determine when to start and stop the timer.
168 // Only used on the creating thread and not used to control any media flow.
Niels Möller1e062892018-02-07 10:18:32 +0100169 bool playing_ RTC_GUARDED_BY(main_thread_checker_);
170 bool recording_ RTC_GUARDED_BY(main_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
henrika5588a132016-10-18 05:14:30 -0700172 // Buffer used for audio samples to be played out. Size can be changed
henrika51e96082016-11-10 00:40:37 -0800173 // dynamically. The 16-bit samples are interleaved, hence the size is
174 // proportional to the number of channels.
henrika36b31792018-09-13 13:01:14 +0200175 rtc::BufferT<int16_t> play_buffer_;
henrikaf5022222016-11-07 15:56:59 +0100176
177 // Byte buffer used for recorded audio samples. Size can be changed
178 // dynamically.
henrika36b31792018-09-13 13:01:14 +0200179 rtc::BufferT<int16_t> rec_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
henrika49810512016-08-22 05:56:12 -0700181 // Contains true of a key-press has been detected.
henrika36b31792018-09-13 13:01:14 +0200182 bool typing_status_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000183
henrika49810512016-08-22 05:56:12 -0700184 // Delay values used by the AEC.
henrika36b31792018-09-13 13:01:14 +0200185 int play_delay_ms_;
186 int rec_delay_ms_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000187
henrika6c4d0f02016-07-14 05:54:19 -0700188 // Counts number of times LogStats() has been called.
Niels Möller1e062892018-02-07 10:18:32 +0100189 size_t num_stat_reports_ RTC_GUARDED_BY(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700190
henrikaf5022222016-11-07 15:56:59 +0100191 // Time stamp of last timer task (drives logging).
Niels Möller1e062892018-02-07 10:18:32 +0100192 int64_t last_timer_task_time_ RTC_GUARDED_BY(task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200193
henrika3355f6d2016-10-21 12:45:25 +0200194 // Counts number of audio callbacks modulo 50 to create a signal when
195 // a new storage of audio stats shall be done.
henrika36b31792018-09-13 13:01:14 +0200196 int16_t rec_stat_count_;
197 int16_t play_stat_count_;
henrikaba156cf2016-10-31 08:18:50 -0700198
199 // Time stamps of when playout and recording starts.
Niels Möller1e062892018-02-07 10:18:32 +0100200 int64_t play_start_time_ RTC_GUARDED_BY(main_thread_checker_);
201 int64_t rec_start_time_ RTC_GUARDED_BY(main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700202
henrika87d11cd2017-02-08 07:16:56 -0800203 // Contains counters for playout and recording statistics.
danilchap56359be2017-09-07 07:53:45 -0700204 Stats stats_ RTC_GUARDED_BY(lock_);
henrika87d11cd2017-02-08 07:16:56 -0800205
206 // Stores current stats at each timer task. Used to calculate differences
207 // between two successive timer events.
Niels Möller1e062892018-02-07 10:18:32 +0100208 Stats last_stats_ RTC_GUARDED_BY(task_queue_);
henrika87d11cd2017-02-08 07:16:56 -0800209
henrikaba156cf2016-10-31 08:18:50 -0700210 // Set to true at construction and modified to false as soon as one audio-
211 // level estimate larger than zero is detected.
212 bool only_silence_recorded_;
henrika0b3a6382016-11-11 02:28:50 -0800213
214 // Set to true when logging of audio stats is enabled for the first time in
215 // StartPeriodicLogging() and set to false by StopPeriodicLogging().
216 // Setting this member to false prevents (possiby invalid) log messages from
217 // being printed in the LogStats() task.
Niels Möller1e062892018-02-07 10:18:32 +0100218 bool log_stats_ RTC_GUARDED_BY(task_queue_);
henrika7be78832017-06-13 17:34:16 +0200219
220// Should *never* be defined in production builds. Only used for testing.
221// When defined, the output signal will be replaced by a sinus tone at 440Hz.
222#ifdef AUDIO_DEVICE_PLAYS_SINUS_TONE
henrika36b31792018-09-13 13:01:14 +0200223 double phase_;
henrika7be78832017-06-13 17:34:16 +0200224#endif
niklase@google.com470e71d2011-07-07 08:21:25 +0000225};
226
227} // namespace webrtc
228
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200229#endif // MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_