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 
76 StreamDescriptor::~StreamDescriptor() {}
77 
78 bool InsertStreamDescriptor(const std::string& descriptor_string,
79  StreamDescriptorList* descriptor_list) {
80  StreamDescriptor descriptor;
81 
82  // Split descriptor string into name/value pairs.
83  base::StringPairs pairs;
84  if (!base::SplitStringIntoKeyValuePairs(descriptor_string,
85  '=',
86  ',',
87  &pairs)) {
88  LOG(ERROR) << "Invalid stream descriptors name/value pairs.";
89  return false;
90  }
91  for (base::StringPairs::const_iterator iter = pairs.begin();
92  iter != pairs.end(); ++iter) {
93  switch (GetFieldType(iter->first)) {
94  case kStreamSelectorField:
95  descriptor.stream_selector = iter->second;
96  break;
97  case kInputField:
98  descriptor.input = iter->second;
99  break;
100  case kOutputField:
101  descriptor.output = iter->second;
102  break;
103  case kSegmentTemplateField:
104  descriptor.segment_template = iter->second;
105  break;
106  case kBandwidthField: {
107  unsigned bw;
108  if (!base::StringToUint(iter->second, &bw)) {
109  LOG(ERROR) << "Non-numeric bandwidth specified.";
110  return false;
111  }
112  descriptor.bandwidth = bw;
113  break;
114  }
115  case kLanguageField: {
116  std::string language = LanguageToISO_639_2(iter->second);
117  if (language == "und") {
118  LOG(ERROR) << "Unknown/invalid language specified: " << iter->second;
119  return false;
120  }
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  case kTrickPlayRateField: {
147  unsigned rate;
148  if (!base::StringToUint(iter->second, &rate)) {
149  LOG(ERROR) << "Non-numeric trick play rate " << iter->second
150  << " specified.";
151  return false;
152  }
153  if (rate == 0) {
154  LOG(ERROR) << "Stream trick_play_rate should be > 0.";
155  return false;
156  }
157  descriptor.trick_play_rate = rate;
158  break;
159  }
160  default:
161  LOG(ERROR) << "Unknown field in stream descriptor (\"" << iter->first
162  << "\").";
163  return false;
164  }
165  }
166  // Validate and insert the descriptor
167  if (descriptor.input.empty()) {
168  LOG(ERROR) << "Stream input not specified.";
169  return false;
170  }
171  if (!FLAGS_dump_stream_info && descriptor.stream_selector.empty()) {
172  LOG(ERROR) << "Stream stream_selector not specified.";
173  return false;
174  }
175 
176  if (descriptor.output_format == CONTAINER_UNKNOWN) {
177  const std::string& output_name = descriptor.output.empty()
178  ? descriptor.segment_template
179  : descriptor.output;
180  if (!output_name.empty()) {
181  descriptor.output_format = DetermineContainerFromFileName(output_name);
182  if (descriptor.output_format == CONTAINER_UNKNOWN) {
183  LOG(ERROR) << "Unable to determine output format for file "
184  << output_name;
185  return false;
186  }
187  }
188  }
189 
190  if (descriptor.output_format == MediaContainerName::CONTAINER_MPEG2TS) {
191  if (descriptor.segment_template.empty()) {
192  LOG(ERROR) << "Please specify segment_template. Single file TS output is "
193  "not supported.";
194  return false;
195  }
196  // Note that MPEG2 TS doesn't need a separate initialization segment, so
197  // output field is not needed.
198  if (!descriptor.output.empty()) {
199  LOG(WARNING) << "TS output '" << descriptor.output
200  << "' ignored. TS muxer does not support initialization "
201  "segment generation.";
202  }
203  }
204 
205  // For TS output, segment template is sufficient, and does not require an
206  // output entry.
207  const bool output_specified =
208  !descriptor.output.empty() ||
209  (descriptor.output_format == CONTAINER_MPEG2TS &&
210  !descriptor.segment_template.empty());
211  if (!FLAGS_dump_stream_info && !output_specified) {
212  LOG(ERROR) << "Stream output not specified.";
213  return false;
214  }
215  descriptor_list->insert(descriptor);
216  return true;
217 }
218 
219 } // namespace media
220 } // namespace shaka
std::string LanguageToISO_639_2(const std::string &language)