• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_
12 #define MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_
13 
14 #include <stddef.h>
15 
16 #include <string>
17 
18 #include "rtc_base/checks.h"
19 #include "rtc_base/deprecation.h"
20 #include "rtc_base/strings/string_builder.h"
21 
22 namespace webrtc {
23 
24 static const int kAdmMaxDeviceNameSize = 128;
25 static const int kAdmMaxFileNameSize = 512;
26 static const int kAdmMaxGuidSize = 128;
27 
28 static const int kAdmMinPlayoutBufferSizeMs = 10;
29 static const int kAdmMaxPlayoutBufferSizeMs = 250;
30 
31 // ----------------------------------------------------------------------------
32 //  AudioTransport
33 // ----------------------------------------------------------------------------
34 
35 class AudioTransport {
36  public:
37   virtual int32_t RecordedDataIsAvailable(const void* audioSamples,
38                                           const size_t nSamples,
39                                           const size_t nBytesPerSample,
40                                           const size_t nChannels,
41                                           const uint32_t samplesPerSec,
42                                           const uint32_t totalDelayMS,
43                                           const int32_t clockDrift,
44                                           const uint32_t currentMicLevel,
45                                           const bool keyPressed,
46                                           uint32_t& newMicLevel) = 0;  // NOLINT
47 
48   // Implementation has to setup safe values for all specified out parameters.
49   virtual int32_t NeedMorePlayData(const size_t nSamples,
50                                    const size_t nBytesPerSample,
51                                    const size_t nChannels,
52                                    const uint32_t samplesPerSec,
53                                    void* audioSamples,
54                                    size_t& nSamplesOut,  // NOLINT
55                                    int64_t* elapsed_time_ms,
56                                    int64_t* ntp_time_ms) = 0;  // NOLINT
57 
58   // Method to pull mixed render audio data from all active VoE channels.
59   // The data will not be passed as reference for audio processing internally.
60   virtual void PullRenderData(int bits_per_sample,
61                               int sample_rate,
62                               size_t number_of_channels,
63                               size_t number_of_frames,
64                               void* audio_data,
65                               int64_t* elapsed_time_ms,
66                               int64_t* ntp_time_ms) = 0;
67 
68  protected:
~AudioTransport()69   virtual ~AudioTransport() {}
70 };
71 
72 // Helper class for storage of fundamental audio parameters such as sample rate,
73 // number of channels, native buffer size etc.
74 // Note that one audio frame can contain more than one channel sample and each
75 // sample is assumed to be a 16-bit PCM sample. Hence, one audio frame in
76 // stereo contains 2 * (16/8) = 4 bytes of data.
77 class AudioParameters {
78  public:
79   // This implementation does only support 16-bit PCM samples.
80   static const size_t kBitsPerSample = 16;
AudioParameters()81   AudioParameters()
82       : sample_rate_(0),
83         channels_(0),
84         frames_per_buffer_(0),
85         frames_per_10ms_buffer_(0) {}
AudioParameters(int sample_rate,size_t channels,size_t frames_per_buffer)86   AudioParameters(int sample_rate, size_t channels, size_t frames_per_buffer)
87       : sample_rate_(sample_rate),
88         channels_(channels),
89         frames_per_buffer_(frames_per_buffer),
90         frames_per_10ms_buffer_(static_cast<size_t>(sample_rate / 100)) {}
reset(int sample_rate,size_t channels,size_t frames_per_buffer)91   void reset(int sample_rate, size_t channels, size_t frames_per_buffer) {
92     sample_rate_ = sample_rate;
93     channels_ = channels;
94     frames_per_buffer_ = frames_per_buffer;
95     frames_per_10ms_buffer_ = static_cast<size_t>(sample_rate / 100);
96   }
bits_per_sample()97   size_t bits_per_sample() const { return kBitsPerSample; }
reset(int sample_rate,size_t channels,double buffer_duration)98   void reset(int sample_rate, size_t channels, double buffer_duration) {
99     reset(sample_rate, channels,
100           static_cast<size_t>(sample_rate * buffer_duration + 0.5));
101   }
reset(int sample_rate,size_t channels)102   void reset(int sample_rate, size_t channels) {
103     reset(sample_rate, channels, static_cast<size_t>(0));
104   }
sample_rate()105   int sample_rate() const { return sample_rate_; }
channels()106   size_t channels() const { return channels_; }
frames_per_buffer()107   size_t frames_per_buffer() const { return frames_per_buffer_; }
frames_per_10ms_buffer()108   size_t frames_per_10ms_buffer() const { return frames_per_10ms_buffer_; }
GetBytesPerFrame()109   size_t GetBytesPerFrame() const { return channels_ * kBitsPerSample / 8; }
GetBytesPerBuffer()110   size_t GetBytesPerBuffer() const {
111     return frames_per_buffer_ * GetBytesPerFrame();
112   }
113   // The WebRTC audio device buffer (ADB) only requires that the sample rate
114   // and number of channels are configured. Hence, to be "valid", only these
115   // two attributes must be set.
is_valid()116   bool is_valid() const { return ((sample_rate_ > 0) && (channels_ > 0)); }
117   // Most platforms also require that a native buffer size is defined.
118   // An audio parameter instance is considered to be "complete" if it is both
119   // "valid" (can be used by the ADB) and also has a native frame size.
is_complete()120   bool is_complete() const { return (is_valid() && (frames_per_buffer_ > 0)); }
GetBytesPer10msBuffer()121   size_t GetBytesPer10msBuffer() const {
122     return frames_per_10ms_buffer_ * GetBytesPerFrame();
123   }
GetBufferSizeInMilliseconds()124   double GetBufferSizeInMilliseconds() const {
125     if (sample_rate_ == 0)
126       return 0.0;
127     return frames_per_buffer_ / (sample_rate_ / 1000.0);
128   }
GetBufferSizeInSeconds()129   double GetBufferSizeInSeconds() const {
130     if (sample_rate_ == 0)
131       return 0.0;
132     return static_cast<double>(frames_per_buffer_) / (sample_rate_);
133   }
ToString()134   std::string ToString() const {
135     char ss_buf[1024];
136     rtc::SimpleStringBuilder ss(ss_buf);
137     ss << "AudioParameters: ";
138     ss << "sample_rate=" << sample_rate() << ", channels=" << channels();
139     ss << ", frames_per_buffer=" << frames_per_buffer();
140     ss << ", frames_per_10ms_buffer=" << frames_per_10ms_buffer();
141     ss << ", bytes_per_frame=" << GetBytesPerFrame();
142     ss << ", bytes_per_buffer=" << GetBytesPerBuffer();
143     ss << ", bytes_per_10ms_buffer=" << GetBytesPer10msBuffer();
144     ss << ", size_in_ms=" << GetBufferSizeInMilliseconds();
145     return ss.str();
146   }
147 
148  private:
149   int sample_rate_;
150   size_t channels_;
151   size_t frames_per_buffer_;
152   size_t frames_per_10ms_buffer_;
153 };
154 
155 }  // namespace webrtc
156 
157 #endif  // MODULES_AUDIO_DEVICE_INCLUDE_AUDIO_DEVICE_DEFINES_H_
158