blob: c0f7f23f33ee576913f6b477830f0765888e60e3 [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;
70 rtc::DiffServCodePoint dscp;
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
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000221 if (!SetDtlsSrtpCiphers(transport_channel(), false)) {
222 return false;
223 }
deadbeefcbecd352015-09-23 11:50:27 -0700224 if (rtcp_transport_enabled() &&
225 !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,
426 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000427 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428}
429
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000430bool BaseChannel::SendRtcp(rtc::Buffer* packet,
431 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000432 return SendPacket(true, packet, dscp);
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
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000501bool BaseChannel::SendPacket(bool rtcp, rtc::Buffer* packet,
502 rtc::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 // SendPacket gets called from MediaEngine, typically on an encoder thread.
504 // If the thread is not our worker thread, we will post to our worker
505 // so that the real work happens on our worker. This avoids us having to
506 // synchronize access to all the pieces of the send path, including
507 // SRTP and the inner workings of the transport channels.
508 // The only downside is that we can't return a proper failure code if
509 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000510 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 // Avoid a copy by transferring the ownership of the packet data.
512 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
513 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200514 data->packet = packet->Pass();
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000515 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516 worker_thread_->Post(this, message_id, data);
517 return true;
518 }
519
520 // Now that we are on the correct thread, ensure we have a place to send this
521 // packet before doing anything. (We might get RTCP packets that we don't
522 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
523 // transport.
524 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
525 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000526 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 return false;
528 }
529
530 // Protect ourselves against crazy data.
531 if (!ValidPacket(rtcp, packet)) {
532 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000533 << PacketType(rtcp)
534 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 return false;
536 }
537
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000538 rtc::PacketOptions options(dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 // Protect if needed.
540 if (srtp_filter_.IsActive()) {
541 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200542 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000543 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000545 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
546 // inside libsrtp for a RTP packet. A external HMAC module will be writing
547 // a fake HMAC value. This is ONLY done for a RTP packet.
548 // Socket layer will update rtp sendtime extension header if present in
549 // packet with current time before updating the HMAC.
550#if !defined(ENABLE_EXTERNAL_AUTH)
551 res = srtp_filter_.ProtectRtp(
552 data, len, static_cast<int>(packet->capacity()), &len);
553#else
henrike@webrtc.org05376342014-03-10 15:53:12 +0000554 options.packet_time_params.rtp_sendtime_extension_id =
555 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000556 res = srtp_filter_.ProtectRtp(
557 data, len, static_cast<int>(packet->capacity()), &len,
558 &options.packet_time_params.srtp_packet_index);
559 // If protection succeeds, let's get auth params from srtp.
560 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200561 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000562 int key_len;
563 res = srtp_filter_.GetRtpAuthParams(
564 &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
565 if (res) {
566 options.packet_time_params.srtp_auth_key.resize(key_len);
567 options.packet_time_params.srtp_auth_key.assign(auth_key,
568 auth_key + key_len);
569 }
570 }
571#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 if (!res) {
573 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200574 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 GetRtpSeqNum(data, len, &seq_num);
576 GetRtpSsrc(data, len, &ssrc);
577 LOG(LS_ERROR) << "Failed to protect " << content_name_
578 << " RTP packet: size=" << len
579 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
580 return false;
581 }
582 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000583 res = srtp_filter_.ProtectRtcp(data, len,
584 static_cast<int>(packet->capacity()),
585 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 if (!res) {
587 int type = -1;
588 GetRtcpType(data, len, &type);
589 LOG(LS_ERROR) << "Failed to protect " << content_name_
590 << " RTCP packet: size=" << len << ", type=" << type;
591 return false;
592 }
593 }
594
595 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000596 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 } else if (secure_required_) {
598 // This is a double check for something that supposedly can't happen.
599 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
600 << " packet when SRTP is inactive and crypto is required";
601
602 ASSERT(false);
603 return false;
604 }
605
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000607 int ret =
Karl Wiberg94784372015-04-20 14:03:07 +0200608 channel->SendPacket(packet->data<char>(), packet->size(), options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000609 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
610 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 if (channel->GetError() == EWOULDBLOCK) {
612 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700613 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 }
615 return false;
616 }
617 return true;
618}
619
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000620bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 // Protect ourselves against crazy data.
622 if (!ValidPacket(rtcp, packet)) {
623 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000624 << PacketType(rtcp)
625 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 return false;
627 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000628
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000629 // Bundle filter handles both rtp and rtcp packets.
Karl Wiberg94784372015-04-20 14:03:07 +0200630 return bundle_filter_.DemuxPacket(packet->data<char>(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631}
632
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000633void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
634 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 if (!WantsPacket(rtcp, packet)) {
636 return;
637 }
638
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000639 // We are only interested in the first rtp packet because that
640 // indicates the media has started flowing.
641 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 has_received_packet_ = true;
643 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
644 }
645
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 // Unprotect the packet, if needed.
647 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200648 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000649 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 bool res;
651 if (!rtcp) {
652 res = srtp_filter_.UnprotectRtp(data, len, &len);
653 if (!res) {
654 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200655 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 GetRtpSeqNum(data, len, &seq_num);
657 GetRtpSsrc(data, len, &ssrc);
658 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
659 << " RTP packet: size=" << len
660 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
661 return;
662 }
663 } else {
664 res = srtp_filter_.UnprotectRtcp(data, len, &len);
665 if (!res) {
666 int type = -1;
667 GetRtcpType(data, len, &type);
668 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
669 << " RTCP packet: size=" << len << ", type=" << type;
670 return;
671 }
672 }
673
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000674 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 } else if (secure_required_) {
676 // Our session description indicates that SRTP is required, but we got a
677 // packet before our SRTP filter is active. This means either that
678 // a) we got SRTP packets before we received the SDES keys, in which case
679 // we can't decrypt it anyway, or
680 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
681 // channels, so we haven't yet extracted keys, even if DTLS did complete
682 // on the channel that the packets are being sent on. It's really good
683 // practice to wait for both RTP and RTCP to be good to go before sending
684 // media, to prevent weird failure modes, so it's fine for us to just eat
685 // packets here. This is all sidestepped if RTCP mux is used anyway.
686 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
687 << " packet when SRTP is inactive and crypto is required";
688 return;
689 }
690
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 // Push it down to the media channel.
692 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000693 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000695 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 }
697}
698
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000699bool BaseChannel::PushdownLocalDescription(
700 const SessionDescription* local_desc, ContentAction action,
701 std::string* error_desc) {
702 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 const MediaContentDescription* content_desc =
704 GetContentDescription(content_info);
705 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000706 !SetLocalContent(content_desc, action, error_desc)) {
707 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
708 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000710 return true;
711}
712
713bool BaseChannel::PushdownRemoteDescription(
714 const SessionDescription* remote_desc, ContentAction action,
715 std::string* error_desc) {
716 const ContentInfo* content_info = GetFirstContent(remote_desc);
717 const MediaContentDescription* content_desc =
718 GetContentDescription(content_info);
719 if (content_desc && content_info && !content_info->rejected &&
720 !SetRemoteContent(content_desc, action, error_desc)) {
721 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
722 return false;
723 }
724 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725}
726
727void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000728 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 if (enabled_)
730 return;
731
732 LOG(LS_INFO) << "Channel enabled";
733 enabled_ = true;
734 ChangeState();
735}
736
737void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000738 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 if (!enabled_)
740 return;
741
742 LOG(LS_INFO) << "Channel disabled";
743 enabled_ = false;
744 ChangeState();
745}
746
deadbeefcbecd352015-09-23 11:50:27 -0700747void BaseChannel::UpdateWritableState_w() {
748 if (transport_channel_ && transport_channel_->writable() &&
749 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
750 ChannelWritable_w();
751 } else {
752 ChannelNotWritable_w();
753 }
754}
755
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000757 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 if (writable_)
759 return;
760
deadbeefcbecd352015-09-23 11:50:27 -0700761 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 << (was_ever_writable_ ? "" : " for the first time");
763
764 std::vector<ConnectionInfo> infos;
765 transport_channel_->GetStats(&infos);
766 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
767 it != infos.end(); ++it) {
768 if (it->best_connection) {
769 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
770 << "->" << it->remote_candidate.ToSensitiveString();
771 break;
772 }
773 }
774
775 // If we're doing DTLS-SRTP, now is the time.
776 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
777 if (!SetupDtlsSrtp(false)) {
deadbeefcbecd352015-09-23 11:50:27 -0700778 SignalDtlsSetupFailure_w(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 return;
780 }
781
782 if (rtcp_transport_channel_) {
783 if (!SetupDtlsSrtp(true)) {
deadbeefcbecd352015-09-23 11:50:27 -0700784 SignalDtlsSetupFailure_w(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 return;
786 }
787 }
788 }
789
790 was_ever_writable_ = true;
791 writable_ = true;
792 ChangeState();
793}
794
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000795void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
796 ASSERT(worker_thread() == rtc::Thread::Current());
797 signaling_thread()->Invoke<void>(Bind(
798 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
799}
800
801void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
802 ASSERT(signaling_thread() == rtc::Thread::Current());
803 SignalDtlsSetupFailure(this, rtcp);
804}
805
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
807 std::vector<std::string> ciphers;
808 // We always use the default SRTP ciphers for RTCP, but we may use different
809 // ciphers for RTP depending on the media type.
810 if (!rtcp) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700811 GetSrtpCryptoSuiteNames(&ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 } else {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700813 GetDefaultSrtpCryptoSuiteNames(&ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 }
815 return tc->SetSrtpCiphers(ciphers);
816}
817
818bool BaseChannel::ShouldSetupDtlsSrtp() const {
819 return true;
820}
821
822// This function returns true if either DTLS-SRTP is not in use
823// *or* DTLS-SRTP is successfully set up.
824bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
825 bool ret = false;
826
deadbeefcbecd352015-09-23 11:50:27 -0700827 TransportChannel* channel =
828 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829
830 // No DTLS
831 if (!channel->IsDtlsActive())
832 return true;
833
834 std::string selected_cipher;
835
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700836 if (!channel->GetSrtpCryptoSuite(&selected_cipher)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
838 return false;
839 }
840
841 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
842 << content_name() << " "
843 << PacketType(rtcp_channel);
844
845 // OK, we're now doing DTLS (RFC 5764)
846 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
847 SRTP_MASTER_KEY_SALT_LEN * 2);
848
849 // RFC 5705 exporter using the RFC 5764 parameters
850 if (!channel->ExportKeyingMaterial(
851 kDtlsSrtpExporterLabel,
852 NULL, 0, false,
853 &dtls_buffer[0], dtls_buffer.size())) {
854 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
855 ASSERT(false); // This should never happen
856 return false;
857 }
858
859 // Sync up the keys with the DTLS-SRTP interface
860 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
861 SRTP_MASTER_KEY_SALT_LEN);
862 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
863 SRTP_MASTER_KEY_SALT_LEN);
864 size_t offset = 0;
865 memcpy(&client_write_key[0], &dtls_buffer[offset],
866 SRTP_MASTER_KEY_KEY_LEN);
867 offset += SRTP_MASTER_KEY_KEY_LEN;
868 memcpy(&server_write_key[0], &dtls_buffer[offset],
869 SRTP_MASTER_KEY_KEY_LEN);
870 offset += SRTP_MASTER_KEY_KEY_LEN;
871 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
872 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
873 offset += SRTP_MASTER_KEY_SALT_LEN;
874 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
875 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
876
877 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000878 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000879 if (!channel->GetSslRole(&role)) {
880 LOG(LS_WARNING) << "GetSslRole failed";
881 return false;
882 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000884 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 send_key = &server_write_key;
886 recv_key = &client_write_key;
887 } else {
888 send_key = &client_write_key;
889 recv_key = &server_write_key;
890 }
891
892 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000893 ret = srtp_filter_.SetRtcpParams(
894 selected_cipher,
895 &(*send_key)[0],
896 static_cast<int>(send_key->size()),
897 selected_cipher,
898 &(*recv_key)[0],
899 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000901 ret = srtp_filter_.SetRtpParams(
902 selected_cipher,
903 &(*send_key)[0],
904 static_cast<int>(send_key->size()),
905 selected_cipher,
906 &(*recv_key)[0],
907 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 }
909
910 if (!ret)
911 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
912 else
913 dtls_keyed_ = true;
914
915 return ret;
916}
917
918void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000919 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 if (!writable_)
921 return;
922
deadbeefcbecd352015-09-23 11:50:27 -0700923 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 writable_ = false;
925 ChangeState();
926}
927
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700928bool BaseChannel::SetRtpTransportParameters_w(
929 const MediaContentDescription* content,
930 ContentAction action,
931 ContentSource src,
932 std::string* error_desc) {
933 if (action == CA_UPDATE) {
934 // These parameters never get changed by a CA_UDPATE.
935 return true;
936 }
937
938 // Cache secure_required_ for belt and suspenders check on SendPacket
939 if (src == CS_LOCAL) {
940 set_secure_required(content->crypto_required() != CT_NONE);
941 }
942
943 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
944 return false;
945 }
946
947 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
948 return false;
949 }
950
951 return true;
952}
953
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000954// |dtls| will be set to true if DTLS is active for transport channel and
955// crypto is empty.
956bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000957 bool* dtls,
958 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000959 *dtls = transport_channel_->IsDtlsActive();
960 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000961 SafeSetError("Cryptos must be empty when DTLS is active.",
962 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000963 return false;
964 }
965 return true;
966}
967
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000969 ContentAction action,
970 ContentSource src,
971 std::string* error_desc) {
972 if (action == CA_UPDATE) {
973 // no crypto params.
974 return true;
975 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000977 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000978 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
979 if (!ret) {
980 return false;
981 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 switch (action) {
983 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000984 // If DTLS is already active on the channel, we could be renegotiating
985 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000986 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000987 ret = srtp_filter_.SetOffer(cryptos, src);
988 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 break;
990 case CA_PRANSWER:
991 // If we're doing DTLS-SRTP, we don't want to update the filter
992 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000993 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
995 }
996 break;
997 case CA_ANSWER:
998 // If we're doing DTLS-SRTP, we don't want to update the filter
999 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001000 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 ret = srtp_filter_.SetAnswer(cryptos, src);
1002 }
1003 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 default:
1005 break;
1006 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001007 if (!ret) {
1008 SafeSetError("Failed to setup SRTP filter.", error_desc);
1009 return false;
1010 }
1011 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012}
1013
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001014void BaseChannel::ActivateRtcpMux() {
1015 worker_thread_->Invoke<void>(Bind(
1016 &BaseChannel::ActivateRtcpMux_w, this));
1017}
1018
1019void BaseChannel::ActivateRtcpMux_w() {
1020 if (!rtcp_mux_filter_.IsActive()) {
1021 rtcp_mux_filter_.SetActive();
deadbeefcbecd352015-09-23 11:50:27 -07001022 set_rtcp_transport_channel(nullptr);
1023 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001024 }
1025}
1026
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001028 ContentSource src,
1029 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030 bool ret = false;
1031 switch (action) {
1032 case CA_OFFER:
1033 ret = rtcp_mux_filter_.SetOffer(enable, src);
1034 break;
1035 case CA_PRANSWER:
1036 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1037 break;
1038 case CA_ANSWER:
1039 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1040 if (ret && rtcp_mux_filter_.IsActive()) {
1041 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001042 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1043 << " by destroying RTCP transport channel for "
1044 << transport_name();
1045 set_rtcp_transport_channel(nullptr);
1046 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 }
1048 break;
1049 case CA_UPDATE:
1050 // No RTCP mux info.
1051 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001052 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 default:
1054 break;
1055 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001056 if (!ret) {
1057 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1058 return false;
1059 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1061 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1062 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001063 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 // If the RTP transport is already writable, then so are we.
1065 if (transport_channel_->writable()) {
1066 ChannelWritable_w();
1067 }
1068 }
1069
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001070 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071}
1072
1073bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001074 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 if (!media_channel()->AddRecvStream(sp))
1076 return false;
1077
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001078 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079}
1080
Peter Boström0c4e06b2015-10-07 12:23:21 +02001081bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001082 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001083 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 return media_channel()->RemoveRecvStream(ssrc);
1085}
1086
1087bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001088 ContentAction action,
1089 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1091 action == CA_PRANSWER || action == CA_UPDATE))
1092 return false;
1093
1094 // If this is an update, streams only contain streams that have changed.
1095 if (action == CA_UPDATE) {
1096 for (StreamParamsVec::const_iterator it = streams.begin();
1097 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001098 const StreamParams* existing_stream =
1099 GetStreamByIds(local_streams_, it->groupid, it->id);
1100 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 if (media_channel()->AddSendStream(*it)) {
1102 local_streams_.push_back(*it);
1103 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1104 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001105 std::ostringstream desc;
1106 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1107 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 return false;
1109 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001110 } else if (existing_stream && !it->has_ssrcs()) {
1111 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001112 std::ostringstream desc;
1113 desc << "Failed to remove send stream with ssrc "
1114 << it->first_ssrc() << ".";
1115 SafeSetError(desc.str(), error_desc);
1116 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001118 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 } else {
1120 LOG(LS_WARNING) << "Ignore unsupported stream update";
1121 }
1122 }
1123 return true;
1124 }
1125 // Else streams are all the streams we want to send.
1126
1127 // Check for streams that have been removed.
1128 bool ret = true;
1129 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1130 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001131 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001133 std::ostringstream desc;
1134 desc << "Failed to remove send stream with ssrc "
1135 << it->first_ssrc() << ".";
1136 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 ret = false;
1138 }
1139 }
1140 }
1141 // Check for new streams.
1142 for (StreamParamsVec::const_iterator it = streams.begin();
1143 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001144 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 if (media_channel()->AddSendStream(*it)) {
1146 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1147 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001148 std::ostringstream desc;
1149 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1150 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151 ret = false;
1152 }
1153 }
1154 }
1155 local_streams_ = streams;
1156 return ret;
1157}
1158
1159bool BaseChannel::UpdateRemoteStreams_w(
1160 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001161 ContentAction action,
1162 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1164 action == CA_PRANSWER || action == CA_UPDATE))
1165 return false;
1166
1167 // If this is an update, streams only contain streams that have changed.
1168 if (action == CA_UPDATE) {
1169 for (StreamParamsVec::const_iterator it = streams.begin();
1170 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001171 const StreamParams* existing_stream =
1172 GetStreamByIds(remote_streams_, it->groupid, it->id);
1173 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001174 if (AddRecvStream_w(*it)) {
1175 remote_streams_.push_back(*it);
1176 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1177 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001178 std::ostringstream desc;
1179 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1180 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 return false;
1182 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001183 } else if (existing_stream && !it->has_ssrcs()) {
1184 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001185 std::ostringstream desc;
1186 desc << "Failed to remove remote stream with ssrc "
1187 << it->first_ssrc() << ".";
1188 SafeSetError(desc.str(), error_desc);
1189 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001191 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 } else {
1193 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001194 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 << " new stream = " << it->ToString();
1196 }
1197 }
1198 return true;
1199 }
1200 // Else streams are all the streams we want to receive.
1201
1202 // Check for streams that have been removed.
1203 bool ret = true;
1204 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1205 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001206 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001208 std::ostringstream desc;
1209 desc << "Failed to remove remote stream with ssrc "
1210 << it->first_ssrc() << ".";
1211 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 ret = false;
1213 }
1214 }
1215 }
1216 // Check for new streams.
1217 for (StreamParamsVec::const_iterator it = streams.begin();
1218 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001219 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 if (AddRecvStream_w(*it)) {
1221 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1222 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001223 std::ostringstream desc;
1224 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1225 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 ret = false;
1227 }
1228 }
1229 }
1230 remote_streams_ = streams;
1231 return ret;
1232}
1233
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001234void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1235 const std::vector<RtpHeaderExtension>& extensions) {
1236 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001237 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001238 rtp_abs_sendtime_extn_id_ =
1239 send_time_extension ? send_time_extension->id : -1;
1240}
1241
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001242void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 case MSG_RTPPACKET:
1245 case MSG_RTCPPACKET: {
1246 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001247 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 delete data; // because it is Posted
1249 break;
1250 }
1251 case MSG_FIRSTPACKETRECEIVED: {
1252 SignalFirstPacketReceived(this);
1253 break;
1254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 }
1256}
1257
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258void BaseChannel::FlushRtcpMessages() {
1259 // Flush all remaining RTCP messages. This should only be called in
1260 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001261 ASSERT(rtc::Thread::Current() == worker_thread_);
1262 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001263 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001264 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001266 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267 }
1268}
1269
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001270VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 MediaEngineInterface* media_engine,
1272 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001273 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 const std::string& content_name,
1275 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001276 : BaseChannel(thread,
1277 media_channel,
1278 transport_controller,
1279 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001281 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001282 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283
1284VoiceChannel::~VoiceChannel() {
1285 StopAudioMonitor();
1286 StopMediaMonitor();
1287 // this can't be done in the base class, since it calls a virtual
1288 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001289 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290}
1291
1292bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001293 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 return false;
1295 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 return true;
1297}
1298
Peter Boström0c4e06b2015-10-07 12:23:21 +02001299bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001300 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001301 const AudioOptions* options,
1302 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001303 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001304 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305}
1306
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307// TODO(juberti): Handle early media the right way. We should get an explicit
1308// ringing message telling us to start playing local ringback, which we cancel
1309// if any early media actually arrives. For now, we do the opposite, which is
1310// to wait 1 second for early media, and start playing local ringback if none
1311// arrives.
1312void VoiceChannel::SetEarlyMedia(bool enable) {
1313 if (enable) {
1314 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001315 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1316 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 } else {
1318 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001319 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320 }
1321}
1322
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323bool VoiceChannel::PressDTMF(int digit, bool playout) {
1324 int flags = DF_SEND;
1325 if (playout) {
1326 flags |= DF_PLAY;
1327 }
1328 int duration_ms = 160;
1329 return InsertDtmf(0, digit, duration_ms, flags);
1330}
1331
1332bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001333 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1334 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335}
1336
Peter Boström0c4e06b2015-10-07 12:23:21 +02001337bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1338 int event_code,
1339 int duration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001341 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1342 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343}
1344
solenberg4bac9c52015-10-09 02:32:53 -07001345bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1346 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1347 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001349
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001351 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1352 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353}
1354
1355void VoiceChannel::StartMediaMonitor(int cms) {
1356 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001357 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 media_monitor_->SignalUpdate.connect(
1359 this, &VoiceChannel::OnMediaMonitorUpdate);
1360 media_monitor_->Start(cms);
1361}
1362
1363void VoiceChannel::StopMediaMonitor() {
1364 if (media_monitor_) {
1365 media_monitor_->Stop();
1366 media_monitor_->SignalUpdate.disconnect(this);
1367 media_monitor_.reset();
1368 }
1369}
1370
1371void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001372 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373 audio_monitor_
1374 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1375 audio_monitor_->Start(cms);
1376}
1377
1378void VoiceChannel::StopAudioMonitor() {
1379 if (audio_monitor_) {
1380 audio_monitor_->Stop();
1381 audio_monitor_.reset();
1382 }
1383}
1384
1385bool VoiceChannel::IsAudioMonitorRunning() const {
1386 return (audio_monitor_.get() != NULL);
1387}
1388
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001389int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001390 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391}
1392
1393int VoiceChannel::GetOutputLevel_w() {
1394 return media_channel()->GetOutputLevel();
1395}
1396
1397void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1398 media_channel()->GetActiveStreams(actives);
1399}
1400
1401void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001402 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001403 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001404 int flags) {
1405 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001406
1407 // Set a flag when we've received an RTP packet. If we're waiting for early
1408 // media, this will disable the timeout.
1409 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1410 received_media_ = true;
1411 }
1412}
1413
1414void VoiceChannel::ChangeState() {
1415 // Render incoming data if we're the active call, and we have the local
1416 // content. We receive data on the default channel and multiplexed streams.
1417 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001418 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419
1420 // Send outgoing data if we're the active call, we have the remote content,
1421 // and we have had some form of connectivity.
1422 bool send = IsReadyToSend();
1423 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1424 if (!media_channel()->SetSend(send_flag)) {
1425 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426 }
1427
1428 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1429}
1430
1431const ContentInfo* VoiceChannel::GetFirstContent(
1432 const SessionDescription* sdesc) {
1433 return GetFirstAudioContent(sdesc);
1434}
1435
1436bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001437 ContentAction action,
1438 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001439 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440 LOG(LS_INFO) << "Setting local voice description";
1441
1442 const AudioContentDescription* audio =
1443 static_cast<const AudioContentDescription*>(content);
1444 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001445 if (!audio) {
1446 SafeSetError("Can't find audio content in local description.", error_desc);
1447 return false;
1448 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001450 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1451 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452 }
1453
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001454 AudioRecvParameters recv_params = last_recv_params_;
1455 RtpParametersFromMediaDescription(audio, &recv_params);
1456 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001457 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001458 error_desc);
1459 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001461 for (const AudioCodec& codec : audio->codecs()) {
1462 bundle_filter()->AddPayloadType(codec.id);
1463 }
1464 last_recv_params_ = recv_params;
1465
1466 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1467 // only give it to the media channel once we have a remote
1468 // description too (without a remote description, we won't be able
1469 // to send them anyway).
1470 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1471 SafeSetError("Failed to set local audio description streams.", error_desc);
1472 return false;
1473 }
1474
1475 set_local_content_direction(content->direction());
1476 ChangeState();
1477 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478}
1479
1480bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001481 ContentAction action,
1482 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001483 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484 LOG(LS_INFO) << "Setting remote voice description";
1485
1486 const AudioContentDescription* audio =
1487 static_cast<const AudioContentDescription*>(content);
1488 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001489 if (!audio) {
1490 SafeSetError("Can't find audio content in remote description.", error_desc);
1491 return false;
1492 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001494 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1495 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 }
1497
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001498 AudioSendParameters send_params = last_send_params_;
1499 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001500 if (audio->agc_minus_10db()) {
1501 send_params.options.adjust_agc_delta.Set(kAgcMinus10db);
1502 }
1503 if (!media_channel()->SetSendParameters(send_params)) {
1504 SafeSetError("Failed to set remote audio description send parameters.",
1505 error_desc);
1506 return false;
1507 }
1508 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001510 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1511 // and only give it to the media channel once we have a local
1512 // description too (without a local description, we won't be able to
1513 // recv them anyway).
1514 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1515 SafeSetError("Failed to set remote audio description streams.", error_desc);
1516 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001517 }
1518
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001519 if (audio->rtp_header_extensions_set()) {
1520 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1521 }
1522
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001523 set_remote_content_direction(content->direction());
1524 ChangeState();
1525 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526}
1527
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528void VoiceChannel::HandleEarlyMediaTimeout() {
1529 // This occurs on the main thread, not the worker thread.
1530 if (!received_media_) {
1531 LOG(LS_INFO) << "No early media received before timeout";
1532 SignalEarlyMediaTimeout(this);
1533 }
1534}
1535
Peter Boström0c4e06b2015-10-07 12:23:21 +02001536bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1537 int event,
1538 int duration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539 int flags) {
1540 if (!enabled()) {
1541 return false;
1542 }
1543
1544 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1545}
1546
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001547void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 case MSG_EARLYMEDIATIMEOUT:
1550 HandleEarlyMediaTimeout();
1551 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 case MSG_CHANNEL_ERROR: {
1553 VoiceChannelErrorMessageData* data =
1554 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 delete data;
1556 break;
1557 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 default:
1559 BaseChannel::OnMessage(pmsg);
1560 break;
1561 }
1562}
1563
1564void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001565 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 SignalConnectionMonitor(this, infos);
1567}
1568
1569void VoiceChannel::OnMediaMonitorUpdate(
1570 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1571 ASSERT(media_channel == this->media_channel());
1572 SignalMediaMonitor(this, info);
1573}
1574
1575void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1576 const AudioInfo& info) {
1577 SignalAudioMonitor(this, info);
1578}
1579
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001580void VoiceChannel::GetSrtpCryptoSuiteNames(
1581 std::vector<std::string>* ciphers) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 GetSupportedAudioCryptoSuites(ciphers);
1583}
1584
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001585VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001587 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001589 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001590 : BaseChannel(thread,
1591 media_channel,
1592 transport_controller,
1593 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001596 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597
1598bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001599 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 return false;
1601 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 return true;
1603}
1604
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605VideoChannel::~VideoChannel() {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001606 std::vector<uint32_t> screencast_ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 ScreencastMap::iterator iter;
1608 while (!screencast_capturers_.empty()) {
1609 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1610 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1611 << screencast_capturers_.begin()->first;
1612 ASSERT(false);
1613 break;
1614 }
1615 }
1616
1617 StopMediaMonitor();
1618 // this can't be done in the base class, since it calls a virtual
1619 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001620
1621 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622}
1623
Peter Boström0c4e06b2015-10-07 12:23:21 +02001624bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001625 worker_thread()->Invoke<void>(Bind(
1626 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627 return true;
1628}
1629
1630bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001631 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632}
1633
Peter Boström0c4e06b2015-10-07 12:23:21 +02001634bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001635 return worker_thread()->Invoke<bool>(Bind(
1636 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637}
1638
Peter Boström0c4e06b2015-10-07 12:23:21 +02001639bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001640 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1641 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642}
1643
Peter Boström0c4e06b2015-10-07 12:23:21 +02001644bool VideoChannel::RemoveScreencast(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001645 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646}
1647
1648bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001649 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650}
1651
Peter Boström0c4e06b2015-10-07 12:23:21 +02001652int VideoChannel::GetScreencastFps(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001653 ScreencastDetailsData data(ssrc);
1654 worker_thread()->Invoke<void>(Bind(
1655 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001656 return data.fps;
1657}
1658
Peter Boström0c4e06b2015-10-07 12:23:21 +02001659int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001660 ScreencastDetailsData data(ssrc);
1661 worker_thread()->Invoke<void>(Bind(
1662 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001663 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664}
1665
1666bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001667 worker_thread()->Invoke<void>(Bind(
1668 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 return true;
1670}
1671
1672bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001673 worker_thread()->Invoke<void>(Bind(
1674 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 return true;
1676}
1677
Peter Boström0c4e06b2015-10-07 12:23:21 +02001678bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001679 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001680 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001681 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1682 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001683}
1684
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686 // Send outgoing data if we're the active call, we have the remote content,
1687 // and we have had some form of connectivity.
1688 bool send = IsReadyToSend();
1689 if (!media_channel()->SetSend(send)) {
1690 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1691 // TODO(gangji): Report error back to server.
1692 }
1693
Peter Boström34fbfff2015-09-24 19:20:30 +02001694 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695}
1696
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001697bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1698 return InvokeOnWorker(
1699 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700}
1701
1702void VideoChannel::StartMediaMonitor(int cms) {
1703 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001704 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 media_monitor_->SignalUpdate.connect(
1706 this, &VideoChannel::OnMediaMonitorUpdate);
1707 media_monitor_->Start(cms);
1708}
1709
1710void VideoChannel::StopMediaMonitor() {
1711 if (media_monitor_) {
1712 media_monitor_->Stop();
1713 media_monitor_.reset();
1714 }
1715}
1716
1717const ContentInfo* VideoChannel::GetFirstContent(
1718 const SessionDescription* sdesc) {
1719 return GetFirstVideoContent(sdesc);
1720}
1721
1722bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001723 ContentAction action,
1724 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001725 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 LOG(LS_INFO) << "Setting local video description";
1727
1728 const VideoContentDescription* video =
1729 static_cast<const VideoContentDescription*>(content);
1730 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001731 if (!video) {
1732 SafeSetError("Can't find video content in local description.", error_desc);
1733 return false;
1734 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001736 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1737 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 }
1739
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001740 VideoRecvParameters recv_params = last_recv_params_;
1741 RtpParametersFromMediaDescription(video, &recv_params);
1742 if (!media_channel()->SetRecvParameters(recv_params)) {
1743 SafeSetError("Failed to set local video description recv parameters.",
1744 error_desc);
1745 return false;
1746 }
1747 for (const VideoCodec& codec : video->codecs()) {
1748 bundle_filter()->AddPayloadType(codec.id);
1749 }
1750 last_recv_params_ = recv_params;
1751
1752 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1753 // only give it to the media channel once we have a remote
1754 // description too (without a remote description, we won't be able
1755 // to send them anyway).
1756 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1757 SafeSetError("Failed to set local video description streams.", error_desc);
1758 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 }
1760
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001761 set_local_content_direction(content->direction());
1762 ChangeState();
1763 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764}
1765
1766bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001767 ContentAction action,
1768 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001769 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 LOG(LS_INFO) << "Setting remote video description";
1771
1772 const VideoContentDescription* video =
1773 static_cast<const VideoContentDescription*>(content);
1774 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001775 if (!video) {
1776 SafeSetError("Can't find video content in remote description.", error_desc);
1777 return false;
1778 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001780
1781 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1782 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 }
1784
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001785 VideoSendParameters send_params = last_send_params_;
1786 RtpSendParametersFromMediaDescription(video, &send_params);
1787 if (video->conference_mode()) {
1788 send_params.options.conference_mode.Set(true);
1789 }
1790 if (!media_channel()->SetSendParameters(send_params)) {
1791 SafeSetError("Failed to set remote video description send parameters.",
1792 error_desc);
1793 return false;
1794 }
1795 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001797 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1798 // and only give it to the media channel once we have a local
1799 // description too (without a local description, we won't be able to
1800 // recv them anyway).
1801 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1802 SafeSetError("Failed to set remote video description streams.", error_desc);
1803 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 }
1805
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001806 if (video->rtp_header_extensions_set()) {
1807 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001809
1810 set_remote_content_direction(content->direction());
1811 ChangeState();
1812 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813}
1814
1815bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1816 bool ret = true;
1817 // Set the send format for each of the local streams. If the view request
1818 // does not contain a local stream, set its send format to 0x0, which will
1819 // drop all frames.
1820 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1821 it != local_streams().end(); ++it) {
1822 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1823 StaticVideoViews::const_iterator view;
1824 for (view = request.static_video_views.begin();
1825 view != request.static_video_views.end(); ++view) {
1826 if (view->selector.Matches(*it)) {
1827 format.width = view->width;
1828 format.height = view->height;
1829 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1830 break;
1831 }
1832 }
1833
1834 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1835 }
1836
1837 // Check if the view request has invalid streams.
1838 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1839 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001840 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 LOG(LS_WARNING) << "View request for ("
1842 << it->selector.ssrc << ", '"
1843 << it->selector.groupid << "', '"
1844 << it->selector.streamid << "'"
1845 << ") is not in the local streams.";
1846 }
1847 }
1848
1849 return ret;
1850}
1851
Peter Boström0c4e06b2015-10-07 12:23:21 +02001852bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001854 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001856 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1857 screencast_capturers_[ssrc] = capturer;
1858 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859}
1860
Peter Boström0c4e06b2015-10-07 12:23:21 +02001861bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1863 if (iter == screencast_capturers_.end()) {
1864 return false;
1865 }
1866 // Clean up VideoCapturer.
1867 delete iter->second;
1868 screencast_capturers_.erase(iter);
1869 return true;
1870}
1871
1872bool VideoChannel::IsScreencasting_w() const {
1873 return !screencast_capturers_.empty();
1874}
1875
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001876void VideoChannel::GetScreencastDetails_w(
1877 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001878 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001880 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 }
1882 VideoCapturer* capturer = iter->second;
1883 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001884 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1885 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886}
1887
Peter Boström0c4e06b2015-10-07 12:23:21 +02001888void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001889 rtc::WindowEvent we) {
1890 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 SignalScreencastWindowEvent(ssrc, we);
1892}
1893
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001894void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896 case MSG_SCREENCASTWINDOWEVENT: {
1897 const ScreencastEventMessageData* data =
1898 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1899 OnScreencastWindowEvent_s(data->ssrc, data->event);
1900 delete data;
1901 break;
1902 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903 case MSG_CHANNEL_ERROR: {
1904 const VideoChannelErrorMessageData* data =
1905 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 delete data;
1907 break;
1908 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909 default:
1910 BaseChannel::OnMessage(pmsg);
1911 break;
1912 }
1913}
1914
1915void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001916 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917 SignalConnectionMonitor(this, infos);
1918}
1919
1920// TODO(pthatcher): Look into removing duplicate code between
1921// audio, video, and data, perhaps by using templates.
1922void VideoChannel::OnMediaMonitorUpdate(
1923 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1924 ASSERT(media_channel == this->media_channel());
1925 SignalMediaMonitor(this, info);
1926}
1927
Peter Boström0c4e06b2015-10-07 12:23:21 +02001928void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001929 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 ScreencastEventMessageData* pdata =
1931 new ScreencastEventMessageData(ssrc, event);
1932 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1933}
1934
1935void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1936 // Map capturer events to window events. In the future we may want to simply
1937 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001938 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001940 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001942 we = rtc::WE_MINIMIZE;
1943 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1944 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945 } else {
1946 return;
1947 }
1948 previous_we_ = we;
1949
Peter Boström0c4e06b2015-10-07 12:23:21 +02001950 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 if (!GetLocalSsrc(capturer, &ssrc)) {
1952 return;
1953 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001954
1955 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956}
1957
Peter Boström0c4e06b2015-10-07 12:23:21 +02001958bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 *ssrc = 0;
1960 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
1961 iter != screencast_capturers_.end(); ++iter) {
1962 if (iter->second == capturer) {
1963 *ssrc = iter->first;
1964 return true;
1965 }
1966 }
1967 return false;
1968}
1969
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001970void VideoChannel::GetSrtpCryptoSuiteNames(
1971 std::vector<std::string>* ciphers) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972 GetSupportedVideoCryptoSuites(ciphers);
1973}
1974
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001975DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001977 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 const std::string& content_name,
1979 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001980 : BaseChannel(thread,
1981 media_channel,
1982 transport_controller,
1983 content_name,
1984 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001985 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07001986 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987
1988DataChannel::~DataChannel() {
1989 StopMediaMonitor();
1990 // this can't be done in the base class, since it calls a virtual
1991 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001992
1993 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994}
1995
1996bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001997 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 return false;
1999 }
2000 media_channel()->SignalDataReceived.connect(
2001 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002002 media_channel()->SignalReadyToSend.connect(
2003 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002004 media_channel()->SignalStreamClosedRemotely.connect(
2005 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 return true;
2007}
2008
2009bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002010 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002012 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2013 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014}
2015
2016const ContentInfo* DataChannel::GetFirstContent(
2017 const SessionDescription* sdesc) {
2018 return GetFirstDataContent(sdesc);
2019}
2020
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002021bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022 if (data_channel_type_ == DCT_SCTP) {
2023 // TODO(pthatcher): Do this in a more robust way by checking for
2024 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002025 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026 } else if (data_channel_type_ == DCT_RTP) {
2027 return BaseChannel::WantsPacket(rtcp, packet);
2028 }
2029 return false;
2030}
2031
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002032bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2033 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 // It hasn't been set before, so set it now.
2035 if (data_channel_type_ == DCT_NONE) {
2036 data_channel_type_ = new_data_channel_type;
2037 return true;
2038 }
2039
2040 // It's been set before, but doesn't match. That's bad.
2041 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002042 std::ostringstream desc;
2043 desc << "Data channel type mismatch."
2044 << " Expected " << data_channel_type_
2045 << " Got " << new_data_channel_type;
2046 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047 return false;
2048 }
2049
2050 // It's hasn't changed. Nothing to do.
2051 return true;
2052}
2053
2054bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002055 const DataContentDescription* content,
2056 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2058 (content->protocol() == kMediaProtocolDtlsSctp));
2059 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002060 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061}
2062
2063bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002064 ContentAction action,
2065 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002066 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067 LOG(LS_INFO) << "Setting local data description";
2068
2069 const DataContentDescription* data =
2070 static_cast<const DataContentDescription*>(content);
2071 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002072 if (!data) {
2073 SafeSetError("Can't find data content in local description.", error_desc);
2074 return false;
2075 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002077 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 return false;
2079 }
2080
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002081 if (data_channel_type_ == DCT_RTP) {
2082 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2083 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 }
2085 }
2086
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002087 // FYI: We send the SCTP port number (not to be confused with the
2088 // underlying UDP port number) as a codec parameter. So even SCTP
2089 // data channels need codecs.
2090 DataRecvParameters recv_params = last_recv_params_;
2091 RtpParametersFromMediaDescription(data, &recv_params);
2092 if (!media_channel()->SetRecvParameters(recv_params)) {
2093 SafeSetError("Failed to set remote data description recv parameters.",
2094 error_desc);
2095 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002097 if (data_channel_type_ == DCT_RTP) {
2098 for (const DataCodec& codec : data->codecs()) {
2099 bundle_filter()->AddPayloadType(codec.id);
2100 }
2101 }
2102 last_recv_params_ = recv_params;
2103
2104 // TODO(pthatcher): Move local streams into DataSendParameters, and
2105 // only give it to the media channel once we have a remote
2106 // description too (without a remote description, we won't be able
2107 // to send them anyway).
2108 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2109 SafeSetError("Failed to set local data description streams.", error_desc);
2110 return false;
2111 }
2112
2113 set_local_content_direction(content->direction());
2114 ChangeState();
2115 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116}
2117
2118bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002119 ContentAction action,
2120 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002121 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122
2123 const DataContentDescription* data =
2124 static_cast<const DataContentDescription*>(content);
2125 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002126 if (!data) {
2127 SafeSetError("Can't find data content in remote description.", error_desc);
2128 return false;
2129 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002130
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002131 // If the remote data doesn't have codecs and isn't an update, it
2132 // must be empty, so ignore it.
2133 if (!data->has_codecs() && action != CA_UPDATE) {
2134 return true;
2135 }
2136
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002137 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138 return false;
2139 }
2140
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002141 LOG(LS_INFO) << "Setting remote data description";
2142 if (data_channel_type_ == DCT_RTP &&
2143 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2144 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145 }
2146
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002147
2148 DataSendParameters send_params = last_send_params_;
2149 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2150 if (!media_channel()->SetSendParameters(send_params)) {
2151 SafeSetError("Failed to set remote data description send parameters.",
2152 error_desc);
2153 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002155 last_send_params_ = send_params;
2156
2157 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2158 // and only give it to the media channel once we have a local
2159 // description too (without a local description, we won't be able to
2160 // recv them anyway).
2161 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2162 SafeSetError("Failed to set remote data description streams.",
2163 error_desc);
2164 return false;
2165 }
2166
2167 set_remote_content_direction(content->direction());
2168 ChangeState();
2169 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170}
2171
2172void DataChannel::ChangeState() {
2173 // Render incoming data if we're the active call, and we have the local
2174 // content. We receive data on the default channel and multiplexed streams.
2175 bool recv = IsReadyToReceive();
2176 if (!media_channel()->SetReceive(recv)) {
2177 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2178 }
2179
2180 // Send outgoing data if we're the active call, we have the remote content,
2181 // and we have had some form of connectivity.
2182 bool send = IsReadyToSend();
2183 if (!media_channel()->SetSend(send)) {
2184 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2185 }
2186
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002187 // Trigger SignalReadyToSendData asynchronously.
2188 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189
2190 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2191}
2192
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002193void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194 switch (pmsg->message_id) {
2195 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002196 DataChannelReadyToSendMessageData* data =
2197 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002198 ready_to_send_data_ = data->data();
2199 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200 delete data;
2201 break;
2202 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203 case MSG_DATARECEIVED: {
2204 DataReceivedMessageData* data =
2205 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2206 SignalDataReceived(this, data->params, data->payload);
2207 delete data;
2208 break;
2209 }
2210 case MSG_CHANNEL_ERROR: {
2211 const DataChannelErrorMessageData* data =
2212 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 delete data;
2214 break;
2215 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002216 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002217 rtc::TypedMessageData<uint32_t>* data =
2218 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002219 SignalStreamClosedRemotely(data->data());
2220 delete data;
2221 break;
2222 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002223 default:
2224 BaseChannel::OnMessage(pmsg);
2225 break;
2226 }
2227}
2228
2229void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002230 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231 SignalConnectionMonitor(this, infos);
2232}
2233
2234void DataChannel::StartMediaMonitor(int cms) {
2235 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002236 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237 media_monitor_->SignalUpdate.connect(
2238 this, &DataChannel::OnMediaMonitorUpdate);
2239 media_monitor_->Start(cms);
2240}
2241
2242void DataChannel::StopMediaMonitor() {
2243 if (media_monitor_) {
2244 media_monitor_->Stop();
2245 media_monitor_->SignalUpdate.disconnect(this);
2246 media_monitor_.reset();
2247 }
2248}
2249
2250void DataChannel::OnMediaMonitorUpdate(
2251 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2252 ASSERT(media_channel == this->media_channel());
2253 SignalMediaMonitor(this, info);
2254}
2255
2256void DataChannel::OnDataReceived(
2257 const ReceiveDataParams& params, const char* data, size_t len) {
2258 DataReceivedMessageData* msg = new DataReceivedMessageData(
2259 params, data, len);
2260 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2261}
2262
Peter Boström0c4e06b2015-10-07 12:23:21 +02002263void DataChannel::OnDataChannelError(uint32_t ssrc,
2264 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002265 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2266 ssrc, err);
2267 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2268}
2269
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002270void DataChannel::OnDataChannelReadyToSend(bool writable) {
2271 // This is usded for congestion control to indicate that the stream is ready
2272 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2273 // that the transport channel is ready.
2274 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2275 new DataChannelReadyToSendMessageData(writable));
2276}
2277
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002278void DataChannel::GetSrtpCryptoSuiteNames(
2279 std::vector<std::string>* ciphers) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280 GetSupportedDataCryptoSuites(ciphers);
2281}
2282
2283bool DataChannel::ShouldSetupDtlsSrtp() const {
2284 return (data_channel_type_ == DCT_RTP);
2285}
2286
Peter Boström0c4e06b2015-10-07 12:23:21 +02002287void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2288 rtc::TypedMessageData<uint32_t>* message =
2289 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002290 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2291}
2292
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002293} // namespace cricket