Shaka Packager SDK
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 shaka {
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 std::shared_ptr<AudioStreamInfo> WebMAudioClient::GetAudioStreamInfo(
32  int64_t track_num,
33  const std::string& codec_id,
34  const std::vector<uint8_t>& codec_private,
35  int64_t seek_preroll,
36  int64_t codec_delay,
37  const std::string& language,
38  bool is_encrypted) {
39  Codec audio_codec = kUnknownCodec;
40  if (codec_id == "A_VORBIS") {
41  audio_codec = kCodecVorbis;
42  } else if (codec_id == "A_OPUS") {
43  audio_codec = kCodecOpus;
44  } else {
45  LOG(ERROR) << "Unsupported audio codec_id " << codec_id;
46  return std::shared_ptr<AudioStreamInfo>();
47  }
48 
49  if (samples_per_second_ <= 0)
50  return std::shared_ptr<AudioStreamInfo>();
51 
52  // Set channel layout default if a Channels element was not present.
53  if (channels_ == -1)
54  channels_ = 1;
55 
56  uint32_t sampling_frequency = samples_per_second_;
57  // Always use 48kHz for OPUS. See the "Input Sample Rate" section of the
58  // spec: http://tools.ietf.org/html/draft-terriberry-oggopus-01#page-11
59  if (audio_codec == kCodecOpus) {
60  sampling_frequency = 48000;
61  }
62 
63  const uint8_t* codec_config = NULL;
64  size_t codec_config_size = 0;
65  if (codec_private.size() > 0) {
66  codec_config = &codec_private[0];
67  codec_config_size = codec_private.size();
68  }
69 
70  const uint8_t kSampleSizeInBits = 16u;
71  return std::make_shared<AudioStreamInfo>(
72  track_num, kWebMTimeScale, 0, audio_codec,
73  AudioStreamInfo::GetCodecString(audio_codec, 0), codec_config,
74  codec_config_size, kSampleSizeInBits, channels_, sampling_frequency,
75  seek_preroll < 0 ? 0 : seek_preroll, codec_delay < 0 ? 0 : codec_delay, 0,
76  0, language, is_encrypted);
77 }
78 
79 bool WebMAudioClient::OnUInt(int id, int64_t val) {
80  if (id == kWebMIdChannels) {
81  if (channels_ != -1) {
82  LOG(ERROR) << "Multiple values for id " << std::hex << id
83  << " specified. (" << channels_ << " and " << val << ")";
84  return false;
85  }
86 
87  channels_ = val;
88  }
89  return true;
90 }
91 
92 bool WebMAudioClient::OnFloat(int id, double val) {
93  double* dst = NULL;
94 
95  switch (id) {
96  case kWebMIdSamplingFrequency:
97  dst = &samples_per_second_;
98  break;
99  case kWebMIdOutputSamplingFrequency:
100  dst = &output_samples_per_second_;
101  break;
102  default:
103  return true;
104  }
105 
106  if (val <= 0)
107  return false;
108 
109  if (*dst != -1) {
110  LOG(ERROR) << "Multiple values for id " << std::hex << id << " specified ("
111  << *dst << " and " << val << ")";
112  return false;
113  }
114 
115  *dst = val;
116  return true;
117 }
118 
119 } // namespace media
120 } // namespace shaka
All the methods that are virtual are virtual for mocking.
std::shared_ptr< AudioStreamInfo > GetAudioStreamInfo(int64_t track_num, const std::string &codec_id, const std::vector< uint8_t > &codec_private, int64_t seek_preroll, int64_t codec_delay, const std::string &language, bool is_encrypted)
static std::string GetCodecString(Codec codec, uint8_t audio_object_type)
void Reset()
Reset this object&#39;s state so it can process a new audio track element.