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