audio_device_defines.h (7316B)
1 /* 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 3 * 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 11 #ifndef API_AUDIO_AUDIO_DEVICE_DEFINES_H_ 12 #define API_AUDIO_AUDIO_DEVICE_DEFINES_H_ 13 14 #include <stddef.h> 15 16 #include <cstdint> 17 #include <optional> 18 #include <string> 19 20 #include "rtc_base/strings/string_builder.h" 21 22 namespace webrtc { 23 24 static const int kAdmMaxDeviceNameSize = 128; 25 static const int kAdmMaxFileNameSize = 512; 26 static const int kAdmMaxGuidSize = 128; 27 28 static const int kAdmMinPlayoutBufferSizeMs = 10; 29 static const int kAdmMaxPlayoutBufferSizeMs = 250; 30 31 // ---------------------------------------------------------------------------- 32 // AudioTransport 33 // ---------------------------------------------------------------------------- 34 35 class AudioTransport { 36 public: 37 // TODO(bugs.webrtc.org/13620) Deprecate this function 38 virtual int32_t RecordedDataIsAvailable(const void* audioSamples, 39 size_t nSamples, 40 size_t nBytesPerSample, 41 size_t nChannels, 42 uint32_t samplesPerSec, 43 uint32_t totalDelayMS, 44 int32_t clockDrift, 45 uint32_t currentMicLevel, 46 bool keyPressed, 47 uint32_t& newMicLevel) = 0; // NOLINT 48 49 virtual int32_t RecordedDataIsAvailable( 50 const void* audioSamples, 51 size_t nSamples, 52 size_t nBytesPerSample, 53 size_t nChannels, 54 uint32_t samplesPerSec, 55 uint32_t totalDelayMS, 56 int32_t clockDrift, 57 uint32_t currentMicLevel, 58 bool keyPressed, 59 uint32_t& newMicLevel, 60 std::optional<int64_t> /* estimatedCaptureTimeNS */) { // NOLINT 61 // TODO(webrtc:13620) Make the default behaver of the new API to behave as 62 // the old API. This can be pure virtual if all uses of the old API is 63 // removed. 64 return RecordedDataIsAvailable( 65 audioSamples, nSamples, nBytesPerSample, nChannels, samplesPerSec, 66 totalDelayMS, clockDrift, currentMicLevel, keyPressed, newMicLevel); 67 } 68 69 // Implementation has to setup safe values for all specified out parameters. 70 virtual int32_t NeedMorePlayData(size_t nSamples, 71 size_t nBytesPerSample, 72 size_t nChannels, 73 uint32_t samplesPerSec, 74 void* audioSamples, 75 size_t& nSamplesOut, // NOLINT 76 int64_t* elapsed_time_ms, 77 int64_t* ntp_time_ms) = 0; // NOLINT 78 79 // Method to pull mixed render audio data from all active VoE channels. 80 // The data will not be passed as reference for audio processing internally. 81 virtual void PullRenderData(int bits_per_sample, 82 int sample_rate, 83 size_t number_of_channels, 84 size_t number_of_frames, 85 void* audio_data, 86 int64_t* elapsed_time_ms, 87 int64_t* ntp_time_ms) = 0; 88 89 protected: 90 virtual ~AudioTransport() {} 91 }; 92 93 // Helper class for storage of fundamental audio parameters such as sample rate, 94 // number of channels, native buffer size etc. 95 // Note that one audio frame can contain more than one channel sample and each 96 // sample is assumed to be a 16-bit PCM sample. Hence, one audio frame in 97 // stereo contains 2 * (16/8) = 4 bytes of data. 98 class AudioParameters { 99 public: 100 // This implementation does only support 16-bit PCM samples. 101 static const size_t kBitsPerSample = 16; 102 AudioParameters() 103 : sample_rate_(0), 104 channels_(0), 105 frames_per_buffer_(0), 106 frames_per_10ms_buffer_(0) {} 107 AudioParameters(int sample_rate, size_t channels, size_t frames_per_buffer) 108 : sample_rate_(sample_rate), 109 channels_(channels), 110 frames_per_buffer_(frames_per_buffer), 111 frames_per_10ms_buffer_(static_cast<size_t>(sample_rate / 100)) {} 112 void reset(int sample_rate, size_t channels, size_t frames_per_buffer) { 113 sample_rate_ = sample_rate; 114 channels_ = channels; 115 frames_per_buffer_ = frames_per_buffer; 116 frames_per_10ms_buffer_ = static_cast<size_t>(sample_rate / 100); 117 } 118 size_t bits_per_sample() const { return kBitsPerSample; } 119 void reset(int sample_rate, size_t channels, double buffer_duration) { 120 reset(sample_rate, channels, 121 static_cast<size_t>(sample_rate * buffer_duration + 0.5)); 122 } 123 void reset(int sample_rate, size_t channels) { 124 reset(sample_rate, channels, static_cast<size_t>(0)); 125 } 126 int sample_rate() const { return sample_rate_; } 127 size_t channels() const { return channels_; } 128 size_t frames_per_buffer() const { return frames_per_buffer_; } 129 size_t frames_per_10ms_buffer() const { return frames_per_10ms_buffer_; } 130 size_t GetBytesPerFrame() const { return channels_ * kBitsPerSample / 8; } 131 size_t GetBytesPerBuffer() const { 132 return frames_per_buffer_ * GetBytesPerFrame(); 133 } 134 // The WebRTC audio device buffer (ADB) only requires that the sample rate 135 // and number of channels are configured. Hence, to be "valid", only these 136 // two attributes must be set. 137 bool is_valid() const { return ((sample_rate_ > 0) && (channels_ > 0)); } 138 // Most platforms also require that a native buffer size is defined. 139 // An audio parameter instance is considered to be "complete" if it is both 140 // "valid" (can be used by the ADB) and also has a native frame size. 141 bool is_complete() const { return (is_valid() && (frames_per_buffer_ > 0)); } 142 size_t GetBytesPer10msBuffer() const { 143 return frames_per_10ms_buffer_ * GetBytesPerFrame(); 144 } 145 double GetBufferSizeInMilliseconds() const { 146 if (sample_rate_ == 0) 147 return 0.0; 148 return frames_per_buffer_ / (sample_rate_ / 1000.0); 149 } 150 double GetBufferSizeInSeconds() const { 151 if (sample_rate_ == 0) 152 return 0.0; 153 return static_cast<double>(frames_per_buffer_) / (sample_rate_); 154 } 155 std::string ToString() const { 156 char ss_buf[1024]; 157 SimpleStringBuilder ss(ss_buf); 158 ss << "AudioParameters: "; 159 ss << "sample_rate=" << sample_rate() << ", channels=" << channels(); 160 ss << ", frames_per_buffer=" << frames_per_buffer(); 161 ss << ", frames_per_10ms_buffer=" << frames_per_10ms_buffer(); 162 ss << ", bytes_per_frame=" << GetBytesPerFrame(); 163 ss << ", bytes_per_buffer=" << GetBytesPerBuffer(); 164 ss << ", bytes_per_10ms_buffer=" << GetBytesPer10msBuffer(); 165 ss << ", size_in_ms=" << GetBufferSizeInMilliseconds(); 166 return ss.str(); 167 } 168 169 private: 170 int sample_rate_; 171 size_t channels_; 172 size_t frames_per_buffer_; 173 size_t frames_per_10ms_buffer_; 174 }; 175 176 } // namespace webrtc 177 178 #endif // API_AUDIO_AUDIO_DEVICE_DEFINES_H_