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