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