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,
35 struct FieldNameToTypeMapping {
36 const char* field_name;
40 const FieldNameToTypeMapping kFieldNameTypeMappings[] = {
41 {
"stream_selector", kStreamSelectorField},
42 {
"stream", kStreamSelectorField},
43 {
"input", kInputField},
45 {
"output", kOutputField},
46 {
"out", kOutputField},
47 {
"init_segment", kOutputField},
48 {
"segment_template", kSegmentTemplateField},
49 {
"template", kSegmentTemplateField},
50 {
"bandwidth", kBandwidthField},
51 {
"bw", kBandwidthField},
52 {
"bitrate", kBandwidthField},
53 {
"language", kLanguageField},
54 {
"lang", kLanguageField},
55 {
"output_format", kOutputFormatField},
56 {
"format", kOutputFormatField},
57 {
"hls_name", kHlsNameField},
58 {
"hls_group_id", kHlsGroupIdField},
59 {
"playlist_name", kHlsPlaylistNameField},
62 FieldType GetFieldType(
const std::string& field_name) {
63 for (
size_t idx = 0; idx < arraysize(kFieldNameTypeMappings); ++idx) {
64 if (field_name == kFieldNameTypeMappings[idx].field_name)
65 return kFieldNameTypeMappings[idx].field_type;
72 StreamDescriptor::StreamDescriptor()
73 : bandwidth(0), output_format(CONTAINER_UNKNOWN) {}
75 StreamDescriptor::~StreamDescriptor() {}
77 bool InsertStreamDescriptor(
const std::string& descriptor_string,
78 StreamDescriptorList* descriptor_list) {
79 StreamDescriptor descriptor;
82 base::StringPairs pairs;
83 if (!base::SplitStringIntoKeyValuePairs(descriptor_string,
87 LOG(ERROR) <<
"Invalid stream descriptors name/value pairs.";
90 for (base::StringPairs::const_iterator iter = pairs.begin();
91 iter != pairs.end(); ++iter) {
92 switch (GetFieldType(iter->first)) {
93 case kStreamSelectorField:
94 descriptor.stream_selector = iter->second;
97 descriptor.input = iter->second;
100 descriptor.output = iter->second;
102 case kSegmentTemplateField:
103 descriptor.segment_template = iter->second;
105 case kBandwidthField: {
107 if (!base::StringToUint(iter->second, &bw)) {
108 LOG(ERROR) <<
"Non-numeric bandwidth specified.";
111 descriptor.bandwidth = bw;
114 case kLanguageField: {
116 if (language ==
"und") {
117 LOG(ERROR) <<
"Unknown/invalid language specified: " << iter->second;
120 descriptor.language = language;
123 case kOutputFormatField: {
124 MediaContainerName output_format =
125 DetermineContainerFromFormatName(iter->second);
126 if (output_format == CONTAINER_UNKNOWN) {
127 LOG(ERROR) <<
"Unrecognized output format " << iter->second;
130 descriptor.output_format = output_format;
133 case kHlsNameField: {
134 descriptor.hls_name = iter->second;
137 case kHlsGroupIdField: {
138 descriptor.hls_group_id = iter->second;
141 case kHlsPlaylistNameField: {
142 descriptor.hls_playlist_name = iter->second;
146 LOG(ERROR) <<
"Unknown field in stream descriptor (\"" << iter->first
152 if (descriptor.input.empty()) {
153 LOG(ERROR) <<
"Stream input not specified.";
156 if (!FLAGS_dump_stream_info && descriptor.stream_selector.empty()) {
157 LOG(ERROR) <<
"Stream stream_selector not specified.";
161 if (descriptor.output_format == CONTAINER_UNKNOWN) {
162 const std::string& output_name = descriptor.output.empty()
163 ? descriptor.segment_template
165 if (!output_name.empty()) {
166 descriptor.output_format = DetermineContainerFromFileName(output_name);
167 if (descriptor.output_format == CONTAINER_UNKNOWN) {
168 LOG(ERROR) <<
"Unable to determine output format for file "
175 if (descriptor.output_format == MediaContainerName::CONTAINER_MPEG2TS) {
176 if (descriptor.segment_template.empty()) {
177 LOG(ERROR) <<
"Please specify segment_template. Single file TS output is "
183 if (!descriptor.output.empty()) {
184 LOG(WARNING) <<
"TS output '" << descriptor.output
185 <<
"' ignored. TS muxer does not support initialization "
186 "segment generation.";
190 descriptor.output = descriptor.segment_template;
193 if (!FLAGS_dump_stream_info && descriptor.output.empty()) {
194 LOG(ERROR) <<
"Stream output not specified.";
197 descriptor_list->insert(descriptor);
std::string LanguageToISO_639_2(const std::string &language)