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/media/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  kTrickPlayRateField,
34 };
35 
36 struct FieldNameToTypeMapping {
37  const char* field_name;
38  FieldType field_type;
39 };
40 
41 const FieldNameToTypeMapping kFieldNameTypeMappings[] = {
42  {"stream_selector", kStreamSelectorField},
43  {"stream", kStreamSelectorField},
44  {"input", kInputField},
45  {"in", 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},
62 };
63 
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;
68  }
69  return kUnknownField;
70 }
71 
72 } // anonymous namespace
73 
74 StreamDescriptor::StreamDescriptor()
75  : bandwidth(0), output_format(CONTAINER_UNKNOWN) {}
76 
77 StreamDescriptor::~StreamDescriptor() {}
78 
79 bool InsertStreamDescriptor(const std::string& descriptor_string,
80  StreamDescriptorList* descriptor_list) {
81  StreamDescriptor descriptor;
82 
83  // Split descriptor string into name/value pairs.
84  base::StringPairs pairs;
85  if (!base::SplitStringIntoKeyValuePairs(descriptor_string,
86  '=',
87  ',',
88  &pairs)) {
89  LOG(ERROR) << "Invalid stream descriptors name/value pairs.";
90  return false;
91  }
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;
97  break;
98  case kInputField:
99  descriptor.input = iter->second;
100  break;
101  case kOutputField:
102  descriptor.output = iter->second;
103  break;
104  case kSegmentTemplateField:
105  descriptor.segment_template = iter->second;
106  break;
107  case kBandwidthField: {
108  unsigned bw;
109  if (!base::StringToUint(iter->second, &bw)) {
110  LOG(ERROR) << "Non-numeric bandwidth specified.";
111  return false;
112  }
113  descriptor.bandwidth = bw;
114  break;
115  }
116  case kLanguageField: {
117  std::string language = LanguageToISO_639_2(iter->second);
118  if (language == "und") {
119  LOG(ERROR) << "Unknown/invalid language specified: " << iter->second;
120  return false;
121  }
122  descriptor.language = language;
123  break;
124  }
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;
130  return false;
131  }
132  descriptor.output_format = output_format;
133  break;
134  }
135  case kHlsNameField: {
136  descriptor.hls_name = iter->second;
137  break;
138  }
139  case kHlsGroupIdField: {
140  descriptor.hls_group_id = iter->second;
141  break;
142  }
143  case kHlsPlaylistNameField: {
144  descriptor.hls_playlist_name = iter->second;
145  break;
146  }
147  default:
148  LOG(ERROR) << "Unknown field in stream descriptor (\"" << iter->first
149  << "\").";
150  return false;
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  }
191 
192  // For TS output, segment template is sufficient, and does not require an
193  // output entry.
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.";
200  return false;
201  }
202  descriptor_list->insert(descriptor);
203  return true;
204 }
205 
206 } // namespace media
207 } // namespace shaka
std::string LanguageToISO_639_2(const std::string &language)