7 #include "packager/app/stream_descriptor.h"
9 #include "packager/app/packager_util.h"
10 #include "packager/base/logging.h"
11 #include "packager/base/strings/string_number_conversions.h"
12 #include "packager/base/strings/string_split.h"
13 #include "packager/media/base/container_names.h"
14 #include "packager/media/base/language_utils.h"
26 kSegmentTemplateField,
32 kHlsPlaylistNameField,
36 struct FieldNameToTypeMapping {
37 const char* field_name;
41 const FieldNameToTypeMapping kFieldNameTypeMappings[] = {
42 {
"stream_selector", kStreamSelectorField},
43 {
"stream", kStreamSelectorField},
44 {
"input", kInputField},
46 {
"output", kOutputField},
47 {
"out", kOutputField},
48 {
"init_segment", kOutputField},
49 {
"segment_template", kSegmentTemplateField},
50 {
"template", kSegmentTemplateField},
51 {
"bandwidth", kBandwidthField},
52 {
"bw", kBandwidthField},
53 {
"bitrate", kBandwidthField},
54 {
"language", kLanguageField},
55 {
"lang", kLanguageField},
56 {
"output_format", kOutputFormatField},
57 {
"format", kOutputFormatField},
58 {
"hls_name", kHlsNameField},
59 {
"hls_group_id", kHlsGroupIdField},
60 {
"playlist_name", kHlsPlaylistNameField},
61 {
"trick_play_rate", kTrickPlayRateField},
64 FieldType GetFieldType(
const std::string& field_name) {
65 for (
size_t idx = 0; idx < arraysize(kFieldNameTypeMappings); ++idx) {
66 if (field_name == kFieldNameTypeMappings[idx].field_name)
67 return kFieldNameTypeMappings[idx].field_type;
74 StreamDescriptor::StreamDescriptor()
75 : bandwidth(0), output_format(CONTAINER_UNKNOWN) {}
77 StreamDescriptor::~StreamDescriptor() {}
79 bool InsertStreamDescriptor(
const std::string& descriptor_string,
80 StreamDescriptorList* descriptor_list) {
81 StreamDescriptor descriptor;
84 base::StringPairs pairs;
85 if (!base::SplitStringIntoKeyValuePairs(descriptor_string,
89 LOG(ERROR) <<
"Invalid stream descriptors name/value pairs.";
92 for (base::StringPairs::const_iterator iter = pairs.begin();
93 iter != pairs.end(); ++iter) {
94 switch (GetFieldType(iter->first)) {
95 case kStreamSelectorField:
96 descriptor.stream_selector = iter->second;
99 descriptor.input = iter->second;
102 descriptor.output = iter->second;
104 case kSegmentTemplateField:
105 descriptor.segment_template = iter->second;
107 case kBandwidthField: {
109 if (!base::StringToUint(iter->second, &bw)) {
110 LOG(ERROR) <<
"Non-numeric bandwidth specified.";
113 descriptor.bandwidth = bw;
116 case kLanguageField: {
118 if (language ==
"und") {
119 LOG(ERROR) <<
"Unknown/invalid language specified: " << iter->second;
122 descriptor.language = language;
125 case kOutputFormatField: {
126 MediaContainerName output_format =
127 DetermineContainerFromFormatName(iter->second);
128 if (output_format == CONTAINER_UNKNOWN) {
129 LOG(ERROR) <<
"Unrecognized output format " << iter->second;
132 descriptor.output_format = output_format;
135 case kHlsNameField: {
136 descriptor.hls_name = iter->second;
139 case kHlsGroupIdField: {
140 descriptor.hls_group_id = iter->second;
143 case kHlsPlaylistNameField: {
144 descriptor.hls_playlist_name = iter->second;
148 LOG(ERROR) <<
"Unknown field in stream descriptor (\"" << iter->first
154 if (descriptor.input.empty()) {
155 LOG(ERROR) <<
"Stream input not specified.";
158 if (!FLAGS_dump_stream_info && descriptor.stream_selector.empty()) {
159 LOG(ERROR) <<
"Stream stream_selector not specified.";
163 if (descriptor.output_format == CONTAINER_UNKNOWN) {
164 const std::string& output_name = descriptor.output.empty()
165 ? descriptor.segment_template
167 if (!output_name.empty()) {
168 descriptor.output_format = DetermineContainerFromFileName(output_name);
169 if (descriptor.output_format == CONTAINER_UNKNOWN) {
170 LOG(ERROR) <<
"Unable to determine output format for file "
177 if (descriptor.output_format == MediaContainerName::CONTAINER_MPEG2TS) {
178 if (descriptor.segment_template.empty()) {
179 LOG(ERROR) <<
"Please specify segment_template. Single file TS output is "
185 if (!descriptor.output.empty()) {
186 LOG(WARNING) <<
"TS output '" << descriptor.output
187 <<
"' ignored. TS muxer does not support initialization "
188 "segment generation.";
194 const bool output_specified =
195 !descriptor.output.empty() ||
196 (descriptor.output_format == CONTAINER_MPEG2TS &&
197 !descriptor.segment_template.empty());
198 if (!FLAGS_dump_stream_info && !output_specified) {
199 LOG(ERROR) <<
"Stream output not specified.";
202 descriptor_list->insert(descriptor);
std::string LanguageToISO_639_2(const std::string &language)