blob: ee6b2297c77e8e3a981615e52d3c0ccf0db7c767 [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
henrika6c4d0f02016-07-14 05:54:19 -070014#include "webrtc/base/criticalsection.h"
15#include "webrtc/base/task_queue.h"
16#include "webrtc/base/thread_checker.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000017#include "webrtc/modules/audio_device/include/audio_device.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010018#include "webrtc/system_wrappers/include/file_wrapper.h"
pbos@webrtc.org811269d2013-07-11 13:24:38 +000019#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
22class CriticalSectionWrapper;
23
pbos@webrtc.org25509882013-04-09 10:30:35 +000024const uint32_t kPulsePeriodMs = 1000;
henrika0fd68012016-07-04 13:01:19 +020025const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz
niklase@google.com470e71d2011-07-07 08:21:25 +000026
27class AudioDeviceObserver;
niklase@google.com470e71d2011-07-07 08:21:25 +000028
henrika0fd68012016-07-04 13:01:19 +020029class AudioDeviceBuffer {
30 public:
31 AudioDeviceBuffer();
32 virtual ~AudioDeviceBuffer();
henrike@webrtc.org82f014a2013-09-10 18:24:07 +000033
henrika3f33e2a2016-07-06 00:33:57 -070034 void SetId(uint32_t id) {};
henrika0fd68012016-07-04 13:01:19 +020035 int32_t RegisterAudioCallback(AudioTransport* audioCallback);
niklase@google.com470e71d2011-07-07 08:21:25 +000036
henrika0fd68012016-07-04 13:01:19 +020037 int32_t InitPlayout();
38 int32_t InitRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000039
henrika0fd68012016-07-04 13:01:19 +020040 virtual int32_t SetRecordingSampleRate(uint32_t fsHz);
41 virtual int32_t SetPlayoutSampleRate(uint32_t fsHz);
42 int32_t RecordingSampleRate() const;
43 int32_t PlayoutSampleRate() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000044
henrika0fd68012016-07-04 13:01:19 +020045 virtual int32_t SetRecordingChannels(size_t channels);
46 virtual int32_t SetPlayoutChannels(size_t channels);
47 size_t RecordingChannels() const;
48 size_t PlayoutChannels() const;
49 int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel);
50 int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const;
niklase@google.com470e71d2011-07-07 08:21:25 +000051
henrika0fd68012016-07-04 13:01:19 +020052 virtual int32_t SetRecordedBuffer(const void* audioBuffer, size_t nSamples);
53 int32_t SetCurrentMicLevel(uint32_t level);
54 virtual void SetVQEData(int playDelayMS, int recDelayMS, int clockDrift);
55 virtual int32_t DeliverRecordedData();
56 uint32_t NewMicLevel() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000057
henrika0fd68012016-07-04 13:01:19 +020058 virtual int32_t RequestPlayoutData(size_t nSamples);
59 virtual int32_t GetPlayoutData(void* audioBuffer);
niklase@google.com470e71d2011-07-07 08:21:25 +000060
henrika0fd68012016-07-04 13:01:19 +020061 int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]);
62 int32_t StopInputFileRecording();
63 int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]);
64 int32_t StopOutputFileRecording();
niklase@google.com470e71d2011-07-07 08:21:25 +000065
henrika0fd68012016-07-04 13:01:19 +020066 int32_t SetTypingStatus(bool typingStatus);
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +000067
henrika0fd68012016-07-04 13:01:19 +020068 private:
henrika6c4d0f02016-07-14 05:54:19 -070069 // Posts the first delayed task in the task queue and starts the periodic
70 // timer.
71 void StartTimer();
72
73 // Called periodically on the internal thread created by the TaskQueue.
74 void LogStats();
75
76 // Updates counters in each play/record callback but does it on the task
77 // queue to ensure that they can be read by LogStats() without any locks since
78 // each task is serialized by the task queue.
79 void UpdateRecStats(size_t num_samples);
80 void UpdatePlayStats(size_t num_samples);
81
82 // Ensures that methods are called on the same thread as the thread that
83 // creates this object.
84 rtc::ThreadChecker thread_checker_;
85
86 rtc::CriticalSection _critSect;
87 rtc::CriticalSection _critSectCb;
niklase@google.com470e71d2011-07-07 08:21:25 +000088
henrika0fd68012016-07-04 13:01:19 +020089 AudioTransport* _ptrCbAudioTransport;
niklase@google.com470e71d2011-07-07 08:21:25 +000090
henrika6c4d0f02016-07-14 05:54:19 -070091 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
92 // worker thread but it does not necessarily have to be the same thread for
93 // each task.
94 rtc::TaskQueue task_queue_;
95
96 // Ensures that the timer is only started once.
97 bool timer_has_started_;
98
henrika0fd68012016-07-04 13:01:19 +020099 uint32_t _recSampleRate;
100 uint32_t _playSampleRate;
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
henrika0fd68012016-07-04 13:01:19 +0200102 size_t _recChannels;
103 size_t _playChannels;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
henrika0fd68012016-07-04 13:01:19 +0200105 // selected recording channel (left/right/both)
106 AudioDeviceModule::ChannelType _recChannel;
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
henrika0fd68012016-07-04 13:01:19 +0200108 // 2 or 4 depending on mono or stereo
109 size_t _recBytesPerSample;
110 size_t _playBytesPerSample;
niklase@google.com470e71d2011-07-07 08:21:25 +0000111
henrika0fd68012016-07-04 13:01:19 +0200112 // 10ms in stereo @ 96kHz
113 int8_t _recBuffer[kMaxBufferSizeBytes];
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
henrika0fd68012016-07-04 13:01:19 +0200115 // one sample <=> 2 or 4 bytes
116 size_t _recSamples;
117 size_t _recSize; // in bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
henrika0fd68012016-07-04 13:01:19 +0200119 // 10ms in stereo @ 96kHz
120 int8_t _playBuffer[kMaxBufferSizeBytes];
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
henrika0fd68012016-07-04 13:01:19 +0200122 // one sample <=> 2 or 4 bytes
123 size_t _playSamples;
124 size_t _playSize; // in bytes
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
henrika0fd68012016-07-04 13:01:19 +0200126 FileWrapper& _recFile;
127 FileWrapper& _playFile;
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
henrika0fd68012016-07-04 13:01:19 +0200129 uint32_t _currentMicLevel;
130 uint32_t _newMicLevel;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
henrika0fd68012016-07-04 13:01:19 +0200132 bool _typingStatus;
niklas.enbom@webrtc.org3be565b2013-05-07 21:04:24 +0000133
henrika0fd68012016-07-04 13:01:19 +0200134 int _playDelayMS;
135 int _recDelayMS;
136 int _clockDrift;
137 int high_delay_counter_;
henrika6c4d0f02016-07-14 05:54:19 -0700138
139 // Counts number of times LogStats() has been called.
140 size_t num_stat_reports_;
141
142 // Total number of recording callbacks where the source provides 10ms audio
143 // data each time.
144 uint64_t rec_callbacks_;
145
146 // Total number of recording callbacks stored at the last timer task.
147 uint64_t last_rec_callbacks_;
148
149 // Total number of playback callbacks where the sink asks for 10ms audio
150 // data each time.
151 uint64_t play_callbacks_;
152
153 // Total number of playout callbacks stored at the last timer task.
154 uint64_t last_play_callbacks_;
155
156 // Total number of recorded audio samples.
157 uint64_t rec_samples_;
158
159 // Total number of recorded samples stored at the previous timer task.
160 uint64_t last_rec_samples_;
161
162 // Total number of played audio samples.
163 uint64_t play_samples_;
164
165 // Total number of played samples stored at the previous timer task.
166 uint64_t last_play_samples_;
167
168 // Time stamp of last stat report.
169 uint64_t last_log_stat_time_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000170};
171
172} // namespace webrtc
173
henrika6c4d0f02016-07-14 05:54:19 -0700174#endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_