Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: webrtc/p2p/rawudp/rawudptransportchannel.cc

Issue 2377883003: First step in providing a UdpTransportChannel. (Closed)
Patch Set: Remove SetRemoteAddrAndPort() method. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/p2p/rawudp/rawudptransportchannel.h"
12
13 #include <string>
14
15 #include "webrtc/base/asyncudpsocket.h"
16 #include "webrtc/base/logging.h"
17 #include "webrtc/base/physicalsocketserver.h"
18 #include "webrtc/base/socketaddress.h"
19 #include "webrtc/base/thread.h"
20 #include "webrtc/base/thread_checker.h"
21
22 namespace cricket {
23
24 RawUdpTransportChannel::RawUdpTransportChannel(
25 const std::string& transport_name,
26 int component)
27 : RawUdpTransportChannel(transport_name,
28 component,
29 rtc::Thread::Current()->socketserver()) {
30 RTC_DCHECK_RUN_ON(&network_thread_checker_);
Taylor Brandstetter 2016/10/07 18:24:03 The constructor is where the thread checker is ini
johan 2016/10/11 13:36:09 You have a point here.
31 }
32
33 RawUdpTransportChannel::RawUdpTransportChannel(
34 const std::string& transport_name,
35 int component,
36 rtc::SocketServer* socket_server)
37 : TransportChannelImpl(transport_name, component),
38 tch_state_(STATE_INIT),
39 gathering_state_(kIceGatheringNew) {
Taylor Brandstetter 2016/10/07 18:24:03 Can initialize these to default values in the head
johan 2016/10/11 13:36:09 Done.
40 socket_server_ = socket_server;
Taylor Brandstetter 2016/10/07 18:24:03 Can set socket_server_ in initializer list.
johan 2016/10/11 13:36:09 Acknowledged.
41 RTC_DCHECK_RUN_ON(&network_thread_checker_);
42 }
43
44 RawUdpTransportChannel::~RawUdpTransportChannel() {
45 RTC_DCHECK_RUN_ON(&network_thread_checker_);
46 }
47
48 void RawUdpTransportChannel::OnSocketReadPacket(
49 rtc::AsyncPacketSocket* socket,
50 const char* data,
51 size_t len,
52 const rtc::SocketAddress& remote_addr,
53 const rtc::PacketTime& packet_time,
54 bool is_rtcp) {
55 // No thread_checker in high frequency network function.
56 // Serialisation of processing RTP and RTCP socket is implicit done by event
57 // loop.
pthatcher1 2016/10/07 17:36:36 I don't think this is comment is needed, nor is an
Taylor Brandstetter 2016/10/07 18:24:03 I see the value of these checks. But since they're
johan 2016/10/11 13:36:09 FYI: similar checks are present in some methods of
58 SignalReadPacket(this, data, len, packet_time, 0);
59 }
60
61 void RawUdpTransportChannel::OnRtpSocketReadPacket(
62 rtc::AsyncPacketSocket* socket,
63 const char* data,
64 size_t len,
65 const rtc::SocketAddress& remote_addr,
66 const rtc::PacketTime& packet_time) {
67 // No thread_checker in high frequency network function.
68 // TODO(johan) check, if remote_addr is valid and legit.
69 OnSocketReadPacket(socket, data, len, remote_addr, packet_time,
70 false /*rtcp*/);
71 }
72
73 void RawUdpTransportChannel::OnRtcpSocketReadPacket(
74 rtc::AsyncPacketSocket* socket,
75 const char* data,
76 size_t len,
77 const rtc::SocketAddress& remote_addr,
78 const rtc::PacketTime& packet_time) {
79 // No thread_checker in high frequency network function.
80 // TODO(johan) check, if remote_addr is valid and legit.
81 OnSocketReadPacket(socket, data, len, remote_addr, packet_time,
82 true /*rtcp*/);
pthatcher1 2016/10/07 17:36:36 Please remove these and just have OnSocketReadPack
83 }
84
85 int RawUdpTransportChannel::SendPacket(const char* data,
86 size_t len,
87 const rtc::PacketOptions& options,
88 int flags) {
89 // No thread_checker in high frequency network function.
90 int32_t is_rtcp = 0;
91 // TODO(johan): add logic to send rtcp packets over rtcp socket.
92 int result = -1;
93 if (!is_rtcp) {
94 result = rtp_socket_->SendTo((const void*)data, len, remote_addr_, options);
95 }
pthatcher1 2016/10/07 17:36:36 Again, you don't need any of that. Just do socket
96 LOG(LS_VERBOSE) << "SendPacket() " << result;
97 return result;
98 }
99
100 bool RawUdpTransportChannel::TryAllocateSockets() {
101 RTC_DCHECK_RUN_ON(&network_thread_checker_);
102 static constexpr uint16_t kMaxTries = 100;
103 static constexpr uint16_t kMinPortNumber = 2000;
104 // TODO(johan) provide configuration option for kMinPortNumber.
105 rtc::SocketAddress rtp_socket_addr("0.0.0.0", 0);
106 rtc::SocketAddress rtcp_socket_addr("0.0.0.0", 1);
107 // TODO(johan) allocate sockets only once, and loop over
108 // rtc::AsyncSocketAdapter::bind().
109 for (uint16_t count = 0; count < kMaxTries; ++count) {
pthatcher1 2016/10/07 17:36:36 It seems like you should use a rtc::PacketSocketFa
johan 2016/10/11 13:36:09 rtc::PacketSocketFactory seems to be the right thi
110 uint16_t rtpport = kMinPortNumber + (2 * count);
111 rtp_socket_addr.SetPort(rtpport);
112 rtp_socket_.reset(
113 rtc::AsyncUDPSocket::Create(socket_server_, rtp_socket_addr));
114 if (!rtp_socket_) {
115 continue;
116 }
117 rtcp_socket_addr.SetPort(rtpport + 1);
pthatcher1 2016/10/07 17:36:36 How important is this? Cause it's pretty obnoxiou
johan 2016/10/11 13:36:09 Hard requirement. RFC 3550 Section 11 describes "s
118 rtcp_socket_.reset(
119 rtc::AsyncUDPSocket::Create(socket_server_, rtcp_socket_addr));
120 if (rtp_socket_ && rtcp_socket_) {
121 LOG(INFO) << "Allocated Rtp socket with local port " << rtpport;
122 rtp_socket_->SignalReadPacket.connect(
123 this, &RawUdpTransportChannel::OnRtpSocketReadPacket);
124 rtcp_socket_->SignalReadPacket.connect(
125 this, &RawUdpTransportChannel::OnRtcpSocketReadPacket);
126 local_candidate_.set_address(rtp_socket_addr);
127 return true;
128 }
129 }
130 rtp_socket_.reset();
131 rtcp_socket_.reset();
132 return false;
133 }
134
135 void RawUdpTransportChannel::MaybeStartGathering() {
pthatcher1 2016/10/07 17:36:36 A better name for this would be Start(), which goe
johan 2016/10/11 13:36:09 Renaming depends on PacketTransportInterface.
136 RTC_DCHECK_RUN_ON(&network_thread_checker_);
137 if (gathering_state_ != kIceGatheringNew) {
138 LOG(INFO) << "candidates gathering already done, early return";
139 return;
140 }
141 SetGatheringState(kIceGatheringGathering);
142 TryAllocateSockets();
Taylor Brandstetter 2016/10/07 18:24:03 Why does this return a bool if it's not used?
johan 2016/10/11 13:36:09 Should be used, will fix it.
143 SignalCandidateGathered(this, local_candidate_);
144 SetGatheringState(kIceGatheringComplete);
145 }
146
147 void RawUdpTransportChannel::UpdateWritableState() {
148 RTC_DCHECK_RUN_ON(&network_thread_checker_);
149 bool writable = true;
150 if (!rtp_socket_) {
151 writable = false;
152 LOG(INFO) << "rtp_socket_ is false";
153 }
154 if (!remote_addr_.IsComplete()) {
155 writable = false;
156 LOG(INFO) << "remote_addr_ not complete";
157 }
158 if (gathering_state_ != kIceGatheringComplete) {
159 writable = false;
160 LOG(INFO) << "gathering_state_ " << gathering_state_;
161 }
162 set_writable(writable);
163 }
164
165 void RawUdpTransportChannel::AddRemoteCandidate(const Candidate& candidate) {
166 RTC_DCHECK_RUN_ON(&network_thread_checker_);
167 // TODO(johan) check for ipv4, other settings.
168 remote_candidate_ = candidate;
169 remote_addr_ = candidate.address();
170 // TODO(johan) - only set STATE_COMPLETED if *both* local and remote addr are
171 // set. Maybe use an rtc::Optional to indicate unset addr?
pthatcher1 2016/10/07 17:36:36 Yes, rtc::Optional<UdpTransportChannelParameters>
172 SetTransportChannelState(STATE_COMPLETED);
173 // TODO(johan) - set STATE_FAILED, if remote addr is invalid.
174 UpdateWritableState();
175 }
176
177 IceGatheringState RawUdpTransportChannel::gathering_state() const {
178 RTC_DCHECK_RUN_ON(&network_thread_checker_);
179 return gathering_state_;
180 }
181
182 TransportChannelState RawUdpTransportChannel::GetState() const {
183 RTC_DCHECK_RUN_ON(&network_thread_checker_);
184 return tch_state_;
185 }
pthatcher1 2016/10/07 17:36:36 These could just go in the .h.
johan 2016/10/11 13:36:09 Acknowledged.
186
187 void RawUdpTransportChannel::SetGatheringState(
188 IceGatheringState new_gathering_state) {
pthatcher1 2016/10/07 17:36:36 You don't need "new_"
johan 2016/10/11 13:36:10 Acknowledged.
189 RTC_DCHECK_RUN_ON(&network_thread_checker_);
190 if (gathering_state_ != new_gathering_state) {
191 gathering_state_ = new_gathering_state;
192 SignalGatheringState(this);
193 }
194 }
195
196 void RawUdpTransportChannel::SetTransportChannelState(
197 TransportChannelState new_tch_state) {
198 RTC_DCHECK_RUN_ON(&network_thread_checker_);
199 if (tch_state_ != new_tch_state) {
200 tch_state_ = new_tch_state;
201 SignalStateChanged(this);
202 }
203 }
204 } // namespace cricket
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698