blob: 8d3efec4fa5144ef9f6a5aa99816f29dfdf88b70 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/session/media/channel.h"
29
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000030#include "talk/media/base/constants.h"
31#include "talk/media/base/rtputils.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032#include "webrtc/p2p/base/transportchannel.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000033#include "talk/session/media/channelmanager.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000034#include "webrtc/base/bind.h"
35#include "webrtc/base/buffer.h"
36#include "webrtc/base/byteorder.h"
37#include "webrtc/base/common.h"
38#include "webrtc/base/dscp.h"
39#include "webrtc/base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
41namespace cricket {
42
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000043using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000044
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000046 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 MSG_SCREENCASTWINDOWEVENT,
48 MSG_RTPPACKET,
49 MSG_RTCPPACKET,
50 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000054 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055};
56
57// Value specified in RFC 5764.
58static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
59
60static const int kAgcMinus10db = -10;
61
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062static void SafeSetError(const std::string& message, std::string* error_desc) {
63 if (error_desc) {
64 *error_desc = message;
65 }
66}
67
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000068struct PacketMessageData : public rtc::MessageData {
69 rtc::Buffer packet;
stefanc1aeaf02015-10-15 07:26:07 -070070 rtc::PacketOptions options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071};
72
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073struct ScreencastEventMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020074 ScreencastEventMessageData(uint32_t s, rtc::WindowEvent we)
75 : ssrc(s), event(we) {}
76 uint32_t ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000077 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078};
79
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020081 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020083 : ssrc(in_ssrc), error(in_error) {}
84 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 VoiceMediaChannel::Error error;
86};
87
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020091 : ssrc(in_ssrc), error(in_error) {}
92 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 VideoMediaChannel::Error error;
94};
95
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000096struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020097 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020099 : ssrc(in_ssrc), error(in_error) {}
100 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 DataMediaChannel::Error error;
102};
103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000105struct VideoChannel::ScreencastDetailsData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200106 explicit ScreencastDetailsData(uint32_t s)
107 : ssrc(s), fps(0), screencast_max_pixels(0) {}
108 uint32_t ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000109 int fps;
110 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111};
112
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113static const char* PacketType(bool rtcp) {
114 return (!rtcp) ? "RTP" : "RTCP";
115}
116
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000117static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 // Check the packet size. We could check the header too if needed.
119 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000120 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
121 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122}
123
124static bool IsReceiveContentDirection(MediaContentDirection direction) {
125 return direction == MD_SENDRECV || direction == MD_RECVONLY;
126}
127
128static bool IsSendContentDirection(MediaContentDirection direction) {
129 return direction == MD_SENDRECV || direction == MD_SENDONLY;
130}
131
132static const MediaContentDescription* GetContentDescription(
133 const ContentInfo* cinfo) {
134 if (cinfo == NULL)
135 return NULL;
136 return static_cast<const MediaContentDescription*>(cinfo->description);
137}
138
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700139template <class Codec>
140void RtpParametersFromMediaDescription(
141 const MediaContentDescriptionImpl<Codec>* desc,
142 RtpParameters<Codec>* params) {
143 // TODO(pthatcher): Remove this once we're sure no one will give us
144 // a description without codecs (currently a CA_UPDATE with just
145 // streams can).
146 if (desc->has_codecs()) {
147 params->codecs = desc->codecs();
148 }
149 // TODO(pthatcher): See if we really need
150 // rtp_header_extensions_set() and remove it if we don't.
151 if (desc->rtp_header_extensions_set()) {
152 params->extensions = desc->rtp_header_extensions();
153 }
154}
155
156template <class Codec, class Options>
157void RtpSendParametersFromMediaDescription(
158 const MediaContentDescriptionImpl<Codec>* desc,
159 RtpSendParameters<Codec, Options>* send_params) {
160 RtpParametersFromMediaDescription(desc, send_params);
161 send_params->max_bandwidth_bps = desc->bandwidth();
162}
163
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000164BaseChannel::BaseChannel(rtc::Thread* thread,
deadbeefcbecd352015-09-23 11:50:27 -0700165 MediaChannel* media_channel,
166 TransportController* transport_controller,
167 const std::string& content_name,
168 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 : worker_thread_(thread),
deadbeefcbecd352015-09-23 11:50:27 -0700170 transport_controller_(transport_controller),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 media_channel_(media_channel),
172 content_name_(content_name),
deadbeefcbecd352015-09-23 11:50:27 -0700173 rtcp_transport_enabled_(rtcp),
174 transport_channel_(nullptr),
175 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 enabled_(false),
177 writable_(false),
178 rtp_ready_to_send_(false),
179 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 was_ever_writable_(false),
181 local_content_direction_(MD_INACTIVE),
182 remote_content_direction_(MD_INACTIVE),
183 has_received_packet_(false),
184 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000185 secure_required_(false),
186 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000187 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 LOG(LS_INFO) << "Created channel for " << content_name;
189}
190
191BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000192 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000193 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 StopConnectionMonitor();
195 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000196 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 // We must destroy the media channel before the transport channel, otherwise
198 // the media channel may try to send on the dead transport channel. NULLing
199 // is not an effective strategy since the sends will come on another thread.
200 delete media_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700201 // Note that we don't just call set_transport_channel(nullptr) because that
202 // would call a pure virtual method which we can't do from a destructor.
203 if (transport_channel_) {
204 DisconnectFromTransportChannel(transport_channel_);
205 transport_controller_->DestroyTransportChannel_w(
206 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
207 }
208 if (rtcp_transport_channel_) {
209 DisconnectFromTransportChannel(rtcp_transport_channel_);
210 transport_controller_->DestroyTransportChannel_w(
211 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
212 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 LOG(LS_INFO) << "Destroyed channel";
214}
215
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000216bool BaseChannel::Init() {
deadbeefcbecd352015-09-23 11:50:27 -0700217 if (!SetTransport(content_name())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 return false;
219 }
220
guoweis318166b2015-11-18 19:03:38 -0800221 if (!SetDtlsSrtpCiphers(transport_channel(), false)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000222 return false;
223 }
deadbeefcbecd352015-09-23 11:50:27 -0700224 if (rtcp_transport_enabled() &&
guoweis318166b2015-11-18 19:03:38 -0800225 !SetDtlsSrtpCiphers(rtcp_transport_channel(), true)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000226 return false;
227 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228
wu@webrtc.orgde305012013-10-31 15:40:38 +0000229 // Both RTP and RTCP channels are set, we can call SetInterface on
230 // media channel and it can set network options.
231 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 return true;
233}
234
wu@webrtc.org78187522013-10-07 23:32:02 +0000235void BaseChannel::Deinit() {
236 media_channel_->SetInterface(NULL);
237}
238
deadbeefcbecd352015-09-23 11:50:27 -0700239bool BaseChannel::SetTransport(const std::string& transport_name) {
240 return worker_thread_->Invoke<bool>(
241 Bind(&BaseChannel::SetTransport_w, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000242}
243
deadbeefcbecd352015-09-23 11:50:27 -0700244bool BaseChannel::SetTransport_w(const std::string& transport_name) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000245 ASSERT(worker_thread_ == rtc::Thread::Current());
246
deadbeefcbecd352015-09-23 11:50:27 -0700247 if (transport_name == transport_name_) {
248 // Nothing to do if transport name isn't changing
249 return true;
250 }
251
252 set_transport_channel(transport_controller_->CreateTransportChannel_w(
253 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000254 if (!transport_channel()) {
255 return false;
256 }
deadbeefcbecd352015-09-23 11:50:27 -0700257 if (rtcp_transport_enabled()) {
258 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
259 << " on " << transport_name << " transport ";
260 set_rtcp_transport_channel(transport_controller_->CreateTransportChannel_w(
261 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000262 if (!rtcp_transport_channel()) {
263 return false;
264 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000265 }
266
deadbeefcbecd352015-09-23 11:50:27 -0700267 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000268 return true;
269}
270
271void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
272 ASSERT(worker_thread_ == rtc::Thread::Current());
273
274 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700275 if (!old_tc && !new_tc) {
276 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000277 return;
278 }
deadbeefcbecd352015-09-23 11:50:27 -0700279 ASSERT(old_tc != new_tc);
280
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000281 if (old_tc) {
282 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700283 transport_controller_->DestroyTransportChannel_w(
284 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000285 }
286
287 transport_channel_ = new_tc;
288
289 if (new_tc) {
290 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700291 for (const auto& pair : socket_options_) {
292 new_tc->SetOption(pair.first, pair.second);
293 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000294 }
deadbeefcbecd352015-09-23 11:50:27 -0700295
296 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
297 // setting new channel
298 UpdateWritableState_w();
299 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000300}
301
302void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
303 ASSERT(worker_thread_ == rtc::Thread::Current());
304
305 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700306 if (!old_tc && !new_tc) {
307 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000308 return;
309 }
deadbeefcbecd352015-09-23 11:50:27 -0700310 ASSERT(old_tc != new_tc);
311
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000312 if (old_tc) {
313 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700314 transport_controller_->DestroyTransportChannel_w(
315 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000316 }
317
318 rtcp_transport_channel_ = new_tc;
319
320 if (new_tc) {
321 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700322 for (const auto& pair : rtcp_socket_options_) {
323 new_tc->SetOption(pair.first, pair.second);
324 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000325 }
deadbeefcbecd352015-09-23 11:50:27 -0700326
327 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
328 // setting new channel
329 UpdateWritableState_w();
330 SetReadyToSend(true, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000331}
332
333void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
334 ASSERT(worker_thread_ == rtc::Thread::Current());
335
336 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
337 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
338 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
339}
340
341void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
342 ASSERT(worker_thread_ == rtc::Thread::Current());
343
344 tc->SignalWritableState.disconnect(this);
345 tc->SignalReadPacket.disconnect(this);
346 tc->SignalReadyToSend.disconnect(this);
347}
348
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000350 worker_thread_->Invoke<void>(Bind(
351 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
352 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 return true;
354}
355
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000357 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358}
359
Peter Boström0c4e06b2015-10-07 12:23:21 +0200360bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000361 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362}
363
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000364bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000365 return InvokeOnWorker(
366 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000367}
368
Peter Boström0c4e06b2015-10-07 12:23:21 +0200369bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000370 return InvokeOnWorker(
371 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000372}
373
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000375 ContentAction action,
376 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000377 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
378 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379}
380
381bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000382 ContentAction action,
383 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000384 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
385 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386}
387
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000389 // We pass in the BaseChannel instead of the transport_channel_
390 // because if the transport_channel_ changes, the ConnectionMonitor
391 // would be pointing to the wrong TransportChannel.
392 connection_monitor_.reset(new ConnectionMonitor(
393 this, worker_thread(), rtc::Thread::Current()));
394 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000396 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397}
398
399void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000400 if (connection_monitor_) {
401 connection_monitor_->Stop();
402 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 }
404}
405
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000406bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
407 ASSERT(worker_thread_ == rtc::Thread::Current());
408 return transport_channel_->GetStats(infos);
409}
410
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411bool BaseChannel::IsReadyToReceive() const {
412 // Receive data if we are enabled and have local content,
413 return enabled() && IsReceiveContentDirection(local_content_direction_);
414}
415
416bool BaseChannel::IsReadyToSend() const {
417 // Send outgoing data if we are enabled, have local and remote content,
418 // and we have had some form of connectivity.
419 return enabled() &&
420 IsReceiveContentDirection(remote_content_direction_) &&
421 IsSendContentDirection(local_content_direction_) &&
422 was_ever_writable();
423}
424
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000425bool BaseChannel::SendPacket(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700426 const rtc::PacketOptions& options) {
427 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428}
429
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000430bool BaseChannel::SendRtcp(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700431 const rtc::PacketOptions& options) {
432 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433}
434
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000435int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000437 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000439 case ST_RTP:
440 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700441 socket_options_.push_back(
442 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000443 break;
444 case ST_RTCP:
445 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700446 rtcp_socket_options_.push_back(
447 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000448 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000450 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451}
452
453void BaseChannel::OnWritableState(TransportChannel* channel) {
454 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
deadbeefcbecd352015-09-23 11:50:27 -0700455 UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456}
457
458void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000459 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000460 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000461 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000463 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464
465 // When using RTCP multiplexing we might get RTCP packets on the RTP
466 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
467 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000468 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000469 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470}
471
472void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700473 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
474 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475}
476
deadbeefcbecd352015-09-23 11:50:27 -0700477void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
478 if (rtcp) {
479 rtcp_ready_to_send_ = ready;
480 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 rtp_ready_to_send_ = ready;
482 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483
deadbeefcbecd352015-09-23 11:50:27 -0700484 if (rtp_ready_to_send_ &&
485 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
486 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
torbjornga81a42f2015-09-23 02:16:58 -0700487 // Notify the MediaChannel when both rtp and rtcp channel can send.
488 media_channel_->OnReadyToSend(true);
deadbeefcbecd352015-09-23 11:50:27 -0700489 } else {
490 // Notify the MediaChannel when either rtp or rtcp channel can't send.
491 media_channel_->OnReadyToSend(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 }
493}
494
495bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
496 const char* data, size_t len) {
497 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000498 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499}
500
stefanc1aeaf02015-10-15 07:26:07 -0700501bool BaseChannel::SendPacket(bool rtcp,
502 rtc::Buffer* packet,
503 const rtc::PacketOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 // SendPacket gets called from MediaEngine, typically on an encoder thread.
505 // If the thread is not our worker thread, we will post to our worker
506 // so that the real work happens on our worker. This avoids us having to
507 // synchronize access to all the pieces of the send path, including
508 // SRTP and the inner workings of the transport channels.
509 // The only downside is that we can't return a proper failure code if
510 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000511 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 // Avoid a copy by transferring the ownership of the packet data.
513 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
514 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200515 data->packet = packet->Pass();
stefanc1aeaf02015-10-15 07:26:07 -0700516 data->options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 worker_thread_->Post(this, message_id, data);
518 return true;
519 }
520
521 // Now that we are on the correct thread, ensure we have a place to send this
522 // packet before doing anything. (We might get RTCP packets that we don't
523 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
524 // transport.
525 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
526 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000527 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 return false;
529 }
530
531 // Protect ourselves against crazy data.
532 if (!ValidPacket(rtcp, packet)) {
533 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000534 << PacketType(rtcp)
535 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 return false;
537 }
538
stefanc1aeaf02015-10-15 07:26:07 -0700539 rtc::PacketOptions updated_options;
540 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 // Protect if needed.
542 if (srtp_filter_.IsActive()) {
543 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200544 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000545 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000547 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
548 // inside libsrtp for a RTP packet. A external HMAC module will be writing
549 // a fake HMAC value. This is ONLY done for a RTP packet.
550 // Socket layer will update rtp sendtime extension header if present in
551 // packet with current time before updating the HMAC.
552#if !defined(ENABLE_EXTERNAL_AUTH)
553 res = srtp_filter_.ProtectRtp(
554 data, len, static_cast<int>(packet->capacity()), &len);
555#else
stefanc1aeaf02015-10-15 07:26:07 -0700556 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000557 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000558 res = srtp_filter_.ProtectRtp(
559 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700560 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000561 // If protection succeeds, let's get auth params from srtp.
562 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200563 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000564 int key_len;
565 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700566 &auth_key, &key_len,
567 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000568 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700569 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
570 updated_options.packet_time_params.srtp_auth_key.assign(
571 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000572 }
573 }
574#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 if (!res) {
576 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200577 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 GetRtpSeqNum(data, len, &seq_num);
579 GetRtpSsrc(data, len, &ssrc);
580 LOG(LS_ERROR) << "Failed to protect " << content_name_
581 << " RTP packet: size=" << len
582 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
583 return false;
584 }
585 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000586 res = srtp_filter_.ProtectRtcp(data, len,
587 static_cast<int>(packet->capacity()),
588 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 if (!res) {
590 int type = -1;
591 GetRtcpType(data, len, &type);
592 LOG(LS_ERROR) << "Failed to protect " << content_name_
593 << " RTCP packet: size=" << len << ", type=" << type;
594 return false;
595 }
596 }
597
598 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000599 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 } else if (secure_required_) {
601 // This is a double check for something that supposedly can't happen.
602 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
603 << " packet when SRTP is inactive and crypto is required";
604
605 ASSERT(false);
606 return false;
607 }
608
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000610 int ret =
stefanc1aeaf02015-10-15 07:26:07 -0700611 channel->SendPacket(packet->data<char>(), packet->size(), updated_options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000612 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
613 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 if (channel->GetError() == EWOULDBLOCK) {
615 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700616 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 }
618 return false;
619 }
620 return true;
621}
622
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000623bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 // Protect ourselves against crazy data.
625 if (!ValidPacket(rtcp, packet)) {
626 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000627 << PacketType(rtcp)
628 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 return false;
630 }
pbos482b12e2015-11-16 10:19:58 -0800631 if (rtcp) {
632 // Permit all (seemingly valid) RTCP packets.
633 return true;
634 }
635 // Check whether we handle this payload.
636 return bundle_filter_.DemuxPacket(packet->data<uint8_t>(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637}
638
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000639void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
640 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 if (!WantsPacket(rtcp, packet)) {
642 return;
643 }
644
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000645 // We are only interested in the first rtp packet because that
646 // indicates the media has started flowing.
647 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 has_received_packet_ = true;
649 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
650 }
651
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 // Unprotect the packet, if needed.
653 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200654 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000655 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 bool res;
657 if (!rtcp) {
658 res = srtp_filter_.UnprotectRtp(data, len, &len);
659 if (!res) {
660 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200661 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 GetRtpSeqNum(data, len, &seq_num);
663 GetRtpSsrc(data, len, &ssrc);
664 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
665 << " RTP packet: size=" << len
666 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
667 return;
668 }
669 } else {
670 res = srtp_filter_.UnprotectRtcp(data, len, &len);
671 if (!res) {
672 int type = -1;
673 GetRtcpType(data, len, &type);
674 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
675 << " RTCP packet: size=" << len << ", type=" << type;
676 return;
677 }
678 }
679
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000680 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 } else if (secure_required_) {
682 // Our session description indicates that SRTP is required, but we got a
683 // packet before our SRTP filter is active. This means either that
684 // a) we got SRTP packets before we received the SDES keys, in which case
685 // we can't decrypt it anyway, or
686 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
687 // channels, so we haven't yet extracted keys, even if DTLS did complete
688 // on the channel that the packets are being sent on. It's really good
689 // practice to wait for both RTP and RTCP to be good to go before sending
690 // media, to prevent weird failure modes, so it's fine for us to just eat
691 // packets here. This is all sidestepped if RTCP mux is used anyway.
692 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
693 << " packet when SRTP is inactive and crypto is required";
694 return;
695 }
696
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 // Push it down to the media channel.
698 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000699 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000701 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 }
703}
704
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000705bool BaseChannel::PushdownLocalDescription(
706 const SessionDescription* local_desc, ContentAction action,
707 std::string* error_desc) {
708 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 const MediaContentDescription* content_desc =
710 GetContentDescription(content_info);
711 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000712 !SetLocalContent(content_desc, action, error_desc)) {
713 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
714 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000716 return true;
717}
718
719bool BaseChannel::PushdownRemoteDescription(
720 const SessionDescription* remote_desc, ContentAction action,
721 std::string* error_desc) {
722 const ContentInfo* content_info = GetFirstContent(remote_desc);
723 const MediaContentDescription* content_desc =
724 GetContentDescription(content_info);
725 if (content_desc && content_info && !content_info->rejected &&
726 !SetRemoteContent(content_desc, action, error_desc)) {
727 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
728 return false;
729 }
730 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731}
732
733void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000734 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 if (enabled_)
736 return;
737
738 LOG(LS_INFO) << "Channel enabled";
739 enabled_ = true;
740 ChangeState();
741}
742
743void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000744 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 if (!enabled_)
746 return;
747
748 LOG(LS_INFO) << "Channel disabled";
749 enabled_ = false;
750 ChangeState();
751}
752
deadbeefcbecd352015-09-23 11:50:27 -0700753void BaseChannel::UpdateWritableState_w() {
754 if (transport_channel_ && transport_channel_->writable() &&
755 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
756 ChannelWritable_w();
757 } else {
758 ChannelNotWritable_w();
759 }
760}
761
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000763 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 if (writable_)
765 return;
766
deadbeefcbecd352015-09-23 11:50:27 -0700767 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 << (was_ever_writable_ ? "" : " for the first time");
769
770 std::vector<ConnectionInfo> infos;
771 transport_channel_->GetStats(&infos);
772 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
773 it != infos.end(); ++it) {
774 if (it->best_connection) {
775 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
776 << "->" << it->remote_candidate.ToSensitiveString();
777 break;
778 }
779 }
780
781 // If we're doing DTLS-SRTP, now is the time.
782 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
783 if (!SetupDtlsSrtp(false)) {
deadbeefcbecd352015-09-23 11:50:27 -0700784 SignalDtlsSetupFailure_w(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 return;
786 }
787
788 if (rtcp_transport_channel_) {
789 if (!SetupDtlsSrtp(true)) {
deadbeefcbecd352015-09-23 11:50:27 -0700790 SignalDtlsSetupFailure_w(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 return;
792 }
793 }
794 }
795
796 was_ever_writable_ = true;
797 writable_ = true;
798 ChangeState();
799}
800
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000801void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
802 ASSERT(worker_thread() == rtc::Thread::Current());
803 signaling_thread()->Invoke<void>(Bind(
804 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
805}
806
807void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
808 ASSERT(signaling_thread() == rtc::Thread::Current());
809 SignalDtlsSetupFailure(this, rtcp);
810}
811
guoweis318166b2015-11-18 19:03:38 -0800812bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
813 std::vector<std::string> ciphers;
814 // We always use the default SRTP ciphers for RTCP, but we may use different
815 // ciphers for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 if (!rtcp) {
guoweis318166b2015-11-18 19:03:38 -0800817 GetSrtpCryptoSuiteNames(&ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 } else {
guoweis318166b2015-11-18 19:03:38 -0800819 GetDefaultSrtpCryptoSuiteNames(&ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 }
guoweis318166b2015-11-18 19:03:38 -0800821 return tc->SetSrtpCiphers(ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822}
823
824bool BaseChannel::ShouldSetupDtlsSrtp() const {
825 return true;
826}
827
828// This function returns true if either DTLS-SRTP is not in use
829// *or* DTLS-SRTP is successfully set up.
830bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
831 bool ret = false;
832
deadbeefcbecd352015-09-23 11:50:27 -0700833 TransportChannel* channel =
834 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835
836 // No DTLS
837 if (!channel->IsDtlsActive())
838 return true;
839
guoweis318166b2015-11-18 19:03:38 -0800840 std::string selected_cipher;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841
guoweis318166b2015-11-18 19:03:38 -0800842 if (!channel->GetSrtpCryptoSuite(&selected_cipher)) {
843 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 return false;
845 }
846
847 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
848 << content_name() << " "
849 << PacketType(rtcp_channel);
850
851 // OK, we're now doing DTLS (RFC 5764)
852 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
853 SRTP_MASTER_KEY_SALT_LEN * 2);
854
855 // RFC 5705 exporter using the RFC 5764 parameters
856 if (!channel->ExportKeyingMaterial(
857 kDtlsSrtpExporterLabel,
858 NULL, 0, false,
859 &dtls_buffer[0], dtls_buffer.size())) {
860 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
861 ASSERT(false); // This should never happen
862 return false;
863 }
864
865 // Sync up the keys with the DTLS-SRTP interface
866 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
867 SRTP_MASTER_KEY_SALT_LEN);
868 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
869 SRTP_MASTER_KEY_SALT_LEN);
870 size_t offset = 0;
871 memcpy(&client_write_key[0], &dtls_buffer[offset],
872 SRTP_MASTER_KEY_KEY_LEN);
873 offset += SRTP_MASTER_KEY_KEY_LEN;
874 memcpy(&server_write_key[0], &dtls_buffer[offset],
875 SRTP_MASTER_KEY_KEY_LEN);
876 offset += SRTP_MASTER_KEY_KEY_LEN;
877 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
878 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
879 offset += SRTP_MASTER_KEY_SALT_LEN;
880 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
881 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
882
883 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000884 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000885 if (!channel->GetSslRole(&role)) {
886 LOG(LS_WARNING) << "GetSslRole failed";
887 return false;
888 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000890 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 send_key = &server_write_key;
892 recv_key = &client_write_key;
893 } else {
894 send_key = &client_write_key;
895 recv_key = &server_write_key;
896 }
897
898 if (rtcp_channel) {
guoweis318166b2015-11-18 19:03:38 -0800899 ret = srtp_filter_.SetRtcpParams(
900 selected_cipher,
901 &(*send_key)[0],
902 static_cast<int>(send_key->size()),
903 selected_cipher,
904 &(*recv_key)[0],
905 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 } else {
guoweis318166b2015-11-18 19:03:38 -0800907 ret = srtp_filter_.SetRtpParams(
908 selected_cipher,
909 &(*send_key)[0],
910 static_cast<int>(send_key->size()),
911 selected_cipher,
912 &(*recv_key)[0],
913 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
915
916 if (!ret)
917 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
918 else
919 dtls_keyed_ = true;
920
921 return ret;
922}
923
924void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000925 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 if (!writable_)
927 return;
928
deadbeefcbecd352015-09-23 11:50:27 -0700929 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 writable_ = false;
931 ChangeState();
932}
933
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700934bool BaseChannel::SetRtpTransportParameters_w(
935 const MediaContentDescription* content,
936 ContentAction action,
937 ContentSource src,
938 std::string* error_desc) {
939 if (action == CA_UPDATE) {
940 // These parameters never get changed by a CA_UDPATE.
941 return true;
942 }
943
944 // Cache secure_required_ for belt and suspenders check on SendPacket
945 if (src == CS_LOCAL) {
946 set_secure_required(content->crypto_required() != CT_NONE);
947 }
948
949 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
950 return false;
951 }
952
953 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
954 return false;
955 }
956
957 return true;
958}
959
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000960// |dtls| will be set to true if DTLS is active for transport channel and
961// crypto is empty.
962bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000963 bool* dtls,
964 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000965 *dtls = transport_channel_->IsDtlsActive();
966 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000967 SafeSetError("Cryptos must be empty when DTLS is active.",
968 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000969 return false;
970 }
971 return true;
972}
973
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000975 ContentAction action,
976 ContentSource src,
977 std::string* error_desc) {
978 if (action == CA_UPDATE) {
979 // no crypto params.
980 return true;
981 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000983 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000984 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
985 if (!ret) {
986 return false;
987 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 switch (action) {
989 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000990 // If DTLS is already active on the channel, we could be renegotiating
991 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000992 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000993 ret = srtp_filter_.SetOffer(cryptos, src);
994 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 break;
996 case CA_PRANSWER:
997 // If we're doing DTLS-SRTP, we don't want to update the filter
998 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000999 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1001 }
1002 break;
1003 case CA_ANSWER:
1004 // If we're doing DTLS-SRTP, we don't want to update the filter
1005 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001006 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 ret = srtp_filter_.SetAnswer(cryptos, src);
1008 }
1009 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 default:
1011 break;
1012 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001013 if (!ret) {
1014 SafeSetError("Failed to setup SRTP filter.", error_desc);
1015 return false;
1016 }
1017 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018}
1019
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001020void BaseChannel::ActivateRtcpMux() {
1021 worker_thread_->Invoke<void>(Bind(
1022 &BaseChannel::ActivateRtcpMux_w, this));
1023}
1024
1025void BaseChannel::ActivateRtcpMux_w() {
1026 if (!rtcp_mux_filter_.IsActive()) {
1027 rtcp_mux_filter_.SetActive();
deadbeefcbecd352015-09-23 11:50:27 -07001028 set_rtcp_transport_channel(nullptr);
1029 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001030 }
1031}
1032
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001034 ContentSource src,
1035 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 bool ret = false;
1037 switch (action) {
1038 case CA_OFFER:
1039 ret = rtcp_mux_filter_.SetOffer(enable, src);
1040 break;
1041 case CA_PRANSWER:
1042 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1043 break;
1044 case CA_ANSWER:
1045 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1046 if (ret && rtcp_mux_filter_.IsActive()) {
1047 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001048 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1049 << " by destroying RTCP transport channel for "
1050 << transport_name();
1051 set_rtcp_transport_channel(nullptr);
1052 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 }
1054 break;
1055 case CA_UPDATE:
1056 // No RTCP mux info.
1057 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001058 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 default:
1060 break;
1061 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001062 if (!ret) {
1063 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1064 return false;
1065 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1067 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1068 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001069 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 // If the RTP transport is already writable, then so are we.
1071 if (transport_channel_->writable()) {
1072 ChannelWritable_w();
1073 }
1074 }
1075
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001076 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077}
1078
1079bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001080 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001081 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082}
1083
Peter Boström0c4e06b2015-10-07 12:23:21 +02001084bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001085 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086 return media_channel()->RemoveRecvStream(ssrc);
1087}
1088
1089bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001090 ContentAction action,
1091 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1093 action == CA_PRANSWER || action == CA_UPDATE))
1094 return false;
1095
1096 // If this is an update, streams only contain streams that have changed.
1097 if (action == CA_UPDATE) {
1098 for (StreamParamsVec::const_iterator it = streams.begin();
1099 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001100 const StreamParams* existing_stream =
1101 GetStreamByIds(local_streams_, it->groupid, it->id);
1102 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 if (media_channel()->AddSendStream(*it)) {
1104 local_streams_.push_back(*it);
1105 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1106 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001107 std::ostringstream desc;
1108 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1109 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 return false;
1111 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001112 } else if (existing_stream && !it->has_ssrcs()) {
1113 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001114 std::ostringstream desc;
1115 desc << "Failed to remove send stream with ssrc "
1116 << it->first_ssrc() << ".";
1117 SafeSetError(desc.str(), error_desc);
1118 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001120 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121 } else {
1122 LOG(LS_WARNING) << "Ignore unsupported stream update";
1123 }
1124 }
1125 return true;
1126 }
1127 // Else streams are all the streams we want to send.
1128
1129 // Check for streams that have been removed.
1130 bool ret = true;
1131 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1132 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001133 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001135 std::ostringstream desc;
1136 desc << "Failed to remove send stream with ssrc "
1137 << it->first_ssrc() << ".";
1138 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 ret = false;
1140 }
1141 }
1142 }
1143 // Check for new streams.
1144 for (StreamParamsVec::const_iterator it = streams.begin();
1145 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001146 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001148 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001150 std::ostringstream desc;
1151 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1152 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153 ret = false;
1154 }
1155 }
1156 }
1157 local_streams_ = streams;
1158 return ret;
1159}
1160
1161bool BaseChannel::UpdateRemoteStreams_w(
1162 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001163 ContentAction action,
1164 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1166 action == CA_PRANSWER || action == CA_UPDATE))
1167 return false;
1168
1169 // If this is an update, streams only contain streams that have changed.
1170 if (action == CA_UPDATE) {
1171 for (StreamParamsVec::const_iterator it = streams.begin();
1172 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001173 const StreamParams* existing_stream =
1174 GetStreamByIds(remote_streams_, it->groupid, it->id);
1175 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 if (AddRecvStream_w(*it)) {
1177 remote_streams_.push_back(*it);
1178 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1179 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001180 std::ostringstream desc;
1181 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1182 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 return false;
1184 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001185 } else if (existing_stream && !it->has_ssrcs()) {
1186 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001187 std::ostringstream desc;
1188 desc << "Failed to remove remote stream with ssrc "
1189 << it->first_ssrc() << ".";
1190 SafeSetError(desc.str(), error_desc);
1191 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001193 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 } else {
1195 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001196 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 << " new stream = " << it->ToString();
1198 }
1199 }
1200 return true;
1201 }
1202 // Else streams are all the streams we want to receive.
1203
1204 // Check for streams that have been removed.
1205 bool ret = true;
1206 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1207 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001208 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001210 std::ostringstream desc;
1211 desc << "Failed to remove remote stream with ssrc "
1212 << it->first_ssrc() << ".";
1213 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 ret = false;
1215 }
1216 }
1217 }
1218 // Check for new streams.
1219 for (StreamParamsVec::const_iterator it = streams.begin();
1220 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001221 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 if (AddRecvStream_w(*it)) {
1223 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1224 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001225 std::ostringstream desc;
1226 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1227 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 ret = false;
1229 }
1230 }
1231 }
1232 remote_streams_ = streams;
1233 return ret;
1234}
1235
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001236void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1237 const std::vector<RtpHeaderExtension>& extensions) {
1238 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001239 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001240 rtp_abs_sendtime_extn_id_ =
1241 send_time_extension ? send_time_extension->id : -1;
1242}
1243
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001244void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 case MSG_RTPPACKET:
1247 case MSG_RTCPPACKET: {
1248 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
stefanc1aeaf02015-10-15 07:26:07 -07001249 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet,
1250 data->options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 delete data; // because it is Posted
1252 break;
1253 }
1254 case MSG_FIRSTPACKETRECEIVED: {
1255 SignalFirstPacketReceived(this);
1256 break;
1257 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 }
1259}
1260
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261void BaseChannel::FlushRtcpMessages() {
1262 // Flush all remaining RTCP messages. This should only be called in
1263 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001264 ASSERT(rtc::Thread::Current() == worker_thread_);
1265 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001266 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001267 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001269 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 }
1271}
1272
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001273VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 MediaEngineInterface* media_engine,
1275 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001276 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 const std::string& content_name,
1278 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001279 : BaseChannel(thread,
1280 media_channel,
1281 transport_controller,
1282 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001284 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001285 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286
1287VoiceChannel::~VoiceChannel() {
1288 StopAudioMonitor();
1289 StopMediaMonitor();
1290 // this can't be done in the base class, since it calls a virtual
1291 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001292 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293}
1294
1295bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001296 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 return false;
1298 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 return true;
1300}
1301
Peter Boström0c4e06b2015-10-07 12:23:21 +02001302bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001303 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001304 const AudioOptions* options,
1305 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001306 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001307 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308}
1309
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310// TODO(juberti): Handle early media the right way. We should get an explicit
1311// ringing message telling us to start playing local ringback, which we cancel
1312// if any early media actually arrives. For now, we do the opposite, which is
1313// to wait 1 second for early media, and start playing local ringback if none
1314// arrives.
1315void VoiceChannel::SetEarlyMedia(bool enable) {
1316 if (enable) {
1317 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001318 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1319 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320 } else {
1321 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001322 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 }
1324}
1325
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326bool VoiceChannel::PressDTMF(int digit, bool playout) {
1327 int flags = DF_SEND;
1328 if (playout) {
1329 flags |= DF_PLAY;
1330 }
1331 int duration_ms = 160;
1332 return InsertDtmf(0, digit, duration_ms, flags);
1333}
1334
1335bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001336 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1337 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338}
1339
Peter Boström0c4e06b2015-10-07 12:23:21 +02001340bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1341 int event_code,
1342 int duration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001344 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1345 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346}
1347
solenberg4bac9c52015-10-09 02:32:53 -07001348bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1349 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1350 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001352
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001354 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1355 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356}
1357
1358void VoiceChannel::StartMediaMonitor(int cms) {
1359 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001360 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361 media_monitor_->SignalUpdate.connect(
1362 this, &VoiceChannel::OnMediaMonitorUpdate);
1363 media_monitor_->Start(cms);
1364}
1365
1366void VoiceChannel::StopMediaMonitor() {
1367 if (media_monitor_) {
1368 media_monitor_->Stop();
1369 media_monitor_->SignalUpdate.disconnect(this);
1370 media_monitor_.reset();
1371 }
1372}
1373
1374void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001375 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 audio_monitor_
1377 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1378 audio_monitor_->Start(cms);
1379}
1380
1381void VoiceChannel::StopAudioMonitor() {
1382 if (audio_monitor_) {
1383 audio_monitor_->Stop();
1384 audio_monitor_.reset();
1385 }
1386}
1387
1388bool VoiceChannel::IsAudioMonitorRunning() const {
1389 return (audio_monitor_.get() != NULL);
1390}
1391
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001393 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001394}
1395
1396int VoiceChannel::GetOutputLevel_w() {
1397 return media_channel()->GetOutputLevel();
1398}
1399
1400void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1401 media_channel()->GetActiveStreams(actives);
1402}
1403
1404void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001405 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001406 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001407 int flags) {
1408 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409
1410 // Set a flag when we've received an RTP packet. If we're waiting for early
1411 // media, this will disable the timeout.
1412 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1413 received_media_ = true;
1414 }
1415}
1416
1417void VoiceChannel::ChangeState() {
1418 // Render incoming data if we're the active call, and we have the local
1419 // content. We receive data on the default channel and multiplexed streams.
1420 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001421 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422
1423 // Send outgoing data if we're the active call, we have the remote content,
1424 // and we have had some form of connectivity.
1425 bool send = IsReadyToSend();
1426 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1427 if (!media_channel()->SetSend(send_flag)) {
1428 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429 }
1430
1431 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1432}
1433
1434const ContentInfo* VoiceChannel::GetFirstContent(
1435 const SessionDescription* sdesc) {
1436 return GetFirstAudioContent(sdesc);
1437}
1438
1439bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001440 ContentAction action,
1441 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001442 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443 LOG(LS_INFO) << "Setting local voice description";
1444
1445 const AudioContentDescription* audio =
1446 static_cast<const AudioContentDescription*>(content);
1447 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001448 if (!audio) {
1449 SafeSetError("Can't find audio content in local description.", error_desc);
1450 return false;
1451 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001453 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1454 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 }
1456
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001457 AudioRecvParameters recv_params = last_recv_params_;
1458 RtpParametersFromMediaDescription(audio, &recv_params);
1459 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001460 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001461 error_desc);
1462 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001464 for (const AudioCodec& codec : audio->codecs()) {
1465 bundle_filter()->AddPayloadType(codec.id);
1466 }
1467 last_recv_params_ = recv_params;
1468
1469 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1470 // only give it to the media channel once we have a remote
1471 // description too (without a remote description, we won't be able
1472 // to send them anyway).
1473 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1474 SafeSetError("Failed to set local audio description streams.", error_desc);
1475 return false;
1476 }
1477
1478 set_local_content_direction(content->direction());
1479 ChangeState();
1480 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481}
1482
1483bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001484 ContentAction action,
1485 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001486 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487 LOG(LS_INFO) << "Setting remote voice description";
1488
1489 const AudioContentDescription* audio =
1490 static_cast<const AudioContentDescription*>(content);
1491 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001492 if (!audio) {
1493 SafeSetError("Can't find audio content in remote description.", error_desc);
1494 return false;
1495 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001497 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1498 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 }
1500
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001501 AudioSendParameters send_params = last_send_params_;
1502 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001503 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001504 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001505 }
1506 if (!media_channel()->SetSendParameters(send_params)) {
1507 SafeSetError("Failed to set remote audio description send parameters.",
1508 error_desc);
1509 return false;
1510 }
1511 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001513 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1514 // and only give it to the media channel once we have a local
1515 // description too (without a local description, we won't be able to
1516 // recv them anyway).
1517 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1518 SafeSetError("Failed to set remote audio description streams.", error_desc);
1519 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001520 }
1521
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001522 if (audio->rtp_header_extensions_set()) {
1523 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1524 }
1525
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001526 set_remote_content_direction(content->direction());
1527 ChangeState();
1528 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529}
1530
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531void VoiceChannel::HandleEarlyMediaTimeout() {
1532 // This occurs on the main thread, not the worker thread.
1533 if (!received_media_) {
1534 LOG(LS_INFO) << "No early media received before timeout";
1535 SignalEarlyMediaTimeout(this);
1536 }
1537}
1538
Peter Boström0c4e06b2015-10-07 12:23:21 +02001539bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1540 int event,
1541 int duration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 int flags) {
1543 if (!enabled()) {
1544 return false;
1545 }
1546
1547 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1548}
1549
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001550void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 case MSG_EARLYMEDIATIMEOUT:
1553 HandleEarlyMediaTimeout();
1554 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 case MSG_CHANNEL_ERROR: {
1556 VoiceChannelErrorMessageData* data =
1557 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 delete data;
1559 break;
1560 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 default:
1562 BaseChannel::OnMessage(pmsg);
1563 break;
1564 }
1565}
1566
1567void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001568 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 SignalConnectionMonitor(this, infos);
1570}
1571
1572void VoiceChannel::OnMediaMonitorUpdate(
1573 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1574 ASSERT(media_channel == this->media_channel());
1575 SignalMediaMonitor(this, info);
1576}
1577
1578void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1579 const AudioInfo& info) {
1580 SignalAudioMonitor(this, info);
1581}
1582
guoweis318166b2015-11-18 19:03:38 -08001583void VoiceChannel::GetSrtpCryptoSuiteNames(
1584 std::vector<std::string>* ciphers) const {
1585 GetSupportedAudioCryptoSuites(ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586}
1587
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001588VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001590 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001592 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001593 : BaseChannel(thread,
1594 media_channel,
1595 transport_controller,
1596 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001599 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600
1601bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001602 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 return false;
1604 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 return true;
1606}
1607
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608VideoChannel::~VideoChannel() {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001609 std::vector<uint32_t> screencast_ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 ScreencastMap::iterator iter;
1611 while (!screencast_capturers_.empty()) {
1612 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1613 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1614 << screencast_capturers_.begin()->first;
1615 ASSERT(false);
1616 break;
1617 }
1618 }
1619
1620 StopMediaMonitor();
1621 // this can't be done in the base class, since it calls a virtual
1622 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001623
1624 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625}
1626
Peter Boström0c4e06b2015-10-07 12:23:21 +02001627bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001628 worker_thread()->Invoke<void>(Bind(
1629 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 return true;
1631}
1632
1633bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001634 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635}
1636
Peter Boström0c4e06b2015-10-07 12:23:21 +02001637bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001638 return worker_thread()->Invoke<bool>(Bind(
1639 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640}
1641
Peter Boström0c4e06b2015-10-07 12:23:21 +02001642bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001643 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1644 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645}
1646
Peter Boström0c4e06b2015-10-07 12:23:21 +02001647bool VideoChannel::RemoveScreencast(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001648 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649}
1650
1651bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001652 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653}
1654
Peter Boström0c4e06b2015-10-07 12:23:21 +02001655int VideoChannel::GetScreencastFps(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001656 ScreencastDetailsData data(ssrc);
1657 worker_thread()->Invoke<void>(Bind(
1658 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001659 return data.fps;
1660}
1661
Peter Boström0c4e06b2015-10-07 12:23:21 +02001662int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001663 ScreencastDetailsData data(ssrc);
1664 worker_thread()->Invoke<void>(Bind(
1665 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001666 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667}
1668
1669bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001670 worker_thread()->Invoke<void>(Bind(
1671 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 return true;
1673}
1674
1675bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001676 worker_thread()->Invoke<void>(Bind(
1677 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 return true;
1679}
1680
Peter Boström0c4e06b2015-10-07 12:23:21 +02001681bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001682 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001683 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001684 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1685 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001686}
1687
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 // Send outgoing data if we're the active call, we have the remote content,
1690 // and we have had some form of connectivity.
1691 bool send = IsReadyToSend();
1692 if (!media_channel()->SetSend(send)) {
1693 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1694 // TODO(gangji): Report error back to server.
1695 }
1696
Peter Boström34fbfff2015-09-24 19:20:30 +02001697 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698}
1699
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001700bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1701 return InvokeOnWorker(
1702 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703}
1704
1705void VideoChannel::StartMediaMonitor(int cms) {
1706 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001707 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 media_monitor_->SignalUpdate.connect(
1709 this, &VideoChannel::OnMediaMonitorUpdate);
1710 media_monitor_->Start(cms);
1711}
1712
1713void VideoChannel::StopMediaMonitor() {
1714 if (media_monitor_) {
1715 media_monitor_->Stop();
1716 media_monitor_.reset();
1717 }
1718}
1719
1720const ContentInfo* VideoChannel::GetFirstContent(
1721 const SessionDescription* sdesc) {
1722 return GetFirstVideoContent(sdesc);
1723}
1724
1725bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001726 ContentAction action,
1727 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001728 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 LOG(LS_INFO) << "Setting local video description";
1730
1731 const VideoContentDescription* video =
1732 static_cast<const VideoContentDescription*>(content);
1733 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001734 if (!video) {
1735 SafeSetError("Can't find video content in local description.", error_desc);
1736 return false;
1737 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001739 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1740 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 }
1742
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001743 VideoRecvParameters recv_params = last_recv_params_;
1744 RtpParametersFromMediaDescription(video, &recv_params);
1745 if (!media_channel()->SetRecvParameters(recv_params)) {
1746 SafeSetError("Failed to set local video description recv parameters.",
1747 error_desc);
1748 return false;
1749 }
1750 for (const VideoCodec& codec : video->codecs()) {
1751 bundle_filter()->AddPayloadType(codec.id);
1752 }
1753 last_recv_params_ = recv_params;
1754
1755 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1756 // only give it to the media channel once we have a remote
1757 // description too (without a remote description, we won't be able
1758 // to send them anyway).
1759 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1760 SafeSetError("Failed to set local video description streams.", error_desc);
1761 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762 }
1763
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001764 set_local_content_direction(content->direction());
1765 ChangeState();
1766 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767}
1768
1769bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001770 ContentAction action,
1771 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001772 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 LOG(LS_INFO) << "Setting remote video description";
1774
1775 const VideoContentDescription* video =
1776 static_cast<const VideoContentDescription*>(content);
1777 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001778 if (!video) {
1779 SafeSetError("Can't find video content in remote description.", error_desc);
1780 return false;
1781 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001783
1784 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1785 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786 }
1787
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001788 VideoSendParameters send_params = last_send_params_;
1789 RtpSendParametersFromMediaDescription(video, &send_params);
1790 if (video->conference_mode()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001791 send_params.options.conference_mode = rtc::Optional<bool>(true);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001792 }
1793 if (!media_channel()->SetSendParameters(send_params)) {
1794 SafeSetError("Failed to set remote video description send parameters.",
1795 error_desc);
1796 return false;
1797 }
1798 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001800 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1801 // and only give it to the media channel once we have a local
1802 // description too (without a local description, we won't be able to
1803 // recv them anyway).
1804 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1805 SafeSetError("Failed to set remote video description streams.", error_desc);
1806 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 }
1808
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001809 if (video->rtp_header_extensions_set()) {
1810 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001812
1813 set_remote_content_direction(content->direction());
1814 ChangeState();
1815 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816}
1817
1818bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1819 bool ret = true;
1820 // Set the send format for each of the local streams. If the view request
1821 // does not contain a local stream, set its send format to 0x0, which will
1822 // drop all frames.
1823 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1824 it != local_streams().end(); ++it) {
1825 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1826 StaticVideoViews::const_iterator view;
1827 for (view = request.static_video_views.begin();
1828 view != request.static_video_views.end(); ++view) {
1829 if (view->selector.Matches(*it)) {
1830 format.width = view->width;
1831 format.height = view->height;
1832 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1833 break;
1834 }
1835 }
1836
1837 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1838 }
1839
1840 // Check if the view request has invalid streams.
1841 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1842 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001843 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 LOG(LS_WARNING) << "View request for ("
1845 << it->selector.ssrc << ", '"
1846 << it->selector.groupid << "', '"
1847 << it->selector.streamid << "'"
1848 << ") is not in the local streams.";
1849 }
1850 }
1851
1852 return ret;
1853}
1854
Peter Boström0c4e06b2015-10-07 12:23:21 +02001855bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001857 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001859 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1860 screencast_capturers_[ssrc] = capturer;
1861 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862}
1863
Peter Boström0c4e06b2015-10-07 12:23:21 +02001864bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1866 if (iter == screencast_capturers_.end()) {
1867 return false;
1868 }
1869 // Clean up VideoCapturer.
1870 delete iter->second;
1871 screencast_capturers_.erase(iter);
1872 return true;
1873}
1874
1875bool VideoChannel::IsScreencasting_w() const {
1876 return !screencast_capturers_.empty();
1877}
1878
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001879void VideoChannel::GetScreencastDetails_w(
1880 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001881 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001883 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 }
1885 VideoCapturer* capturer = iter->second;
1886 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001887 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1888 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889}
1890
Peter Boström0c4e06b2015-10-07 12:23:21 +02001891void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001892 rtc::WindowEvent we) {
1893 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 SignalScreencastWindowEvent(ssrc, we);
1895}
1896
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001897void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 case MSG_SCREENCASTWINDOWEVENT: {
1900 const ScreencastEventMessageData* data =
1901 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1902 OnScreencastWindowEvent_s(data->ssrc, data->event);
1903 delete data;
1904 break;
1905 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 case MSG_CHANNEL_ERROR: {
1907 const VideoChannelErrorMessageData* data =
1908 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909 delete data;
1910 break;
1911 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001912 default:
1913 BaseChannel::OnMessage(pmsg);
1914 break;
1915 }
1916}
1917
1918void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001919 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 SignalConnectionMonitor(this, infos);
1921}
1922
1923// TODO(pthatcher): Look into removing duplicate code between
1924// audio, video, and data, perhaps by using templates.
1925void VideoChannel::OnMediaMonitorUpdate(
1926 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1927 ASSERT(media_channel == this->media_channel());
1928 SignalMediaMonitor(this, info);
1929}
1930
Peter Boström0c4e06b2015-10-07 12:23:21 +02001931void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001932 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 ScreencastEventMessageData* pdata =
1934 new ScreencastEventMessageData(ssrc, event);
1935 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1936}
1937
1938void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1939 // Map capturer events to window events. In the future we may want to simply
1940 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001941 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001943 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001945 we = rtc::WE_MINIMIZE;
1946 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1947 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948 } else {
1949 return;
1950 }
1951 previous_we_ = we;
1952
Peter Boström0c4e06b2015-10-07 12:23:21 +02001953 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 if (!GetLocalSsrc(capturer, &ssrc)) {
1955 return;
1956 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001957
1958 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959}
1960
Peter Boström0c4e06b2015-10-07 12:23:21 +02001961bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 *ssrc = 0;
1963 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
1964 iter != screencast_capturers_.end(); ++iter) {
1965 if (iter->second == capturer) {
1966 *ssrc = iter->first;
1967 return true;
1968 }
1969 }
1970 return false;
1971}
1972
guoweis318166b2015-11-18 19:03:38 -08001973void VideoChannel::GetSrtpCryptoSuiteNames(
1974 std::vector<std::string>* ciphers) const {
1975 GetSupportedVideoCryptoSuites(ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976}
1977
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001978DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001980 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 const std::string& content_name,
1982 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001983 : BaseChannel(thread,
1984 media_channel,
1985 transport_controller,
1986 content_name,
1987 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001988 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07001989 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990
1991DataChannel::~DataChannel() {
1992 StopMediaMonitor();
1993 // this can't be done in the base class, since it calls a virtual
1994 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001995
1996 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997}
1998
1999bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002000 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 return false;
2002 }
2003 media_channel()->SignalDataReceived.connect(
2004 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002005 media_channel()->SignalReadyToSend.connect(
2006 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002007 media_channel()->SignalStreamClosedRemotely.connect(
2008 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009 return true;
2010}
2011
2012bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002013 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002015 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2016 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017}
2018
2019const ContentInfo* DataChannel::GetFirstContent(
2020 const SessionDescription* sdesc) {
2021 return GetFirstDataContent(sdesc);
2022}
2023
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002024bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 if (data_channel_type_ == DCT_SCTP) {
2026 // TODO(pthatcher): Do this in a more robust way by checking for
2027 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002028 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 } else if (data_channel_type_ == DCT_RTP) {
2030 return BaseChannel::WantsPacket(rtcp, packet);
2031 }
2032 return false;
2033}
2034
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002035bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2036 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037 // It hasn't been set before, so set it now.
2038 if (data_channel_type_ == DCT_NONE) {
2039 data_channel_type_ = new_data_channel_type;
2040 return true;
2041 }
2042
2043 // It's been set before, but doesn't match. That's bad.
2044 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002045 std::ostringstream desc;
2046 desc << "Data channel type mismatch."
2047 << " Expected " << data_channel_type_
2048 << " Got " << new_data_channel_type;
2049 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050 return false;
2051 }
2052
2053 // It's hasn't changed. Nothing to do.
2054 return true;
2055}
2056
2057bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002058 const DataContentDescription* content,
2059 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2061 (content->protocol() == kMediaProtocolDtlsSctp));
2062 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002063 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064}
2065
2066bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002067 ContentAction action,
2068 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002069 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070 LOG(LS_INFO) << "Setting local data description";
2071
2072 const DataContentDescription* data =
2073 static_cast<const DataContentDescription*>(content);
2074 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002075 if (!data) {
2076 SafeSetError("Can't find data content in local description.", error_desc);
2077 return false;
2078 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002080 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002081 return false;
2082 }
2083
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002084 if (data_channel_type_ == DCT_RTP) {
2085 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2086 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 }
2088 }
2089
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002090 // FYI: We send the SCTP port number (not to be confused with the
2091 // underlying UDP port number) as a codec parameter. So even SCTP
2092 // data channels need codecs.
2093 DataRecvParameters recv_params = last_recv_params_;
2094 RtpParametersFromMediaDescription(data, &recv_params);
2095 if (!media_channel()->SetRecvParameters(recv_params)) {
2096 SafeSetError("Failed to set remote data description recv parameters.",
2097 error_desc);
2098 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002100 if (data_channel_type_ == DCT_RTP) {
2101 for (const DataCodec& codec : data->codecs()) {
2102 bundle_filter()->AddPayloadType(codec.id);
2103 }
2104 }
2105 last_recv_params_ = recv_params;
2106
2107 // TODO(pthatcher): Move local streams into DataSendParameters, and
2108 // only give it to the media channel once we have a remote
2109 // description too (without a remote description, we won't be able
2110 // to send them anyway).
2111 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2112 SafeSetError("Failed to set local data description streams.", error_desc);
2113 return false;
2114 }
2115
2116 set_local_content_direction(content->direction());
2117 ChangeState();
2118 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119}
2120
2121bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002122 ContentAction action,
2123 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002124 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125
2126 const DataContentDescription* data =
2127 static_cast<const DataContentDescription*>(content);
2128 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002129 if (!data) {
2130 SafeSetError("Can't find data content in remote description.", error_desc);
2131 return false;
2132 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002134 // If the remote data doesn't have codecs and isn't an update, it
2135 // must be empty, so ignore it.
2136 if (!data->has_codecs() && action != CA_UPDATE) {
2137 return true;
2138 }
2139
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002140 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141 return false;
2142 }
2143
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002144 LOG(LS_INFO) << "Setting remote data description";
2145 if (data_channel_type_ == DCT_RTP &&
2146 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2147 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148 }
2149
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002150
2151 DataSendParameters send_params = last_send_params_;
2152 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2153 if (!media_channel()->SetSendParameters(send_params)) {
2154 SafeSetError("Failed to set remote data description send parameters.",
2155 error_desc);
2156 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002158 last_send_params_ = send_params;
2159
2160 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2161 // and only give it to the media channel once we have a local
2162 // description too (without a local description, we won't be able to
2163 // recv them anyway).
2164 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2165 SafeSetError("Failed to set remote data description streams.",
2166 error_desc);
2167 return false;
2168 }
2169
2170 set_remote_content_direction(content->direction());
2171 ChangeState();
2172 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173}
2174
2175void DataChannel::ChangeState() {
2176 // Render incoming data if we're the active call, and we have the local
2177 // content. We receive data on the default channel and multiplexed streams.
2178 bool recv = IsReadyToReceive();
2179 if (!media_channel()->SetReceive(recv)) {
2180 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2181 }
2182
2183 // Send outgoing data if we're the active call, we have the remote content,
2184 // and we have had some form of connectivity.
2185 bool send = IsReadyToSend();
2186 if (!media_channel()->SetSend(send)) {
2187 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2188 }
2189
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002190 // Trigger SignalReadyToSendData asynchronously.
2191 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192
2193 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2194}
2195
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002196void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 switch (pmsg->message_id) {
2198 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002199 DataChannelReadyToSendMessageData* data =
2200 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002201 ready_to_send_data_ = data->data();
2202 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203 delete data;
2204 break;
2205 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 case MSG_DATARECEIVED: {
2207 DataReceivedMessageData* data =
2208 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2209 SignalDataReceived(this, data->params, data->payload);
2210 delete data;
2211 break;
2212 }
2213 case MSG_CHANNEL_ERROR: {
2214 const DataChannelErrorMessageData* data =
2215 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216 delete data;
2217 break;
2218 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002219 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002220 rtc::TypedMessageData<uint32_t>* data =
2221 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002222 SignalStreamClosedRemotely(data->data());
2223 delete data;
2224 break;
2225 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226 default:
2227 BaseChannel::OnMessage(pmsg);
2228 break;
2229 }
2230}
2231
2232void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002233 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 SignalConnectionMonitor(this, infos);
2235}
2236
2237void DataChannel::StartMediaMonitor(int cms) {
2238 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002239 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240 media_monitor_->SignalUpdate.connect(
2241 this, &DataChannel::OnMediaMonitorUpdate);
2242 media_monitor_->Start(cms);
2243}
2244
2245void DataChannel::StopMediaMonitor() {
2246 if (media_monitor_) {
2247 media_monitor_->Stop();
2248 media_monitor_->SignalUpdate.disconnect(this);
2249 media_monitor_.reset();
2250 }
2251}
2252
2253void DataChannel::OnMediaMonitorUpdate(
2254 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2255 ASSERT(media_channel == this->media_channel());
2256 SignalMediaMonitor(this, info);
2257}
2258
2259void DataChannel::OnDataReceived(
2260 const ReceiveDataParams& params, const char* data, size_t len) {
2261 DataReceivedMessageData* msg = new DataReceivedMessageData(
2262 params, data, len);
2263 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2264}
2265
Peter Boström0c4e06b2015-10-07 12:23:21 +02002266void DataChannel::OnDataChannelError(uint32_t ssrc,
2267 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002268 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2269 ssrc, err);
2270 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2271}
2272
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002273void DataChannel::OnDataChannelReadyToSend(bool writable) {
2274 // This is usded for congestion control to indicate that the stream is ready
2275 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2276 // that the transport channel is ready.
2277 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2278 new DataChannelReadyToSendMessageData(writable));
2279}
2280
guoweis318166b2015-11-18 19:03:38 -08002281void DataChannel::GetSrtpCryptoSuiteNames(
2282 std::vector<std::string>* ciphers) const {
2283 GetSupportedDataCryptoSuites(ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284}
2285
2286bool DataChannel::ShouldSetupDtlsSrtp() const {
2287 return (data_channel_type_ == DCT_RTP);
2288}
2289
Peter Boström0c4e06b2015-10-07 12:23:21 +02002290void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2291 rtc::TypedMessageData<uint32_t>* message =
2292 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002293 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2294}
2295
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296} // namespace cricket