DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
udp_options.cc
1 // Copyright 2016 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/media/file/udp_options.h"
8 
9 #include <gflags/gflags.h>
10 
11 #include "packager/base/strings/string_number_conversions.h"
12 #include "packager/base/strings/string_split.h"
13 
14 DEFINE_string(udp_interface_address,
15  "",
16  "IP address of the interface over which to receive UDP unicast"
17  " or multicast streams");
18 
19 namespace shaka {
20 namespace media {
21 
22 namespace {
23 
24 enum FieldType {
25  kUnknownField = 0,
26  kReuseField,
27  kInterfaceAddressField,
28  kTimeoutField,
29 };
30 
31 struct FieldNameToTypeMapping {
32  const char* field_name;
33  FieldType field_type;
34 };
35 
36 const FieldNameToTypeMapping kFieldNameTypeMappings[] = {
37  {"reuse", kReuseField},
38  {"interface", kInterfaceAddressField},
39  {"source", kInterfaceAddressField},
40  {"timeout", kTimeoutField},
41 };
42 
43 FieldType GetFieldType(const std::string& field_name) {
44  for (size_t idx = 0; idx < arraysize(kFieldNameTypeMappings); ++idx) {
45  if (field_name == kFieldNameTypeMappings[idx].field_name)
46  return kFieldNameTypeMappings[idx].field_type;
47  }
48  return kUnknownField;
49 }
50 
51 bool StringToAddressAndPort(base::StringPiece addr_and_port,
52  std::string* addr,
53  uint16_t* port) {
54  DCHECK(addr);
55  DCHECK(port);
56 
57  const size_t colon_pos = addr_and_port.find(':');
58  if (colon_pos == base::StringPiece::npos) {
59  return false;
60  }
61  *addr = addr_and_port.substr(0, colon_pos).as_string();
62  unsigned port_value;
63  if (!base::StringToUint(addr_and_port.substr(colon_pos + 1), &port_value) ||
64  (port_value > 65535)) {
65  return false;
66  }
67  *port = port_value;
68  return true;
69 }
70 
71 } // namespace
72 
73 std::unique_ptr<UdpOptions> UdpOptions::ParseFromString(
74  base::StringPiece udp_url) {
75  std::unique_ptr<UdpOptions> options(new UdpOptions);
76 
77  const size_t question_mark_pos = udp_url.find('?');
78  base::StringPiece address_str = udp_url.substr(0, question_mark_pos);
79 
80  if (question_mark_pos != base::StringPiece::npos) {
81  base::StringPiece options_str = udp_url.substr(question_mark_pos + 1);
82 
83  base::StringPairs pairs;
84  if (!base::SplitStringIntoKeyValuePairs(options_str, '=', '&', &pairs)) {
85  LOG(ERROR) << "Invalid udp options name/value pairs " << options_str;
86  return nullptr;
87  }
88  for (const auto& pair : pairs) {
89  switch (GetFieldType(pair.first)) {
90  case kReuseField: {
91  int reuse_value = 0;
92  if (!base::StringToInt(pair.second, &reuse_value)) {
93  LOG(ERROR) << "Invalid udp option for reuse field " << pair.second;
94  return nullptr;
95  }
96  options->reuse_ = reuse_value > 0;
97  break;
98  }
99  case kInterfaceAddressField:
100  options->interface_address_ = pair.second;
101  break;
102  case kTimeoutField:
103  if (!base::StringToUint(pair.second, &options->timeout_us_)) {
104  LOG(ERROR) << "Invalid udp option for timeout field " << pair.second;
105  return nullptr;
106  }
107  break;
108  default:
109  LOG(ERROR) << "Unknown field in udp options (\"" << pair.first
110  << "\").";
111  return nullptr;
112  }
113  }
114  }
115 
116  if (!FLAGS_udp_interface_address.empty()) {
117  LOG(WARNING) << "--udp_interface_address is deprecated. Consider switching "
118  "to udp options instead, something like "
119  "udp:://ip:port?interface=interface_ip.";
120  options->interface_address_ = FLAGS_udp_interface_address;
121  }
122 
123  if (!StringToAddressAndPort(address_str, &options->address_,
124  &options->port_)) {
125  LOG(ERROR) << "Malformed address:port UDP url " << address_str;
126  return nullptr;
127  }
128  return options;
129 }
130 
131 } // namespace media
132 } // namespace shaka
Options parsed from UDP url string of the form: udp://ip:port[?options].
Definition: udp_options.h:16
static std::unique_ptr< UdpOptions > ParseFromString(base::StringPiece udp_url)
Definition: udp_options.cc:73