From 631cc4039b6236c9d86b23fa29e592a421f82d6c Mon Sep 17 00:00:00 2001 From: "hclam@chromium.org" Date: Thu, 13 Feb 2014 14:51:04 +0000 Subject: Cast: UdpTransport to use UdpSocket instead of UdpServerSocket UdpTransport is used by cast sender and receiver. When used in the cast sender a bind cannot be done. So UdpTransport should use UdpSocket and perform bind or connect based on the value of remote and local address. Tested this with cast_sender_app and cast_receiver_app. Review URL: https://codereview.chromium.org/161913002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@251001 0039d316-1c4b-4281-b951-d872f2087c98 --- media/cast/test/sender.cc | 26 +++++++++------------ media/cast/transport/transport/udp_transport.cc | 30 +++++++++++++++++++------ media/cast/transport/transport/udp_transport.h | 7 +++--- 3 files changed, 37 insertions(+), 26 deletions(-) (limited to 'media') diff --git a/media/cast/test/sender.cc b/media/cast/test/sender.cc index bd52746..603b2de 100644 --- a/media/cast/test/sender.cc +++ b/media/cast/test/sender.cc @@ -26,9 +26,8 @@ namespace media { namespace cast { // Settings chosen to match default receiver settings. -#define DEFAULT_SEND_PORT "2344" -#define DEFAULT_RECEIVE_PORT "2346" -#define DEFAULT_SEND_IP "127.0.0.1" +#define DEFAULT_RECEIVER_PORT "2344" +#define DEFAULT_RECEIVER_IP "127.0.0.1" #define DEFAULT_READ_FROM_FILE "0" #define DEFAULT_AUDIO_SENDER_SSRC "1" #define DEFAULT_AUDIO_RECEIVER_SSRC "2" @@ -57,18 +56,14 @@ static const int kFrameTimerMs = 33; void OwnThatAudioBus(scoped_ptr audio_bus) {} } // namespace -void GetPorts(int* tx_port, int* rx_port) { - test::InputBuilder tx_input( - "Enter send port.", DEFAULT_SEND_PORT, 1, INT_MAX); - *tx_port = tx_input.GetIntInput(); - - test::InputBuilder rx_input( - "Enter receive port.", DEFAULT_RECEIVE_PORT, 1, INT_MAX); - *rx_port = rx_input.GetIntInput(); +void GetPort(int* port) { + test::InputBuilder input( + "Enter receiver port.", DEFAULT_RECEIVER_PORT, 1, INT_MAX); + *port = input.GetIntInput(); } std::string GetIpAddress(const std::string display_text) { - test::InputBuilder input(display_text, DEFAULT_SEND_IP, INT_MIN, INT_MAX); + test::InputBuilder input(display_text, DEFAULT_RECEIVER_IP, INT_MIN, INT_MAX); std::string ip_address = input.GetStringInput(); // Verify correct form: while (std::count(ip_address.begin(), ip_address.end(), '.') != 3) { @@ -327,12 +322,11 @@ int main(int argc, char** argv) { scoped_ptr clock(new base::DefaultTickClock()); base::MessageLoopForIO io_message_loop; - int remote_port, local_port; - media::cast::GetPorts(&remote_port, &local_port); + int remote_port; + media::cast::GetPort(&remote_port); std::string remote_ip_address = media::cast::GetIpAddress("Enter receiver IP."); - std::string local_ip_address = media::cast::GetIpAddress("Enter local IP."); media::cast::AudioSenderConfig audio_config = media::cast::GetAudioSenderConfig(); @@ -342,7 +336,7 @@ int main(int argc, char** argv) { // Setting up transport config. media::cast::transport::CastTransportConfig config; config.receiver_endpoint = CreateUDPAddress(remote_ip_address, remote_port); - config.local_endpoint = CreateUDPAddress(local_ip_address, local_port); + config.local_endpoint = CreateUDPAddress("0.0.0.0", 0); config.audio_ssrc = audio_config.sender_ssrc; config.video_ssrc = video_config.sender_ssrc; config.audio_rtp_config = audio_config.rtp_config; diff --git a/media/cast/transport/transport/udp_transport.cc b/media/cast/transport/transport/udp_transport.cc index fd86edc..d16a117 100644 --- a/media/cast/transport/transport/udp_transport.cc +++ b/media/cast/transport/transport/udp_transport.cc @@ -27,7 +27,8 @@ const int kMaxPacketSize = 1500; bool IsEmpty(const net::IPEndPoint& addr) { net::IPAddressNumber empty_addr(addr.address().size()); return std::equal( - empty_addr.begin(), empty_addr.end(), addr.address().begin()); + empty_addr.begin(), empty_addr.end(), addr.address().begin()) && + !addr.port(); } bool IsEqual(const net::IPEndPoint& addr1, const net::IPEndPoint& addr2) { @@ -45,11 +46,16 @@ UdpTransport::UdpTransport( : io_thread_proxy_(io_thread_proxy), local_addr_(local_end_point), remote_addr_(remote_end_point), - udp_socket_(new net::UDPServerSocket(NULL, net::NetLog::Source())), + udp_socket_(new net::UDPSocket(net::DatagramSocket::DEFAULT_BIND, + net::RandIntCallback(), + NULL, + net::NetLog::Source())), send_pending_(false), recv_buf_(new net::IOBuffer(kMaxPacketSize)), status_callback_(status_callback), - weak_factory_(this) {} + weak_factory_(this) { + DCHECK(!IsEmpty(local_end_point) || !IsEmpty(remote_end_point)); +} UdpTransport::~UdpTransport() {} @@ -60,10 +66,20 @@ void UdpTransport::StartReceiving( packet_receiver_ = packet_receiver; udp_socket_->AllowAddressReuse(); udp_socket_->SetMulticastLoopbackMode(true); - if (udp_socket_->Listen(local_addr_) < 0) { - status_callback_.Run(TRANSPORT_SOCKET_ERROR); - LOG(ERROR) << "Failed to bind local address"; - return; + if (!IsEmpty(local_addr_)) { + if (udp_socket_->Bind(local_addr_) < 0) { + status_callback_.Run(TRANSPORT_SOCKET_ERROR); + LOG(ERROR) << "Failed to bind local address."; + return; + } + } else if (!IsEmpty(remote_addr_)) { + if (udp_socket_->Connect(remote_addr_) < 0) { + status_callback_.Run(TRANSPORT_SOCKET_ERROR); + LOG(ERROR) << "Failed to connect to remote address."; + return; + } + } else { + NOTREACHED() << "Either local or remote address has to be defined."; } ReceiveOnePacket(); } diff --git a/media/cast/transport/transport/udp_transport.h b/media/cast/transport/transport/udp_transport.h index c4aa6e3..ebea539 100644 --- a/media/cast/transport/transport/udp_transport.h +++ b/media/cast/transport/transport/udp_transport.h @@ -12,7 +12,7 @@ #include "media/cast/transport/cast_transport_config.h" #include "media/cast/transport/cast_transport_sender.h" #include "net/base/ip_endpoint.h" -#include "net/udp/udp_server_socket.h" +#include "net/udp/udp_socket.h" namespace net { class IOBuffer; @@ -29,7 +29,8 @@ class UdpTransport : public PacketSender { // Construct a UDP transport. // All methods must be called on |io_thread_proxy|. // |local_end_point| specifies the address and port to bind and listen - // to incoming packets. + // to incoming packets. If the value is 0.0.0.0:0 then a bind is not + // performed. // |remote_end_point| specifies the address and port to send packets // to. If the value is 0.0.0.0:0 the the end point is set to the source // address of the first packet received. @@ -54,7 +55,7 @@ class UdpTransport : public PacketSender { scoped_refptr io_thread_proxy_; net::IPEndPoint local_addr_; net::IPEndPoint remote_addr_; - scoped_ptr udp_socket_; + scoped_ptr udp_socket_; bool send_pending_; scoped_refptr recv_buf_; net::IPEndPoint recv_addr_; -- cgit v1.1