blob: ef26704f1a4f0058d895e7fb3d539030c7a300e3 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/session/media/channel.h"
29
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000030#include "talk/media/base/constants.h"
31#include "talk/media/base/rtputils.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032#include "webrtc/p2p/base/transportchannel.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000033#include "talk/session/media/channelmanager.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000034#include "webrtc/base/bind.h"
35#include "webrtc/base/buffer.h"
36#include "webrtc/base/byteorder.h"
37#include "webrtc/base/common.h"
38#include "webrtc/base/dscp.h"
39#include "webrtc/base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
41namespace cricket {
42
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000043using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000044
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000046 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 MSG_SCREENCASTWINDOWEVENT,
48 MSG_RTPPACKET,
49 MSG_RTCPPACKET,
50 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000054 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055};
56
57// Value specified in RFC 5764.
58static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
59
60static const int kAgcMinus10db = -10;
61
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062static void SafeSetError(const std::string& message, std::string* error_desc) {
63 if (error_desc) {
64 *error_desc = message;
65 }
66}
67
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000068struct PacketMessageData : public rtc::MessageData {
69 rtc::Buffer packet;
stefanc1aeaf02015-10-15 07:26:07 -070070 rtc::PacketOptions options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071};
72
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073struct ScreencastEventMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020074 ScreencastEventMessageData(uint32_t s, rtc::WindowEvent we)
75 : ssrc(s), event(we) {}
76 uint32_t ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000077 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078};
79
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020081 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020083 : ssrc(in_ssrc), error(in_error) {}
84 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 VoiceMediaChannel::Error error;
86};
87
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020091 : ssrc(in_ssrc), error(in_error) {}
92 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 VideoMediaChannel::Error error;
94};
95
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000096struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020097 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020099 : ssrc(in_ssrc), error(in_error) {}
100 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 DataMediaChannel::Error error;
102};
103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000105struct VideoChannel::ScreencastDetailsData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200106 explicit ScreencastDetailsData(uint32_t s)
107 : ssrc(s), fps(0), screencast_max_pixels(0) {}
108 uint32_t ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000109 int fps;
110 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111};
112
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113static const char* PacketType(bool rtcp) {
114 return (!rtcp) ? "RTP" : "RTCP";
115}
116
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000117static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 // Check the packet size. We could check the header too if needed.
119 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000120 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
121 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122}
123
124static bool IsReceiveContentDirection(MediaContentDirection direction) {
125 return direction == MD_SENDRECV || direction == MD_RECVONLY;
126}
127
128static bool IsSendContentDirection(MediaContentDirection direction) {
129 return direction == MD_SENDRECV || direction == MD_SENDONLY;
130}
131
132static const MediaContentDescription* GetContentDescription(
133 const ContentInfo* cinfo) {
134 if (cinfo == NULL)
135 return NULL;
136 return static_cast<const MediaContentDescription*>(cinfo->description);
137}
138
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700139template <class Codec>
140void RtpParametersFromMediaDescription(
141 const MediaContentDescriptionImpl<Codec>* desc,
142 RtpParameters<Codec>* params) {
143 // TODO(pthatcher): Remove this once we're sure no one will give us
144 // a description without codecs (currently a CA_UPDATE with just
145 // streams can).
146 if (desc->has_codecs()) {
147 params->codecs = desc->codecs();
148 }
149 // TODO(pthatcher): See if we really need
150 // rtp_header_extensions_set() and remove it if we don't.
151 if (desc->rtp_header_extensions_set()) {
152 params->extensions = desc->rtp_header_extensions();
153 }
154}
155
156template <class Codec, class Options>
157void RtpSendParametersFromMediaDescription(
158 const MediaContentDescriptionImpl<Codec>* desc,
159 RtpSendParameters<Codec, Options>* send_params) {
160 RtpParametersFromMediaDescription(desc, send_params);
161 send_params->max_bandwidth_bps = desc->bandwidth();
162}
163
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000164BaseChannel::BaseChannel(rtc::Thread* thread,
deadbeefcbecd352015-09-23 11:50:27 -0700165 MediaChannel* media_channel,
166 TransportController* transport_controller,
167 const std::string& content_name,
168 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 : worker_thread_(thread),
deadbeefcbecd352015-09-23 11:50:27 -0700170 transport_controller_(transport_controller),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 media_channel_(media_channel),
172 content_name_(content_name),
deadbeefcbecd352015-09-23 11:50:27 -0700173 rtcp_transport_enabled_(rtcp),
174 transport_channel_(nullptr),
175 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 enabled_(false),
177 writable_(false),
178 rtp_ready_to_send_(false),
179 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 was_ever_writable_(false),
181 local_content_direction_(MD_INACTIVE),
182 remote_content_direction_(MD_INACTIVE),
183 has_received_packet_(false),
184 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000185 secure_required_(false),
186 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000187 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 LOG(LS_INFO) << "Created channel for " << content_name;
189}
190
191BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000192 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000193 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 StopConnectionMonitor();
195 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000196 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 // We must destroy the media channel before the transport channel, otherwise
198 // the media channel may try to send on the dead transport channel. NULLing
199 // is not an effective strategy since the sends will come on another thread.
200 delete media_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700201 // Note that we don't just call set_transport_channel(nullptr) because that
202 // would call a pure virtual method which we can't do from a destructor.
203 if (transport_channel_) {
204 DisconnectFromTransportChannel(transport_channel_);
205 transport_controller_->DestroyTransportChannel_w(
206 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
207 }
208 if (rtcp_transport_channel_) {
209 DisconnectFromTransportChannel(rtcp_transport_channel_);
210 transport_controller_->DestroyTransportChannel_w(
211 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
212 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 LOG(LS_INFO) << "Destroyed channel";
214}
215
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000216bool BaseChannel::Init() {
deadbeefcbecd352015-09-23 11:50:27 -0700217 if (!SetTransport(content_name())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 return false;
219 }
220
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800221 if (!SetDtlsSrtpCryptoSuites(transport_channel(), false)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000222 return false;
223 }
deadbeefcbecd352015-09-23 11:50:27 -0700224 if (rtcp_transport_enabled() &&
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800225 !SetDtlsSrtpCryptoSuites(rtcp_transport_channel(), true)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000226 return false;
227 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228
wu@webrtc.orgde305012013-10-31 15:40:38 +0000229 // Both RTP and RTCP channels are set, we can call SetInterface on
230 // media channel and it can set network options.
231 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 return true;
233}
234
wu@webrtc.org78187522013-10-07 23:32:02 +0000235void BaseChannel::Deinit() {
236 media_channel_->SetInterface(NULL);
237}
238
deadbeefcbecd352015-09-23 11:50:27 -0700239bool BaseChannel::SetTransport(const std::string& transport_name) {
240 return worker_thread_->Invoke<bool>(
241 Bind(&BaseChannel::SetTransport_w, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000242}
243
deadbeefcbecd352015-09-23 11:50:27 -0700244bool BaseChannel::SetTransport_w(const std::string& transport_name) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000245 ASSERT(worker_thread_ == rtc::Thread::Current());
246
deadbeefcbecd352015-09-23 11:50:27 -0700247 if (transport_name == transport_name_) {
248 // Nothing to do if transport name isn't changing
249 return true;
250 }
251
252 set_transport_channel(transport_controller_->CreateTransportChannel_w(
253 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000254 if (!transport_channel()) {
255 return false;
256 }
deadbeefcbecd352015-09-23 11:50:27 -0700257 if (rtcp_transport_enabled()) {
258 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
259 << " on " << transport_name << " transport ";
260 set_rtcp_transport_channel(transport_controller_->CreateTransportChannel_w(
261 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000262 if (!rtcp_transport_channel()) {
263 return false;
264 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000265 }
266
deadbeefcbecd352015-09-23 11:50:27 -0700267 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000268 return true;
269}
270
271void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
272 ASSERT(worker_thread_ == rtc::Thread::Current());
273
274 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700275 if (!old_tc && !new_tc) {
276 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000277 return;
278 }
deadbeefcbecd352015-09-23 11:50:27 -0700279 ASSERT(old_tc != new_tc);
280
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000281 if (old_tc) {
282 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700283 transport_controller_->DestroyTransportChannel_w(
284 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000285 }
286
287 transport_channel_ = new_tc;
288
289 if (new_tc) {
290 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700291 for (const auto& pair : socket_options_) {
292 new_tc->SetOption(pair.first, pair.second);
293 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000294 }
deadbeefcbecd352015-09-23 11:50:27 -0700295
296 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
297 // setting new channel
298 UpdateWritableState_w();
299 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000300}
301
302void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
303 ASSERT(worker_thread_ == rtc::Thread::Current());
304
305 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700306 if (!old_tc && !new_tc) {
307 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000308 return;
309 }
deadbeefcbecd352015-09-23 11:50:27 -0700310 ASSERT(old_tc != new_tc);
311
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000312 if (old_tc) {
313 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700314 transport_controller_->DestroyTransportChannel_w(
315 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000316 }
317
318 rtcp_transport_channel_ = new_tc;
319
320 if (new_tc) {
321 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700322 for (const auto& pair : rtcp_socket_options_) {
323 new_tc->SetOption(pair.first, pair.second);
324 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000325 }
deadbeefcbecd352015-09-23 11:50:27 -0700326
327 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
328 // setting new channel
329 UpdateWritableState_w();
330 SetReadyToSend(true, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000331}
332
333void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
334 ASSERT(worker_thread_ == rtc::Thread::Current());
335
336 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
337 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
338 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
339}
340
341void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
342 ASSERT(worker_thread_ == rtc::Thread::Current());
343
344 tc->SignalWritableState.disconnect(this);
345 tc->SignalReadPacket.disconnect(this);
346 tc->SignalReadyToSend.disconnect(this);
347}
348
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000350 worker_thread_->Invoke<void>(Bind(
351 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
352 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 return true;
354}
355
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000357 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358}
359
Peter Boström0c4e06b2015-10-07 12:23:21 +0200360bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000361 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362}
363
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000364bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000365 return InvokeOnWorker(
366 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000367}
368
Peter Boström0c4e06b2015-10-07 12:23:21 +0200369bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000370 return InvokeOnWorker(
371 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000372}
373
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000375 ContentAction action,
376 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000377 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
378 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379}
380
381bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000382 ContentAction action,
383 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000384 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
385 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386}
387
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000389 // We pass in the BaseChannel instead of the transport_channel_
390 // because if the transport_channel_ changes, the ConnectionMonitor
391 // would be pointing to the wrong TransportChannel.
392 connection_monitor_.reset(new ConnectionMonitor(
393 this, worker_thread(), rtc::Thread::Current()));
394 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000396 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397}
398
399void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000400 if (connection_monitor_) {
401 connection_monitor_->Stop();
402 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 }
404}
405
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000406bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
407 ASSERT(worker_thread_ == rtc::Thread::Current());
408 return transport_channel_->GetStats(infos);
409}
410
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411bool BaseChannel::IsReadyToReceive() const {
412 // Receive data if we are enabled and have local content,
413 return enabled() && IsReceiveContentDirection(local_content_direction_);
414}
415
416bool BaseChannel::IsReadyToSend() const {
417 // Send outgoing data if we are enabled, have local and remote content,
418 // and we have had some form of connectivity.
419 return enabled() &&
420 IsReceiveContentDirection(remote_content_direction_) &&
421 IsSendContentDirection(local_content_direction_) &&
422 was_ever_writable();
423}
424
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000425bool BaseChannel::SendPacket(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700426 const rtc::PacketOptions& options) {
427 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428}
429
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000430bool BaseChannel::SendRtcp(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700431 const rtc::PacketOptions& options) {
432 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433}
434
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000435int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000437 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000439 case ST_RTP:
440 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700441 socket_options_.push_back(
442 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000443 break;
444 case ST_RTCP:
445 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700446 rtcp_socket_options_.push_back(
447 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000448 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000450 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451}
452
453void BaseChannel::OnWritableState(TransportChannel* channel) {
454 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
deadbeefcbecd352015-09-23 11:50:27 -0700455 UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456}
457
458void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000459 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000460 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000461 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000463 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464
465 // When using RTCP multiplexing we might get RTCP packets on the RTP
466 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
467 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000468 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000469 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470}
471
472void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700473 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
474 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475}
476
deadbeefcbecd352015-09-23 11:50:27 -0700477void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
478 if (rtcp) {
479 rtcp_ready_to_send_ = ready;
480 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 rtp_ready_to_send_ = ready;
482 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483
deadbeefcbecd352015-09-23 11:50:27 -0700484 if (rtp_ready_to_send_ &&
485 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
486 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
torbjornga81a42f2015-09-23 02:16:58 -0700487 // Notify the MediaChannel when both rtp and rtcp channel can send.
488 media_channel_->OnReadyToSend(true);
deadbeefcbecd352015-09-23 11:50:27 -0700489 } else {
490 // Notify the MediaChannel when either rtp or rtcp channel can't send.
491 media_channel_->OnReadyToSend(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 }
493}
494
495bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
496 const char* data, size_t len) {
497 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000498 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499}
500
stefanc1aeaf02015-10-15 07:26:07 -0700501bool BaseChannel::SendPacket(bool rtcp,
502 rtc::Buffer* packet,
503 const rtc::PacketOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 // SendPacket gets called from MediaEngine, typically on an encoder thread.
505 // If the thread is not our worker thread, we will post to our worker
506 // so that the real work happens on our worker. This avoids us having to
507 // synchronize access to all the pieces of the send path, including
508 // SRTP and the inner workings of the transport channels.
509 // The only downside is that we can't return a proper failure code if
510 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000511 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 // Avoid a copy by transferring the ownership of the packet data.
513 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
514 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200515 data->packet = packet->Pass();
stefanc1aeaf02015-10-15 07:26:07 -0700516 data->options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 worker_thread_->Post(this, message_id, data);
518 return true;
519 }
520
521 // Now that we are on the correct thread, ensure we have a place to send this
522 // packet before doing anything. (We might get RTCP packets that we don't
523 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
524 // transport.
525 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
526 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000527 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 return false;
529 }
530
531 // Protect ourselves against crazy data.
532 if (!ValidPacket(rtcp, packet)) {
533 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000534 << PacketType(rtcp)
535 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 return false;
537 }
538
stefanc1aeaf02015-10-15 07:26:07 -0700539 rtc::PacketOptions updated_options;
540 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 // Protect if needed.
542 if (srtp_filter_.IsActive()) {
543 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200544 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000545 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000547 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
548 // inside libsrtp for a RTP packet. A external HMAC module will be writing
549 // a fake HMAC value. This is ONLY done for a RTP packet.
550 // Socket layer will update rtp sendtime extension header if present in
551 // packet with current time before updating the HMAC.
552#if !defined(ENABLE_EXTERNAL_AUTH)
553 res = srtp_filter_.ProtectRtp(
554 data, len, static_cast<int>(packet->capacity()), &len);
555#else
stefanc1aeaf02015-10-15 07:26:07 -0700556 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000557 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000558 res = srtp_filter_.ProtectRtp(
559 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700560 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000561 // If protection succeeds, let's get auth params from srtp.
562 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200563 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000564 int key_len;
565 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700566 &auth_key, &key_len,
567 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000568 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700569 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
570 updated_options.packet_time_params.srtp_auth_key.assign(
571 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000572 }
573 }
574#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 if (!res) {
576 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200577 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 GetRtpSeqNum(data, len, &seq_num);
579 GetRtpSsrc(data, len, &ssrc);
580 LOG(LS_ERROR) << "Failed to protect " << content_name_
581 << " RTP packet: size=" << len
582 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
583 return false;
584 }
585 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000586 res = srtp_filter_.ProtectRtcp(data, len,
587 static_cast<int>(packet->capacity()),
588 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 if (!res) {
590 int type = -1;
591 GetRtcpType(data, len, &type);
592 LOG(LS_ERROR) << "Failed to protect " << content_name_
593 << " RTCP packet: size=" << len << ", type=" << type;
594 return false;
595 }
596 }
597
598 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000599 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 } else if (secure_required_) {
601 // This is a double check for something that supposedly can't happen.
602 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
603 << " packet when SRTP is inactive and crypto is required";
604
605 ASSERT(false);
606 return false;
607 }
608
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000610 int ret =
stefanc1aeaf02015-10-15 07:26:07 -0700611 channel->SendPacket(packet->data<char>(), packet->size(), updated_options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000612 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
613 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 if (channel->GetError() == EWOULDBLOCK) {
615 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700616 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 }
618 return false;
619 }
620 return true;
621}
622
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000623bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 // Protect ourselves against crazy data.
625 if (!ValidPacket(rtcp, packet)) {
626 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000627 << PacketType(rtcp)
628 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 return false;
630 }
pbos482b12e2015-11-16 10:19:58 -0800631 if (rtcp) {
632 // Permit all (seemingly valid) RTCP packets.
633 return true;
634 }
635 // Check whether we handle this payload.
636 return bundle_filter_.DemuxPacket(packet->data<uint8_t>(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637}
638
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000639void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
640 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 if (!WantsPacket(rtcp, packet)) {
642 return;
643 }
644
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000645 // We are only interested in the first rtp packet because that
646 // indicates the media has started flowing.
647 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 has_received_packet_ = true;
649 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
650 }
651
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 // Unprotect the packet, if needed.
653 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200654 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000655 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 bool res;
657 if (!rtcp) {
658 res = srtp_filter_.UnprotectRtp(data, len, &len);
659 if (!res) {
660 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200661 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 GetRtpSeqNum(data, len, &seq_num);
663 GetRtpSsrc(data, len, &ssrc);
664 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
665 << " RTP packet: size=" << len
666 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
667 return;
668 }
669 } else {
670 res = srtp_filter_.UnprotectRtcp(data, len, &len);
671 if (!res) {
672 int type = -1;
673 GetRtcpType(data, len, &type);
674 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
675 << " RTCP packet: size=" << len << ", type=" << type;
676 return;
677 }
678 }
679
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000680 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 } else if (secure_required_) {
682 // Our session description indicates that SRTP is required, but we got a
683 // packet before our SRTP filter is active. This means either that
684 // a) we got SRTP packets before we received the SDES keys, in which case
685 // we can't decrypt it anyway, or
686 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
687 // channels, so we haven't yet extracted keys, even if DTLS did complete
688 // on the channel that the packets are being sent on. It's really good
689 // practice to wait for both RTP and RTCP to be good to go before sending
690 // media, to prevent weird failure modes, so it's fine for us to just eat
691 // packets here. This is all sidestepped if RTCP mux is used anyway.
692 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
693 << " packet when SRTP is inactive and crypto is required";
694 return;
695 }
696
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 // Push it down to the media channel.
698 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000699 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000701 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 }
703}
704
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000705bool BaseChannel::PushdownLocalDescription(
706 const SessionDescription* local_desc, ContentAction action,
707 std::string* error_desc) {
708 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 const MediaContentDescription* content_desc =
710 GetContentDescription(content_info);
711 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000712 !SetLocalContent(content_desc, action, error_desc)) {
713 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
714 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000716 return true;
717}
718
719bool BaseChannel::PushdownRemoteDescription(
720 const SessionDescription* remote_desc, ContentAction action,
721 std::string* error_desc) {
722 const ContentInfo* content_info = GetFirstContent(remote_desc);
723 const MediaContentDescription* content_desc =
724 GetContentDescription(content_info);
725 if (content_desc && content_info && !content_info->rejected &&
726 !SetRemoteContent(content_desc, action, error_desc)) {
727 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
728 return false;
729 }
730 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731}
732
733void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000734 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 if (enabled_)
736 return;
737
738 LOG(LS_INFO) << "Channel enabled";
739 enabled_ = true;
740 ChangeState();
741}
742
743void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000744 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 if (!enabled_)
746 return;
747
748 LOG(LS_INFO) << "Channel disabled";
749 enabled_ = false;
750 ChangeState();
751}
752
deadbeefcbecd352015-09-23 11:50:27 -0700753void BaseChannel::UpdateWritableState_w() {
754 if (transport_channel_ && transport_channel_->writable() &&
755 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
756 ChannelWritable_w();
757 } else {
758 ChannelNotWritable_w();
759 }
760}
761
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000763 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 if (writable_)
765 return;
766
deadbeefcbecd352015-09-23 11:50:27 -0700767 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 << (was_ever_writable_ ? "" : " for the first time");
769
770 std::vector<ConnectionInfo> infos;
771 transport_channel_->GetStats(&infos);
772 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
773 it != infos.end(); ++it) {
774 if (it->best_connection) {
775 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
776 << "->" << it->remote_candidate.ToSensitiveString();
777 break;
778 }
779 }
780
781 // If we're doing DTLS-SRTP, now is the time.
782 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
783 if (!SetupDtlsSrtp(false)) {
deadbeefcbecd352015-09-23 11:50:27 -0700784 SignalDtlsSetupFailure_w(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 return;
786 }
787
788 if (rtcp_transport_channel_) {
789 if (!SetupDtlsSrtp(true)) {
deadbeefcbecd352015-09-23 11:50:27 -0700790 SignalDtlsSetupFailure_w(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 return;
792 }
793 }
794 }
795
796 was_ever_writable_ = true;
797 writable_ = true;
798 ChangeState();
799}
800
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000801void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
802 ASSERT(worker_thread() == rtc::Thread::Current());
803 signaling_thread()->Invoke<void>(Bind(
804 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
805}
806
807void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
808 ASSERT(signaling_thread() == rtc::Thread::Current());
809 SignalDtlsSetupFailure(this, rtcp);
810}
811
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800812bool BaseChannel::SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp) {
813 std::vector<int> crypto_suites;
814 // We always use the default SRTP crypto suites for RTCP, but we may use
815 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 if (!rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800817 GetSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800819 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800821 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822}
823
824bool BaseChannel::ShouldSetupDtlsSrtp() const {
825 return true;
826}
827
828// This function returns true if either DTLS-SRTP is not in use
829// *or* DTLS-SRTP is successfully set up.
830bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
831 bool ret = false;
832
deadbeefcbecd352015-09-23 11:50:27 -0700833 TransportChannel* channel =
834 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835
836 // No DTLS
837 if (!channel->IsDtlsActive())
838 return true;
839
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800840 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800842 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
843 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 return false;
845 }
846
847 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
848 << content_name() << " "
849 << PacketType(rtcp_channel);
850
851 // OK, we're now doing DTLS (RFC 5764)
852 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
853 SRTP_MASTER_KEY_SALT_LEN * 2);
854
855 // RFC 5705 exporter using the RFC 5764 parameters
856 if (!channel->ExportKeyingMaterial(
857 kDtlsSrtpExporterLabel,
858 NULL, 0, false,
859 &dtls_buffer[0], dtls_buffer.size())) {
860 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
861 ASSERT(false); // This should never happen
862 return false;
863 }
864
865 // Sync up the keys with the DTLS-SRTP interface
866 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
867 SRTP_MASTER_KEY_SALT_LEN);
868 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
869 SRTP_MASTER_KEY_SALT_LEN);
870 size_t offset = 0;
871 memcpy(&client_write_key[0], &dtls_buffer[offset],
872 SRTP_MASTER_KEY_KEY_LEN);
873 offset += SRTP_MASTER_KEY_KEY_LEN;
874 memcpy(&server_write_key[0], &dtls_buffer[offset],
875 SRTP_MASTER_KEY_KEY_LEN);
876 offset += SRTP_MASTER_KEY_KEY_LEN;
877 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
878 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
879 offset += SRTP_MASTER_KEY_SALT_LEN;
880 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
881 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
882
883 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000884 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000885 if (!channel->GetSslRole(&role)) {
886 LOG(LS_WARNING) << "GetSslRole failed";
887 return false;
888 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000890 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 send_key = &server_write_key;
892 recv_key = &client_write_key;
893 } else {
894 send_key = &client_write_key;
895 recv_key = &server_write_key;
896 }
897
898 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800899 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
900 static_cast<int>(send_key->size()),
901 selected_crypto_suite, &(*recv_key)[0],
902 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800904 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
905 static_cast<int>(send_key->size()),
906 selected_crypto_suite, &(*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());
pbos482b12e2015-11-16 10:19:58 -08001075 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076}
1077
Peter Boström0c4e06b2015-10-07 12:23:21 +02001078bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001079 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 return media_channel()->RemoveRecvStream(ssrc);
1081}
1082
1083bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001084 ContentAction action,
1085 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1087 action == CA_PRANSWER || action == CA_UPDATE))
1088 return false;
1089
1090 // If this is an update, streams only contain streams that have changed.
1091 if (action == CA_UPDATE) {
1092 for (StreamParamsVec::const_iterator it = streams.begin();
1093 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001094 const StreamParams* existing_stream =
1095 GetStreamByIds(local_streams_, it->groupid, it->id);
1096 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 if (media_channel()->AddSendStream(*it)) {
1098 local_streams_.push_back(*it);
1099 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1100 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001101 std::ostringstream desc;
1102 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1103 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 return false;
1105 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001106 } else if (existing_stream && !it->has_ssrcs()) {
1107 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001108 std::ostringstream desc;
1109 desc << "Failed to remove send stream with ssrc "
1110 << it->first_ssrc() << ".";
1111 SafeSetError(desc.str(), error_desc);
1112 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001114 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 } else {
1116 LOG(LS_WARNING) << "Ignore unsupported stream update";
1117 }
1118 }
1119 return true;
1120 }
1121 // Else streams are all the streams we want to send.
1122
1123 // Check for streams that have been removed.
1124 bool ret = true;
1125 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1126 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001127 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001129 std::ostringstream desc;
1130 desc << "Failed to remove send stream with ssrc "
1131 << it->first_ssrc() << ".";
1132 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 ret = false;
1134 }
1135 }
1136 }
1137 // Check for new streams.
1138 for (StreamParamsVec::const_iterator it = streams.begin();
1139 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001140 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001142 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001144 std::ostringstream desc;
1145 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1146 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 ret = false;
1148 }
1149 }
1150 }
1151 local_streams_ = streams;
1152 return ret;
1153}
1154
1155bool BaseChannel::UpdateRemoteStreams_w(
1156 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001157 ContentAction action,
1158 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1160 action == CA_PRANSWER || action == CA_UPDATE))
1161 return false;
1162
1163 // If this is an update, streams only contain streams that have changed.
1164 if (action == CA_UPDATE) {
1165 for (StreamParamsVec::const_iterator it = streams.begin();
1166 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001167 const StreamParams* existing_stream =
1168 GetStreamByIds(remote_streams_, it->groupid, it->id);
1169 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 if (AddRecvStream_w(*it)) {
1171 remote_streams_.push_back(*it);
1172 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1173 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001174 std::ostringstream desc;
1175 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1176 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 return false;
1178 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001179 } else if (existing_stream && !it->has_ssrcs()) {
1180 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001181 std::ostringstream desc;
1182 desc << "Failed to remove remote stream with ssrc "
1183 << it->first_ssrc() << ".";
1184 SafeSetError(desc.str(), error_desc);
1185 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001187 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 } else {
1189 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001190 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 << " new stream = " << it->ToString();
1192 }
1193 }
1194 return true;
1195 }
1196 // Else streams are all the streams we want to receive.
1197
1198 // Check for streams that have been removed.
1199 bool ret = true;
1200 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1201 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001202 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001204 std::ostringstream desc;
1205 desc << "Failed to remove remote stream with ssrc "
1206 << it->first_ssrc() << ".";
1207 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 ret = false;
1209 }
1210 }
1211 }
1212 // Check for new streams.
1213 for (StreamParamsVec::const_iterator it = streams.begin();
1214 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001215 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 if (AddRecvStream_w(*it)) {
1217 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1218 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001219 std::ostringstream desc;
1220 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1221 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 ret = false;
1223 }
1224 }
1225 }
1226 remote_streams_ = streams;
1227 return ret;
1228}
1229
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001230void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1231 const std::vector<RtpHeaderExtension>& extensions) {
1232 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001233 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001234 rtp_abs_sendtime_extn_id_ =
1235 send_time_extension ? send_time_extension->id : -1;
1236}
1237
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001238void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 case MSG_RTPPACKET:
1241 case MSG_RTCPPACKET: {
1242 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
stefanc1aeaf02015-10-15 07:26:07 -07001243 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet,
1244 data->options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 delete data; // because it is Posted
1246 break;
1247 }
1248 case MSG_FIRSTPACKETRECEIVED: {
1249 SignalFirstPacketReceived(this);
1250 break;
1251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 }
1253}
1254
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255void BaseChannel::FlushRtcpMessages() {
1256 // Flush all remaining RTCP messages. This should only be called in
1257 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001258 ASSERT(rtc::Thread::Current() == worker_thread_);
1259 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001260 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001261 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001263 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 }
1265}
1266
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001267VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 MediaEngineInterface* media_engine,
1269 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001270 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 const std::string& content_name,
1272 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001273 : BaseChannel(thread,
1274 media_channel,
1275 transport_controller,
1276 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001278 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001279 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280
1281VoiceChannel::~VoiceChannel() {
1282 StopAudioMonitor();
1283 StopMediaMonitor();
1284 // this can't be done in the base class, since it calls a virtual
1285 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001286 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287}
1288
1289bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001290 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 return false;
1292 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 return true;
1294}
1295
Peter Boström0c4e06b2015-10-07 12:23:21 +02001296bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001297 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001298 const AudioOptions* options,
1299 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001300 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001301 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302}
1303
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304// TODO(juberti): Handle early media the right way. We should get an explicit
1305// ringing message telling us to start playing local ringback, which we cancel
1306// if any early media actually arrives. For now, we do the opposite, which is
1307// to wait 1 second for early media, and start playing local ringback if none
1308// arrives.
1309void VoiceChannel::SetEarlyMedia(bool enable) {
1310 if (enable) {
1311 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001312 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1313 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 } else {
1315 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001316 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 }
1318}
1319
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320bool VoiceChannel::PressDTMF(int digit, bool playout) {
1321 int flags = DF_SEND;
1322 if (playout) {
1323 flags |= DF_PLAY;
1324 }
1325 int duration_ms = 160;
1326 return InsertDtmf(0, digit, duration_ms, flags);
1327}
1328
1329bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001330 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1331 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332}
1333
Peter Boström0c4e06b2015-10-07 12:23:21 +02001334bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1335 int event_code,
1336 int duration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001338 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1339 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340}
1341
solenberg4bac9c52015-10-09 02:32:53 -07001342bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1343 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1344 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001346
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001348 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1349 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350}
1351
1352void VoiceChannel::StartMediaMonitor(int cms) {
1353 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001354 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355 media_monitor_->SignalUpdate.connect(
1356 this, &VoiceChannel::OnMediaMonitorUpdate);
1357 media_monitor_->Start(cms);
1358}
1359
1360void VoiceChannel::StopMediaMonitor() {
1361 if (media_monitor_) {
1362 media_monitor_->Stop();
1363 media_monitor_->SignalUpdate.disconnect(this);
1364 media_monitor_.reset();
1365 }
1366}
1367
1368void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001369 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370 audio_monitor_
1371 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1372 audio_monitor_->Start(cms);
1373}
1374
1375void VoiceChannel::StopAudioMonitor() {
1376 if (audio_monitor_) {
1377 audio_monitor_->Stop();
1378 audio_monitor_.reset();
1379 }
1380}
1381
1382bool VoiceChannel::IsAudioMonitorRunning() const {
1383 return (audio_monitor_.get() != NULL);
1384}
1385
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001387 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388}
1389
1390int VoiceChannel::GetOutputLevel_w() {
1391 return media_channel()->GetOutputLevel();
1392}
1393
1394void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1395 media_channel()->GetActiveStreams(actives);
1396}
1397
1398void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001399 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001400 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001401 int flags) {
1402 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403
1404 // Set a flag when we've received an RTP packet. If we're waiting for early
1405 // media, this will disable the timeout.
1406 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1407 received_media_ = true;
1408 }
1409}
1410
1411void VoiceChannel::ChangeState() {
1412 // Render incoming data if we're the active call, and we have the local
1413 // content. We receive data on the default channel and multiplexed streams.
1414 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001415 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416
1417 // Send outgoing data if we're the active call, we have the remote content,
1418 // and we have had some form of connectivity.
1419 bool send = IsReadyToSend();
1420 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1421 if (!media_channel()->SetSend(send_flag)) {
1422 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423 }
1424
1425 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1426}
1427
1428const ContentInfo* VoiceChannel::GetFirstContent(
1429 const SessionDescription* sdesc) {
1430 return GetFirstAudioContent(sdesc);
1431}
1432
1433bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001434 ContentAction action,
1435 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001436 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 LOG(LS_INFO) << "Setting local voice description";
1438
1439 const AudioContentDescription* audio =
1440 static_cast<const AudioContentDescription*>(content);
1441 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001442 if (!audio) {
1443 SafeSetError("Can't find audio content in local description.", error_desc);
1444 return false;
1445 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001447 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1448 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449 }
1450
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001451 AudioRecvParameters recv_params = last_recv_params_;
1452 RtpParametersFromMediaDescription(audio, &recv_params);
1453 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001454 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001455 error_desc);
1456 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001458 for (const AudioCodec& codec : audio->codecs()) {
1459 bundle_filter()->AddPayloadType(codec.id);
1460 }
1461 last_recv_params_ = recv_params;
1462
1463 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1464 // only give it to the media channel once we have a remote
1465 // description too (without a remote description, we won't be able
1466 // to send them anyway).
1467 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1468 SafeSetError("Failed to set local audio description streams.", error_desc);
1469 return false;
1470 }
1471
1472 set_local_content_direction(content->direction());
1473 ChangeState();
1474 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475}
1476
1477bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001478 ContentAction action,
1479 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001480 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 LOG(LS_INFO) << "Setting remote voice description";
1482
1483 const AudioContentDescription* audio =
1484 static_cast<const AudioContentDescription*>(content);
1485 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001486 if (!audio) {
1487 SafeSetError("Can't find audio content in remote description.", error_desc);
1488 return false;
1489 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001491 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1492 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 }
1494
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001495 AudioSendParameters send_params = last_send_params_;
1496 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001497 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001498 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001499 }
1500 if (!media_channel()->SetSendParameters(send_params)) {
1501 SafeSetError("Failed to set remote audio description send parameters.",
1502 error_desc);
1503 return false;
1504 }
1505 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001507 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1508 // and only give it to the media channel once we have a local
1509 // description too (without a local description, we won't be able to
1510 // recv them anyway).
1511 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1512 SafeSetError("Failed to set remote audio description streams.", error_desc);
1513 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 }
1515
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001516 if (audio->rtp_header_extensions_set()) {
1517 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1518 }
1519
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001520 set_remote_content_direction(content->direction());
1521 ChangeState();
1522 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523}
1524
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001525void VoiceChannel::HandleEarlyMediaTimeout() {
1526 // This occurs on the main thread, not the worker thread.
1527 if (!received_media_) {
1528 LOG(LS_INFO) << "No early media received before timeout";
1529 SignalEarlyMediaTimeout(this);
1530 }
1531}
1532
Peter Boström0c4e06b2015-10-07 12:23:21 +02001533bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1534 int event,
1535 int duration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 int flags) {
1537 if (!enabled()) {
1538 return false;
1539 }
1540
1541 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1542}
1543
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001544void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546 case MSG_EARLYMEDIATIMEOUT:
1547 HandleEarlyMediaTimeout();
1548 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 case MSG_CHANNEL_ERROR: {
1550 VoiceChannelErrorMessageData* data =
1551 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 delete data;
1553 break;
1554 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 default:
1556 BaseChannel::OnMessage(pmsg);
1557 break;
1558 }
1559}
1560
1561void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001562 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563 SignalConnectionMonitor(this, infos);
1564}
1565
1566void VoiceChannel::OnMediaMonitorUpdate(
1567 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1568 ASSERT(media_channel == this->media_channel());
1569 SignalMediaMonitor(this, info);
1570}
1571
1572void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1573 const AudioInfo& info) {
1574 SignalAudioMonitor(this, info);
1575}
1576
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001577void VoiceChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1578 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001579}
1580
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001581VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001583 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001585 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001586 : BaseChannel(thread,
1587 media_channel,
1588 transport_controller,
1589 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001592 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593
1594bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001595 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 return false;
1597 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 return true;
1599}
1600
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601VideoChannel::~VideoChannel() {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001602 std::vector<uint32_t> screencast_ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 ScreencastMap::iterator iter;
1604 while (!screencast_capturers_.empty()) {
1605 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1606 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1607 << screencast_capturers_.begin()->first;
1608 ASSERT(false);
1609 break;
1610 }
1611 }
1612
1613 StopMediaMonitor();
1614 // this can't be done in the base class, since it calls a virtual
1615 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001616
1617 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618}
1619
Peter Boström0c4e06b2015-10-07 12:23:21 +02001620bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001621 worker_thread()->Invoke<void>(Bind(
1622 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 return true;
1624}
1625
1626bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001627 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628}
1629
Peter Boström0c4e06b2015-10-07 12:23:21 +02001630bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001631 return worker_thread()->Invoke<bool>(Bind(
1632 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633}
1634
Peter Boström0c4e06b2015-10-07 12:23:21 +02001635bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001636 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1637 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638}
1639
Peter Boström0c4e06b2015-10-07 12:23:21 +02001640bool VideoChannel::RemoveScreencast(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001641 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642}
1643
1644bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001645 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646}
1647
Peter Boström0c4e06b2015-10-07 12:23:21 +02001648int VideoChannel::GetScreencastFps(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001649 ScreencastDetailsData data(ssrc);
1650 worker_thread()->Invoke<void>(Bind(
1651 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001652 return data.fps;
1653}
1654
Peter Boström0c4e06b2015-10-07 12:23:21 +02001655int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001656 ScreencastDetailsData data(ssrc);
1657 worker_thread()->Invoke<void>(Bind(
1658 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001659 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660}
1661
1662bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001663 worker_thread()->Invoke<void>(Bind(
1664 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 return true;
1666}
1667
1668bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001669 worker_thread()->Invoke<void>(Bind(
1670 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 return true;
1672}
1673
Peter Boström0c4e06b2015-10-07 12:23:21 +02001674bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001675 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001676 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001677 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1678 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001679}
1680
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 // Send outgoing data if we're the active call, we have the remote content,
1683 // and we have had some form of connectivity.
1684 bool send = IsReadyToSend();
1685 if (!media_channel()->SetSend(send)) {
1686 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1687 // TODO(gangji): Report error back to server.
1688 }
1689
Peter Boström34fbfff2015-09-24 19:20:30 +02001690 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691}
1692
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001693bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1694 return InvokeOnWorker(
1695 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696}
1697
1698void VideoChannel::StartMediaMonitor(int cms) {
1699 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001700 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701 media_monitor_->SignalUpdate.connect(
1702 this, &VideoChannel::OnMediaMonitorUpdate);
1703 media_monitor_->Start(cms);
1704}
1705
1706void VideoChannel::StopMediaMonitor() {
1707 if (media_monitor_) {
1708 media_monitor_->Stop();
1709 media_monitor_.reset();
1710 }
1711}
1712
1713const ContentInfo* VideoChannel::GetFirstContent(
1714 const SessionDescription* sdesc) {
1715 return GetFirstVideoContent(sdesc);
1716}
1717
1718bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001719 ContentAction action,
1720 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001721 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 LOG(LS_INFO) << "Setting local video description";
1723
1724 const VideoContentDescription* video =
1725 static_cast<const VideoContentDescription*>(content);
1726 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001727 if (!video) {
1728 SafeSetError("Can't find video content in local description.", error_desc);
1729 return false;
1730 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001732 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1733 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 }
1735
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001736 VideoRecvParameters recv_params = last_recv_params_;
1737 RtpParametersFromMediaDescription(video, &recv_params);
1738 if (!media_channel()->SetRecvParameters(recv_params)) {
1739 SafeSetError("Failed to set local video description recv parameters.",
1740 error_desc);
1741 return false;
1742 }
1743 for (const VideoCodec& codec : video->codecs()) {
1744 bundle_filter()->AddPayloadType(codec.id);
1745 }
1746 last_recv_params_ = recv_params;
1747
1748 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1749 // only give it to the media channel once we have a remote
1750 // description too (without a remote description, we won't be able
1751 // to send them anyway).
1752 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1753 SafeSetError("Failed to set local video description streams.", error_desc);
1754 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 }
1756
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001757 set_local_content_direction(content->direction());
1758 ChangeState();
1759 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760}
1761
1762bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001763 ContentAction action,
1764 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001765 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 LOG(LS_INFO) << "Setting remote video description";
1767
1768 const VideoContentDescription* video =
1769 static_cast<const VideoContentDescription*>(content);
1770 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001771 if (!video) {
1772 SafeSetError("Can't find video content in remote description.", error_desc);
1773 return false;
1774 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001776
1777 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1778 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 }
1780
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001781 VideoSendParameters send_params = last_send_params_;
1782 RtpSendParametersFromMediaDescription(video, &send_params);
1783 if (video->conference_mode()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001784 send_params.options.conference_mode = rtc::Optional<bool>(true);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001785 }
1786 if (!media_channel()->SetSendParameters(send_params)) {
1787 SafeSetError("Failed to set remote video description send parameters.",
1788 error_desc);
1789 return false;
1790 }
1791 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001793 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1794 // and only give it to the media channel once we have a local
1795 // description too (without a local description, we won't be able to
1796 // recv them anyway).
1797 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1798 SafeSetError("Failed to set remote video description streams.", error_desc);
1799 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800 }
1801
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001802 if (video->rtp_header_extensions_set()) {
1803 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001805
1806 set_remote_content_direction(content->direction());
1807 ChangeState();
1808 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809}
1810
1811bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1812 bool ret = true;
1813 // Set the send format for each of the local streams. If the view request
1814 // does not contain a local stream, set its send format to 0x0, which will
1815 // drop all frames.
1816 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1817 it != local_streams().end(); ++it) {
1818 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1819 StaticVideoViews::const_iterator view;
1820 for (view = request.static_video_views.begin();
1821 view != request.static_video_views.end(); ++view) {
1822 if (view->selector.Matches(*it)) {
1823 format.width = view->width;
1824 format.height = view->height;
1825 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1826 break;
1827 }
1828 }
1829
1830 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1831 }
1832
1833 // Check if the view request has invalid streams.
1834 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1835 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001836 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 LOG(LS_WARNING) << "View request for ("
1838 << it->selector.ssrc << ", '"
1839 << it->selector.groupid << "', '"
1840 << it->selector.streamid << "'"
1841 << ") is not in the local streams.";
1842 }
1843 }
1844
1845 return ret;
1846}
1847
Peter Boström0c4e06b2015-10-07 12:23:21 +02001848bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001850 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001852 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1853 screencast_capturers_[ssrc] = capturer;
1854 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855}
1856
Peter Boström0c4e06b2015-10-07 12:23:21 +02001857bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1859 if (iter == screencast_capturers_.end()) {
1860 return false;
1861 }
1862 // Clean up VideoCapturer.
1863 delete iter->second;
1864 screencast_capturers_.erase(iter);
1865 return true;
1866}
1867
1868bool VideoChannel::IsScreencasting_w() const {
1869 return !screencast_capturers_.empty();
1870}
1871
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001872void VideoChannel::GetScreencastDetails_w(
1873 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001874 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001876 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 }
1878 VideoCapturer* capturer = iter->second;
1879 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001880 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1881 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882}
1883
Peter Boström0c4e06b2015-10-07 12:23:21 +02001884void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001885 rtc::WindowEvent we) {
1886 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 SignalScreencastWindowEvent(ssrc, we);
1888}
1889
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001890void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 case MSG_SCREENCASTWINDOWEVENT: {
1893 const ScreencastEventMessageData* data =
1894 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1895 OnScreencastWindowEvent_s(data->ssrc, data->event);
1896 delete data;
1897 break;
1898 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 case MSG_CHANNEL_ERROR: {
1900 const VideoChannelErrorMessageData* data =
1901 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 delete data;
1903 break;
1904 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001905 default:
1906 BaseChannel::OnMessage(pmsg);
1907 break;
1908 }
1909}
1910
1911void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001912 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 SignalConnectionMonitor(this, infos);
1914}
1915
1916// TODO(pthatcher): Look into removing duplicate code between
1917// audio, video, and data, perhaps by using templates.
1918void VideoChannel::OnMediaMonitorUpdate(
1919 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1920 ASSERT(media_channel == this->media_channel());
1921 SignalMediaMonitor(this, info);
1922}
1923
Peter Boström0c4e06b2015-10-07 12:23:21 +02001924void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001925 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926 ScreencastEventMessageData* pdata =
1927 new ScreencastEventMessageData(ssrc, event);
1928 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1929}
1930
1931void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1932 // Map capturer events to window events. In the future we may want to simply
1933 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001934 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001936 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001938 we = rtc::WE_MINIMIZE;
1939 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1940 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 } else {
1942 return;
1943 }
1944 previous_we_ = we;
1945
Peter Boström0c4e06b2015-10-07 12:23:21 +02001946 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947 if (!GetLocalSsrc(capturer, &ssrc)) {
1948 return;
1949 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001950
1951 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952}
1953
Peter Boström0c4e06b2015-10-07 12:23:21 +02001954bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 *ssrc = 0;
1956 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
1957 iter != screencast_capturers_.end(); ++iter) {
1958 if (iter->second == capturer) {
1959 *ssrc = iter->first;
1960 return true;
1961 }
1962 }
1963 return false;
1964}
1965
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001966void VideoChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1967 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968}
1969
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001970DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001972 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973 const std::string& content_name,
1974 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001975 : BaseChannel(thread,
1976 media_channel,
1977 transport_controller,
1978 content_name,
1979 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001980 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07001981 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982
1983DataChannel::~DataChannel() {
1984 StopMediaMonitor();
1985 // this can't be done in the base class, since it calls a virtual
1986 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001987
1988 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989}
1990
1991bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001992 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 return false;
1994 }
1995 media_channel()->SignalDataReceived.connect(
1996 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001997 media_channel()->SignalReadyToSend.connect(
1998 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001999 media_channel()->SignalStreamClosedRemotely.connect(
2000 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 return true;
2002}
2003
2004bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002005 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002007 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2008 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009}
2010
2011const ContentInfo* DataChannel::GetFirstContent(
2012 const SessionDescription* sdesc) {
2013 return GetFirstDataContent(sdesc);
2014}
2015
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002016bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 if (data_channel_type_ == DCT_SCTP) {
2018 // TODO(pthatcher): Do this in a more robust way by checking for
2019 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002020 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002021 } else if (data_channel_type_ == DCT_RTP) {
2022 return BaseChannel::WantsPacket(rtcp, packet);
2023 }
2024 return false;
2025}
2026
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002027bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2028 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 // It hasn't been set before, so set it now.
2030 if (data_channel_type_ == DCT_NONE) {
2031 data_channel_type_ = new_data_channel_type;
2032 return true;
2033 }
2034
2035 // It's been set before, but doesn't match. That's bad.
2036 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002037 std::ostringstream desc;
2038 desc << "Data channel type mismatch."
2039 << " Expected " << data_channel_type_
2040 << " Got " << new_data_channel_type;
2041 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 return false;
2043 }
2044
2045 // It's hasn't changed. Nothing to do.
2046 return true;
2047}
2048
2049bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002050 const DataContentDescription* content,
2051 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2053 (content->protocol() == kMediaProtocolDtlsSctp));
2054 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002055 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056}
2057
2058bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002059 ContentAction action,
2060 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002061 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 LOG(LS_INFO) << "Setting local data description";
2063
2064 const DataContentDescription* data =
2065 static_cast<const DataContentDescription*>(content);
2066 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002067 if (!data) {
2068 SafeSetError("Can't find data content in local description.", error_desc);
2069 return false;
2070 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002071
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002072 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 return false;
2074 }
2075
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002076 if (data_channel_type_ == DCT_RTP) {
2077 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2078 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079 }
2080 }
2081
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002082 // FYI: We send the SCTP port number (not to be confused with the
2083 // underlying UDP port number) as a codec parameter. So even SCTP
2084 // data channels need codecs.
2085 DataRecvParameters recv_params = last_recv_params_;
2086 RtpParametersFromMediaDescription(data, &recv_params);
2087 if (!media_channel()->SetRecvParameters(recv_params)) {
2088 SafeSetError("Failed to set remote data description recv parameters.",
2089 error_desc);
2090 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002092 if (data_channel_type_ == DCT_RTP) {
2093 for (const DataCodec& codec : data->codecs()) {
2094 bundle_filter()->AddPayloadType(codec.id);
2095 }
2096 }
2097 last_recv_params_ = recv_params;
2098
2099 // TODO(pthatcher): Move local streams into DataSendParameters, and
2100 // only give it to the media channel once we have a remote
2101 // description too (without a remote description, we won't be able
2102 // to send them anyway).
2103 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2104 SafeSetError("Failed to set local data description streams.", error_desc);
2105 return false;
2106 }
2107
2108 set_local_content_direction(content->direction());
2109 ChangeState();
2110 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111}
2112
2113bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002114 ContentAction action,
2115 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002116 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117
2118 const DataContentDescription* data =
2119 static_cast<const DataContentDescription*>(content);
2120 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002121 if (!data) {
2122 SafeSetError("Can't find data content in remote description.", error_desc);
2123 return false;
2124 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002126 // If the remote data doesn't have codecs and isn't an update, it
2127 // must be empty, so ignore it.
2128 if (!data->has_codecs() && action != CA_UPDATE) {
2129 return true;
2130 }
2131
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002132 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 return false;
2134 }
2135
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002136 LOG(LS_INFO) << "Setting remote data description";
2137 if (data_channel_type_ == DCT_RTP &&
2138 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2139 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140 }
2141
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002142
2143 DataSendParameters send_params = last_send_params_;
2144 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2145 if (!media_channel()->SetSendParameters(send_params)) {
2146 SafeSetError("Failed to set remote data description send parameters.",
2147 error_desc);
2148 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002150 last_send_params_ = send_params;
2151
2152 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2153 // and only give it to the media channel once we have a local
2154 // description too (without a local description, we won't be able to
2155 // recv them anyway).
2156 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2157 SafeSetError("Failed to set remote data description streams.",
2158 error_desc);
2159 return false;
2160 }
2161
2162 set_remote_content_direction(content->direction());
2163 ChangeState();
2164 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165}
2166
2167void DataChannel::ChangeState() {
2168 // Render incoming data if we're the active call, and we have the local
2169 // content. We receive data on the default channel and multiplexed streams.
2170 bool recv = IsReadyToReceive();
2171 if (!media_channel()->SetReceive(recv)) {
2172 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2173 }
2174
2175 // Send outgoing data if we're the active call, we have the remote content,
2176 // and we have had some form of connectivity.
2177 bool send = IsReadyToSend();
2178 if (!media_channel()->SetSend(send)) {
2179 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2180 }
2181
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002182 // Trigger SignalReadyToSendData asynchronously.
2183 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002184
2185 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2186}
2187
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002188void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189 switch (pmsg->message_id) {
2190 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002191 DataChannelReadyToSendMessageData* data =
2192 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002193 ready_to_send_data_ = data->data();
2194 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 delete data;
2196 break;
2197 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198 case MSG_DATARECEIVED: {
2199 DataReceivedMessageData* data =
2200 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2201 SignalDataReceived(this, data->params, data->payload);
2202 delete data;
2203 break;
2204 }
2205 case MSG_CHANNEL_ERROR: {
2206 const DataChannelErrorMessageData* data =
2207 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208 delete data;
2209 break;
2210 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002211 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002212 rtc::TypedMessageData<uint32_t>* data =
2213 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002214 SignalStreamClosedRemotely(data->data());
2215 delete data;
2216 break;
2217 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 default:
2219 BaseChannel::OnMessage(pmsg);
2220 break;
2221 }
2222}
2223
2224void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002225 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226 SignalConnectionMonitor(this, infos);
2227}
2228
2229void DataChannel::StartMediaMonitor(int cms) {
2230 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002231 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002232 media_monitor_->SignalUpdate.connect(
2233 this, &DataChannel::OnMediaMonitorUpdate);
2234 media_monitor_->Start(cms);
2235}
2236
2237void DataChannel::StopMediaMonitor() {
2238 if (media_monitor_) {
2239 media_monitor_->Stop();
2240 media_monitor_->SignalUpdate.disconnect(this);
2241 media_monitor_.reset();
2242 }
2243}
2244
2245void DataChannel::OnMediaMonitorUpdate(
2246 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2247 ASSERT(media_channel == this->media_channel());
2248 SignalMediaMonitor(this, info);
2249}
2250
2251void DataChannel::OnDataReceived(
2252 const ReceiveDataParams& params, const char* data, size_t len) {
2253 DataReceivedMessageData* msg = new DataReceivedMessageData(
2254 params, data, len);
2255 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2256}
2257
Peter Boström0c4e06b2015-10-07 12:23:21 +02002258void DataChannel::OnDataChannelError(uint32_t ssrc,
2259 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002260 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2261 ssrc, err);
2262 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2263}
2264
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002265void DataChannel::OnDataChannelReadyToSend(bool writable) {
2266 // This is usded for congestion control to indicate that the stream is ready
2267 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2268 // that the transport channel is ready.
2269 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2270 new DataChannelReadyToSendMessageData(writable));
2271}
2272
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002273void DataChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2274 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275}
2276
2277bool DataChannel::ShouldSetupDtlsSrtp() const {
2278 return (data_channel_type_ == DCT_RTP);
2279}
2280
Peter Boström0c4e06b2015-10-07 12:23:21 +02002281void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2282 rtc::TypedMessageData<uint32_t>* message =
2283 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002284 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2285}
2286
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287} // namespace cricket