Home | History | Annotate | Download | only in net
      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_NET_UDP_TRANSPORT_H_
      6 #define MEDIA_CAST_NET_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/net/cast_transport_config.h"
     13 #include "media/cast/net/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 
     27 // This class implements UDP transport mechanism for Cast.
     28 class UdpTransport : public PacketSender {
     29  public:
     30   // Construct a UDP transport.
     31   // All methods must be called on |io_thread_proxy|.
     32   // |local_end_point| specifies the address and port to bind and listen
     33   // to incoming packets. If the value is 0.0.0.0:0 then a bind is not
     34   // performed.
     35   // |remote_end_point| specifies the address and port to send packets
     36   // to. If the value is 0.0.0.0:0 the the end point is set to the source
     37   // address of the first packet received.
     38   UdpTransport(
     39       net::NetLog* net_log,
     40       const scoped_refptr<base::SingleThreadTaskRunner>& io_thread_proxy,
     41       const net::IPEndPoint& local_end_point,
     42       const net::IPEndPoint& remote_end_point,
     43       const CastTransportStatusCallback& status_callback);
     44   virtual ~UdpTransport();
     45 
     46   // Start receiving packets. Packets are submitted to |packet_receiver|.
     47   void StartReceiving(const PacketReceiverCallback& packet_receiver);
     48 
     49   // Set a new DSCP value to the socket. The value will be set right before
     50   // the next send.
     51   void SetDscp(net::DiffServCodePoint dscp);
     52 
     53   // PacketSender implementations.
     54   virtual bool SendPacket(PacketRef packet,
     55                           const base::Closure& cb) OVERRIDE;
     56   virtual int64 GetBytesSent() 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   int bytes_sent_;
     87 
     88   // NOTE: Weak pointers must be invalidated before all other member variables.
     89   base::WeakPtrFactory<UdpTransport> weak_factory_;
     90 
     91   DISALLOW_COPY_AND_ASSIGN(UdpTransport);
     92 };
     93 
     94 }  // namespace cast
     95 }  // namespace media
     96 
     97 #endif  // MEDIA_CAST_NET_UDP_TRANSPORT_H_
     98