blob: ccc263c57b0a6e7ad6ae44a45592cd4cfc4f9994 [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
henrika86d907c2015-09-07 16:09:50 +020011#ifndef WEBRTC_MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_
12#define WEBRTC_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
pbos@webrtc.org811269d2013-07-11 13:24:38 +000016#include "webrtc/typedefs.h"
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// ----------------------------------------------------------------------------
28// AudioDeviceObserver
29// ----------------------------------------------------------------------------
30
henrikaba35d052015-07-14 17:04:08 +020031class AudioDeviceObserver {
32 public:
33 enum ErrorCode { kRecordingError = 0, kPlayoutError = 1 };
34 enum WarningCode { kRecordingWarning = 0, kPlayoutWarning = 1 };
niklase@google.com470e71d2011-07-07 08:21:25 +000035
henrikaba35d052015-07-14 17:04:08 +020036 virtual void OnErrorIsReported(const ErrorCode error) = 0;
37 virtual void OnWarningIsReported(const WarningCode warning) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000038
henrikaba35d052015-07-14 17:04:08 +020039 protected:
40 virtual ~AudioDeviceObserver() {}
niklase@google.com470e71d2011-07-07 08:21:25 +000041};
42
43// ----------------------------------------------------------------------------
44// AudioTransport
45// ----------------------------------------------------------------------------
46
henrikaba35d052015-07-14 17:04:08 +020047class AudioTransport {
48 public:
49 virtual int32_t RecordedDataIsAvailable(const void* audioSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -070050 const size_t nSamples,
51 const size_t nBytesPerSample,
Peter Kasting69558702016-01-12 16:26:35 -080052 const size_t nChannels,
henrikaba35d052015-07-14 17:04:08 +020053 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.com470e71d2011-07-07 08:21:25 +000059
Peter Kastingdce40cf2015-08-24 14:52:23 -070060 virtual int32_t NeedMorePlayData(const size_t nSamples,
61 const size_t nBytesPerSample,
Peter Kasting69558702016-01-12 16:26:35 -080062 const size_t nChannels,
henrikaba35d052015-07-14 17:04:08 +020063 const uint32_t samplesPerSec,
64 void* audioSamples,
Peter Kastingdce40cf2015-08-24 14:52:23 -070065 size_t& nSamplesOut,
henrikaba35d052015-07-14 17:04:08 +020066 int64_t* elapsed_time_ms,
67 int64_t* ntp_time_ms) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000068
henrikaba35d052015-07-14 17:04:08 +020069 // 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.
henrikaba35d052015-07-14 17:04:08 +020073 virtual void PushCaptureData(int voe_channel,
74 const void* audio_data,
75 int bits_per_sample,
76 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -080077 size_t number_of_channels,
maxmorin1aee0b52016-08-15 11:46:19 -070078 size_t number_of_frames) = 0;
xians@webrtc.org56925312014-04-14 10:50:37 +000079
henrikaba35d052015-07-14 17:04:08 +020080 // 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 Kasting69558702016-01-12 16:26:35 -080086 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -070087 size_t number_of_frames,
henrikaba35d052015-07-14 17:04:08 +020088 void* audio_data,
89 int64_t* elapsed_time_ms,
maxmorin1aee0b52016-08-15 11:46:19 -070090 int64_t* ntp_time_ms) = 0;
xians@webrtc.org56925312014-04-14 10:50:37 +000091
henrikaba35d052015-07-14 17:04:08 +020092 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.
101class AudioParameters {
102 public:
103 // This implementation does only support 16-bit PCM samples.
Peter Kasting1380e262015-08-28 17:31:03 -0700104 static const size_t kBitsPerSample = 16;
henrikaba35d052015-07-14 17:04:08 +0200105 AudioParameters()
106 : sample_rate_(0),
107 channels_(0),
108 frames_per_buffer_(0),
109 frames_per_10ms_buffer_(0) {}
Peter Kasting69558702016-01-12 16:26:35 -0800110 AudioParameters(int sample_rate, size_t channels, size_t frames_per_buffer)
henrikaba35d052015-07-14 17:04:08 +0200111 : sample_rate_(sample_rate),
112 channels_(channels),
113 frames_per_buffer_(frames_per_buffer),
Peter Kastingdce40cf2015-08-24 14:52:23 -0700114 frames_per_10ms_buffer_(static_cast<size_t>(sample_rate / 100)) {}
Peter Kasting69558702016-01-12 16:26:35 -0800115 void reset(int sample_rate, size_t channels, size_t frames_per_buffer) {
henrikaba35d052015-07-14 17:04:08 +0200116 sample_rate_ = sample_rate;
117 channels_ = channels;
118 frames_per_buffer_ = frames_per_buffer;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700119 frames_per_10ms_buffer_ = static_cast<size_t>(sample_rate / 100);
henrikaba35d052015-07-14 17:04:08 +0200120 }
Peter Kasting1380e262015-08-28 17:31:03 -0700121 size_t bits_per_sample() const { return kBitsPerSample; }
Peter Kasting69558702016-01-12 16:26:35 -0800122 void reset(int sample_rate, size_t channels, double ms_per_buffer) {
henrika86d907c2015-09-07 16:09:50 +0200123 reset(sample_rate, channels,
124 static_cast<size_t>(sample_rate * ms_per_buffer + 0.5));
125 }
Peter Kasting69558702016-01-12 16:26:35 -0800126 void reset(int sample_rate, size_t channels) {
henrika86d907c2015-09-07 16:09:50 +0200127 reset(sample_rate, channels, static_cast<size_t>(0));
128 }
henrikaba35d052015-07-14 17:04:08 +0200129 int sample_rate() const { return sample_rate_; }
Peter Kasting69558702016-01-12 16:26:35 -0800130 size_t channels() const { return channels_; }
Peter Kasting1380e262015-08-28 17:31:03 -0700131 size_t frames_per_buffer() const { return frames_per_buffer_; }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700132 size_t frames_per_10ms_buffer() const { return frames_per_10ms_buffer_; }
Peter Kasting1380e262015-08-28 17:31:03 -0700133 size_t GetBytesPerFrame() const { return channels_ * kBitsPerSample / 8; }
134 size_t GetBytesPerBuffer() const {
henrikaba35d052015-07-14 17:04:08 +0200135 return frames_per_buffer_ * GetBytesPerFrame();
136 }
henrika86d907c2015-09-07 16:09:50 +0200137 // 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 Kastingdce40cf2015-08-24 14:52:23 -0700145 size_t GetBytesPer10msBuffer() const {
henrikaba35d052015-07-14 17:04:08 +0200146 return frames_per_10ms_buffer_ * GetBytesPerFrame();
147 }
henrika86d907c2015-09-07 16:09:50 +0200148 double GetBufferSizeInMilliseconds() const {
henrikaba35d052015-07-14 17:04:08 +0200149 if (sample_rate_ == 0)
henrika86d907c2015-09-07 16:09:50 +0200150 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_);
henrikaba35d052015-07-14 17:04:08 +0200157 }
158
159 private:
160 int sample_rate_;
Peter Kasting69558702016-01-12 16:26:35 -0800161 size_t channels_;
Peter Kasting1380e262015-08-28 17:31:03 -0700162 size_t frames_per_buffer_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700163 size_t frames_per_10ms_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000164};
165
166} // namespace webrtc
167
henrika86d907c2015-09-07 16:09:50 +0200168#endif // WEBRTC_MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_