| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MEDIA_AUDIO_AUDIO_PARAMETERS_H_ | |
| 6 #define MEDIA_AUDIO_AUDIO_PARAMETERS_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "build/build_config.h" | |
| 14 #include "media/audio/point.h" | |
| 15 #include "media/base/audio_bus.h" | |
| 16 #include "media/base/channel_layout.h" | |
| 17 #include "media/base/media_export.h" | |
| 18 | |
| 19 namespace media { | |
| 20 | |
| 21 // Use a struct-in-struct approach to ensure that we can calculate the required | |
| 22 // size as sizeof(Audio{Input,Output}BufferParameters) + #(bytes in audio | |
| 23 // buffer) without using packing. Also align Audio{Input,Output}BufferParameters | |
| 24 // instead of in Audio{Input,Output}Buffer to be able to calculate size like so. | |
| 25 // Use a macro for the alignment value that's the same as | |
| 26 // AudioBus::kChannelAlignment, since MSVC doesn't accept the latter to be used. | |
| 27 #if defined(OS_WIN) | |
| 28 #pragma warning(push) | |
| 29 #pragma warning(disable: 4324) // Disable warning for added padding. | |
| 30 #endif | |
| 31 #define PARAMETERS_ALIGNMENT 16 | |
| 32 static_assert(AudioBus::kChannelAlignment == PARAMETERS_ALIGNMENT, | |
| 33 "Audio buffer parameters struct alignment not same as AudioBus"); | |
| 34 struct MEDIA_EXPORT ALIGNAS(PARAMETERS_ALIGNMENT) AudioInputBufferParameters { | |
| 35 double volume; | |
| 36 uint32_t size; | |
| 37 uint32_t hardware_delay_bytes; | |
| 38 uint32_t id; | |
| 39 bool key_pressed; | |
| 40 }; | |
| 41 struct MEDIA_EXPORT ALIGNAS(PARAMETERS_ALIGNMENT) AudioOutputBufferParameters { | |
| 42 uint32_t frames_skipped; | |
| 43 }; | |
| 44 #undef PARAMETERS_ALIGNMENT | |
| 45 #if defined(OS_WIN) | |
| 46 #pragma warning(pop) | |
| 47 #endif | |
| 48 | |
| 49 static_assert(sizeof(AudioInputBufferParameters) % | |
| 50 AudioBus::kChannelAlignment == | |
| 51 0, | |
| 52 "AudioInputBufferParameters not aligned"); | |
| 53 static_assert(sizeof(AudioOutputBufferParameters) % | |
| 54 AudioBus::kChannelAlignment == | |
| 55 0, | |
| 56 "AudioOutputBufferParameters not aligned"); | |
| 57 | |
| 58 struct MEDIA_EXPORT AudioInputBuffer { | |
| 59 AudioInputBufferParameters params; | |
| 60 int8_t audio[1]; | |
| 61 }; | |
| 62 struct MEDIA_EXPORT AudioOutputBuffer { | |
| 63 AudioOutputBufferParameters params; | |
| 64 int8_t audio[1]; | |
| 65 }; | |
| 66 | |
| 67 class MEDIA_EXPORT AudioParameters { | |
| 68 public: | |
| 69 // TODO(miu): Rename this enum to something that correctly reflects its | |
| 70 // semantics, such as "TransportScheme." | |
| 71 enum Format { | |
| 72 AUDIO_PCM_LINEAR = 0, // PCM is 'raw' amplitude samples. | |
| 73 AUDIO_PCM_LOW_LATENCY, // Linear PCM, low latency requested. | |
| 74 AUDIO_FAKE, // Creates a fake AudioOutputStream object. | |
| 75 AUDIO_FORMAT_LAST = AUDIO_FAKE, // Only used for validation of format. | |
| 76 }; | |
| 77 | |
| 78 enum { | |
| 79 // Telephone quality sample rate, mostly for speech-only audio. | |
| 80 kTelephoneSampleRate = 8000, | |
| 81 // CD sampling rate is 44.1 KHz or conveniently 2x2x3x3x5x5x7x7. | |
| 82 kAudioCDSampleRate = 44100, | |
| 83 }; | |
| 84 | |
| 85 // Bitmasks to determine whether certain platform (typically hardware) audio | |
| 86 // effects should be enabled. | |
| 87 enum PlatformEffectsMask { | |
| 88 NO_EFFECTS = 0x0, | |
| 89 ECHO_CANCELLER = 0x1, | |
| 90 DUCKING = 0x2, // Enables ducking if the OS supports it. | |
| 91 KEYBOARD_MIC = 0x4, | |
| 92 HOTWORD = 0x8, | |
| 93 }; | |
| 94 | |
| 95 AudioParameters(); | |
| 96 AudioParameters(Format format, | |
| 97 ChannelLayout channel_layout, | |
| 98 int sample_rate, | |
| 99 int bits_per_sample, | |
| 100 int frames_per_buffer); | |
| 101 | |
| 102 ~AudioParameters(); | |
| 103 | |
| 104 // Re-initializes all members. | |
| 105 void Reset(Format format, | |
| 106 ChannelLayout channel_layout, | |
| 107 int sample_rate, | |
| 108 int bits_per_sample, | |
| 109 int frames_per_buffer); | |
| 110 | |
| 111 // Checks that all values are in the expected range. All limits are specified | |
| 112 // in media::Limits. | |
| 113 bool IsValid() const; | |
| 114 | |
| 115 // Returns a human-readable string describing |*this|. For debugging & test | |
| 116 // output only. | |
| 117 std::string AsHumanReadableString() const; | |
| 118 | |
| 119 // Returns size of audio buffer in bytes. | |
| 120 int GetBytesPerBuffer() const; | |
| 121 | |
| 122 // Returns the number of bytes representing one second of audio. | |
| 123 int GetBytesPerSecond() const; | |
| 124 | |
| 125 // Returns the number of bytes representing a frame of audio. | |
| 126 int GetBytesPerFrame() const; | |
| 127 | |
| 128 // Returns the number of microseconds per frame of audio. Intentionally | |
| 129 // reported as a double to surface of partial microseconds per frame, which | |
| 130 // is common for many sample rates. Failing to account for these nanoseconds | |
| 131 // can lead to audio/video sync drift. | |
| 132 double GetMicrosecondsPerFrame() const; | |
| 133 | |
| 134 // Returns the duration of this buffer as calculated from frames_per_buffer() | |
| 135 // and sample_rate(). | |
| 136 base::TimeDelta GetBufferDuration() const; | |
| 137 | |
| 138 // Comparison with other AudioParams. | |
| 139 bool Equals(const AudioParameters& other) const; | |
| 140 | |
| 141 void set_format(Format format) { format_ = format; } | |
| 142 Format format() const { return format_; } | |
| 143 | |
| 144 // A setter for channel_layout_ is intentionally excluded. | |
| 145 ChannelLayout channel_layout() const { return channel_layout_; } | |
| 146 | |
| 147 // The number of channels is usually computed from channel_layout_. Setting | |
| 148 // this explictly is only required with CHANNEL_LAYOUT_DISCRETE. | |
| 149 void set_channels_for_discrete(int channels) { | |
| 150 DCHECK(channel_layout_ == CHANNEL_LAYOUT_DISCRETE || | |
| 151 channels == ChannelLayoutToChannelCount(channel_layout_)); | |
| 152 channels_ = channels; | |
| 153 } | |
| 154 int channels() const { return channels_; } | |
| 155 | |
| 156 void set_sample_rate(int sample_rate) { sample_rate_ = sample_rate; } | |
| 157 int sample_rate() const { return sample_rate_; } | |
| 158 | |
| 159 void set_bits_per_sample(int bits_per_sample) { | |
| 160 bits_per_sample_ = bits_per_sample; | |
| 161 } | |
| 162 int bits_per_sample() const { return bits_per_sample_; } | |
| 163 | |
| 164 void set_frames_per_buffer(int frames_per_buffer) { | |
| 165 frames_per_buffer_ = frames_per_buffer; | |
| 166 } | |
| 167 int frames_per_buffer() const { return frames_per_buffer_; } | |
| 168 | |
| 169 void set_effects(int effects) { effects_ = effects; } | |
| 170 int effects() const { return effects_; } | |
| 171 | |
| 172 void set_mic_positions(const std::vector<Point>& mic_positions) { | |
| 173 mic_positions_ = mic_positions; | |
| 174 } | |
| 175 const std::vector<Point>& mic_positions() const { return mic_positions_; } | |
| 176 | |
| 177 AudioParameters(const AudioParameters&); | |
| 178 AudioParameters& operator=(const AudioParameters&); | |
| 179 | |
| 180 // Creates reasonable dummy parameters in case no device is available. | |
| 181 static AudioParameters UnavailableDeviceParams(); | |
| 182 | |
| 183 private: | |
| 184 Format format_; // Format of the stream. | |
| 185 ChannelLayout channel_layout_; // Order of surround sound channels. | |
| 186 int channels_; // Number of channels. Value set based on | |
| 187 // |channel_layout|. | |
| 188 int sample_rate_; // Sampling frequency/rate. | |
| 189 int bits_per_sample_; // Number of bits per sample. | |
| 190 int frames_per_buffer_; // Number of frames in a buffer. | |
| 191 int effects_; // Bitmask using PlatformEffectsMask. | |
| 192 | |
| 193 // Microphone positions using Cartesian coordinates: | |
| 194 // x: the horizontal dimension, with positive to the right from the camera's | |
| 195 // perspective. | |
| 196 // y: the depth dimension, with positive forward from the camera's | |
| 197 // perspective. | |
| 198 // z: the vertical dimension, with positive upwards. | |
| 199 // | |
| 200 // Usually, the center of the microphone array will be treated as the origin | |
| 201 // (often the position of the camera). | |
| 202 // | |
| 203 // An empty vector indicates unknown positions. | |
| 204 std::vector<Point> mic_positions_; | |
| 205 }; | |
| 206 | |
| 207 // Comparison is useful when AudioParameters is used with std structures. | |
| 208 inline bool operator<(const AudioParameters& a, const AudioParameters& b) { | |
| 209 if (a.format() != b.format()) | |
| 210 return a.format() < b.format(); | |
| 211 if (a.channels() != b.channels()) | |
| 212 return a.channels() < b.channels(); | |
| 213 if (a.sample_rate() != b.sample_rate()) | |
| 214 return a.sample_rate() < b.sample_rate(); | |
| 215 if (a.bits_per_sample() != b.bits_per_sample()) | |
| 216 return a.bits_per_sample() < b.bits_per_sample(); | |
| 217 return a.frames_per_buffer() < b.frames_per_buffer(); | |
| 218 } | |
| 219 | |
| 220 } // namespace media | |
| 221 | |
| 222 #endif // MEDIA_AUDIO_AUDIO_PARAMETERS_H_ | |
| OLD | NEW |