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),
23 is_encrypted_(false) {
26 AudioDecoderConfig::AudioDecoderConfig(AudioCodec codec
,
27 SampleFormat sample_format
,
28 ChannelLayout channel_layout
,
29 int samples_per_second
,
30 const uint8
* extra_data
,
31 size_t extra_data_size
,
33 Initialize(codec
, sample_format
, channel_layout
, samples_per_second
,
34 extra_data
, extra_data_size
, is_encrypted
, true,
35 base::TimeDelta(), base::TimeDelta());
38 void AudioDecoderConfig::Initialize(AudioCodec codec
,
39 SampleFormat sample_format
,
40 ChannelLayout channel_layout
,
41 int samples_per_second
,
42 const uint8
* extra_data
,
43 size_t extra_data_size
,
46 base::TimeDelta seek_preroll
,
47 base::TimeDelta codec_delay
) {
48 CHECK((extra_data_size
!= 0) == (extra_data
!= NULL
));
51 UMA_HISTOGRAM_ENUMERATION("Media.AudioCodec", codec
, kAudioCodecMax
);
52 UMA_HISTOGRAM_ENUMERATION("Media.AudioSampleFormat", sample_format
,
54 UMA_HISTOGRAM_ENUMERATION("Media.AudioChannelLayout", channel_layout
,
56 AudioSampleRate asr
= media::AsAudioSampleRate(samples_per_second
);
57 if (asr
!= kUnexpectedAudioSampleRate
) {
58 UMA_HISTOGRAM_ENUMERATION("Media.AudioSamplesPerSecond", asr
,
59 kUnexpectedAudioSampleRate
);
62 "Media.AudioSamplesPerSecondUnexpected", samples_per_second
);
67 channel_layout_
= channel_layout
;
68 samples_per_second_
= samples_per_second
;
69 sample_format_
= sample_format
;
70 bytes_per_channel_
= SampleFormatToBytesPerChannel(sample_format
);
71 extra_data_
.assign(extra_data
, extra_data
+ extra_data_size
);
72 is_encrypted_
= is_encrypted
;
73 seek_preroll_
= seek_preroll
;
74 codec_delay_
= codec_delay
;
76 int channels
= ChannelLayoutToChannelCount(channel_layout_
);
77 bytes_per_frame_
= channels
* bytes_per_channel_
;
80 AudioDecoderConfig::~AudioDecoderConfig() {}
82 bool AudioDecoderConfig::IsValidConfig() const {
83 return codec_
!= kUnknownAudioCodec
&&
84 channel_layout_
!= CHANNEL_LAYOUT_UNSUPPORTED
&&
85 bytes_per_channel_
> 0 &&
86 bytes_per_channel_
<= limits::kMaxBytesPerSample
&&
87 samples_per_second_
> 0 &&
88 samples_per_second_
<= limits::kMaxSampleRate
&&
89 sample_format_
!= kUnknownSampleFormat
&&
90 seek_preroll_
>= base::TimeDelta() &&
91 codec_delay_
>= base::TimeDelta();
94 bool AudioDecoderConfig::Matches(const AudioDecoderConfig
& config
) const {
95 return ((codec() == config
.codec()) &&
96 (bytes_per_channel() == config
.bytes_per_channel()) &&
97 (channel_layout() == config
.channel_layout()) &&
98 (samples_per_second() == config
.samples_per_second()) &&
99 (extra_data_size() == config
.extra_data_size()) &&
100 (!extra_data() || !memcmp(extra_data(), config
.extra_data(),
101 extra_data_size())) &&
102 (is_encrypted() == config
.is_encrypted()) &&
103 (sample_format() == config
.sample_format()) &&
104 (seek_preroll() == config
.seek_preroll()) &&
105 (codec_delay() == config
.codec_delay()));