blob: 510b07c049f152fa4eafe403c6a05fd57eb22222 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_
12#define MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Peter Kastingdce40cf2015-08-24 14:52:23 -070014#include <stddef.h>
15
Mirko Bonadei71207422017-09-15 13:58:09 +020016#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000017
18namespace webrtc {
19
20static const int kAdmMaxDeviceNameSize = 128;
21static const int kAdmMaxFileNameSize = 512;
22static const int kAdmMaxGuidSize = 128;
23
24static const int kAdmMinPlayoutBufferSizeMs = 10;
25static const int kAdmMaxPlayoutBufferSizeMs = 250;
26
27// ----------------------------------------------------------------------------
niklase@google.com470e71d2011-07-07 08:21:25 +000028// AudioTransport
29// ----------------------------------------------------------------------------
30
henrikaba35d052015-07-14 17:04:08 +020031class AudioTransport {
32 public:
33 virtual int32_t RecordedDataIsAvailable(const void* audioSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -070034 const size_t nSamples,
35 const size_t nBytesPerSample,
Peter Kasting69558702016-01-12 16:26:35 -080036 const size_t nChannels,
henrikaba35d052015-07-14 17:04:08 +020037 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.com470e71d2011-07-07 08:21:25 +000043
Peter Kastingdce40cf2015-08-24 14:52:23 -070044 virtual int32_t NeedMorePlayData(const size_t nSamples,
45 const size_t nBytesPerSample,
Peter Kasting69558702016-01-12 16:26:35 -080046 const size_t nChannels,
henrikaba35d052015-07-14 17:04:08 +020047 const uint32_t samplesPerSec,
48 void* audioSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -070049 size_t& nSamplesOut,
henrikaba35d052015-07-14 17:04:08 +020050 int64_t* elapsed_time_ms,
51 int64_t* ntp_time_ms) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000052
henrikaba35d052015-07-14 17:04:08 +020053 // 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.
henrikaba35d052015-07-14 17:04:08 +020057 virtual void PushCaptureData(int voe_channel,
58 const void* audio_data,
59 int bits_per_sample,
60 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -080061 size_t number_of_channels,
maxmorin1aee0b52016-08-15 11:46:19 -070062 size_t number_of_frames) = 0;
xians@webrtc.org56925312014-04-14 10:50:37 +000063
henrikaba35d052015-07-14 17:04:08 +020064 // 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 Kasting69558702016-01-12 16:26:35 -080070 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070071 size_t number_of_frames,
henrikaba35d052015-07-14 17:04:08 +020072 void* audio_data,
73 int64_t* elapsed_time_ms,
maxmorin1aee0b52016-08-15 11:46:19 -070074 int64_t* ntp_time_ms) = 0;
xians@webrtc.org56925312014-04-14 10:50:37 +000075
henrikaba35d052015-07-14 17:04:08 +020076 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.
85class AudioParameters {
86 public:
87 // This implementation does only support 16-bit PCM samples.
Peter Kasting1380e262015-08-28 17:31:03 -070088 static const size_t kBitsPerSample = 16;
henrikaba35d052015-07-14 17:04:08 +020089 AudioParameters()
90 : sample_rate_(0),
91 channels_(0),
92 frames_per_buffer_(0),
93 frames_per_10ms_buffer_(0) {}
Peter Kasting69558702016-01-12 16:26:35 -080094 AudioParameters(int sample_rate, size_t channels, size_t frames_per_buffer)
henrikaba35d052015-07-14 17:04:08 +020095 : sample_rate_(sample_rate),
96 channels_(channels),
97 frames_per_buffer_(frames_per_buffer),
Peter Kastingdce40cf2015-08-24 14:52:23 -070098 frames_per_10ms_buffer_(static_cast<size_t>(sample_rate / 100)) {}
Peter Kasting69558702016-01-12 16:26:35 -080099 void reset(int sample_rate, size_t channels, size_t frames_per_buffer) {
henrikaba35d052015-07-14 17:04:08 +0200100 sample_rate_ = sample_rate;
101 channels_ = channels;
102 frames_per_buffer_ = frames_per_buffer;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700103 frames_per_10ms_buffer_ = static_cast<size_t>(sample_rate / 100);
henrikaba35d052015-07-14 17:04:08 +0200104 }
Peter Kasting1380e262015-08-28 17:31:03 -0700105 size_t bits_per_sample() const { return kBitsPerSample; }
Peter Kasting69558702016-01-12 16:26:35 -0800106 void reset(int sample_rate, size_t channels, double ms_per_buffer) {
henrika86d907c2015-09-07 16:09:50 +0200107 reset(sample_rate, channels,
108 static_cast<size_t>(sample_rate * ms_per_buffer + 0.5));
109 }
Peter Kasting69558702016-01-12 16:26:35 -0800110 void reset(int sample_rate, size_t channels) {
henrika86d907c2015-09-07 16:09:50 +0200111 reset(sample_rate, channels, static_cast<size_t>(0));
112 }
henrikaba35d052015-07-14 17:04:08 +0200113 int sample_rate() const { return sample_rate_; }
Peter Kasting69558702016-01-12 16:26:35 -0800114 size_t channels() const { return channels_; }
Peter Kasting1380e262015-08-28 17:31:03 -0700115 size_t frames_per_buffer() const { return frames_per_buffer_; }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700116 size_t frames_per_10ms_buffer() const { return frames_per_10ms_buffer_; }
Peter Kasting1380e262015-08-28 17:31:03 -0700117 size_t GetBytesPerFrame() const { return channels_ * kBitsPerSample / 8; }
118 size_t GetBytesPerBuffer() const {
henrikaba35d052015-07-14 17:04:08 +0200119 return frames_per_buffer_ * GetBytesPerFrame();
120 }
henrika86d907c2015-09-07 16:09:50 +0200121 // 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 Kastingdce40cf2015-08-24 14:52:23 -0700129 size_t GetBytesPer10msBuffer() const {
henrikaba35d052015-07-14 17:04:08 +0200130 return frames_per_10ms_buffer_ * GetBytesPerFrame();
131 }
henrika86d907c2015-09-07 16:09:50 +0200132 double GetBufferSizeInMilliseconds() const {
henrikaba35d052015-07-14 17:04:08 +0200133 if (sample_rate_ == 0)
henrika86d907c2015-09-07 16:09:50 +0200134 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_);
henrikaba35d052015-07-14 17:04:08 +0200141 }
142
143 private:
144 int sample_rate_;
Peter Kasting69558702016-01-12 16:26:35 -0800145 size_t channels_;
Peter Kasting1380e262015-08-28 17:31:03 -0700146 size_t frames_per_buffer_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700147 size_t frames_per_10ms_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000148};
149
150} // namespace webrtc
151
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200152#endif // MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_