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 WEBRTC_MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_ 12 #define WEBRTC_MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_ 13 14 #include <stddef.h> 15 16 #include "webrtc/typedefs.h" 17 18 namespace webrtc { 19 20 static const int kAdmMaxDeviceNameSize = 128; 21 static const int kAdmMaxFileNameSize = 512; 22 static const int kAdmMaxGuidSize = 128; 23 24 static const int kAdmMinPlayoutBufferSizeMs = 10; 25 static const int kAdmMaxPlayoutBufferSizeMs = 250; 26 27 // ---------------------------------------------------------------------------- 28 // AudioDeviceObserver 29 // ---------------------------------------------------------------------------- 30 31 class AudioDeviceObserver { 32 public: 33 enum ErrorCode { kRecordingError = 0, kPlayoutError = 1 }; 34 enum WarningCode { kRecordingWarning = 0, kPlayoutWarning = 1 }; 35 36 virtual void OnErrorIsReported(const ErrorCode error) = 0; 37 virtual void OnWarningIsReported(const WarningCode warning) = 0; 38 39 protected: 40 virtual ~AudioDeviceObserver() {} 41 }; 42 43 // ---------------------------------------------------------------------------- 44 // AudioTransport 45 // ---------------------------------------------------------------------------- 46 47 class AudioTransport { 48 public: 49 virtual int32_t RecordedDataIsAvailable(const void* audioSamples, 50 const size_t nSamples, 51 const size_t nBytesPerSample, 52 const size_t nChannels, 53 const uint32_t samplesPerSec, 54 const uint32_t totalDelayMS, 55 const int32_t clockDrift, 56 const uint32_t currentMicLevel, 57 const bool keyPressed, 58 uint32_t& newMicLevel) = 0; 59 60 virtual int32_t NeedMorePlayData(const size_t nSamples, 61 const size_t nBytesPerSample, 62 const size_t nChannels, 63 const uint32_t samplesPerSec, 64 void* audioSamples, 65 size_t& nSamplesOut, 66 int64_t* elapsed_time_ms, 67 int64_t* ntp_time_ms) = 0; 68 69 // Method to pass captured data directly and unmixed to network channels. 70 // |channel_ids| contains a list of VoE channels which are the 71 // sinks to the capture data. |audio_delay_milliseconds| is the sum of 72 // recording delay and playout delay of the hardware. |current_volume| is 73 // in the range of [0, 255], representing the current microphone analog 74 // volume. |key_pressed| is used by the typing detection. 75 // |need_audio_processing| specify if the data needs to be processed by APM. 76 // Currently WebRtc supports only one APM, and Chrome will make sure only 77 // one stream goes through APM. When |need_audio_processing| is false, the 78 // values of |audio_delay_milliseconds|, |current_volume| and |key_pressed| 79 // will be ignored. 80 // The return value is the new microphone volume, in the range of |0, 255]. 81 // When the volume does not need to be updated, it returns 0. 82 // TODO(xians): Remove this interface after Chrome and Libjingle switches 83 // to OnData(). 84 virtual int OnDataAvailable(const int voe_channels[], 85 size_t number_of_voe_channels, 86 const int16_t* audio_data, 87 int sample_rate, 88 size_t number_of_channels, 89 size_t number_of_frames, 90 int audio_delay_milliseconds, 91 int current_volume, 92 bool key_pressed, 93 bool need_audio_processing) { 94 return 0; 95 } 96 97 // Method to pass the captured audio data to the specific VoE channel. 98 // |voe_channel| is the id of the VoE channel which is the sink to the 99 // capture data. 100 // TODO(xians): Remove this interface after Libjingle switches to 101 // PushCaptureData(). 102 virtual void OnData(int voe_channel, 103 const void* audio_data, 104 int bits_per_sample, 105 int sample_rate, 106 size_t number_of_channels, 107 size_t number_of_frames) {} 108 109 // Method to push the captured audio data to the specific VoE channel. 110 // The data will not undergo audio processing. 111 // |voe_channel| is the id of the VoE channel which is the sink to the 112 // capture data. 113 // TODO(xians): Make the interface pure virtual after Libjingle 114 // has its implementation. 115 virtual void PushCaptureData(int voe_channel, 116 const void* audio_data, 117 int bits_per_sample, 118 int sample_rate, 119 size_t number_of_channels, 120 size_t number_of_frames) {} 121 122 // Method to pull mixed render audio data from all active VoE channels. 123 // The data will not be passed as reference for audio processing internally. 124 // TODO(xians): Support getting the unmixed render data from specific VoE 125 // channel. 126 virtual void PullRenderData(int bits_per_sample, 127 int sample_rate, 128 size_t number_of_channels, 129 size_t number_of_frames, 130 void* audio_data, 131 int64_t* elapsed_time_ms, 132 int64_t* ntp_time_ms) {} 133 134 protected: 135 virtual ~AudioTransport() {} 136 }; 137 138 // Helper class for storage of fundamental audio parameters such as sample rate, 139 // number of channels, native buffer size etc. 140 // Note that one audio frame can contain more than one channel sample and each 141 // sample is assumed to be a 16-bit PCM sample. Hence, one audio frame in 142 // stereo contains 2 * (16/8) = 4 bytes of data. 143 class AudioParameters { 144 public: 145 // This implementation does only support 16-bit PCM samples. 146 static const size_t kBitsPerSample = 16; 147 AudioParameters() 148 : sample_rate_(0), 149 channels_(0), 150 frames_per_buffer_(0), 151 frames_per_10ms_buffer_(0) {} 152 AudioParameters(int sample_rate, size_t channels, size_t frames_per_buffer) 153 : sample_rate_(sample_rate), 154 channels_(channels), 155 frames_per_buffer_(frames_per_buffer), 156 frames_per_10ms_buffer_(static_cast<size_t>(sample_rate / 100)) {} 157 void reset(int sample_rate, size_t channels, size_t frames_per_buffer) { 158 sample_rate_ = sample_rate; 159 channels_ = channels; 160 frames_per_buffer_ = frames_per_buffer; 161 frames_per_10ms_buffer_ = static_cast<size_t>(sample_rate / 100); 162 } 163 size_t bits_per_sample() const { return kBitsPerSample; } 164 void reset(int sample_rate, size_t channels, double ms_per_buffer) { 165 reset(sample_rate, channels, 166 static_cast<size_t>(sample_rate * ms_per_buffer + 0.5)); 167 } 168 void reset(int sample_rate, size_t channels) { 169 reset(sample_rate, channels, static_cast<size_t>(0)); 170 } 171 int sample_rate() const { return sample_rate_; } 172 size_t channels() const { return channels_; } 173 size_t frames_per_buffer() const { return frames_per_buffer_; } 174 size_t frames_per_10ms_buffer() const { return frames_per_10ms_buffer_; } 175 size_t GetBytesPerFrame() const { return channels_ * kBitsPerSample / 8; } 176 size_t GetBytesPerBuffer() const { 177 return frames_per_buffer_ * GetBytesPerFrame(); 178 } 179 // The WebRTC audio device buffer (ADB) only requires that the sample rate 180 // and number of channels are configured. Hence, to be "valid", only these 181 // two attributes must be set. 182 bool is_valid() const { return ((sample_rate_ > 0) && (channels_ > 0)); } 183 // Most platforms also require that a native buffer size is defined. 184 // An audio parameter instance is considered to be "complete" if it is both 185 // "valid" (can be used by the ADB) and also has a native frame size. 186 bool is_complete() const { return (is_valid() && (frames_per_buffer_ > 0)); } 187 size_t GetBytesPer10msBuffer() const { 188 return frames_per_10ms_buffer_ * GetBytesPerFrame(); 189 } 190 double GetBufferSizeInMilliseconds() const { 191 if (sample_rate_ == 0) 192 return 0.0; 193 return frames_per_buffer_ / (sample_rate_ / 1000.0); 194 } 195 double GetBufferSizeInSeconds() const { 196 if (sample_rate_ == 0) 197 return 0.0; 198 return static_cast<double>(frames_per_buffer_) / (sample_rate_); 199 } 200 201 private: 202 int sample_rate_; 203 size_t channels_; 204 size_t frames_per_buffer_; 205 size_t frames_per_10ms_buffer_; 206 }; 207 208 } // namespace webrtc 209 210 #endif // WEBRTC_MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_ 211