niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | |
pbos@webrtc.org | 811269d | 2013-07-11 13:24:38 +0000 | [diff] [blame] | 16 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 31 | class AudioDeviceObserver { |
| 32 | public: |
| 33 | enum ErrorCode { kRecordingError = 0, kPlayoutError = 1 }; |
| 34 | enum WarningCode { kRecordingWarning = 0, kPlayoutWarning = 1 }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 36 | virtual void OnErrorIsReported(const ErrorCode error) = 0; |
| 37 | virtual void OnWarningIsReported(const WarningCode warning) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 39 | protected: |
| 40 | virtual ~AudioDeviceObserver() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | // AudioTransport |
| 45 | // ---------------------------------------------------------------------------- |
| 46 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 47 | class AudioTransport { |
| 48 | public: |
| 49 | virtual int32_t RecordedDataIsAvailable(const void* audioSamples, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 50 | const size_t nSamples, |
| 51 | const size_t nBytesPerSample, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 52 | const size_t nChannels, |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 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; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 60 | virtual int32_t NeedMorePlayData(const size_t nSamples, |
| 61 | const size_t nBytesPerSample, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 62 | const size_t nChannels, |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 63 | const uint32_t samplesPerSec, |
| 64 | void* audioSamples, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 65 | size_t& nSamplesOut, |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 66 | int64_t* elapsed_time_ms, |
| 67 | int64_t* ntp_time_ms) = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 69 | // Method to push the captured audio data to the specific VoE channel. |
| 70 | // The data will not undergo audio processing. |
| 71 | // |voe_channel| is the id of the VoE channel which is the sink to the |
| 72 | // capture data. |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 73 | virtual void PushCaptureData(int voe_channel, |
| 74 | const void* audio_data, |
| 75 | int bits_per_sample, |
| 76 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 77 | size_t number_of_channels, |
maxmorin | 1aee0b5 | 2016-08-15 11:46:19 -0700 | [diff] [blame] | 78 | size_t number_of_frames) = 0; |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 79 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 80 | // Method to pull mixed render audio data from all active VoE channels. |
| 81 | // The data will not be passed as reference for audio processing internally. |
| 82 | // TODO(xians): Support getting the unmixed render data from specific VoE |
| 83 | // channel. |
| 84 | virtual void PullRenderData(int bits_per_sample, |
| 85 | int sample_rate, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 86 | size_t number_of_channels, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 87 | size_t number_of_frames, |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 88 | void* audio_data, |
| 89 | int64_t* elapsed_time_ms, |
maxmorin | 1aee0b5 | 2016-08-15 11:46:19 -0700 | [diff] [blame] | 90 | int64_t* ntp_time_ms) = 0; |
xians@webrtc.org | 5692531 | 2014-04-14 10:50:37 +0000 | [diff] [blame] | 91 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 92 | protected: |
| 93 | virtual ~AudioTransport() {} |
| 94 | }; |
| 95 | |
| 96 | // Helper class for storage of fundamental audio parameters such as sample rate, |
| 97 | // number of channels, native buffer size etc. |
| 98 | // Note that one audio frame can contain more than one channel sample and each |
| 99 | // sample is assumed to be a 16-bit PCM sample. Hence, one audio frame in |
| 100 | // stereo contains 2 * (16/8) = 4 bytes of data. |
| 101 | class AudioParameters { |
| 102 | public: |
| 103 | // This implementation does only support 16-bit PCM samples. |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 104 | static const size_t kBitsPerSample = 16; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 105 | AudioParameters() |
| 106 | : sample_rate_(0), |
| 107 | channels_(0), |
| 108 | frames_per_buffer_(0), |
| 109 | frames_per_10ms_buffer_(0) {} |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 110 | AudioParameters(int sample_rate, size_t channels, size_t frames_per_buffer) |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 111 | : sample_rate_(sample_rate), |
| 112 | channels_(channels), |
| 113 | frames_per_buffer_(frames_per_buffer), |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 114 | frames_per_10ms_buffer_(static_cast<size_t>(sample_rate / 100)) {} |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 115 | void reset(int sample_rate, size_t channels, size_t frames_per_buffer) { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 116 | sample_rate_ = sample_rate; |
| 117 | channels_ = channels; |
| 118 | frames_per_buffer_ = frames_per_buffer; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 119 | frames_per_10ms_buffer_ = static_cast<size_t>(sample_rate / 100); |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 120 | } |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 121 | size_t bits_per_sample() const { return kBitsPerSample; } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 122 | void reset(int sample_rate, size_t channels, double ms_per_buffer) { |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 123 | reset(sample_rate, channels, |
| 124 | static_cast<size_t>(sample_rate * ms_per_buffer + 0.5)); |
| 125 | } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 126 | void reset(int sample_rate, size_t channels) { |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 127 | reset(sample_rate, channels, static_cast<size_t>(0)); |
| 128 | } |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 129 | int sample_rate() const { return sample_rate_; } |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 130 | size_t channels() const { return channels_; } |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 131 | size_t frames_per_buffer() const { return frames_per_buffer_; } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 132 | size_t frames_per_10ms_buffer() const { return frames_per_10ms_buffer_; } |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 133 | size_t GetBytesPerFrame() const { return channels_ * kBitsPerSample / 8; } |
| 134 | size_t GetBytesPerBuffer() const { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 135 | return frames_per_buffer_ * GetBytesPerFrame(); |
| 136 | } |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 137 | // The WebRTC audio device buffer (ADB) only requires that the sample rate |
| 138 | // and number of channels are configured. Hence, to be "valid", only these |
| 139 | // two attributes must be set. |
| 140 | bool is_valid() const { return ((sample_rate_ > 0) && (channels_ > 0)); } |
| 141 | // Most platforms also require that a native buffer size is defined. |
| 142 | // An audio parameter instance is considered to be "complete" if it is both |
| 143 | // "valid" (can be used by the ADB) and also has a native frame size. |
| 144 | bool is_complete() const { return (is_valid() && (frames_per_buffer_ > 0)); } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 145 | size_t GetBytesPer10msBuffer() const { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 146 | return frames_per_10ms_buffer_ * GetBytesPerFrame(); |
| 147 | } |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 148 | double GetBufferSizeInMilliseconds() const { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 149 | if (sample_rate_ == 0) |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 150 | return 0.0; |
| 151 | return frames_per_buffer_ / (sample_rate_ / 1000.0); |
| 152 | } |
| 153 | double GetBufferSizeInSeconds() const { |
| 154 | if (sample_rate_ == 0) |
| 155 | return 0.0; |
| 156 | return static_cast<double>(frames_per_buffer_) / (sample_rate_); |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | private: |
| 160 | int sample_rate_; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 161 | size_t channels_; |
Peter Kasting | 1380e26 | 2015-08-28 17:31:03 -0700 | [diff] [blame] | 162 | size_t frames_per_buffer_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 163 | size_t frames_per_10ms_buffer_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | } // namespace webrtc |
| 167 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 168 | #endif // WEBRTC_MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_ |