DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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/mpd/base/language_utils.h"
14 
15 namespace edash_packager {
16 namespace media {
17 
18 namespace {
19 
20 enum FieldType {
21  kUnknownField = 0,
22  kStreamSelectorField,
23  kInputField,
24  kOutputField,
25  kSegmentTemplateField,
26  kBandwidthField,
27  kLanguageField,
28 };
29 
30 struct FieldNameToTypeMapping {
31  const char* field_name;
32  FieldType field_type;
33 };
34 
35 const FieldNameToTypeMapping kFieldNameTypeMappings[] = {
36  { "stream_selector", kStreamSelectorField },
37  { "stream", kStreamSelectorField },
38  { "input", kInputField },
39  { "in", kInputField },
40  { "output", kOutputField },
41  { "out", kOutputField },
42  { "init_segment", kOutputField },
43  { "segment_template", kSegmentTemplateField },
44  { "template", kSegmentTemplateField },
45  { "bandwidth", kBandwidthField },
46  { "bw", kBandwidthField },
47  { "bitrate", kBandwidthField },
48  { "language", kLanguageField },
49  { "lang", kLanguageField },
50 };
51 
52 FieldType GetFieldType(const std::string& field_name) {
53  for (size_t idx = 0; idx < arraysize(kFieldNameTypeMappings); ++idx) {
54  if (field_name == kFieldNameTypeMappings[idx].field_name)
55  return kFieldNameTypeMappings[idx].field_type;
56  }
57  return kUnknownField;
58 }
59 
60 } // anonymous namespace
61 
62 StreamDescriptor::StreamDescriptor() : bandwidth(0) {}
63 
64 StreamDescriptor::~StreamDescriptor() {}
65 
66 bool InsertStreamDescriptor(const std::string& descriptor_string,
67  StreamDescriptorList* descriptor_list) {
68  StreamDescriptor descriptor;
69 
70  // Split descriptor string into name/value pairs.
71  base::StringPairs pairs;
72  if (!base::SplitStringIntoKeyValuePairs(descriptor_string,
73  '=',
74  ',',
75  &pairs)) {
76  LOG(ERROR) << "Invalid stream descriptors name/value pairs.";
77  return false;
78  }
79  for (base::StringPairs::const_iterator iter = pairs.begin();
80  iter != pairs.end(); ++iter) {
81  switch (GetFieldType(iter->first)) {
82  case kStreamSelectorField:
83  descriptor.stream_selector = iter->second;
84  break;
85  case kInputField:
86  descriptor.input = iter->second;
87  break;
88  case kOutputField:
89  descriptor.output = iter->second;
90  break;
91  case kSegmentTemplateField:
92  descriptor.segment_template = iter->second;
93  break;
94  case kBandwidthField: {
95  unsigned bw;
96  if (!base::StringToUint(iter->second, &bw)) {
97  LOG(ERROR) << "Non-numeric bandwidth specified.";
98  return false;
99  }
100  descriptor.bandwidth = bw;
101  break;
102  }
103  case kLanguageField: {
104  std::string language = LanguageToISO_639_2(iter->second);
105  if (language == "und") {
106  LOG(ERROR) << "Unknown/invalid language specified: " << iter->second;
107  return false;
108  }
109  DCHECK_EQ(3u, language.size());
110  descriptor.language = language;
111  break;
112  }
113  default:
114  LOG(ERROR) << "Unknown field in stream descriptor (\"" << iter->first
115  << "\").";
116  return false;
117  break;
118  }
119  }
120  // Validate and insert the descriptor
121  if (descriptor.input.empty()) {
122  LOG(ERROR) << "Stream input not specified.";
123  return false;
124  }
125  if (!FLAGS_dump_stream_info && descriptor.stream_selector.empty()) {
126  LOG(ERROR) << "Stream stream_selector not specified.";
127  return false;
128  }
129  if (!FLAGS_dump_stream_info && descriptor.output.empty()) {
130  LOG(ERROR) << "Stream output not specified.";
131  return false;
132  }
133  descriptor_list->insert(descriptor);
134  return true;
135 }
136 
137 } // namespace media
138 } // namespace edash_packager
std::string LanguageToISO_639_2(const std::string &language)