Chromium Code Reviews| OLD | NEW |
|---|---|
| 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; | 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 | 25 // Delta times between two successive playout callbacks are limited to this |
| 27 // value before added to an internal array. | 26 // value before added to an internal array. |
| 28 const size_t kMaxDeltaTimeInMs = 500; | 27 const size_t kMaxDeltaTimeInMs = 500; |
| 29 | 28 |
| 30 class AudioDeviceObserver; | 29 class AudioDeviceObserver; |
| 31 | 30 |
| 32 class AudioDeviceBuffer { | 31 class AudioDeviceBuffer { |
| 33 public: | 32 public: |
| 34 AudioDeviceBuffer(); | 33 AudioDeviceBuffer(); |
| 35 virtual ~AudioDeviceBuffer(); | 34 virtual ~AudioDeviceBuffer(); |
| 36 | 35 |
| 37 void SetId(uint32_t id) {}; | 36 void SetId(uint32_t id) {}; |
| 38 int32_t RegisterAudioCallback(AudioTransport* audioCallback); | 37 int32_t RegisterAudioCallback(AudioTransport* audioCallback); |
| 39 | 38 |
| 40 int32_t InitPlayout(); | 39 int32_t InitPlayout(); |
| 41 int32_t InitRecording(); | 40 int32_t InitRecording(); |
| 42 | 41 |
| 43 virtual int32_t SetRecordingSampleRate(uint32_t fsHz); | 42 virtual int32_t SetRecordingSampleRate(uint32_t fsHz); |
| 44 virtual int32_t SetPlayoutSampleRate(uint32_t fsHz); | 43 virtual int32_t SetPlayoutSampleRate(uint32_t fsHz); |
| 45 int32_t RecordingSampleRate() const; | 44 int32_t RecordingSampleRate() const; |
| 46 int32_t PlayoutSampleRate() const; | 45 int32_t PlayoutSampleRate() const; |
| 47 | 46 |
| 48 virtual int32_t SetRecordingChannels(size_t channels); | 47 virtual int32_t SetRecordingChannels(size_t channels); |
| 49 virtual int32_t SetPlayoutChannels(size_t channels); | 48 virtual int32_t SetPlayoutChannels(size_t channels); |
| 50 size_t RecordingChannels() const; | 49 size_t RecordingChannels() const; |
| 51 size_t PlayoutChannels() const; | 50 size_t PlayoutChannels() const; |
| 52 int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel); | 51 int32_t SetRecordingChannel(const AudioDeviceModule::ChannelType channel); |
| 53 int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const; | 52 int32_t RecordingChannel(AudioDeviceModule::ChannelType& channel) const; |
| 54 | 53 |
| 55 virtual int32_t SetRecordedBuffer(const void* audioBuffer, size_t nSamples); | 54 virtual int32_t SetRecordedBuffer(const void* audio_buffer, |
|
magjed_webrtc
2016/08/18 14:07:01
Why are many of these functions virtual? I don't f
henrika_webrtc
2016/08/18 14:22:00
Virtual? Legacy ;-) Very old code where I assume t
magjed_webrtc
2016/08/19 07:51:15
Alright.
| |
| 55 size_t num_samples); | |
| 56 int32_t SetCurrentMicLevel(uint32_t level); | 56 int32_t SetCurrentMicLevel(uint32_t level); |
| 57 virtual void SetVQEData(int playDelayMS, int recDelayMS, int clockDrift); | 57 virtual void SetVQEData(int playDelayMS, int recDelayMS, int clockDrift); |
| 58 virtual int32_t DeliverRecordedData(); | 58 virtual int32_t DeliverRecordedData(); |
| 59 uint32_t NewMicLevel() const; | 59 uint32_t NewMicLevel() const; |
| 60 | 60 |
| 61 virtual int32_t RequestPlayoutData(size_t nSamples); | 61 virtual int32_t RequestPlayoutData(size_t num_samples); |
| 62 virtual int32_t GetPlayoutData(void* audioBuffer); | 62 virtual int32_t GetPlayoutData(void* audioBuffer); |
| 63 | 63 |
| 64 // TODO(henrika): these methods should not be used and does not contain any | |
| 65 // valid implementation. Investigate the possibility to either remove them | |
| 66 // or add a proper implementation if needed. | |
| 64 int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]); | 67 int32_t StartInputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 65 int32_t StopInputFileRecording(); | 68 int32_t StopInputFileRecording(); |
| 66 int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]); | 69 int32_t StartOutputFileRecording(const char fileName[kAdmMaxFileNameSize]); |
| 67 int32_t StopOutputFileRecording(); | 70 int32_t StopOutputFileRecording(); |
| 68 | 71 |
| 69 int32_t SetTypingStatus(bool typingStatus); | 72 int32_t SetTypingStatus(bool typingStatus); |
| 70 | 73 |
| 71 private: | 74 private: |
| 72 // Posts the first delayed task in the task queue and starts the periodic | 75 // Posts the first delayed task in the task queue and starts the periodic |
| 73 // timer. | 76 // timer. |
| 74 void StartTimer(); | 77 void StartTimer(); |
| 75 | 78 |
| 76 // Called periodically on the internal thread created by the TaskQueue. | 79 // Called periodically on the internal thread created by the TaskQueue. |
| 77 void LogStats(); | 80 void LogStats(); |
| 78 | 81 |
| 79 // Updates counters in each play/record callback but does it on the task | 82 // 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 | 83 // queue to ensure that they can be read by LogStats() without any locks since |
| 81 // each task is serialized by the task queue. | 84 // each task is serialized by the task queue. |
| 82 void UpdateRecStats(size_t num_samples); | 85 void UpdateRecStats(size_t num_samples); |
| 83 void UpdatePlayStats(size_t num_samples); | 86 void UpdatePlayStats(size_t num_samples); |
| 84 | 87 |
| 85 // Ensures that methods are called on the same thread as the thread that | 88 // Ensures that methods are called on the same thread as the thread that |
| 86 // creates this object. | 89 // creates this object. |
| 87 rtc::ThreadChecker thread_checker_; | 90 rtc::ThreadChecker thread_checker_; |
| 88 | 91 |
| 92 // Raw pointer to AudioTransport instance. Supplied to RegisterAudioCallback() | |
| 93 // and it must outlive this object. | |
| 94 AudioTransport* audio_transport_cb_; | |
| 95 | |
| 96 // TODO(henrika): given usage of thread checker, it should be possible to | |
| 97 // remove all locks in this class. | |
| 89 rtc::CriticalSection _critSect; | 98 rtc::CriticalSection _critSect; |
| 90 rtc::CriticalSection _critSectCb; | 99 rtc::CriticalSection _critSectCb; |
| 91 | 100 |
| 92 AudioTransport* _ptrCbAudioTransport; | |
| 93 | |
| 94 // Task queue used to invoke LogStats() periodically. Tasks are executed on a | 101 // 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 | 102 // worker thread but it does not necessarily have to be the same thread for |
| 96 // each task. | 103 // each task. |
| 97 rtc::TaskQueue task_queue_; | 104 rtc::TaskQueue task_queue_; |
| 98 | 105 |
| 99 // Ensures that the timer is only started once. | 106 // Ensures that the timer is only started once. |
| 100 bool timer_has_started_; | 107 bool timer_has_started_; |
| 101 | 108 |
| 102 uint32_t _recSampleRate; | 109 // Sample rate in Hertz. |
| 103 uint32_t _playSampleRate; | 110 uint32_t rec_sample_rate_; |
| 111 uint32_t play_sample_rate_; | |
| 104 | 112 |
| 105 size_t _recChannels; | 113 // Number of audio channels. |
| 106 size_t _playChannels; | 114 size_t rec_channels_; |
| 115 size_t play_channels_; | |
| 107 | 116 |
| 108 // selected recording channel (left/right/both) | 117 // selected recording channel (left/right/both) |
| 109 AudioDeviceModule::ChannelType _recChannel; | 118 AudioDeviceModule::ChannelType rec_channel_; |
| 110 | 119 |
| 111 // 2 or 4 depending on mono or stereo | 120 // Number of bytes per audio sample (2 or 4). |
| 112 size_t _recBytesPerSample; | 121 size_t rec_bytes_per_sample_; |
| 113 size_t _playBytesPerSample; | 122 size_t play_bytes_per_sample_; |
| 114 | 123 |
| 115 // 10ms in stereo @ 96kHz | 124 // Number of audio samples/bytes per 10ms. |
| 116 int8_t _recBuffer[kMaxBufferSizeBytes]; | 125 size_t rec_samples_per_10ms_; |
| 126 size_t rec_bytes_per_10ms_; | |
| 127 size_t play_samples_per_10ms_; | |
| 128 size_t play_bytes_per_10ms_; | |
| 117 | 129 |
| 118 // one sample <=> 2 or 4 bytes | 130 // Buffer used for recorded audio samples. Size is given by |
| 119 size_t _recSamples; | 131 // |rec_bytes_per_10ms_| and the buffer is allocated in InitRecording() on the |
| 120 size_t _recSize; // in bytes | 132 // main/creating thread. |
| 133 std::unique_ptr<int8_t[]> rec_buffer_; | |
| 121 | 134 |
| 122 // 10ms in stereo @ 96kHz | 135 // Buffer used for audio samples to be played out. Size is given by |
| 123 int8_t _playBuffer[kMaxBufferSizeBytes]; | 136 // |play_bytes_per_10ms_| and the buffer is allocated in InitPlayout() on the |
| 137 // main/creating thread. | |
| 138 std::unique_ptr<int8_t[]> play_buffer_; | |
| 124 | 139 |
| 125 // one sample <=> 2 or 4 bytes | 140 // AGC parameters. |
| 126 size_t _playSamples; | 141 uint32_t current_mic_level_; |
| 127 size_t _playSize; // in bytes | 142 uint32_t new_mic_level_; |
| 128 | 143 |
| 129 FileWrapper& _recFile; | 144 // Contains true of a key-press has been detected. |
| 130 FileWrapper& _playFile; | 145 bool typing_status_; |
| 131 | 146 |
| 132 uint32_t _currentMicLevel; | 147 // Delay values used by the AEC. |
| 133 uint32_t _newMicLevel; | 148 int play_delay_ms_; |
| 149 int rec_delay_ms_; | |
| 134 | 150 |
| 135 bool _typingStatus; | 151 // Contains a clock-drift measurement. |
| 136 | 152 int clock_drift_; |
| 137 int _playDelayMS; | |
| 138 int _recDelayMS; | |
| 139 int _clockDrift; | |
| 140 int high_delay_counter_; | |
| 141 | 153 |
| 142 // Counts number of times LogStats() has been called. | 154 // Counts number of times LogStats() has been called. |
| 143 size_t num_stat_reports_; | 155 size_t num_stat_reports_; |
| 144 | 156 |
| 145 // Total number of recording callbacks where the source provides 10ms audio | 157 // Total number of recording callbacks where the source provides 10ms audio |
| 146 // data each time. | 158 // data each time. |
| 147 uint64_t rec_callbacks_; | 159 uint64_t rec_callbacks_; |
| 148 | 160 |
| 149 // Total number of recording callbacks stored at the last timer task. | 161 // Total number of recording callbacks stored at the last timer task. |
| 150 uint64_t last_rec_callbacks_; | 162 uint64_t last_rec_callbacks_; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 178 // milliseconds) between two successive playout callbacks, and the stored | 190 // milliseconds) between two successive playout callbacks, and the stored |
| 179 // value is the number of times a given time difference was found. | 191 // 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 | 192 // Writing to the array is done without a lock since it is only read once at |
| 181 // destruction when no audio is running. | 193 // destruction when no audio is running. |
| 182 uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0}; | 194 uint32_t playout_diff_times_[kMaxDeltaTimeInMs + 1] = {0}; |
| 183 }; | 195 }; |
| 184 | 196 |
| 185 } // namespace webrtc | 197 } // namespace webrtc |
| 186 | 198 |
| 187 #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ | 199 #endif // WEBRTC_MODULES_AUDIO_DEVICE_AUDIO_DEVICE_BUFFER_H_ |
| OLD | NEW |