OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/cast/net/udp_transport.h" | 5 #include "media/cast/net/udp_transport.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 #include "net/base/rand_callback.h" | 18 #include "net/base/rand_callback.h" |
19 | 19 |
20 namespace media { | 20 namespace media { |
21 namespace cast { | 21 namespace cast { |
22 | 22 |
23 namespace { | 23 namespace { |
24 const int kMaxPacketSize = 1500; | 24 |
| 25 const char kOptionDscp[] = "DSCP"; |
| 26 #if defined(OS_WIN) |
| 27 const char kOptionDisableNonBlockingIO[] = "disable_non_blocking_io"; |
| 28 #endif |
| 29 const char kOptionSendBufferMinSize[] = "send_buffer_min_size"; |
| 30 const char kOptionPacerMaxBurstSize[] = "pacer_max_burst_size"; |
25 | 31 |
26 bool IsEmpty(const net::IPEndPoint& addr) { | 32 bool IsEmpty(const net::IPEndPoint& addr) { |
27 net::IPAddressNumber empty_addr(addr.address().size()); | 33 net::IPAddressNumber empty_addr(addr.address().size()); |
28 return std::equal(empty_addr.begin(), empty_addr.end(), | 34 return std::equal(empty_addr.begin(), empty_addr.end(), |
29 addr.address().bytes().begin()) && | 35 addr.address().bytes().begin()) && |
30 !addr.port(); | 36 !addr.port(); |
31 } | 37 } |
32 | 38 |
| 39 int LookupOptionWithDefault(const base::DictionaryValue& options, |
| 40 const std::string& path, |
| 41 int default_value) { |
| 42 int ret; |
| 43 if (options.GetInteger(path, &ret)) { |
| 44 return ret; |
| 45 } else { |
| 46 return default_value; |
| 47 } |
| 48 } |
| 49 |
| 50 int32_t GetTransportSendBufferSize(const base::DictionaryValue& options) { |
| 51 // Socket send buffer size needs to be at least greater than one burst |
| 52 // size. |
| 53 int32_t max_burst_size = |
| 54 LookupOptionWithDefault(options, kOptionPacerMaxBurstSize, |
| 55 media::cast::kMaxBurstSize) * |
| 56 media::cast::kMaxIpPacketSize; |
| 57 int32_t min_send_buffer_size = |
| 58 LookupOptionWithDefault(options, kOptionSendBufferMinSize, 0); |
| 59 return std::max(max_burst_size, min_send_buffer_size); |
| 60 } |
| 61 |
33 } // namespace | 62 } // namespace |
34 | 63 |
35 UdpTransport::UdpTransport( | 64 UdpTransport::UdpTransport( |
36 net::NetLog* net_log, | 65 net::NetLog* net_log, |
37 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread_proxy, | 66 const scoped_refptr<base::SingleThreadTaskRunner>& io_thread_proxy, |
38 const net::IPEndPoint& local_end_point, | 67 const net::IPEndPoint& local_end_point, |
39 const net::IPEndPoint& remote_end_point, | 68 const net::IPEndPoint& remote_end_point, |
40 int32_t send_buffer_size, | |
41 const CastTransportStatusCallback& status_callback) | 69 const CastTransportStatusCallback& status_callback) |
42 : io_thread_proxy_(io_thread_proxy), | 70 : io_thread_proxy_(io_thread_proxy), |
43 local_addr_(local_end_point), | 71 local_addr_(local_end_point), |
44 remote_addr_(remote_end_point), | 72 remote_addr_(remote_end_point), |
45 udp_socket_(new net::UDPSocket(net::DatagramSocket::DEFAULT_BIND, | 73 udp_socket_(new net::UDPSocket(net::DatagramSocket::DEFAULT_BIND, |
46 net::RandIntCallback(), | 74 net::RandIntCallback(), |
47 net_log, | 75 net_log, |
48 net::NetLog::Source())), | 76 net::NetLog::Source())), |
49 send_pending_(false), | 77 send_pending_(false), |
50 receive_pending_(false), | 78 receive_pending_(false), |
51 client_connected_(false), | 79 client_connected_(false), |
52 next_dscp_value_(net::DSCP_NO_CHANGE), | 80 next_dscp_value_(net::DSCP_NO_CHANGE), |
53 send_buffer_size_(send_buffer_size), | 81 send_buffer_size_(media::cast::kMaxBurstSize * |
| 82 media::cast::kMaxIpPacketSize), |
54 status_callback_(status_callback), | 83 status_callback_(status_callback), |
55 bytes_sent_(0), | 84 bytes_sent_(0), |
56 weak_factory_(this) { | 85 weak_factory_(this) { |
57 DCHECK(!IsEmpty(local_end_point) || !IsEmpty(remote_end_point)); | 86 DCHECK(!IsEmpty(local_end_point) || !IsEmpty(remote_end_point)); |
58 } | 87 } |
59 | 88 |
60 UdpTransport::~UdpTransport() {} | 89 UdpTransport::~UdpTransport() {} |
61 | 90 |
62 void UdpTransport::StartReceiving( | 91 void UdpTransport::StartReceiving( |
63 const PacketReceiverCallbackWithStatus& packet_receiver) { | 92 const PacketReceiverCallbackWithStatus& packet_receiver) { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 if (packet_receiver_.is_null()) | 167 if (packet_receiver_.is_null()) |
139 return; | 168 return; |
140 if (!udp_socket_) | 169 if (!udp_socket_) |
141 return; | 170 return; |
142 | 171 |
143 // Loop while UdpSocket is delivering data synchronously. When it responds | 172 // Loop while UdpSocket is delivering data synchronously. When it responds |
144 // with a "pending" status, break and expect this method to be called back in | 173 // with a "pending" status, break and expect this method to be called back in |
145 // the future when a packet is ready. | 174 // the future when a packet is ready. |
146 while (true) { | 175 while (true) { |
147 if (length_or_status == net::ERR_IO_PENDING) { | 176 if (length_or_status == net::ERR_IO_PENDING) { |
148 next_packet_.reset(new Packet(kMaxPacketSize)); | 177 next_packet_.reset(new Packet(media::cast::kMaxIpPacketSize)); |
149 recv_buf_ = new net::WrappedIOBuffer( | 178 recv_buf_ = new net::WrappedIOBuffer( |
150 reinterpret_cast<char*>(&next_packet_->front())); | 179 reinterpret_cast<char*>(&next_packet_->front())); |
151 length_or_status = | 180 length_or_status = udp_socket_->RecvFrom( |
152 udp_socket_->RecvFrom(recv_buf_.get(), | 181 recv_buf_.get(), media::cast::kMaxIpPacketSize, &recv_addr_, |
153 kMaxPacketSize, | 182 base::Bind(&UdpTransport::ReceiveNextPacket, |
154 &recv_addr_, | 183 weak_factory_.GetWeakPtr())); |
155 base::Bind(&UdpTransport::ReceiveNextPacket, | |
156 weak_factory_.GetWeakPtr())); | |
157 if (length_or_status == net::ERR_IO_PENDING) { | 184 if (length_or_status == net::ERR_IO_PENDING) { |
158 receive_pending_ = true; | 185 receive_pending_ = true; |
159 return; | 186 return; |
160 } | 187 } |
161 } | 188 } |
162 | 189 |
163 // Note: At this point, either a packet is ready or an error has occurred. | 190 // Note: At this point, either a packet is ready or an error has occurred. |
164 if (length_or_status < 0) { | 191 if (length_or_status < 0) { |
165 VLOG(1) << "Failed to receive packet: Status code is " | 192 VLOG(1) << "Failed to receive packet: Status code is " |
166 << length_or_status; | 193 << length_or_status; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 if (result < 0) { | 295 if (result < 0) { |
269 VLOG(1) << "Failed to send packet: " << result << "."; | 296 VLOG(1) << "Failed to send packet: " << result << "."; |
270 } | 297 } |
271 ScheduleReceiveNextPacket(); | 298 ScheduleReceiveNextPacket(); |
272 | 299 |
273 if (!cb.is_null()) { | 300 if (!cb.is_null()) { |
274 cb.Run(); | 301 cb.Run(); |
275 } | 302 } |
276 } | 303 } |
277 | 304 |
| 305 void UdpTransport::SetUdpOptions(const base::DictionaryValue& options) { |
| 306 SetSendBufferSize(GetTransportSendBufferSize(options)); |
| 307 if (options.HasKey(kOptionDscp)) { |
| 308 // The default DSCP value for cast is AF41. Which gives it a higher |
| 309 // priority over other traffic. |
| 310 SetDscp(net::DSCP_AF41); |
| 311 } |
| 312 #if defined(OS_WIN) |
| 313 if (!options.HasKey(kOptionDisableNonBlockingIO)) { |
| 314 UseNonBlockingIO(); |
| 315 } |
| 316 #endif |
| 317 } |
| 318 |
| 319 void UdpTransport::SetSendBufferSize(int32_t send_buffer_size) { |
| 320 send_buffer_size_ = send_buffer_size; |
| 321 } |
| 322 |
278 } // namespace cast | 323 } // namespace cast |
279 } // namespace media | 324 } // namespace media |
OLD | NEW |