1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MEDIA_CAST_TRANSPORT_TRANSPORT_UDP_TRANSPORT_H_ 6 #define MEDIA_CAST_TRANSPORT_TRANSPORT_UDP_TRANSPORT_H_ 7 8 #include "base/memory/ref_counted.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/weak_ptr.h" 11 #include "media/cast/cast_environment.h" 12 #include "media/cast/transport/cast_transport_config.h" 13 #include "media/cast/transport/cast_transport_sender.h" 14 #include "net/base/ip_endpoint.h" 15 #include "net/base/net_util.h" 16 #include "net/udp/udp_socket.h" 17 18 namespace net { 19 class IOBuffer; 20 class IPEndPoint; 21 class NetLog; 22 } // namespace net 23 24 namespace media { 25 namespace cast { 26 namespace transport { 27 28 // This class implements UDP transport mechanism for Cast. 29 class UdpTransport : public PacketSender { 30 public: 31 // Construct a UDP transport. 32 // All methods must be called on |io_thread_proxy|. 33 // |local_end_point| specifies the address and port to bind and listen 34 // to incoming packets. If the value is 0.0.0.0:0 then a bind is not 35 // performed. 36 // |remote_end_point| specifies the address and port to send packets 37 // to. If the value is 0.0.0.0:0 the the end point is set to the source 38 // address of the first packet received. 39 UdpTransport( 40 net::NetLog* net_log, 41 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread_proxy, 42 const net::IPEndPoint& local_end_point, 43 const net::IPEndPoint& remote_end_point, 44 const CastTransportStatusCallback& status_callback); 45 virtual ~UdpTransport(); 46 47 // Start receiving packets. Packets are submitted to |packet_receiver|. 48 void StartReceiving(const PacketReceiverCallback& packet_receiver); 49 50 // Set a new DSCP value to the socket. The value will be set right before 51 // the next send. 52 void SetDscp(net::DiffServCodePoint dscp); 53 54 // PacketSender implementations. 55 virtual bool SendPacket(PacketRef packet, 56 const base::Closure& cb) OVERRIDE; 57 58 private: 59 // Requests and processes packets from |udp_socket_|. This method is called 60 // once with |length_or_status| set to net::ERR_IO_PENDING to start receiving 61 // packets. Thereafter, it is called with some other value as the callback 62 // response from UdpSocket::RecvFrom(). 63 void ReceiveNextPacket(int length_or_status); 64 65 // Schedule packet receiving, if needed. 66 void ScheduleReceiveNextPacket(); 67 68 void OnSent(const scoped_refptr<net::IOBuffer>& buf, 69 PacketRef packet, 70 const base::Closure& cb, 71 int result); 72 73 const scoped_refptr<base::SingleThreadTaskRunner> io_thread_proxy_; 74 const net::IPEndPoint local_addr_; 75 net::IPEndPoint remote_addr_; 76 const scoped_ptr<net::UDPSocket> udp_socket_; 77 bool send_pending_; 78 bool receive_pending_; 79 bool client_connected_; 80 net::DiffServCodePoint next_dscp_value_; 81 scoped_ptr<Packet> next_packet_; 82 scoped_refptr<net::WrappedIOBuffer> recv_buf_; 83 net::IPEndPoint recv_addr_; 84 PacketReceiverCallback packet_receiver_; 85 const CastTransportStatusCallback status_callback_; 86 87 // NOTE: Weak pointers must be invalidated before all other member variables. 88 base::WeakPtrFactory<UdpTransport> weak_factory_; 89 90 DISALLOW_COPY_AND_ASSIGN(UdpTransport); 91 }; 92 93 } // namespace transport 94 } // namespace cast 95 } // namespace media 96 97 #endif // MEDIA_CAST_TRANSPORT_TRANSPORT_UDP_TRANSPORT_H_ 98