| 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 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 status_callback_(status_callback), | 81 status_callback_(status_callback), |
| 82 bytes_sent_(0), | 82 bytes_sent_(0), |
| 83 weak_factory_(this) { | 83 weak_factory_(this) { |
| 84 DCHECK(!IsEmpty(local_end_point) || !IsEmpty(remote_end_point)); | 84 DCHECK(!IsEmpty(local_end_point) || !IsEmpty(remote_end_point)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 UdpTransport::~UdpTransport() {} | 87 UdpTransport::~UdpTransport() {} |
| 88 | 88 |
| 89 void UdpTransport::StartReceiving( | 89 void UdpTransport::StartReceiving( |
| 90 const PacketReceiverCallbackWithStatus& packet_receiver) { | 90 const PacketReceiverCallbackWithStatus& packet_receiver) { |
| 91 DCHECK(io_thread_proxy_->RunsTasksOnCurrentThread()); | 91 DCHECK(io_thread_proxy_->RunsTasksInCurrentSequence()); |
| 92 | 92 |
| 93 if (!udp_socket_) { | 93 if (!udp_socket_) { |
| 94 status_callback_.Run(TRANSPORT_SOCKET_ERROR); | 94 status_callback_.Run(TRANSPORT_SOCKET_ERROR); |
| 95 return; | 95 return; |
| 96 } | 96 } |
| 97 | 97 |
| 98 packet_receiver_ = packet_receiver; | 98 packet_receiver_ = packet_receiver; |
| 99 udp_socket_->SetMulticastLoopbackMode(true); | 99 udp_socket_->SetMulticastLoopbackMode(true); |
| 100 if (!IsEmpty(local_addr_)) { | 100 if (!IsEmpty(local_addr_)) { |
| 101 if (udp_socket_->Open(local_addr_.GetFamily()) < 0 || | 101 if (udp_socket_->Open(local_addr_.GetFamily()) < 0 || |
| (...skipping 20 matching lines...) Expand all Loading... |
| 122 NOTREACHED() << "Either local or remote address has to be defined."; | 122 NOTREACHED() << "Either local or remote address has to be defined."; |
| 123 } | 123 } |
| 124 if (udp_socket_->SetSendBufferSize(send_buffer_size_) != net::OK) { | 124 if (udp_socket_->SetSendBufferSize(send_buffer_size_) != net::OK) { |
| 125 LOG(WARNING) << "Failed to set socket send buffer size."; | 125 LOG(WARNING) << "Failed to set socket send buffer size."; |
| 126 } | 126 } |
| 127 | 127 |
| 128 ScheduleReceiveNextPacket(); | 128 ScheduleReceiveNextPacket(); |
| 129 } | 129 } |
| 130 | 130 |
| 131 void UdpTransport::StopReceiving() { | 131 void UdpTransport::StopReceiving() { |
| 132 DCHECK(io_thread_proxy_->RunsTasksOnCurrentThread()); | 132 DCHECK(io_thread_proxy_->RunsTasksInCurrentSequence()); |
| 133 packet_receiver_ = PacketReceiverCallbackWithStatus(); | 133 packet_receiver_ = PacketReceiverCallbackWithStatus(); |
| 134 } | 134 } |
| 135 | 135 |
| 136 | 136 |
| 137 void UdpTransport::SetDscp(net::DiffServCodePoint dscp) { | 137 void UdpTransport::SetDscp(net::DiffServCodePoint dscp) { |
| 138 DCHECK(io_thread_proxy_->RunsTasksOnCurrentThread()); | 138 DCHECK(io_thread_proxy_->RunsTasksInCurrentSequence()); |
| 139 next_dscp_value_ = dscp; | 139 next_dscp_value_ = dscp; |
| 140 } | 140 } |
| 141 | 141 |
| 142 #if defined(OS_WIN) | 142 #if defined(OS_WIN) |
| 143 void UdpTransport::UseNonBlockingIO() { | 143 void UdpTransport::UseNonBlockingIO() { |
| 144 DCHECK(io_thread_proxy_->RunsTasksOnCurrentThread()); | 144 DCHECK(io_thread_proxy_->RunsTasksInCurrentSequence()); |
| 145 if (!udp_socket_) | 145 if (!udp_socket_) |
| 146 return; | 146 return; |
| 147 udp_socket_->UseNonBlockingIO(); | 147 udp_socket_->UseNonBlockingIO(); |
| 148 } | 148 } |
| 149 #endif | 149 #endif |
| 150 | 150 |
| 151 void UdpTransport::ScheduleReceiveNextPacket() { | 151 void UdpTransport::ScheduleReceiveNextPacket() { |
| 152 DCHECK(io_thread_proxy_->RunsTasksOnCurrentThread()); | 152 DCHECK(io_thread_proxy_->RunsTasksInCurrentSequence()); |
| 153 if (!packet_receiver_.is_null() && !receive_pending_) { | 153 if (!packet_receiver_.is_null() && !receive_pending_) { |
| 154 receive_pending_ = true; | 154 receive_pending_ = true; |
| 155 io_thread_proxy_->PostTask(FROM_HERE, | 155 io_thread_proxy_->PostTask(FROM_HERE, |
| 156 base::Bind(&UdpTransport::ReceiveNextPacket, | 156 base::Bind(&UdpTransport::ReceiveNextPacket, |
| 157 weak_factory_.GetWeakPtr(), | 157 weak_factory_.GetWeakPtr(), |
| 158 net::ERR_IO_PENDING)); | 158 net::ERR_IO_PENDING)); |
| 159 } | 159 } |
| 160 } | 160 } |
| 161 | 161 |
| 162 void UdpTransport::ReceiveNextPacket(int length_or_status) { | 162 void UdpTransport::ReceiveNextPacket(int length_or_status) { |
| 163 DCHECK(io_thread_proxy_->RunsTasksOnCurrentThread()); | 163 DCHECK(io_thread_proxy_->RunsTasksInCurrentSequence()); |
| 164 | 164 |
| 165 if (packet_receiver_.is_null()) | 165 if (packet_receiver_.is_null()) |
| 166 return; | 166 return; |
| 167 if (!udp_socket_) | 167 if (!udp_socket_) |
| 168 return; | 168 return; |
| 169 | 169 |
| 170 // Loop while UdpSocket is delivering data synchronously. When it responds | 170 // Loop while UdpSocket is delivering data synchronously. When it responds |
| 171 // with a "pending" status, break and expect this method to be called back in | 171 // with a "pending" status, break and expect this method to be called back in |
| 172 // the future when a packet is ready. | 172 // the future when a packet is ready. |
| 173 while (true) { | 173 while (true) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 << recv_addr_.ToString() << "."; | 212 << recv_addr_.ToString() << "."; |
| 213 } else { | 213 } else { |
| 214 next_packet_->resize(length_or_status); | 214 next_packet_->resize(length_or_status); |
| 215 packet_receiver_.Run(std::move(next_packet_)); | 215 packet_receiver_.Run(std::move(next_packet_)); |
| 216 } | 216 } |
| 217 length_or_status = net::ERR_IO_PENDING; | 217 length_or_status = net::ERR_IO_PENDING; |
| 218 } | 218 } |
| 219 } | 219 } |
| 220 | 220 |
| 221 bool UdpTransport::SendPacket(PacketRef packet, const base::Closure& cb) { | 221 bool UdpTransport::SendPacket(PacketRef packet, const base::Closure& cb) { |
| 222 DCHECK(io_thread_proxy_->RunsTasksOnCurrentThread()); | 222 DCHECK(io_thread_proxy_->RunsTasksInCurrentSequence()); |
| 223 if (!udp_socket_) | 223 if (!udp_socket_) |
| 224 return true; | 224 return true; |
| 225 | 225 |
| 226 // Increase byte count no matter the packet was sent or dropped. | 226 // Increase byte count no matter the packet was sent or dropped. |
| 227 bytes_sent_ += packet->data.size(); | 227 bytes_sent_ += packet->data.size(); |
| 228 | 228 |
| 229 DCHECK(!send_pending_); | 229 DCHECK(!send_pending_); |
| 230 if (send_pending_) { | 230 if (send_pending_) { |
| 231 VLOG(1) << "Cannot send because of pending IO."; | 231 VLOG(1) << "Cannot send because of pending IO."; |
| 232 return true; | 232 return true; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 } | 280 } |
| 281 | 281 |
| 282 int64_t UdpTransport::GetBytesSent() { | 282 int64_t UdpTransport::GetBytesSent() { |
| 283 return bytes_sent_; | 283 return bytes_sent_; |
| 284 } | 284 } |
| 285 | 285 |
| 286 void UdpTransport::OnSent(const scoped_refptr<net::IOBuffer>& buf, | 286 void UdpTransport::OnSent(const scoped_refptr<net::IOBuffer>& buf, |
| 287 PacketRef packet, | 287 PacketRef packet, |
| 288 const base::Closure& cb, | 288 const base::Closure& cb, |
| 289 int result) { | 289 int result) { |
| 290 DCHECK(io_thread_proxy_->RunsTasksOnCurrentThread()); | 290 DCHECK(io_thread_proxy_->RunsTasksInCurrentSequence()); |
| 291 | 291 |
| 292 send_pending_ = false; | 292 send_pending_ = false; |
| 293 if (result < 0) { | 293 if (result < 0) { |
| 294 VLOG(1) << "Failed to send packet: " << result << "."; | 294 VLOG(1) << "Failed to send packet: " << result << "."; |
| 295 } | 295 } |
| 296 ScheduleReceiveNextPacket(); | 296 ScheduleReceiveNextPacket(); |
| 297 | 297 |
| 298 if (!cb.is_null()) { | 298 if (!cb.is_null()) { |
| 299 cb.Run(); | 299 cb.Run(); |
| 300 } | 300 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 313 } | 313 } |
| 314 #endif | 314 #endif |
| 315 } | 315 } |
| 316 | 316 |
| 317 void UdpTransport::SetSendBufferSize(int32_t send_buffer_size) { | 317 void UdpTransport::SetSendBufferSize(int32_t send_buffer_size) { |
| 318 send_buffer_size_ = send_buffer_size; | 318 send_buffer_size_ = send_buffer_size; |
| 319 } | 319 } |
| 320 | 320 |
| 321 } // namespace cast | 321 } // namespace cast |
| 322 } // namespace media | 322 } // namespace media |
| OLD | NEW |