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.
5 #ifndef MEDIA_CAST_TRANSPORT_TRANSPORT_UDP_TRANSPORT_H_
6 #define MEDIA_CAST_TRANSPORT_TRANSPORT_UDP_TRANSPORT_H_
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"
28 // This class implements UDP transport mechanism for Cast.
29 class UdpTransport : public PacketSender {
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
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.
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();
47 // Start receiving packets. Packets are submitted to |packet_receiver|.
48 void StartReceiving(const PacketReceiverCallback& packet_receiver);
50 // Set a new DSCP value to the socket. The value will be set right before
52 void SetDscp(net::DiffServCodePoint dscp);
54 // PacketSender implementations.
55 virtual bool SendPacket(PacketRef packet,
56 const base::Closure& cb) OVERRIDE;
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);
65 // Schedule packet receiving, if needed.
66 void ScheduleReceiveNextPacket();
68 void OnSent(const scoped_refptr<net::IOBuffer>& buf,
70 const base::Closure& cb,
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_;
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_;
87 // NOTE: Weak pointers must be invalidated before all other member variables.
88 base::WeakPtrFactory<UdpTransport> weak_factory_;
90 DISALLOW_COPY_AND_ASSIGN(UdpTransport);
93 } // namespace transport
97 #endif // MEDIA_CAST_TRANSPORT_TRANSPORT_UDP_TRANSPORT_H_