2015-10-08 21:48:07 +00:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/formats/webm/webm_audio_client.h>
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/log.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/formats/webm/webm_constants.h>
|
2015-10-08 21:48:07 +00:00
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
namespace {
|
2021-08-04 18:56:44 +00:00
|
|
|
// Timestamps are represented in double in WebM. Convert to int64_t in us.
|
|
|
|
const int32_t kWebMTimeScale = 1000000;
|
2015-10-14 23:12:10 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-08 21:48:07 +00:00
|
|
|
namespace media {
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
WebMAudioClient::WebMAudioClient() {
|
2015-10-08 21:48:07 +00:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
WebMAudioClient::~WebMAudioClient() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebMAudioClient::Reset() {
|
|
|
|
channels_ = -1;
|
|
|
|
samples_per_second_ = -1;
|
|
|
|
output_samples_per_second_ = -1;
|
|
|
|
}
|
|
|
|
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<AudioStreamInfo> WebMAudioClient::GetAudioStreamInfo(
|
2015-10-14 23:12:10 +00:00
|
|
|
int64_t track_num,
|
2015-10-14 22:46:23 +00:00
|
|
|
const std::string& codec_id,
|
|
|
|
const std::vector<uint8_t>& codec_private,
|
2016-05-05 22:26:48 +00:00
|
|
|
int64_t seek_preroll,
|
|
|
|
int64_t codec_delay,
|
2015-10-14 23:12:10 +00:00
|
|
|
const std::string& language,
|
|
|
|
bool is_encrypted) {
|
2016-07-27 00:51:08 +00:00
|
|
|
Codec audio_codec = kUnknownCodec;
|
2015-10-08 21:48:07 +00:00
|
|
|
if (codec_id == "A_VORBIS") {
|
|
|
|
audio_codec = kCodecVorbis;
|
|
|
|
} else if (codec_id == "A_OPUS") {
|
|
|
|
audio_codec = kCodecOpus;
|
|
|
|
} else {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Unsupported audio codec_id " << codec_id;
|
2017-01-24 00:55:02 +00:00
|
|
|
return std::shared_ptr<AudioStreamInfo>();
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (samples_per_second_ <= 0)
|
2017-01-24 00:55:02 +00:00
|
|
|
return std::shared_ptr<AudioStreamInfo>();
|
2015-10-08 21:48:07 +00:00
|
|
|
|
|
|
|
// Set channel layout default if a Channels element was not present.
|
|
|
|
if (channels_ == -1)
|
|
|
|
channels_ = 1;
|
|
|
|
|
2015-10-14 23:12:10 +00:00
|
|
|
uint32_t sampling_frequency = samples_per_second_;
|
2015-10-08 21:48:07 +00:00
|
|
|
// Always use 48kHz for OPUS. See the "Input Sample Rate" section of the
|
|
|
|
// spec: http://tools.ietf.org/html/draft-terriberry-oggopus-01#page-11
|
|
|
|
if (audio_codec == kCodecOpus) {
|
2015-10-14 23:12:10 +00:00
|
|
|
sampling_frequency = 48000;
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 19:30:32 +00:00
|
|
|
const uint8_t* codec_config = NULL;
|
|
|
|
size_t codec_config_size = 0;
|
2015-10-08 21:48:07 +00:00
|
|
|
if (codec_private.size() > 0) {
|
2016-06-27 19:30:32 +00:00
|
|
|
codec_config = &codec_private[0];
|
|
|
|
codec_config_size = codec_private.size();
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 18:55:14 +00:00
|
|
|
const uint8_t kSampleSizeInBits = 16u;
|
2017-01-24 00:55:02 +00:00
|
|
|
return std::make_shared<AudioStreamInfo>(
|
2015-10-14 23:12:10 +00:00
|
|
|
track_num, kWebMTimeScale, 0, audio_codec,
|
2016-07-27 00:51:08 +00:00
|
|
|
AudioStreamInfo::GetCodecString(audio_codec, 0), codec_config,
|
|
|
|
codec_config_size, kSampleSizeInBits, channels_, sampling_frequency,
|
2016-05-05 22:26:48 +00:00
|
|
|
seek_preroll < 0 ? 0 : seek_preroll, codec_delay < 0 ? 0 : codec_delay, 0,
|
2017-01-24 00:55:02 +00:00
|
|
|
0, language, is_encrypted);
|
2015-10-08 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 22:46:23 +00:00
|
|
|
bool WebMAudioClient::OnUInt(int id, int64_t val) {
|
2015-10-08 21:48:07 +00:00
|
|
|
if (id == kWebMIdChannels) {
|
|
|
|
if (channels_ != -1) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Multiple values for id " << std::hex << id
|
|
|
|
<< " specified. (" << channels_ << " and " << val << ")";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
channels_ = val;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebMAudioClient::OnFloat(int id, double val) {
|
|
|
|
double* dst = NULL;
|
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case kWebMIdSamplingFrequency:
|
|
|
|
dst = &samples_per_second_;
|
|
|
|
break;
|
|
|
|
case kWebMIdOutputSamplingFrequency:
|
|
|
|
dst = &output_samples_per_second_;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val <= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (*dst != -1) {
|
2015-10-14 22:46:23 +00:00
|
|
|
LOG(ERROR) << "Multiple values for id " << std::hex << id << " specified ("
|
|
|
|
<< *dst << " and " << val << ")";
|
2015-10-08 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|