DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
webm_audio_client.cc
1 // Copyright 2014 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 "packager/media/formats/webm/webm_audio_client.h"
6 
7 #include "packager/base/logging.h"
8 #include "packager/media/formats/webm/webm_constants.h"
9 
10 namespace {
11 // Timestamps are represented in double in WebM. Convert to uint64_t in us.
12 const uint32_t kWebMTimeScale = 1000000u;
13 } // namespace
14 
15 namespace edash_packager {
16 namespace media {
17 
18 WebMAudioClient::WebMAudioClient() {
19  Reset();
20 }
21 
22 WebMAudioClient::~WebMAudioClient() {
23 }
24 
26  channels_ = -1;
27  samples_per_second_ = -1;
28  output_samples_per_second_ = -1;
29 }
30 
31 scoped_refptr<AudioStreamInfo> WebMAudioClient::GetAudioStreamInfo(
32  int64_t track_num,
33  const std::string& codec_id,
34  const std::vector<uint8_t>& codec_private,
35  const std::string& language,
36  bool is_encrypted) {
37  AudioCodec audio_codec = kUnknownAudioCodec;
38  if (codec_id == "A_VORBIS") {
39  audio_codec = kCodecVorbis;
40  } else if (codec_id == "A_OPUS") {
41  audio_codec = kCodecOpus;
42  } else {
43  LOG(ERROR) << "Unsupported audio codec_id " << codec_id;
44  return scoped_refptr<AudioStreamInfo>();
45  }
46 
47  if (samples_per_second_ <= 0)
48  return scoped_refptr<AudioStreamInfo>();
49 
50  // Set channel layout default if a Channels element was not present.
51  if (channels_ == -1)
52  channels_ = 1;
53 
54  uint32_t sampling_frequency = samples_per_second_;
55  // Always use 48kHz for OPUS. See the "Input Sample Rate" section of the
56  // spec: http://tools.ietf.org/html/draft-terriberry-oggopus-01#page-11
57  if (audio_codec == kCodecOpus) {
58  sampling_frequency = 48000;
59  }
60 
61  const uint8_t* extra_data = NULL;
62  size_t extra_data_size = 0;
63  if (codec_private.size() > 0) {
64  extra_data = &codec_private[0];
65  extra_data_size = codec_private.size();
66  }
67 
68  const uint32_t kSampleSizeInBits = 4u;
69  return scoped_refptr<AudioStreamInfo>(new AudioStreamInfo(
70  track_num, kWebMTimeScale, 0, audio_codec,
71  AudioStreamInfo::GetCodecString(audio_codec, 0), language,
72  kSampleSizeInBits, channels_, sampling_frequency, 0, 0, extra_data,
73  extra_data_size, is_encrypted));
74 }
75 
76 bool WebMAudioClient::OnUInt(int id, int64_t val) {
77  if (id == kWebMIdChannels) {
78  if (channels_ != -1) {
79  LOG(ERROR) << "Multiple values for id " << std::hex << id
80  << " specified. (" << channels_ << " and " << val << ")";
81  return false;
82  }
83 
84  channels_ = val;
85  }
86  return true;
87 }
88 
89 bool WebMAudioClient::OnFloat(int id, double val) {
90  double* dst = NULL;
91 
92  switch (id) {
93  case kWebMIdSamplingFrequency:
94  dst = &samples_per_second_;
95  break;
96  case kWebMIdOutputSamplingFrequency:
97  dst = &output_samples_per_second_;
98  break;
99  default:
100  return true;
101  }
102 
103  if (val <= 0)
104  return false;
105 
106  if (*dst != -1) {
107  LOG(ERROR) << "Multiple values for id " << std::hex << id << " specified ("
108  << *dst << " and " << val << ")";
109  return false;
110  }
111 
112  *dst = val;
113  return true;
114 }
115 
116 } // namespace media
117 } // namespace edash_packager
Holds audio stream information.
void Reset()
Reset this object's state so it can process a new audio track element.
static std::string GetCodecString(AudioCodec codec, uint8_t audio_object_type)
scoped_refptr< AudioStreamInfo > GetAudioStreamInfo(int64_t track_num, const std::string &codec_id, const std::vector< uint8_t > &codec_private, const std::string &language, bool is_encrypted)