Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / cast / net / udp_transport.h
blob119aaef565e9bfd2cacc58409e019563e7c4697e
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_NET_UDP_TRANSPORT_H_
6 #define MEDIA_CAST_NET_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/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"
18 namespace net {
19 class IOBuffer;
20 class IPEndPoint;
21 class NetLog;
22 } // namespace net
24 namespace media {
25 namespace cast {
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 // |send_buffer_size| specifies the size of the socket send buffer.
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 int32 send_buffer_size,
45 const CastTransportStatusCallback& status_callback);
46 ~UdpTransport() final;
48 // Start receiving packets. Packets are submitted to |packet_receiver|.
49 void StartReceiving(const PacketReceiverCallbackWithStatus& packet_receiver);
50 void StopReceiving();
52 // Set a new DSCP value to the socket. The value will be set right before
53 // the next send.
54 void SetDscp(net::DiffServCodePoint dscp);
56 #if defined(OS_WIN)
57 // Switch to use non-blocking IO. Must be called before StartReceiving().
58 void UseNonBlockingIO();
59 #endif
61 // PacketSender implementations.
62 bool SendPacket(PacketRef packet, const base::Closure& cb) final;
63 int64 GetBytesSent() final;
65 private:
66 // Requests and processes packets from |udp_socket_|. This method is called
67 // once with |length_or_status| set to net::ERR_IO_PENDING to start receiving
68 // packets. Thereafter, it is called with some other value as the callback
69 // response from UdpSocket::RecvFrom().
70 void ReceiveNextPacket(int length_or_status);
72 // Schedule packet receiving, if needed.
73 void ScheduleReceiveNextPacket();
75 void OnSent(const scoped_refptr<net::IOBuffer>& buf,
76 PacketRef packet,
77 const base::Closure& cb,
78 int result);
80 const scoped_refptr<base::SingleThreadTaskRunner> io_thread_proxy_;
81 const net::IPEndPoint local_addr_;
82 net::IPEndPoint remote_addr_;
83 scoped_ptr<net::UDPSocket> udp_socket_;
84 bool send_pending_;
85 bool receive_pending_;
86 bool client_connected_;
87 net::DiffServCodePoint next_dscp_value_;
88 scoped_ptr<Packet> next_packet_;
89 scoped_refptr<net::WrappedIOBuffer> recv_buf_;
90 net::IPEndPoint recv_addr_;
91 PacketReceiverCallbackWithStatus packet_receiver_;
92 int32 send_buffer_size_;
93 const CastTransportStatusCallback status_callback_;
94 int bytes_sent_;
96 // NOTE: Weak pointers must be invalidated before all other member variables.
97 base::WeakPtrFactory<UdpTransport> weak_factory_;
99 DISALLOW_COPY_AND_ASSIGN(UdpTransport);
102 } // namespace cast
103 } // namespace media
105 #endif // MEDIA_CAST_NET_UDP_TRANSPORT_H_