7 #include "packager/file/udp_options.h"
9 #include <gflags/gflags.h>
11 #include "packager/base/strings/string_number_conversions.h"
12 #include "packager/base/strings/string_split.h"
14 DEFINE_string(udp_interface_address,
16 "IP address of the interface over which to receive UDP unicast"
17 " or multicast streams");
26 kInterfaceAddressField,
30 struct FieldNameToTypeMapping {
31 const char* field_name;
35 const FieldNameToTypeMapping kFieldNameTypeMappings[] = {
36 {
"reuse", kReuseField},
37 {
"interface", kInterfaceAddressField},
38 {
"source", kInterfaceAddressField},
39 {
"timeout", kTimeoutField},
42 FieldType GetFieldType(
const std::string& field_name) {
43 for (
size_t idx = 0; idx < arraysize(kFieldNameTypeMappings); ++idx) {
44 if (field_name == kFieldNameTypeMappings[idx].field_name)
45 return kFieldNameTypeMappings[idx].field_type;
50 bool StringToAddressAndPort(base::StringPiece addr_and_port,
56 const size_t colon_pos = addr_and_port.find(
':');
57 if (colon_pos == base::StringPiece::npos) {
60 *addr = addr_and_port.substr(0, colon_pos).as_string();
62 if (!base::StringToUint(addr_and_port.substr(colon_pos + 1), &port_value) ||
63 (port_value > 65535)) {
73 base::StringPiece udp_url) {
74 std::unique_ptr<UdpOptions> options(
new UdpOptions);
76 const size_t question_mark_pos = udp_url.find(
'?');
77 base::StringPiece address_str = udp_url.substr(0, question_mark_pos);
79 if (question_mark_pos != base::StringPiece::npos) {
80 base::StringPiece options_str = udp_url.substr(question_mark_pos + 1);
82 base::StringPairs pairs;
83 if (!base::SplitStringIntoKeyValuePairs(options_str,
'=',
'&', &pairs)) {
84 LOG(ERROR) <<
"Invalid udp options name/value pairs " << options_str;
87 for (
const auto& pair : pairs) {
88 switch (GetFieldType(pair.first)) {
91 if (!base::StringToInt(pair.second, &reuse_value)) {
92 LOG(ERROR) <<
"Invalid udp option for reuse field " << pair.second;
95 options->reuse_ = reuse_value > 0;
98 case kInterfaceAddressField:
99 options->interface_address_ = pair.second;
102 if (!base::StringToUint(pair.second, &options->timeout_us_)) {
103 LOG(ERROR) <<
"Invalid udp option for timeout field "
109 LOG(ERROR) <<
"Unknown field in udp options (\"" << pair.first
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;
123 if (!StringToAddressAndPort(address_str, &options->address_,
125 LOG(ERROR) <<
"Malformed address:port UDP url " << address_str;
static std::unique_ptr< UdpOptions > ParseFromString(base::StringPiece udp_url)
Options parsed from UDP url string of the form: udp://ip:port[?options].