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 "media/audio/sample_rates.h"
10 #include "media/base/limits.h"
14 AudioDecoderConfig::AudioDecoderConfig()
15 : codec_(kUnknownAudioCodec
),
16 sample_format_(kUnknownSampleFormat
),
17 bytes_per_channel_(0),
18 channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED
),
19 samples_per_second_(0),
25 AudioDecoderConfig::AudioDecoderConfig(AudioCodec codec
,
26 SampleFormat sample_format
,
27 ChannelLayout channel_layout
,
28 int samples_per_second
,
29 const uint8
* extra_data
,
30 size_t extra_data_size
,
32 Initialize(codec
, sample_format
, channel_layout
, samples_per_second
,
33 extra_data
, extra_data_size
, is_encrypted
, true,
34 base::TimeDelta(), 0);
37 void AudioDecoderConfig::Initialize(AudioCodec codec
,
38 SampleFormat sample_format
,
39 ChannelLayout channel_layout
,
40 int samples_per_second
,
41 const uint8
* extra_data
,
42 size_t extra_data_size
,
45 base::TimeDelta seek_preroll
,
47 CHECK((extra_data_size
!= 0) == (extra_data
!= NULL
));
50 UMA_HISTOGRAM_ENUMERATION("Media.AudioCodec", codec
, kAudioCodecMax
+ 1);
51 UMA_HISTOGRAM_ENUMERATION("Media.AudioSampleFormat", sample_format
,
52 kSampleFormatMax
+ 1);
53 UMA_HISTOGRAM_ENUMERATION("Media.AudioChannelLayout", channel_layout
,
54 CHANNEL_LAYOUT_MAX
+ 1);
56 if (ToAudioSampleRate(samples_per_second
, &asr
)) {
57 UMA_HISTOGRAM_ENUMERATION("Media.AudioSamplesPerSecond", asr
,
58 kAudioSampleRateMax
+ 1);
61 "Media.AudioSamplesPerSecondUnexpected", samples_per_second
);
66 channel_layout_
= channel_layout
;
67 samples_per_second_
= samples_per_second
;
68 sample_format_
= sample_format
;
69 bytes_per_channel_
= SampleFormatToBytesPerChannel(sample_format
);
70 extra_data_
.assign(extra_data
, extra_data
+ extra_data_size
);
71 is_encrypted_
= is_encrypted
;
72 seek_preroll_
= seek_preroll
;
73 codec_delay_
= codec_delay
;
75 int channels
= ChannelLayoutToChannelCount(channel_layout_
);
76 bytes_per_frame_
= channels
* bytes_per_channel_
;
79 AudioDecoderConfig::~AudioDecoderConfig() {}
81 bool AudioDecoderConfig::IsValidConfig() const {
82 return codec_
!= kUnknownAudioCodec
&&
83 channel_layout_
!= CHANNEL_LAYOUT_UNSUPPORTED
&&
84 bytes_per_channel_
> 0 &&
85 bytes_per_channel_
<= limits::kMaxBytesPerSample
&&
86 samples_per_second_
> 0 &&
87 samples_per_second_
<= limits::kMaxSampleRate
&&
88 sample_format_
!= kUnknownSampleFormat
&&
89 seek_preroll_
>= base::TimeDelta() &&
93 bool AudioDecoderConfig::Matches(const AudioDecoderConfig
& config
) const {
94 return ((codec() == config
.codec()) &&
95 (bytes_per_channel() == config
.bytes_per_channel()) &&
96 (channel_layout() == config
.channel_layout()) &&
97 (samples_per_second() == config
.samples_per_second()) &&
98 (extra_data_size() == config
.extra_data_size()) &&
99 (!extra_data() || !memcmp(extra_data(), config
.extra_data(),
100 extra_data_size())) &&
101 (is_encrypted() == config
.is_encrypted()) &&
102 (sample_format() == config
.sample_format()) &&
103 (seek_preroll() == config
.seek_preroll()) &&
104 (codec_delay() == config
.codec_delay()));
107 std::string
AudioDecoderConfig::AsHumanReadableString() const {
108 std::ostringstream s
;
109 s
<< "codec: " << GetHumanReadableCodecName()
110 << " bytes_per_channel: " << bytes_per_channel()
111 << " channel_layout: " << channel_layout()
112 << " samples_per_second: " << samples_per_second()
113 << " sample_format: " << sample_format()
114 << " bytes_per_frame: " << bytes_per_frame()
115 << " seek_preroll: " << seek_preroll().InMilliseconds() << "ms"
116 << " codec_delay: " << codec_delay()
117 << " has extra data? " << (extra_data() ? "true" : "false")
118 << " encrypted? " << (is_encrypted() ? "true" : "false");
122 // These names come from src/third_party/ffmpeg/libavcodec/codec_desc.c
123 std::string
AudioDecoderConfig::GetHumanReadableCodecName() const {
125 case kUnknownAudioCodec
:
132 case kCodecPCM_S16BE
:
133 case kCodecPCM_S24BE
:
147 case kCodecPCM_MULAW
: