Shaka Packager SDK
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/base/logging.h"
10 #include "packager/base/strings/string_number_conversions.h"
11 #include "packager/base/strings/string_split.h"
12 
13 namespace shaka {
14 
15 namespace {
16 
17 enum FieldType {
18  kUnknownField = 0,
19  kStreamSelectorField,
20  kInputField,
21  kOutputField,
22  kSegmentTemplateField,
23  kBandwidthField,
24  kLanguageField,
25  kOutputFormatField,
26  kHlsNameField,
27  kHlsGroupIdField,
28  kHlsPlaylistNameField,
29  kHlsIframePlaylistNameField,
30  kTrickPlayFactorField,
31  kSkipEncryptionField,
32  kDrmStreamLabelField,
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  {"iframe_playlist_name", kHlsIframePlaylistNameField},
61  {"trick_play_factor", kTrickPlayFactorField},
62  {"tpf", kTrickPlayFactorField},
63  {"skip_encryption", kSkipEncryptionField},
64  {"drm_stream_label", kDrmStreamLabelField},
65  {"drm_label", kDrmStreamLabelField},
66 };
67 
68 FieldType GetFieldType(const std::string& field_name) {
69  for (size_t idx = 0; idx < arraysize(kFieldNameTypeMappings); ++idx) {
70  if (field_name == kFieldNameTypeMappings[idx].field_name)
71  return kFieldNameTypeMappings[idx].field_type;
72  }
73  return kUnknownField;
74 }
75 
76 } // anonymous namespace
77 
78 base::Optional<StreamDescriptor> ParseStreamDescriptor(
79  const std::string& descriptor_string) {
80  StreamDescriptor descriptor;
81 
82  // Split descriptor string into name/value pairs.
83  base::StringPairs pairs;
84  if (!base::SplitStringIntoKeyValuePairs(descriptor_string, '=', ',',
85  &pairs)) {
86  LOG(ERROR) << "Invalid stream descriptors name/value pairs.";
87  return base::nullopt;
88  }
89  for (base::StringPairs::const_iterator iter = pairs.begin();
90  iter != pairs.end(); ++iter) {
91  switch (GetFieldType(iter->first)) {
92  case kStreamSelectorField:
93  descriptor.stream_selector = iter->second;
94  break;
95  case kInputField:
96  descriptor.input = iter->second;
97  break;
98  case kOutputField:
99  descriptor.output = iter->second;
100  break;
101  case kSegmentTemplateField:
102  descriptor.segment_template = iter->second;
103  break;
104  case kBandwidthField: {
105  unsigned bw;
106  if (!base::StringToUint(iter->second, &bw)) {
107  LOG(ERROR) << "Non-numeric bandwidth specified.";
108  return base::nullopt;
109  }
110  descriptor.bandwidth = bw;
111  break;
112  }
113  case kLanguageField: {
114  descriptor.language = iter->second;
115  break;
116  }
117  case kOutputFormatField: {
118  descriptor.output_format = iter->second;
119  break;
120  }
121  case kHlsNameField: {
122  descriptor.hls_name = iter->second;
123  break;
124  }
125  case kHlsGroupIdField: {
126  descriptor.hls_group_id = iter->second;
127  break;
128  }
129  case kHlsPlaylistNameField: {
130  descriptor.hls_playlist_name = iter->second;
131  break;
132  }
133  case kHlsIframePlaylistNameField: {
134  descriptor.hls_iframe_playlist_name = iter->second;
135  break;
136  }
137  case kTrickPlayFactorField: {
138  unsigned factor;
139  if (!base::StringToUint(iter->second, &factor)) {
140  LOG(ERROR) << "Non-numeric trick play factor " << iter->second
141  << " specified.";
142  return base::nullopt;
143  }
144  if (factor == 0) {
145  LOG(ERROR) << "Stream trick_play_factor should be > 0.";
146  return base::nullopt;
147  }
148  descriptor.trick_play_factor = factor;
149  break;
150  }
151  case kSkipEncryptionField: {
152  unsigned skip_encryption_value;
153  if (!base::StringToUint(iter->second, &skip_encryption_value)) {
154  LOG(ERROR) << "Non-numeric option for skip encryption field "
155  "specified (" << iter->second << ").";
156  return base::nullopt;
157  }
158  if (skip_encryption_value > 1) {
159  LOG(ERROR) << "skip_encryption should be either 0 or 1.";
160  return base::nullopt;
161  }
162 
163  descriptor.skip_encryption = skip_encryption_value > 0;
164  break;
165  }
166  case kDrmStreamLabelField: {
167  descriptor.drm_label = iter->second;
168  break;
169  }
170  default:
171  LOG(ERROR) << "Unknown field in stream descriptor (\"" << iter->first
172  << "\").";
173  return base::nullopt;
174  }
175  }
176  return descriptor;
177 }
178 
179 } // namespace shaka
std::string stream_selector
Definition: packager.h:75
Defines a single input/output stream.
Definition: packager.h:69
std::string input
Input/source media file path or network stream URL. Required.
Definition: packager.h:71
std::string hls_playlist_name
Definition: packager.h:114
std::string hls_name
Definition: packager.h:108
base::Optional< StreamDescriptor > ParseStreamDescriptor(const std::string &descriptor_string)
std::string segment_template
Specifies segment template. Can be empty.
Definition: packager.h:81
std::string output_format
Definition: packager.h:85
All the methods that are virtual are virtual for mocking.
std::string drm_label
Definition: packager.h:93
uint32_t trick_play_factor
Definition: packager.h:97
std::string output
Definition: packager.h:79
std::string hls_iframe_playlist_name
Definition: packager.h:117
std::string language
Definition: packager.h:104
std::string hls_group_id
Definition: packager.h:111