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  << descriptor_string;
88  return base::nullopt;
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 base::nullopt;
110  }
111  descriptor.bandwidth = bw;
112  break;
113  }
114  case kLanguageField: {
115  descriptor.language = iter->second;
116  break;
117  }
118  case kOutputFormatField: {
119  descriptor.output_format = iter->second;
120  break;
121  }
122  case kHlsNameField: {
123  descriptor.hls_name = iter->second;
124  break;
125  }
126  case kHlsGroupIdField: {
127  descriptor.hls_group_id = iter->second;
128  break;
129  }
130  case kHlsPlaylistNameField: {
131  descriptor.hls_playlist_name = iter->second;
132  break;
133  }
134  case kHlsIframePlaylistNameField: {
135  descriptor.hls_iframe_playlist_name = iter->second;
136  break;
137  }
138  case kTrickPlayFactorField: {
139  unsigned factor;
140  if (!base::StringToUint(iter->second, &factor)) {
141  LOG(ERROR) << "Non-numeric trick play factor " << iter->second
142  << " specified.";
143  return base::nullopt;
144  }
145  if (factor == 0) {
146  LOG(ERROR) << "Stream trick_play_factor should be > 0.";
147  return base::nullopt;
148  }
149  descriptor.trick_play_factor = factor;
150  break;
151  }
152  case kSkipEncryptionField: {
153  unsigned skip_encryption_value;
154  if (!base::StringToUint(iter->second, &skip_encryption_value)) {
155  LOG(ERROR) << "Non-numeric option for skip encryption field "
156  "specified (" << iter->second << ").";
157  return base::nullopt;
158  }
159  if (skip_encryption_value > 1) {
160  LOG(ERROR) << "skip_encryption should be either 0 or 1.";
161  return base::nullopt;
162  }
163 
164  descriptor.skip_encryption = skip_encryption_value > 0;
165  break;
166  }
167  case kDrmStreamLabelField: {
168  descriptor.drm_label = iter->second;
169  break;
170  }
171  default:
172  LOG(ERROR) << "Unknown field in stream descriptor (\"" << iter->first
173  << "\").";
174  return base::nullopt;
175  }
176  }
177  return descriptor;
178 }
179 
180 } // namespace shaka
std::string stream_selector
Definition: packager.h:79
Defines a single input/output stream.
Definition: packager.h:73
std::string input
Input/source media file path or network stream URL. Required.
Definition: packager.h:75
std::string hls_playlist_name
Definition: packager.h:118
std::string hls_name
Definition: packager.h:112
base::Optional< StreamDescriptor > ParseStreamDescriptor(const std::string &descriptor_string)
std::string segment_template
Specifies segment template. Can be empty.
Definition: packager.h:85
std::string output_format
Definition: packager.h:89
All the methods that are virtual are virtual for mocking.
std::string drm_label
Definition: packager.h:97
uint32_t trick_play_factor
Definition: packager.h:101
std::string output
Definition: packager.h:83
std::string hls_iframe_playlist_name
Definition: packager.h:121
std::string language
Definition: packager.h:108
std::string hls_group_id
Definition: packager.h:115