2014-06-27 23:07:36 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/app/stream_descriptor.h"
|
2014-06-27 23:07:36 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/strings/string_number_conversions.h"
|
|
|
|
#include "packager/base/strings/string_split.h"
|
2014-06-27 23:07:36 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-06-27 23:07:36 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum FieldType {
|
|
|
|
kUnknownField = 0,
|
|
|
|
kStreamSelectorField,
|
|
|
|
kInputField,
|
|
|
|
kOutputField,
|
|
|
|
kSegmentTemplateField,
|
|
|
|
kBandwidthField,
|
2015-02-02 19:27:32 +00:00
|
|
|
kLanguageField,
|
2020-12-11 20:58:26 +00:00
|
|
|
kCcIndexField,
|
2015-12-16 18:20:13 +00:00
|
|
|
kOutputFormatField,
|
2016-04-16 22:58:47 +00:00
|
|
|
kHlsNameField,
|
|
|
|
kHlsGroupIdField,
|
|
|
|
kHlsPlaylistNameField,
|
2018-02-01 20:27:30 +00:00
|
|
|
kHlsIframePlaylistNameField,
|
2017-05-15 16:22:06 +00:00
|
|
|
kTrickPlayFactorField,
|
2017-05-22 17:17:58 +00:00
|
|
|
kSkipEncryptionField,
|
2017-09-20 22:49:00 +00:00
|
|
|
kDrmStreamLabelField,
|
2018-10-10 22:30:28 +00:00
|
|
|
kHlsCharacteristicsField,
|
2019-06-13 06:01:16 +00:00
|
|
|
kDashAccessiblitiesField,
|
|
|
|
kDashRolesField,
|
2020-03-06 18:19:47 +00:00
|
|
|
kDashOnlyField,
|
|
|
|
kHlsOnlyField,
|
2014-06-27 23:07:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct FieldNameToTypeMapping {
|
|
|
|
const char* field_name;
|
|
|
|
FieldType field_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
const FieldNameToTypeMapping kFieldNameTypeMappings[] = {
|
2016-04-16 22:58:47 +00:00
|
|
|
{"stream_selector", kStreamSelectorField},
|
|
|
|
{"stream", kStreamSelectorField},
|
|
|
|
{"input", kInputField},
|
|
|
|
{"in", kInputField},
|
|
|
|
{"output", kOutputField},
|
|
|
|
{"out", kOutputField},
|
|
|
|
{"init_segment", kOutputField},
|
|
|
|
{"segment_template", kSegmentTemplateField},
|
|
|
|
{"template", kSegmentTemplateField},
|
|
|
|
{"bandwidth", kBandwidthField},
|
|
|
|
{"bw", kBandwidthField},
|
|
|
|
{"bitrate", kBandwidthField},
|
|
|
|
{"language", kLanguageField},
|
|
|
|
{"lang", kLanguageField},
|
2020-12-11 20:58:26 +00:00
|
|
|
{"cc_index", kCcIndexField},
|
2016-04-16 22:58:47 +00:00
|
|
|
{"output_format", kOutputFormatField},
|
|
|
|
{"format", kOutputFormatField},
|
|
|
|
{"hls_name", kHlsNameField},
|
|
|
|
{"hls_group_id", kHlsGroupIdField},
|
|
|
|
{"playlist_name", kHlsPlaylistNameField},
|
2018-02-01 20:27:30 +00:00
|
|
|
{"iframe_playlist_name", kHlsIframePlaylistNameField},
|
2017-05-15 16:22:06 +00:00
|
|
|
{"trick_play_factor", kTrickPlayFactorField},
|
|
|
|
{"tpf", kTrickPlayFactorField},
|
2017-05-22 17:17:58 +00:00
|
|
|
{"skip_encryption", kSkipEncryptionField},
|
2017-09-20 22:49:00 +00:00
|
|
|
{"drm_stream_label", kDrmStreamLabelField},
|
|
|
|
{"drm_label", kDrmStreamLabelField},
|
2018-10-10 22:30:28 +00:00
|
|
|
{"hls_characteristics", kHlsCharacteristicsField},
|
|
|
|
{"characteristics", kHlsCharacteristicsField},
|
|
|
|
{"charcs", kHlsCharacteristicsField},
|
2019-06-13 06:01:16 +00:00
|
|
|
{"dash_accessibilities", kDashAccessiblitiesField},
|
|
|
|
{"dash_accessibility", kDashAccessiblitiesField},
|
|
|
|
{"accessibilities", kDashAccessiblitiesField},
|
|
|
|
{"accessibility", kDashAccessiblitiesField},
|
|
|
|
{"dash_roles", kDashRolesField},
|
|
|
|
{"dash_role", kDashRolesField},
|
|
|
|
{"roles", kDashRolesField},
|
|
|
|
{"role", kDashRolesField},
|
2020-03-06 18:19:47 +00:00
|
|
|
{"dash_only", kDashOnlyField},
|
|
|
|
{"hls_only", kHlsOnlyField},
|
2014-06-27 23:07:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FieldType GetFieldType(const std::string& field_name) {
|
|
|
|
for (size_t idx = 0; idx < arraysize(kFieldNameTypeMappings); ++idx) {
|
|
|
|
if (field_name == kFieldNameTypeMappings[idx].field_name)
|
|
|
|
return kFieldNameTypeMappings[idx].field_type;
|
|
|
|
}
|
|
|
|
return kUnknownField;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-05-22 20:31:41 +00:00
|
|
|
base::Optional<StreamDescriptor> ParseStreamDescriptor(
|
|
|
|
const std::string& descriptor_string) {
|
2014-06-27 23:07:36 +00:00
|
|
|
StreamDescriptor descriptor;
|
|
|
|
|
|
|
|
// Split descriptor string into name/value pairs.
|
|
|
|
base::StringPairs pairs;
|
2017-05-22 20:31:41 +00:00
|
|
|
if (!base::SplitStringIntoKeyValuePairs(descriptor_string, '=', ',',
|
2014-06-27 23:07:36 +00:00
|
|
|
&pairs)) {
|
2018-07-11 18:57:21 +00:00
|
|
|
LOG(ERROR) << "Invalid stream descriptors name/value pairs: "
|
|
|
|
<< descriptor_string;
|
2017-05-22 20:31:41 +00:00
|
|
|
return base::nullopt;
|
2014-06-27 23:07:36 +00:00
|
|
|
}
|
|
|
|
for (base::StringPairs::const_iterator iter = pairs.begin();
|
|
|
|
iter != pairs.end(); ++iter) {
|
|
|
|
switch (GetFieldType(iter->first)) {
|
|
|
|
case kStreamSelectorField:
|
|
|
|
descriptor.stream_selector = iter->second;
|
|
|
|
break;
|
|
|
|
case kInputField:
|
|
|
|
descriptor.input = iter->second;
|
|
|
|
break;
|
|
|
|
case kOutputField:
|
|
|
|
descriptor.output = iter->second;
|
|
|
|
break;
|
|
|
|
case kSegmentTemplateField:
|
|
|
|
descriptor.segment_template = iter->second;
|
|
|
|
break;
|
|
|
|
case kBandwidthField: {
|
|
|
|
unsigned bw;
|
|
|
|
if (!base::StringToUint(iter->second, &bw)) {
|
|
|
|
LOG(ERROR) << "Non-numeric bandwidth specified.";
|
2017-05-22 20:31:41 +00:00
|
|
|
return base::nullopt;
|
2014-06-27 23:07:36 +00:00
|
|
|
}
|
|
|
|
descriptor.bandwidth = bw;
|
|
|
|
break;
|
|
|
|
}
|
2015-02-02 19:27:32 +00:00
|
|
|
case kLanguageField: {
|
2017-09-30 00:45:00 +00:00
|
|
|
descriptor.language = iter->second;
|
2015-02-02 19:27:32 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-12-11 20:58:26 +00:00
|
|
|
case kCcIndexField: {
|
|
|
|
unsigned index;
|
|
|
|
if (!base::StringToUint(iter->second, &index)) {
|
|
|
|
LOG(ERROR) << "Non-numeric cc_index specified.";
|
|
|
|
return base::nullopt;
|
|
|
|
}
|
|
|
|
descriptor.cc_index = index;
|
|
|
|
break;
|
|
|
|
}
|
2015-12-16 18:20:13 +00:00
|
|
|
case kOutputFormatField: {
|
2017-05-22 20:31:41 +00:00
|
|
|
descriptor.output_format = iter->second;
|
2015-12-16 18:20:13 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-04-16 22:58:47 +00:00
|
|
|
case kHlsNameField: {
|
|
|
|
descriptor.hls_name = iter->second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kHlsGroupIdField: {
|
|
|
|
descriptor.hls_group_id = iter->second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kHlsPlaylistNameField: {
|
|
|
|
descriptor.hls_playlist_name = iter->second;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-01 20:27:30 +00:00
|
|
|
case kHlsIframePlaylistNameField: {
|
|
|
|
descriptor.hls_iframe_playlist_name = iter->second;
|
|
|
|
break;
|
|
|
|
}
|
2017-05-15 16:22:06 +00:00
|
|
|
case kTrickPlayFactorField: {
|
|
|
|
unsigned factor;
|
|
|
|
if (!base::StringToUint(iter->second, &factor)) {
|
|
|
|
LOG(ERROR) << "Non-numeric trick play factor " << iter->second
|
2017-03-21 23:14:46 +00:00
|
|
|
<< " specified.";
|
2017-05-22 20:31:41 +00:00
|
|
|
return base::nullopt;
|
2017-03-21 23:14:46 +00:00
|
|
|
}
|
2017-05-15 16:22:06 +00:00
|
|
|
if (factor == 0) {
|
|
|
|
LOG(ERROR) << "Stream trick_play_factor should be > 0.";
|
2017-05-22 20:31:41 +00:00
|
|
|
return base::nullopt;
|
2017-03-21 23:14:46 +00:00
|
|
|
}
|
2017-05-15 16:22:06 +00:00
|
|
|
descriptor.trick_play_factor = factor;
|
2017-03-21 23:14:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-05-22 17:17:58 +00:00
|
|
|
case kSkipEncryptionField: {
|
|
|
|
unsigned skip_encryption_value;
|
|
|
|
if (!base::StringToUint(iter->second, &skip_encryption_value)) {
|
|
|
|
LOG(ERROR) << "Non-numeric option for skip encryption field "
|
|
|
|
"specified (" << iter->second << ").";
|
2017-05-22 20:31:41 +00:00
|
|
|
return base::nullopt;
|
2017-05-22 17:17:58 +00:00
|
|
|
}
|
|
|
|
if (skip_encryption_value > 1) {
|
|
|
|
LOG(ERROR) << "skip_encryption should be either 0 or 1.";
|
2017-05-22 20:31:41 +00:00
|
|
|
return base::nullopt;
|
2017-05-22 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
descriptor.skip_encryption = skip_encryption_value > 0;
|
|
|
|
break;
|
|
|
|
}
|
2018-10-10 22:30:28 +00:00
|
|
|
case kDrmStreamLabelField:
|
2017-09-20 22:49:00 +00:00
|
|
|
descriptor.drm_label = iter->second;
|
|
|
|
break;
|
2018-10-10 22:30:28 +00:00
|
|
|
case kHlsCharacteristicsField:
|
|
|
|
descriptor.hls_characteristics =
|
|
|
|
base::SplitString(iter->second, ";:", base::TRIM_WHITESPACE,
|
|
|
|
base::SPLIT_WANT_NONEMPTY);
|
|
|
|
break;
|
2019-06-13 06:01:16 +00:00
|
|
|
case kDashAccessiblitiesField:
|
|
|
|
descriptor.dash_accessiblities =
|
|
|
|
base::SplitString(iter->second, ";", base::TRIM_WHITESPACE,
|
|
|
|
base::SPLIT_WANT_NONEMPTY);
|
|
|
|
for (const std::string& accessibility :
|
|
|
|
descriptor.dash_accessiblities) {
|
|
|
|
size_t pos = accessibility.find('=');
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
LOG(ERROR)
|
|
|
|
<< "Accessibility should be in scheme=value format, but seeing "
|
|
|
|
<< accessibility;
|
|
|
|
return base::nullopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case kDashRolesField:
|
|
|
|
descriptor.dash_roles =
|
|
|
|
base::SplitString(iter->second, ";", base::TRIM_WHITESPACE,
|
|
|
|
base::SPLIT_WANT_NONEMPTY);
|
|
|
|
break;
|
2020-03-06 18:19:47 +00:00
|
|
|
case kDashOnlyField:
|
|
|
|
unsigned dash_only_value;
|
|
|
|
if (!base::StringToUint(iter->second, &dash_only_value)) {
|
|
|
|
LOG(ERROR) << "Non-numeric option for dash_only field "
|
|
|
|
"specified (" << iter->second << ").";
|
|
|
|
return base::nullopt;
|
|
|
|
}
|
|
|
|
if (dash_only_value > 1) {
|
|
|
|
LOG(ERROR) << "dash_only should be either 0 or 1.";
|
|
|
|
return base::nullopt;
|
|
|
|
}
|
|
|
|
descriptor.dash_only = dash_only_value > 0;
|
|
|
|
break;
|
|
|
|
case kHlsOnlyField:
|
|
|
|
unsigned hls_only_value;
|
|
|
|
if (!base::StringToUint(iter->second, &hls_only_value)) {
|
|
|
|
LOG(ERROR) << "Non-numeric option for hls_only field "
|
|
|
|
"specified (" << iter->second << ").";
|
|
|
|
return base::nullopt;
|
|
|
|
}
|
|
|
|
if (hls_only_value > 1) {
|
|
|
|
LOG(ERROR) << "hls_only should be either 0 or 1.";
|
|
|
|
return base::nullopt;
|
|
|
|
}
|
|
|
|
descriptor.hls_only = hls_only_value > 0;
|
|
|
|
break;
|
2014-06-27 23:07:36 +00:00
|
|
|
default:
|
|
|
|
LOG(ERROR) << "Unknown field in stream descriptor (\"" << iter->first
|
|
|
|
<< "\").";
|
2017-05-22 20:31:41 +00:00
|
|
|
return base::nullopt;
|
2014-06-27 23:07:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-22 20:31:41 +00:00
|
|
|
return descriptor;
|
2014-06-27 23:07:36 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|