blob: e2c6f2871a5fad2f0443ca878c979bddfe2bd899 [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
henrika6c4d0f02016-07-14 05:54:19 -070011#ifndef WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_
12#define WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
henrika5588a132016-10-18 05:14:30 -070014#include "webrtc/base/buffer.h"
henrika6c4d0f02016-07-14 05:54:19 -070015#include "webrtc/base/task_queue.h"
henrikaf5022222016-11-07 15:56:59 +010016#include "webrtc/base/thread_annotations.h"
henrika6c4d0f02016-07-14 05:54:19 -070017#include "webrtc/base/thread_checker.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000018#include "webrtc/modules/audio_device/include/audio_device.h"
tereliusc4b9b942016-10-28 06:51:59 -070019#include "webrtc/system_wrappers/include/file_wrapper.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000020#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
22namespace webrtc {
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
29class AudioDeviceObserver;
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
henrika0fd68012016-07-04 13:01:19 +020039 AudioDeviceBuffer();
40 virtual ~AudioDeviceBuffer();
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000041
tereliusc4b9b942016-10-28 06:51:59 -070042 void SetId(uint32_t id) {};
henrika49810512016-08-22 05:56:12 -070043 int32_t RegisterAudioCallback(AudioTransport* audio_callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000044
henrikaba156cf2016-10-31 08:18:50 -070045 void StartPlayout();
46 void StartRecording();
47 void StopPlayout();
48 void StopRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000049
henrika49810512016-08-22 05:56:12 -070050 int32_t SetRecordingSampleRate(uint32_t fsHz);
51 int32_t SetPlayoutSampleRate(uint32_t fsHz);
henrika0fd68012016-07-04 13:01:19 +020052 int32_t RecordingSampleRate() const;
53 int32_t PlayoutSampleRate() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000054
henrika49810512016-08-22 05:56:12 -070055 int32_t SetRecordingChannels(size_t channels);
56 int32_t SetPlayoutChannels(size_t channels);
henrika0fd68012016-07-04 13:01:19 +020057 size_t RecordingChannels() const;
58 size_t PlayoutChannels() const;
59 int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel);
60 int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000061
henrika49810512016-08-22 05:56:12 -070062 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
henrika51e96082016-11-10 00:40:37 -080063 size_t samples_per_channel);
henrika0fd68012016-07-04 13:01:19 +020064 int32_t SetCurrentMicLevel(uint32_t level);
henrika49810512016-08-22 05:56:12 -070065 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms, int clock_drift);
henrika0fd68012016-07-04 13:01:19 +020066 virtual int32_t DeliverRecordedData();
67 uint32_t NewMicLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000068
henrika51e96082016-11-10 00:40:37 -080069 virtual int32_t RequestPlayoutData(size_t samples_per_channel);
henrika49810512016-08-22 05:56:12 -070070 virtual int32_t GetPlayoutData(void* audio_buffer);
niklase@google.com470e71d2011-07-07 08:21:25 +000071
henrika49810512016-08-22 05:56:12 -070072 // TODO(henrika): these methods should not be used and does not contain any
73 // valid implementation. Investigate the possibility to either remove them
74 // or add a proper implementation if needed.
henrika0fd68012016-07-04 13:01:19 +020075 int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]);
76 int32_t StopInputFileRecording();
77 int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]);
78 int32_t StopOutputFileRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000079
henrika49810512016-08-22 05:56:12 -070080 int32_t SetTypingStatus(bool typing_status);
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +000081
henrika0fd68012016-07-04 13:01:19 +020082 private:
henrikaba156cf2016-10-31 08:18:50 -070083 // Starts/stops periodic logging of audio stats.
84 void StartPeriodicLogging();
85 void StopPeriodicLogging();
henrika6c4d0f02016-07-14 05:54:19 -070086
87 // Called periodically on the internal thread created by the TaskQueue.
henrikaba156cf2016-10-31 08:18:50 -070088 // Updates some stats but dooes it on the task queue to ensure that access of
89 // members is serialized hence avoiding usage of locks.
90 // state = LOG_START => members are initialized and the timer starts.
91 // state = LOG_STOP => no logs are printed and the timer stops.
92 // state = LOG_ACTIVE => logs are printed and the timer is kept alive.
93 void LogStats(LogState state);
henrikaf06f35a2016-09-09 14:23:11 +020094
henrika6c4d0f02016-07-14 05:54:19 -070095 // Updates counters in each play/record callback but does it on the task
96 // queue to ensure that they can be read by LogStats() without any locks since
97 // each task is serialized by the task queue.
henrika51e96082016-11-10 00:40:37 -080098 void UpdateRecStats(int16_t max_abs, size_t samples_per_channel);
99 void UpdatePlayStats(int16_t max_abs, size_t samples_per_channel);
henrika6c4d0f02016-07-14 05:54:19 -0700100
henrikaba156cf2016-10-31 08:18:50 -0700101 // Clears all members tracking stats for recording and playout.
102 // These methods both run on the task queue.
103 void ResetRecStats();
104 void ResetPlayStats();
105
henrikaf5022222016-11-07 15:56:59 +0100106 // This object lives on the main (creating) thread and most methods are
107 // called on that same thread. When audio has started some methods will be
108 // called on either a native audio thread for playout or a native thread for
109 // recording. Some members are not annotated since they are "protected by
110 // design" and adding e.g. a race checker can cause failuries for very few
111 // edge cases and it is IMHO not worth the risk to use them in this class.
112 // TODO(henrika): see if it is possible to refactor and annotate all members.
henrika6c4d0f02016-07-14 05:54:19 -0700113
henrikaf5022222016-11-07 15:56:59 +0100114 // Main thread on which this object is created.
115 rtc::ThreadChecker main_thread_checker_;
henrika49810512016-08-22 05:56:12 -0700116
henrikaf5022222016-11-07 15:56:59 +0100117 // Native (platform specific) audio thread driving the playout side.
118 rtc::ThreadChecker playout_thread_checker_;
119
120 // Native (platform specific) audio thread driving the recording side.
121 rtc::ThreadChecker recording_thread_checker_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
henrika6c4d0f02016-07-14 05:54:19 -0700123 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
124 // worker thread but it does not necessarily have to be the same thread for
125 // each task.
126 rtc::TaskQueue task_queue_;
127
henrikaf5022222016-11-07 15:56:59 +0100128 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback()
129 // and it must outlive this object. It is not possible to change this member
130 // while any media is active. It is possible to start media without calling
131 // RegisterAudioCallback() but that will lead to ignored audio callbacks in
132 // both directions where native audio will be acive but no audio samples will
133 // be transported.
134 AudioTransport* audio_transport_cb_;
135
136 // The members below that are not annotated are protected by design. They are
137 // all set on the main thread (verified by |main_thread_checker_|) and then
138 // read on either the playout or recording audio thread. But, media will never
139 // be active when the member is set; hence no conflict exists. It is too
140 // complex to ensure and verify that this is actually the case.
henrika6c4d0f02016-07-14 05:54:19 -0700141
henrika49810512016-08-22 05:56:12 -0700142 // Sample rate in Hertz.
143 uint32_t rec_sample_rate_;
144 uint32_t play_sample_rate_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
henrika49810512016-08-22 05:56:12 -0700146 // Number of audio channels.
147 size_t rec_channels_;
148 size_t play_channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000149
henrikaf5022222016-11-07 15:56:59 +0100150 // Keeps track of if playout/recording are active or not. A combination
151 // of these states are used to determine when to start and stop the timer.
152 // Only used on the creating thread and not used to control any media flow.
153 bool playing_ ACCESS_ON(main_thread_checker_);
154 bool recording_ ACCESS_ON(main_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000155
henrika5588a132016-10-18 05:14:30 -0700156 // Buffer used for audio samples to be played out. Size can be changed
henrika51e96082016-11-10 00:40:37 -0800157 // dynamically. The 16-bit samples are interleaved, hence the size is
158 // proportional to the number of channels.
159 rtc::BufferT<int16_t> play_buffer_ ACCESS_ON(playout_thread_checker_);
henrikaf5022222016-11-07 15:56:59 +0100160
161 // Byte buffer used for recorded audio samples. Size can be changed
162 // dynamically.
henrika51e96082016-11-10 00:40:37 -0800163 rtc::BufferT<int16_t> rec_buffer_ ACCESS_ON(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000164
henrika49810512016-08-22 05:56:12 -0700165 // AGC parameters.
henrikaf5022222016-11-07 15:56:59 +0100166#if !defined(WEBRTC_WIN)
167 uint32_t current_mic_level_ ACCESS_ON(recording_thread_checker_);
168#else
169 // Windows uses a dedicated thread for volume APIs.
henrika49810512016-08-22 05:56:12 -0700170 uint32_t current_mic_level_;
henrikaf5022222016-11-07 15:56:59 +0100171#endif
172 uint32_t new_mic_level_ ACCESS_ON(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000173
henrika49810512016-08-22 05:56:12 -0700174 // Contains true of a key-press has been detected.
henrikaf5022222016-11-07 15:56:59 +0100175 bool typing_status_ ACCESS_ON(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176
henrika49810512016-08-22 05:56:12 -0700177 // Delay values used by the AEC.
henrikaf5022222016-11-07 15:56:59 +0100178 int play_delay_ms_ ACCESS_ON(recording_thread_checker_);
179 int rec_delay_ms_ ACCESS_ON(recording_thread_checker_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000180
henrika49810512016-08-22 05:56:12 -0700181 // Contains a clock-drift measurement.
henrikaf5022222016-11-07 15:56:59 +0100182 int clock_drift_ ACCESS_ON(recording_thread_checker_);
henrika6c4d0f02016-07-14 05:54:19 -0700183
184 // Counts number of times LogStats() has been called.
henrikaf5022222016-11-07 15:56:59 +0100185 size_t num_stat_reports_ ACCESS_ON(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700186
187 // Total number of recording callbacks where the source provides 10ms audio
188 // data each time.
henrikaf5022222016-11-07 15:56:59 +0100189 uint64_t rec_callbacks_ ACCESS_ON(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700190
191 // Total number of recording callbacks stored at the last timer task.
henrikaf5022222016-11-07 15:56:59 +0100192 uint64_t last_rec_callbacks_ ACCESS_ON(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700193
194 // Total number of playback callbacks where the sink asks for 10ms audio
195 // data each time.
henrikaf5022222016-11-07 15:56:59 +0100196 uint64_t play_callbacks_ ACCESS_ON(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700197
198 // Total number of playout callbacks stored at the last timer task.
henrikaf5022222016-11-07 15:56:59 +0100199 uint64_t last_play_callbacks_ ACCESS_ON(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700200
201 // Total number of recorded audio samples.
henrikaf5022222016-11-07 15:56:59 +0100202 uint64_t rec_samples_ ACCESS_ON(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700203
204 // Total number of recorded samples stored at the previous timer task.
henrikaf5022222016-11-07 15:56:59 +0100205 uint64_t last_rec_samples_ ACCESS_ON(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700206
207 // Total number of played audio samples.
henrikaf5022222016-11-07 15:56:59 +0100208 uint64_t play_samples_ ACCESS_ON(task_queue_);
henrika6c4d0f02016-07-14 05:54:19 -0700209
210 // Total number of played samples stored at the previous timer task.
henrikaf5022222016-11-07 15:56:59 +0100211 uint64_t last_play_samples_ ACCESS_ON(task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200212
213 // Contains max level (max(abs(x))) of recorded audio packets over the last
214 // 10 seconds where a new measurement is done twice per second. The level
henrikaf5022222016-11-07 15:56:59 +0100215 // is reset to zero at each call to LogStats().
216 int16_t max_rec_level_ ACCESS_ON(task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200217
218 // Contains max level of recorded audio packets over the last 10 seconds
219 // where a new measurement is done twice per second.
henrikaf5022222016-11-07 15:56:59 +0100220 int16_t max_play_level_ ACCESS_ON(task_queue_);
221
222 // Time stamp of last timer task (drives logging).
nissedeb95f32016-11-28 01:54:54 -0800223 int64_t last_timer_task_time_ ACCESS_ON(task_queue_);
henrikaf06f35a2016-09-09 14:23:11 +0200224
henrika3355f6d2016-10-21 12:45:25 +0200225 // Counts number of audio callbacks modulo 50 to create a signal when
226 // a new storage of audio stats shall be done.
henrikaf5022222016-11-07 15:56:59 +0100227 int16_t rec_stat_count_ ACCESS_ON(recording_thread_checker_);
228 int16_t play_stat_count_ ACCESS_ON(playout_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700229
230 // Time stamps of when playout and recording starts.
nissedeb95f32016-11-28 01:54:54 -0800231 int64_t play_start_time_ ACCESS_ON(main_thread_checker_);
232 int64_t rec_start_time_ ACCESS_ON(main_thread_checker_);
henrikaba156cf2016-10-31 08:18:50 -0700233
234 // Set to true at construction and modified to false as soon as one audio-
235 // level estimate larger than zero is detected.
236 bool only_silence_recorded_;
henrika0b3a6382016-11-11 02:28:50 -0800237
238 // Set to true when logging of audio stats is enabled for the first time in
239 // StartPeriodicLogging() and set to false by StopPeriodicLogging().
240 // Setting this member to false prevents (possiby invalid) log messages from
241 // being printed in the LogStats() task.
242 bool log_stats_ ACCESS_ON(task_queue_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000243};
244
245} // namespace webrtc
246
henrika6c4d0f02016-07-14 05:54:19 -0700247#endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_