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"
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),
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
,
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
,
47 base::TimeDelta seek_preroll
,
49 CHECK((extra_data_size
!= 0) == (extra_data
!= NULL
));
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);
58 if (ToAudioSampleRate(samples_per_second
, &asr
)) {
59 UMA_HISTOGRAM_ENUMERATION("Media.AudioSamplesPerSecond", asr
,
60 kAudioSampleRateMax
+ 1);
63 "Media.AudioSamplesPerSecondUnexpected", samples_per_second
);
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() &&
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");
124 // These names come from src/third_party/ffmpeg/libavcodec/codec_desc.c
125 std::string
AudioDecoderConfig::GetHumanReadableCodecName() const {
127 case kUnknownAudioCodec
:
134 case kCodecPCM_S16BE
:
135 case kCodecPCM_S24BE
:
149 case kCodecPCM_MULAW
: