Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: webrtc/modules/audio_device/audio_device_buffer.h

Issue 2256833003: Cleanup of the AudioDeviceBuffer class (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Ensures that all tests passes Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | webrtc/modules/audio_device/audio_device_buffer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ 11 #ifndef WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_
12 #define WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ 12 #define WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_
13 13
14 #include "webrtc/base/criticalsection.h" 14 #include "webrtc/base/criticalsection.h"
15 #include "webrtc/base/task_queue.h" 15 #include "webrtc/base/task_queue.h"
16 #include "webrtc/base/thread_checker.h" 16 #include "webrtc/base/thread_checker.h"
17 #include "webrtc/modules/audio_device/include/audio_device.h" 17 #include "webrtc/modules/audio_device/include/audio_device.h"
18 #include "webrtc/system_wrappers/include/file_wrapper.h" 18 #include "webrtc/system_wrappers/include/file_wrapper.h"
19 #include "webrtc/typedefs.h" 19 #include "webrtc/typedefs.h"
20 20
21 namespace webrtc { 21 namespace webrtc {
22 class CriticalSectionWrapper; 22 class CriticalSectionWrapper;
23 23
24 const uint32_t kPulsePeriodMs = 1000;
25 const size_t kMaxBufferSizeBytes = 3840; // 10ms in stereo @ 96kHz
26 // Delta times between two successive playout callbacks are limited to this 24 // Delta times between two successive playout callbacks are limited to this
27 // value before added to an internal array. 25 // value before added to an internal array.
28 const size_t kMaxDeltaTimeInMs = 500; 26 const size_t kMaxDeltaTimeInMs = 500;
29 27
30 class AudioDeviceObserver; 28 class AudioDeviceObserver;
31 29
32 class AudioDeviceBuffer { 30 class AudioDeviceBuffer {
33 public: 31 public:
34 AudioDeviceBuffer(); 32 AudioDeviceBuffer();
35 virtual ~AudioDeviceBuffer(); 33 virtual ~AudioDeviceBuffer();
36 34
37 void SetId(uint32_t id) {}; 35 void SetId(uint32_t id) {};
38 int32_t RegisterAudioCallback(AudioTransport* audioCallback); 36 int32_t RegisterAudioCallback(AudioTransport* audio_callback);
39 37
40 int32_t InitPlayout(); 38 int32_t InitPlayout();
41 int32_t InitRecording(); 39 int32_t InitRecording();
42 40
43 virtual int32_t SetRecordingSampleRate(uint32_t fsHz); 41 int32_t SetRecordingSampleRate(uint32_t fsHz);
44 virtual int32_t SetPlayoutSampleRate(uint32_t fsHz); 42 int32_t SetPlayoutSampleRate(uint32_t fsHz);
45 int32_t RecordingSampleRate() const; 43 int32_t RecordingSampleRate() const;
46 int32_t PlayoutSampleRate() const; 44 int32_t PlayoutSampleRate() const;
47 45
48 virtual int32_t SetRecordingChannels(size_t channels); 46 int32_t SetRecordingChannels(size_t channels);
49 virtual int32_t SetPlayoutChannels(size_t channels); 47 int32_t SetPlayoutChannels(size_t channels);
50 size_t RecordingChannels() const; 48 size_t RecordingChannels() const;
51 size_t PlayoutChannels() const; 49 size_t PlayoutChannels() const;
52 int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel); 50 int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel);
53 int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const; 51 int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const;
54 52
55 virtual int32_t SetRecordedBuffer(const void* audioBuffer, size_t nSamples); 53 virtual int32_t SetRecordedBuffer(const void* audio_buffer,
54 size_t num_samples);
56 int32_t SetCurrentMicLevel(uint32_t level); 55 int32_t SetCurrentMicLevel(uint32_t level);
57 virtual void SetVQEData(int playDelayMS, int recDelayMS, int clockDrift); 56 virtual void SetVQEData(int play_delay_ms, int rec_delay_ms, int clock_drift);
58 virtual int32_t DeliverRecordedData(); 57 virtual int32_t DeliverRecordedData();
59 uint32_t NewMicLevel() const; 58 uint32_t NewMicLevel() const;
60 59
61 virtual int32_t RequestPlayoutData(size_t nSamples); 60 virtual int32_t RequestPlayoutData(size_t num_samples);
62 virtual int32_t GetPlayoutData(void* audioBuffer); 61 virtual int32_t GetPlayoutData(void* audio_buffer);
63 62
63 // TODO(henrika): these methods should not be used and does not contain any
64 // valid implementation. Investigate the possibility to either remove them
65 // or add a proper implementation if needed.
64 int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]); 66 int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]);
65 int32_t StopInputFileRecording(); 67 int32_t StopInputFileRecording();
66 int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]); 68 int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]);
67 int32_t StopOutputFileRecording(); 69 int32_t StopOutputFileRecording();
68 70
69 int32_t SetTypingStatus(bool typingStatus); 71 int32_t SetTypingStatus(bool typing_status);
70 72
71 private: 73 private:
74 void AllocatePlayoutBufferIfNeeded();
75 void AllocateRecordingBufferIfNeeded();
76
72 // Posts the first delayed task in the task queue and starts the periodic 77 // Posts the first delayed task in the task queue and starts the periodic
73 // timer. 78 // timer.
74 void StartTimer(); 79 void StartTimer();
75 80
76 // Called periodically on the internal thread created by the TaskQueue. 81 // Called periodically on the internal thread created by the TaskQueue.
77 void LogStats(); 82 void LogStats();
78 83
79 // Updates counters in each play/record callback but does it on the task 84 // Updates counters in each play/record callback but does it on the task
80 // queue to ensure that they can be read by LogStats() without any locks since 85 // queue to ensure that they can be read by LogStats() without any locks since
81 // each task is serialized by the task queue. 86 // each task is serialized by the task queue.
82 void UpdateRecStats(size_t num_samples); 87 void UpdateRecStats(size_t num_samples);
83 void UpdatePlayStats(size_t num_samples); 88 void UpdatePlayStats(size_t num_samples);
84 89
85 // Ensures that methods are called on the same thread as the thread that 90 // Ensures that methods are called on the same thread as the thread that
86 // creates this object. 91 // creates this object.
87 rtc::ThreadChecker thread_checker_; 92 rtc::ThreadChecker thread_checker_;
88 93
94 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback()
95 // and it must outlive this object.
96 AudioTransport* audio_transport_cb_;
97
98 // TODO(henrika): given usage of thread checker, it should be possible to
99 // remove all locks in this class.
89 rtc::CriticalSection _critSect; 100 rtc::CriticalSection _critSect;
90 rtc::CriticalSection _critSectCb; 101 rtc::CriticalSection _critSectCb;
91 102
92 AudioTransport* _ptrCbAudioTransport;
93
94 // Task queue used to invoke LogStats() periodically. Tasks are executed on a 103 // Task queue used to invoke LogStats() periodically. Tasks are executed on a
95 // worker thread but it does not necessarily have to be the same thread for 104 // worker thread but it does not necessarily have to be the same thread for
96 // each task. 105 // each task.
97 rtc::TaskQueue task_queue_; 106 rtc::TaskQueue task_queue_;
98 107
99 // Ensures that the timer is only started once. 108 // Ensures that the timer is only started once.
100 bool timer_has_started_; 109 bool timer_has_started_;
101 110
102 uint32_t _recSampleRate; 111 // Sample rate in Hertz.
103 uint32_t _playSampleRate; 112 uint32_t rec_sample_rate_;
113 uint32_t play_sample_rate_;
104 114
105 size_t _recChannels; 115 // Number of audio channels.
106 size_t _playChannels; 116 size_t rec_channels_;
117 size_t play_channels_;
107 118
108 // selected recording channel (left/right/both) 119 // selected recording channel (left/right/both)
109 AudioDeviceModule::ChannelType _recChannel; 120 AudioDeviceModule::ChannelType rec_channel_;
110 121
111 // 2 or 4 depending on mono or stereo 122 // Number of bytes per audio sample (2 or 4).
112 size_t _recBytesPerSample; 123 size_t rec_bytes_per_sample_;
113 size_t _playBytesPerSample; 124 size_t play_bytes_per_sample_;
114 125
115 // 10ms in stereo @ 96kHz 126 // Number of audio samples/bytes per 10ms.
116 int8_t _recBuffer[kMaxBufferSizeBytes]; 127 size_t rec_samples_per_10ms_;
128 size_t rec_bytes_per_10ms_;
129 size_t play_samples_per_10ms_;
130 size_t play_bytes_per_10ms_;
117 131
118 // one sample <=> 2 or 4 bytes 132 // Buffer used for recorded audio samples. Size is given by
119 size_t _recSamples; 133 // |rec_bytes_per_10ms_| and the buffer is allocated in InitRecording() on the
120 size_t _recSize; // in bytes 134 // main/creating thread.
135 std::unique_ptr<int8_t[]> rec_buffer_;
121 136
122 // 10ms in stereo @ 96kHz 137 // Buffer used for audio samples to be played out. Size is given by
123 int8_t _playBuffer[kMaxBufferSizeBytes]; 138 // |play_bytes_per_10ms_| and the buffer is allocated in InitPlayout() on the
139 // main/creating thread.
140 std::unique_ptr<int8_t[]> play_buffer_;
124 141
125 // one sample <=> 2 or 4 bytes 142 // AGC parameters.
126 size_t _playSamples; 143 uint32_t current_mic_level_;
127 size_t _playSize; // in bytes 144 uint32_t new_mic_level_;
128 145
129 FileWrapper& _recFile; 146 // Contains true of a key-press has been detected.
130 FileWrapper& _playFile; 147 bool typing_status_;
131 148
132 uint32_t _currentMicLevel; 149 // Delay values used by the AEC.
133 uint32_t _newMicLevel; 150 int play_delay_ms_;
151 int rec_delay_ms_;
134 152
135 bool _typingStatus; 153 // Contains a clock-drift measurement.
136 154 int clock_drift_;
137 int _playDelayMS;
138 int _recDelayMS;
139 int _clockDrift;
140 int high_delay_counter_;
141 155
142 // Counts number of times LogStats() has been called. 156 // Counts number of times LogStats() has been called.
143 size_t num_stat_reports_; 157 size_t num_stat_reports_;
144 158
145 // Total number of recording callbacks where the source provides 10ms audio 159 // Total number of recording callbacks where the source provides 10ms audio
146 // data each time. 160 // data each time.
147 uint64_t rec_callbacks_; 161 uint64_t rec_callbacks_;
148 162
149 // Total number of recording callbacks stored at the last timer task. 163 // Total number of recording callbacks stored at the last timer task.
150 uint64_t last_rec_callbacks_; 164 uint64_t last_rec_callbacks_;
(...skipping 27 matching lines...) Expand all
178 // milliseconds) between two successive playout callbacks, and the stored 192 // milliseconds) between two successive playout callbacks, and the stored
179 // value is the number of times a given time difference was found. 193 // value is the number of times a given time difference was found.
180 // Writing to the array is done without a lock since it is only read once at 194 // Writing to the array is done without a lock since it is only read once at
181 // destruction when no audio is running. 195 // destruction when no audio is running.
182 uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0}; 196 uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0};
183 }; 197 };
184 198
185 } // namespace webrtc 199 } // namespace webrtc
186 200
187 #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ 201 #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/modules/audio_device/audio_device_buffer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698