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,
27 kMulticastSourceField,
31 struct FieldNameToTypeMapping {
32 const char* field_name;
36 const FieldNameToTypeMapping kFieldNameTypeMappings[] = {
37 {
"reuse", kReuseField},
38 {
"interface", kInterfaceAddressField},
39 {
"source", kMulticastSourceField},
40 {
"timeout", kTimeoutField},
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;
51 bool StringToAddressAndPort(base::StringPiece addr_and_port,
57 const size_t colon_pos = addr_and_port.find(
':');
58 if (colon_pos == base::StringPiece::npos) {
61 *addr = addr_and_port.substr(0, colon_pos).as_string();
63 if (!base::StringToUint(addr_and_port.substr(colon_pos + 1), &port_value) ||
64 (port_value > 65535)) {
74 base::StringPiece udp_url) {
75 std::unique_ptr<UdpOptions> options(
new UdpOptions);
77 const size_t question_mark_pos = udp_url.find(
'?');
78 base::StringPiece address_str = udp_url.substr(0, question_mark_pos);
80 if (question_mark_pos != base::StringPiece::npos) {
81 base::StringPiece options_str = udp_url.substr(question_mark_pos + 1);
83 base::StringPairs pairs;
84 if (!base::SplitStringIntoKeyValuePairs(options_str,
'=',
'&', &pairs)) {
85 LOG(ERROR) <<
"Invalid udp options name/value pairs " << options_str;
88 for (
const auto& pair : pairs) {
89 switch (GetFieldType(pair.first)) {
92 if (!base::StringToInt(pair.second, &reuse_value)) {
93 LOG(ERROR) <<
"Invalid udp option for reuse field " << pair.second;
96 options->reuse_ = reuse_value > 0;
99 case kInterfaceAddressField:
100 options->interface_address_ = pair.second;
103 if (!base::StringToUint(pair.second, &options->timeout_us_)) {
104 LOG(ERROR) <<
"Invalid udp option for timeout field " 109 case kMulticastSourceField:
110 options->source_address_ = pair.second;
111 options->is_source_specific_multicast_ =
true;
114 LOG(ERROR) <<
"Unknown field in udp options (\"" << pair.first
121 if (!FLAGS_udp_interface_address.empty()) {
122 LOG(WARNING) <<
"--udp_interface_address is deprecated. Consider switching " 123 "to udp options instead, something like " 124 "udp:://ip:port?interface=interface_ip.";
125 options->interface_address_ = FLAGS_udp_interface_address;
128 if (!StringToAddressAndPort(address_str, &options->address_,
130 LOG(ERROR) <<
"Malformed address:port UDP url " << address_str;
static std::unique_ptr< UdpOptions > ParseFromString(base::StringPiece udp_url)
All the methods that are virtual are virtual for mocking.
Options parsed from UDP url string of the form: udp://ip:port[?options].