Home | History | Annotate | Download | only in base
      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 "media/base/audio_decoder_config.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/metrics/histogram.h"
      9 #include "media/audio/sample_rates.h"
     10 #include "media/base/limits.h"
     11 #include "media/base/sample_format.h"
     12 
     13 namespace media {
     14 
     15 AudioDecoderConfig::AudioDecoderConfig()
     16     : codec_(kUnknownAudioCodec),
     17       sample_format_(kUnknownSampleFormat),
     18       bytes_per_channel_(0),
     19       channel_layout_(CHANNEL_LAYOUT_UNSUPPORTED),
     20       samples_per_second_(0),
     21       bytes_per_frame_(0),
     22       is_encrypted_(false) {
     23 }
     24 
     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,
     31                                        bool is_encrypted) {
     32   Initialize(codec, sample_format, channel_layout, samples_per_second,
     33              extra_data, extra_data_size, is_encrypted, true);
     34 }
     35 
     36 void AudioDecoderConfig::Initialize(AudioCodec codec,
     37                                     SampleFormat sample_format,
     38                                     ChannelLayout channel_layout,
     39                                     int samples_per_second,
     40                                     const uint8* extra_data,
     41                                     size_t extra_data_size,
     42                                     bool is_encrypted,
     43                                     bool record_stats) {
     44   CHECK((extra_data_size != 0) == (extra_data != NULL));
     45 
     46   if (record_stats) {
     47     UMA_HISTOGRAM_ENUMERATION("Media.AudioCodec", codec, kAudioCodecMax);
     48     UMA_HISTOGRAM_ENUMERATION("Media.AudioSampleFormat", sample_format,
     49                               kSampleFormatMax);
     50     UMA_HISTOGRAM_ENUMERATION("Media.AudioChannelLayout", channel_layout,
     51                               CHANNEL_LAYOUT_MAX);
     52     AudioSampleRate asr = media::AsAudioSampleRate(samples_per_second);
     53     if (asr != kUnexpectedAudioSampleRate) {
     54       UMA_HISTOGRAM_ENUMERATION("Media.AudioSamplesPerSecond", asr,
     55                                 kUnexpectedAudioSampleRate);
     56     } else {
     57       UMA_HISTOGRAM_COUNTS(
     58           "Media.AudioSamplesPerSecondUnexpected", samples_per_second);
     59     }
     60   }
     61 
     62   codec_ = codec;
     63   channel_layout_ = channel_layout;
     64   samples_per_second_ = samples_per_second;
     65   sample_format_ = sample_format;
     66   bytes_per_channel_ = SampleFormatToBytesPerChannel(sample_format);
     67   extra_data_.assign(extra_data, extra_data + extra_data_size);
     68   is_encrypted_ = is_encrypted;
     69 
     70   int channels = ChannelLayoutToChannelCount(channel_layout_);
     71   bytes_per_frame_ = channels * bytes_per_channel_;
     72 }
     73 
     74 AudioDecoderConfig::~AudioDecoderConfig() {}
     75 
     76 bool AudioDecoderConfig::IsValidConfig() const {
     77   return codec_ != kUnknownAudioCodec &&
     78          channel_layout_ != CHANNEL_LAYOUT_UNSUPPORTED &&
     79          bytes_per_channel_ > 0 &&
     80          bytes_per_channel_ <= limits::kMaxBytesPerSample &&
     81          samples_per_second_ > 0 &&
     82          samples_per_second_ <= limits::kMaxSampleRate &&
     83          sample_format_ != kUnknownSampleFormat;
     84 }
     85 
     86 bool AudioDecoderConfig::Matches(const AudioDecoderConfig& config) const {
     87   return ((codec() == config.codec()) &&
     88           (bytes_per_channel() == config.bytes_per_channel()) &&
     89           (channel_layout() == config.channel_layout()) &&
     90           (samples_per_second() == config.samples_per_second()) &&
     91           (extra_data_size() == config.extra_data_size()) &&
     92           (!extra_data() || !memcmp(extra_data(), config.extra_data(),
     93                                     extra_data_size())) &&
     94           (is_encrypted() == config.is_encrypted()) &&
     95           (sample_format() == config.sample_format()));
     96 }
     97 
     98 }  // namespace media
     99