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