Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / cast / transport / transport / udp_transport.h
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/udp/udp_socket.h"
16
17 namespace net {
18 class IOBuffer;
19 class IPEndPoint;
20 }  // namespace net
21
22 namespace media {
23 namespace cast {
24 namespace transport {
25
26 // This class implements UDP transport mechanism for Cast.
27 class UdpTransport : public PacketSender {
28  public:
29   // Construct a UDP transport.
30   // All methods must be called on |io_thread_proxy|.
31   // |local_end_point| specifies the address and port to bind and listen
32   // to incoming packets. If the value is 0.0.0.0:0 then a bind is not
33   // performed.
34   // |remote_end_point| specifies the address and port to send packets
35   // to. If the value is 0.0.0.0:0 the the end point is set to the source
36   // address of the first packet received.
37   UdpTransport(
38       const scoped_refptr<base::SingleThreadTaskRunner>& io_thread_proxy,
39       const net::IPEndPoint& local_end_point,
40       const net::IPEndPoint& remote_end_point,
41       const CastTransportStatusCallback& status_callback);
42   virtual ~UdpTransport();
43
44   // Start receiving packets. Packets are submitted to |packet_receiver|.
45   void StartReceiving(const PacketReceiverCallback& packet_receiver);
46
47   // PacketSender implementations.
48   virtual bool SendPacket(const Packet& packet) OVERRIDE;
49
50  private:
51   void ReceiveOnePacket();
52   void OnReceived(int result);
53   void OnSent(const scoped_refptr<net::IOBuffer>& buf, int result);
54
55   scoped_refptr<base::SingleThreadTaskRunner> io_thread_proxy_;
56   net::IPEndPoint local_addr_;
57   net::IPEndPoint remote_addr_;
58   scoped_ptr<net::UDPSocket> udp_socket_;
59   bool send_pending_;
60   scoped_refptr<net::IOBuffer> recv_buf_;
61   net::IPEndPoint recv_addr_;
62   PacketReceiverCallback packet_receiver_;
63   const CastTransportStatusCallback& status_callback_;
64   base::WeakPtrFactory<UdpTransport> weak_factory_;
65
66   DISALLOW_COPY_AND_ASSIGN(UdpTransport);
67 };
68
69 }  // namespace transport
70 }  // namespace cast
71 }  // namespace media
72
73 #endif  // MEDIA_CAST_TRANSPORT_TRANSPORT_UDP_TRANSPORT_H_