DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
stream_descriptor.cc
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/app/stream_descriptor.h"
8 
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/mpd/base/language_utils.h"
15 
16 namespace shaka {
17 namespace media {
18 
19 namespace {
20 
21 enum FieldType {
22  kUnknownField = 0,
23  kStreamSelectorField,
24  kInputField,
25  kOutputField,
26  kSegmentTemplateField,
27  kBandwidthField,
28  kLanguageField,
29  kOutputFormatField,
30  kHlsNameField,
31  kHlsGroupIdField,
32  kHlsPlaylistNameField,
33 };
34 
35 struct FieldNameToTypeMapping {
36  const char* field_name;
37  FieldType field_type;
38 };
39 
40 const FieldNameToTypeMapping kFieldNameTypeMappings[] = {
41  {"stream_selector", kStreamSelectorField},
42  {"stream", kStreamSelectorField},
43  {"input", kInputField},
44  {"in", 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},
60 };
61 
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;
66  }
67  return kUnknownField;
68 }
69 
70 } // anonymous namespace
71 
72 StreamDescriptor::StreamDescriptor()
73  : bandwidth(0), output_format(CONTAINER_UNKNOWN) {}
74 
75 StreamDescriptor::~StreamDescriptor() {}
76 
77 bool InsertStreamDescriptor(const std::string& descriptor_string,
78  StreamDescriptorList* descriptor_list) {
79  StreamDescriptor descriptor;
80 
81  // Split descriptor string into name/value pairs.
82  base::StringPairs pairs;
83  if (!base::SplitStringIntoKeyValuePairs(descriptor_string,
84  '=',
85  ',',
86  &pairs)) {
87  LOG(ERROR) << "Invalid stream descriptors name/value pairs.";
88  return false;
89  }
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;
95  break;
96  case kInputField:
97  descriptor.input = iter->second;
98  break;
99  case kOutputField:
100  descriptor.output = iter->second;
101  break;
102  case kSegmentTemplateField:
103  descriptor.segment_template = iter->second;
104  break;
105  case kBandwidthField: {
106  unsigned bw;
107  if (!base::StringToUint(iter->second, &bw)) {
108  LOG(ERROR) << "Non-numeric bandwidth specified.";
109  return false;
110  }
111  descriptor.bandwidth = bw;
112  break;
113  }
114  case kLanguageField: {
115  std::string language = LanguageToISO_639_2(iter->second);
116  if (language == "und") {
117  LOG(ERROR) << "Unknown/invalid language specified: " << iter->second;
118  return false;
119  }
120  DCHECK_EQ(3u, language.size());
121  descriptor.language = language;
122  break;
123  }
124  case kOutputFormatField: {
125  MediaContainerName output_format =
126  DetermineContainerFromFormatName(iter->second);
127  if (output_format == CONTAINER_UNKNOWN) {
128  LOG(ERROR) << "Unrecognized output format " << iter->second;
129  return false;
130  }
131  descriptor.output_format = output_format;
132  break;
133  }
134  case kHlsNameField: {
135  descriptor.hls_name = iter->second;
136  break;
137  }
138  case kHlsGroupIdField: {
139  descriptor.hls_group_id = iter->second;
140  break;
141  }
142  case kHlsPlaylistNameField: {
143  descriptor.hls_playlist_name = iter->second;
144  break;
145  }
146  default:
147  LOG(ERROR) << "Unknown field in stream descriptor (\"" << iter->first
148  << "\").";
149  return false;
150  break;
151  }
152  }
153  // Validate and insert the descriptor
154  if (descriptor.input.empty()) {
155  LOG(ERROR) << "Stream input not specified.";
156  return false;
157  }
158  if (!FLAGS_dump_stream_info && descriptor.stream_selector.empty()) {
159  LOG(ERROR) << "Stream stream_selector not specified.";
160  return false;
161  }
162 
163  if (descriptor.output_format == CONTAINER_UNKNOWN) {
164  const std::string& output_name = descriptor.output.empty()
165  ? descriptor.segment_template
166  : descriptor.output;
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 "
171  << output_name;
172  return false;
173  }
174  }
175  }
176 
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 "
180  "not supported.";
181  return false;
182  }
183  // Note that MPEG2 TS doesn't need a separate initialization segment, so
184  // output field is not needed.
185  if (!descriptor.output.empty()) {
186  LOG(WARNING) << "TS output '" << descriptor.output
187  << "' ignored. TS muxer does not support initialization "
188  "segment generation.";
189  }
190  // For convenience, set descriptor.output to descriptor.segment_template. It
191  // is only used for flag checking in variuos places.
192  descriptor.output = descriptor.segment_template;
193  }
194 
195  if (!FLAGS_dump_stream_info && descriptor.output.empty()) {
196  LOG(ERROR) << "Stream output not specified.";
197  return false;
198  }
199  descriptor_list->insert(descriptor);
200  return true;
201 }
202 
203 } // namespace media
204 } // namespace shaka
std::string LanguageToISO_639_2(const std::string &language)