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 #include "chromecast/media/cma/public/audio_config.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "media/base/audio_decoder_config.h" |
| 9 |
| 10 // Make sure the enum values of media::AudioCodec and |
| 11 // chromecast::media::AudioConfig::Codec are matched. |
| 12 #define STATIC_ASSERT_ENUM_MATCH_CODEC(name) \ |
| 13 static_assert( \ |
| 14 ::media::name == static_cast< ::media::AudioCodec>( \ |
| 15 chromecast::media::AudioConfig::name), \ |
| 16 #name " value must match in media and chromecast::media::AudioConfig.") |
| 17 |
| 18 // Make sure the enum values of media::ChannelLayout and |
| 19 // chromecast::media::AudioConfig::ChannelLayout are matched. |
| 20 #define STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(name) \ |
| 21 static_assert( \ |
| 22 ::media::name == static_cast< ::media::ChannelLayout>( \ |
| 23 chromecast::media::AudioConfig::name), \ |
| 24 #name " value must match in media and chromecast::media::AudioConfig.") |
| 25 |
| 26 namespace { |
| 27 |
| 28 // Maximum audio bytes per sample. |
| 29 static int kMaxBytesPerSample = 4; |
| 30 |
| 31 // Maximum audio sampling rate. |
| 32 static int kMaxSampleRate = 192000; |
| 33 |
| 34 } // namespace |
| 35 |
| 36 namespace chromecast { |
| 37 namespace media { |
| 38 |
| 39 STATIC_ASSERT_ENUM_MATCH_CODEC(kUnknownAudioCodec); |
| 40 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecAAC); |
| 41 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecMP3); |
| 42 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecPCM); |
| 43 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecVorbis); |
| 44 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecFLAC); |
| 45 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecAMR_NB); |
| 46 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecAMR_WB); |
| 47 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecPCM_MULAW); |
| 48 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecGSM_MS); |
| 49 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecPCM_S16BE); |
| 50 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecPCM_S24BE); |
| 51 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecOpus); |
| 52 // STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecEAC3); |
| 53 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecPCM_ALAW); |
| 54 STATIC_ASSERT_ENUM_MATCH_CODEC(kCodecALAC); |
| 55 |
| 56 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_NONE); |
| 57 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_UNSUPPORTED); |
| 58 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_MONO); |
| 59 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_STEREO); |
| 60 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_2_1); |
| 61 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_SURROUND); |
| 62 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_4_0); |
| 63 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_2_2); |
| 64 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_QUAD); |
| 65 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_5_0); |
| 66 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_5_1); |
| 67 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_5_0_BACK); |
| 68 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_5_1_BACK); |
| 69 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_7_0); |
| 70 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_7_1); |
| 71 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_7_1_WIDE); |
| 72 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_STEREO_DOWNMIX); |
| 73 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_2POINT1); |
| 74 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_3_1); |
| 75 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_4_1); |
| 76 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_6_0); |
| 77 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_6_0_FRONT); |
| 78 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_HEXAGONAL); |
| 79 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_6_1); |
| 80 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_6_1_BACK); |
| 81 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_6_1_FRONT); |
| 82 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_7_0_FRONT); |
| 83 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_7_1_WIDE_BACK); |
| 84 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_OCTAGONAL); |
| 85 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_DISCRETE); |
| 86 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_STEREO_AND_KEYBOARD_MIC); |
| 87 STATIC_ASSERT_ENUM_MATCH_CHANNEL_LAYOUT(CHANNEL_LAYOUT_4_1_QUAD_SIDE); |
| 88 |
| 89 AudioConfig::AudioConfig() |
| 90 : codec_(kUnknownAudioCodec), |
| 91 channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED), |
| 92 channel_number_(0), |
| 93 bytes_per_channel_(0), |
| 94 samples_per_second_(0), |
| 95 is_encrypted_(false) {} |
| 96 |
| 97 AudioConfig::AudioConfig(Codec codec, |
| 98 ChannelLayout channel_layout, |
| 99 int channel_number, |
| 100 int bytes_per_channel, |
| 101 int samples_per_second, |
| 102 const uint8* extra_data, |
| 103 size_t extra_data_size, |
| 104 bool is_encrypted) { |
| 105 Initialize(codec, channel_layout, channel_number, |
| 106 bytes_per_channel, samples_per_second, |
| 107 extra_data, extra_data_size, is_encrypted); |
| 108 } |
| 109 |
| 110 void AudioConfig::Initialize(Codec codec, |
| 111 ChannelLayout channel_layout, |
| 112 int channel_number, |
| 113 int bytes_per_channel, |
| 114 int samples_per_second, |
| 115 const uint8* extra_data, |
| 116 size_t extra_data_size, |
| 117 bool is_encrypted) { |
| 118 DCHECK((extra_data_size != 0) == (extra_data != NULL)); |
| 119 |
| 120 codec_ = codec; |
| 121 channel_layout_ = channel_layout; |
| 122 channel_number_ = channel_number; |
| 123 bytes_per_channel_ = bytes_per_channel; |
| 124 samples_per_second_ = samples_per_second; |
| 125 extra_data_.assign(extra_data, extra_data + extra_data_size); |
| 126 is_encrypted_ = is_encrypted; |
| 127 } |
| 128 |
| 129 AudioConfig::~AudioConfig() {} |
| 130 |
| 131 bool AudioConfig::IsValidConfig() const { |
| 132 return codec_ != kUnknownAudioCodec && |
| 133 channel_layout_ != CHANNEL_LAYOUT_UNSUPPORTED && |
| 134 bytes_per_channel_ > 0 && |
| 135 bytes_per_channel_ <= kMaxBytesPerSample && |
| 136 samples_per_second_ > 0 && |
| 137 samples_per_second_ <= kMaxSampleRate; |
| 138 } |
| 139 |
| 140 bool AudioConfig::Matches(const AudioConfig& config) const { |
| 141 return ((codec() == config.codec()) && |
| 142 (bytes_per_channel() == config.bytes_per_channel()) && |
| 143 (channel_number() == config.channel_number()) && |
| 144 (channel_layout() == config.channel_layout()) && |
| 145 (samples_per_second() == config.samples_per_second()) && |
| 146 (extra_data_size() == config.extra_data_size()) && |
| 147 (!extra_data() || !memcmp(extra_data(), config.extra_data(), |
| 148 extra_data_size())) && |
| 149 (is_encrypted() == config.is_encrypted())); |
| 150 } |
| 151 |
| 152 std::string AudioConfig::AsHumanReadableString() const { |
| 153 std::ostringstream s; |
| 154 s << "codec: " << GetHumanReadableCodecName() |
| 155 << " bytes_per_channel: " << bytes_per_channel() |
| 156 << " channel_layout: " << channel_layout() |
| 157 << " channel_number: " << channel_number() |
| 158 << " samples_per_second: " << samples_per_second() |
| 159 << " has extra data? " << (extra_data() ? "true" : "false") |
| 160 << " encrypted? " << (is_encrypted() ? "true" : "false"); |
| 161 return s.str(); |
| 162 } |
| 163 |
| 164 // These names come from src/third_party/ffmpeg/libavcodec/codec_desc.c |
| 165 std::string AudioConfig::GetHumanReadableCodecName() const { |
| 166 switch (codec()) { |
| 167 case kUnknownAudioCodec: |
| 168 return "unknown"; |
| 169 case kCodecAAC: |
| 170 return "aac"; |
| 171 case kCodecMP3: |
| 172 return "mp3"; |
| 173 case kCodecPCM: |
| 174 case kCodecPCM_S16BE: |
| 175 case kCodecPCM_S24BE: |
| 176 return "pcm"; |
| 177 case kCodecVorbis: |
| 178 return "vorbis"; |
| 179 case kCodecFLAC: |
| 180 return "flac"; |
| 181 case kCodecAMR_NB: |
| 182 return "amr_nb"; |
| 183 case kCodecAMR_WB: |
| 184 return "amr_wb"; |
| 185 case kCodecGSM_MS: |
| 186 return "gsm_ms"; |
| 187 case kCodecPCM_ALAW: |
| 188 return "pcm_alaw"; |
| 189 case kCodecPCM_MULAW: |
| 190 return "pcm_mulaw"; |
| 191 case kCodecOpus: |
| 192 return "opus"; |
| 193 case kCodecALAC: |
| 194 return "alac"; |
| 195 } |
| 196 NOTREACHED(); |
| 197 return ""; |
| 198 } |
| 199 |
| 200 } // namespace media |
| 201 } // namespace chromecast |
OLD | NEW |