Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / audio_decoder_config.cc
blob4526859327399942fcf2038c0c753a3a84f29e38
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.
5 #include "media/base/audio_decoder_config.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "base/time/time.h"
10 #include "media/audio/sample_rates.h"
11 #include "media/base/limits.h"
12 #include "media/base/sample_format.h"
14 namespace media {
16 AudioDecoderConfig::AudioDecoderConfig()
17 : codec_(kUnknownAudioCodec),
18 sample_format_(kUnknownSampleFormat),
19 bytes_per_channel_(0),
20 channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED),
21 samples_per_second_(0),
22 bytes_per_frame_(0),
23 is_encrypted_(false),
24 codec_delay_(0) {
27 AudioDecoderConfig::AudioDecoderConfig(AudioCodec codec,
28 SampleFormat sample_format,
29 ChannelLayout channel_layout,
30 int samples_per_second,
31 const uint8* extra_data,
32 size_t extra_data_size,
33 bool is_encrypted) {
34 Initialize(codec, sample_format, channel_layout, samples_per_second,
35 extra_data, extra_data_size, is_encrypted, true,
36 base::TimeDelta(), 0);
39 void AudioDecoderConfig::Initialize(AudioCodec codec,
40 SampleFormat sample_format,
41 ChannelLayout channel_layout,
42 int samples_per_second,
43 const uint8* extra_data,
44 size_t extra_data_size,
45 bool is_encrypted,
46 bool record_stats,
47 base::TimeDelta seek_preroll,
48 int codec_delay) {
49 CHECK((extra_data_size != 0) == (extra_data != NULL));
51 if (record_stats) {
52 UMA_HISTOGRAM_ENUMERATION("Media.AudioCodec", codec, kAudioCodecMax + 1);
53 UMA_HISTOGRAM_ENUMERATION("Media.AudioSampleFormat", sample_format,
54 kSampleFormatMax + 1);
55 UMA_HISTOGRAM_ENUMERATION("Media.AudioChannelLayout", channel_layout,
56 CHANNEL_LAYOUT_MAX + 1);
57 AudioSampleRate asr;
58 if (ToAudioSampleRate(samples_per_second, &asr)) {
59 UMA_HISTOGRAM_ENUMERATION("Media.AudioSamplesPerSecond", asr,
60 kAudioSampleRateMax + 1);
61 } else {
62 UMA_HISTOGRAM_COUNTS(
63 "Media.AudioSamplesPerSecondUnexpected", samples_per_second);
67 codec_ = codec;
68 channel_layout_ = channel_layout;
69 samples_per_second_ = samples_per_second;
70 sample_format_ = sample_format;
71 bytes_per_channel_ = SampleFormatToBytesPerChannel(sample_format);
72 extra_data_.assign(extra_data, extra_data + extra_data_size);
73 is_encrypted_ = is_encrypted;
74 seek_preroll_ = seek_preroll;
75 codec_delay_ = codec_delay;
77 int channels = ChannelLayoutToChannelCount(channel_layout_);
78 bytes_per_frame_ = channels * bytes_per_channel_;
81 AudioDecoderConfig::~AudioDecoderConfig() {}
83 bool AudioDecoderConfig::IsValidConfig() const {
84 return codec_ != kUnknownAudioCodec &&
85 channel_layout_ != CHANNEL_LAYOUT_UNSUPPORTED &&
86 bytes_per_channel_ > 0 &&
87 bytes_per_channel_ <= limits::kMaxBytesPerSample &&
88 samples_per_second_ > 0 &&
89 samples_per_second_ <= limits::kMaxSampleRate &&
90 sample_format_ != kUnknownSampleFormat &&
91 seek_preroll_ >= base::TimeDelta() &&
92 codec_delay_ >= 0;
95 bool AudioDecoderConfig::Matches(const AudioDecoderConfig& config) const {
96 return ((codec() == config.codec()) &&
97 (bytes_per_channel() == config.bytes_per_channel()) &&
98 (channel_layout() == config.channel_layout()) &&
99 (samples_per_second() == config.samples_per_second()) &&
100 (extra_data_size() == config.extra_data_size()) &&
101 (!extra_data() || !memcmp(extra_data(), config.extra_data(),
102 extra_data_size())) &&
103 (is_encrypted() == config.is_encrypted()) &&
104 (sample_format() == config.sample_format()) &&
105 (seek_preroll() == config.seek_preroll()) &&
106 (codec_delay() == config.codec_delay()));
109 std::string AudioDecoderConfig::AsHumanReadableString() const {
110 std::ostringstream s;
111 s << "codec: " << GetHumanReadableCodecName()
112 << " bytes_per_channel: " << bytes_per_channel()
113 << " channel_layout: " << channel_layout()
114 << " samples_per_second: " << samples_per_second()
115 << " sample_format: " << sample_format()
116 << " bytes_per_frame: " << bytes_per_frame()
117 << " seek_preroll: " << seek_preroll().InMilliseconds() << "ms"
118 << " codec_delay: " << codec_delay()
119 << " has extra data? " << (extra_data() ? "true" : "false")
120 << " encrypted? " << (is_encrypted() ? "true" : "false");
121 return s.str();
124 // These names come from src/third_party/ffmpeg/libavcodec/codec_desc.c
125 std::string AudioDecoderConfig::GetHumanReadableCodecName() const {
126 switch (codec()) {
127 case kUnknownAudioCodec:
128 return "unknown";
129 case kCodecAAC:
130 return "aac";
131 case kCodecMP3:
132 return "mp3";
133 case kCodecPCM:
134 case kCodecPCM_S16BE:
135 case kCodecPCM_S24BE:
136 return "pcm";
137 case kCodecVorbis:
138 return "vorbis";
139 case kCodecFLAC:
140 return "flac";
141 case kCodecAMR_NB:
142 return "amr_nb";
143 case kCodecAMR_WB:
144 return "amr_wb";
145 case kCodecGSM_MS:
146 return "gsm_ms";
147 case kCodecPCM_ALAW:
148 return "pcm_alaw";
149 case kCodecPCM_MULAW:
150 return "pcm_mulaw";
151 case kCodecOpus:
152 return "opus";
153 case kCodecALAC:
154 return "alac";
156 NOTREACHED();
157 return "";
160 } // namespace media