7 #include "packager/media/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");
27 kInterfaceAddressField,
31 struct FieldNameToTypeMapping {
32 const char* field_name;
36 const FieldNameToTypeMapping kFieldNameTypeMappings[] = {
37 {
"reuse", kReuseField},
38 {
"interface", kInterfaceAddressField},
39 {
"source", kInterfaceAddressField},
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 " << pair.second;
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;