blob: 97c1d6691014ff3b79951607d72458de61aa4fa6 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwiberg0eb15ed2015-12-17 03:04:15 -080011#include <utility>
12
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010013#include "webrtc/pc/channel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010015#include "webrtc/audio_sink.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000016#include "webrtc/base/bind.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000017#include "webrtc/base/byteorder.h"
18#include "webrtc/base/common.h"
jbaucheec21bd2016-03-20 06:15:43 -070019#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000020#include "webrtc/base/dscp.h"
21#include "webrtc/base/logging.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070022#include "webrtc/base/networkroute.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010023#include "webrtc/base/trace_event.h"
kjellanderf4752772016-03-02 05:42:30 -080024#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080025#include "webrtc/media/base/rtputils.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010026#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010027#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
29namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000030using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000031
deadbeef2d110be2016-01-13 12:00:26 -080032namespace {
kwiberg31022942016-03-11 14:18:21 -080033// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080034bool SetRawAudioSink_w(VoiceMediaChannel* channel,
35 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080036 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
37 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080038 return true;
39}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020040
41struct SendPacketMessageData : public rtc::MessageData {
42 rtc::CopyOnWriteBuffer packet;
43 rtc::PacketOptions options;
44};
45
deadbeef2d110be2016-01-13 12:00:26 -080046} // namespace
47
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000049 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020050 MSG_SEND_RTP_PACKET,
51 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000056 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057};
58
59// Value specified in RFC 5764.
60static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
61
62static const int kAgcMinus10db = -10;
63
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000064static void SafeSetError(const std::string& message, std::string* error_desc) {
65 if (error_desc) {
66 *error_desc = message;
67 }
68}
69
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000070struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020071 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020073 : ssrc(in_ssrc), error(in_error) {}
74 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 VoiceMediaChannel::Error error;
76};
77
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020079 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020081 : ssrc(in_ssrc), error(in_error) {}
82 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 VideoMediaChannel::Error error;
84};
85
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000086struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 : ssrc(in_ssrc), error(in_error) {}
90 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 DataMediaChannel::Error error;
92};
93
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094static const char* PacketType(bool rtcp) {
95 return (!rtcp) ? "RTP" : "RTCP";
96}
97
jbaucheec21bd2016-03-20 06:15:43 -070098static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 // Check the packet size. We could check the header too if needed.
100 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000101 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
102 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103}
104
105static bool IsReceiveContentDirection(MediaContentDirection direction) {
106 return direction == MD_SENDRECV || direction == MD_RECVONLY;
107}
108
109static bool IsSendContentDirection(MediaContentDirection direction) {
110 return direction == MD_SENDRECV || direction == MD_SENDONLY;
111}
112
113static const MediaContentDescription* GetContentDescription(
114 const ContentInfo* cinfo) {
115 if (cinfo == NULL)
116 return NULL;
117 return static_cast<const MediaContentDescription*>(cinfo->description);
118}
119
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700120template <class Codec>
121void RtpParametersFromMediaDescription(
122 const MediaContentDescriptionImpl<Codec>* desc,
123 RtpParameters<Codec>* params) {
124 // TODO(pthatcher): Remove this once we're sure no one will give us
125 // a description without codecs (currently a CA_UPDATE with just
126 // streams can).
127 if (desc->has_codecs()) {
128 params->codecs = desc->codecs();
129 }
130 // TODO(pthatcher): See if we really need
131 // rtp_header_extensions_set() and remove it if we don't.
132 if (desc->rtp_header_extensions_set()) {
133 params->extensions = desc->rtp_header_extensions();
134 }
deadbeef13871492015-12-09 12:37:51 -0800135 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700136}
137
nisse05103312016-03-16 02:22:50 -0700138template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700139void RtpSendParametersFromMediaDescription(
140 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700141 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700142 RtpParametersFromMediaDescription(desc, send_params);
143 send_params->max_bandwidth_bps = desc->bandwidth();
144}
145
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200146BaseChannel::BaseChannel(rtc::Thread* worker_thread,
147 rtc::Thread* network_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700148 MediaChannel* media_channel,
149 TransportController* transport_controller,
150 const std::string& content_name,
151 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200152 : worker_thread_(worker_thread),
153 network_thread_(network_thread),
154
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 content_name_(content_name),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200156
157 transport_controller_(transport_controller),
deadbeefcbecd352015-09-23 11:50:27 -0700158 rtcp_transport_enabled_(rtcp),
159 transport_channel_(nullptr),
160 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 rtp_ready_to_send_(false),
162 rtcp_ready_to_send_(false),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200163 writable_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 was_ever_writable_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 has_received_packet_(false),
166 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000167 secure_required_(false),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200168 rtp_abs_sendtime_extn_id_(-1),
169
170 media_channel_(media_channel),
171 enabled_(false),
172 local_content_direction_(MD_INACTIVE),
173 remote_content_direction_(MD_INACTIVE) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000174 ASSERT(worker_thread_ == rtc::Thread::Current());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200175 if (transport_controller) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200176 RTC_DCHECK_EQ(network_thread, transport_controller->network_thread());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200177 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 LOG(LS_INFO) << "Created channel for " << content_name;
179}
180
181BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800182 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000183 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000184 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200186 // Eats any outstanding messages or packets.
187 worker_thread_->Clear(&invoker_);
188 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 // We must destroy the media channel before the transport channel, otherwise
190 // the media channel may try to send on the dead transport channel. NULLing
191 // is not an effective strategy since the sends will come on another thread.
192 delete media_channel_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200193 // Note that we don't just call SetTransportChannel_n(nullptr) because that
deadbeefcbecd352015-09-23 11:50:27 -0700194 // would call a pure virtual method which we can't do from a destructor.
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200195 network_thread_->Invoke<void>(
196 Bind(&BaseChannel::DestroyTransportChannels_n, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200197 LOG(LS_INFO) << "Destroyed channel";
198}
199
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200200void BaseChannel::DisconnectTransportChannels_n() {
201 // Send any outstanding RTCP packets.
202 FlushRtcpMessages_n();
203
204 // Stop signals from transport channels, but keep them alive because
205 // media_channel may use them from a different thread.
deadbeefcbecd352015-09-23 11:50:27 -0700206 if (transport_channel_) {
207 DisconnectFromTransportChannel(transport_channel_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200208 }
209 if (rtcp_transport_channel_) {
210 DisconnectFromTransportChannel(rtcp_transport_channel_);
211 }
212
213 // Clear pending read packets/messages.
214 network_thread_->Clear(&invoker_);
215 network_thread_->Clear(this);
216}
217
218void BaseChannel::DestroyTransportChannels_n() {
219 if (transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200220 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700221 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
222 }
223 if (rtcp_transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200224 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700225 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
226 }
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200227 // Clear pending send packets/messages.
228 network_thread_->Clear(&invoker_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200229 network_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230}
231
skvlad6c87a672016-05-17 17:49:52 -0700232bool BaseChannel::Init_w(const std::string* bundle_transport_name) {
233 if (!network_thread_->Invoke<bool>(
234 Bind(&BaseChannel::InitNetwork_n, this, bundle_transport_name))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000235 return false;
236 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237
wu@webrtc.orgde305012013-10-31 15:40:38 +0000238 // Both RTP and RTCP channels are set, we can call SetInterface on
239 // media channel and it can set network options.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200240 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.orgde305012013-10-31 15:40:38 +0000241 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 return true;
243}
244
skvlad6c87a672016-05-17 17:49:52 -0700245bool BaseChannel::InitNetwork_n(const std::string* bundle_transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200246 RTC_DCHECK(network_thread_->IsCurrent());
skvlad6c87a672016-05-17 17:49:52 -0700247 const std::string& transport_name =
248 (bundle_transport_name ? *bundle_transport_name : content_name());
249 if (!SetTransport_n(transport_name)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200250 return false;
251 }
252
253 if (!SetDtlsSrtpCryptoSuites_n(transport_channel_, false)) {
254 return false;
255 }
256 if (rtcp_transport_enabled() &&
257 !SetDtlsSrtpCryptoSuites_n(rtcp_transport_channel_, true)) {
258 return false;
259 }
260 return true;
261}
262
wu@webrtc.org78187522013-10-07 23:32:02 +0000263void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200264 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000265 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200266 // Packets arrive on the network thread, processing packets calls virtual
267 // functions, so need to stop this process in Deinit that is called in
268 // derived classes destructor.
269 network_thread_->Invoke<void>(
270 Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000271}
272
deadbeefcbecd352015-09-23 11:50:27 -0700273bool BaseChannel::SetTransport(const std::string& transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200274 return network_thread_->Invoke<bool>(
275 Bind(&BaseChannel::SetTransport_n, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000276}
277
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200278bool BaseChannel::SetTransport_n(const std::string& transport_name) {
279 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000280
deadbeefcbecd352015-09-23 11:50:27 -0700281 if (transport_name == transport_name_) {
282 // Nothing to do if transport name isn't changing
283 return true;
284 }
285
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800286 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
287 // changes and wait until the DTLS handshake is complete to set the newly
288 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200289 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800290 // Set |writable_| to false such that UpdateWritableState_w can set up
291 // DTLS-SRTP when the writable_ becomes true again.
292 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800293 srtp_filter_.ResetParams();
294 }
295
guoweis46383312015-12-17 16:45:59 -0800296 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
deadbeefcbecd352015-09-23 11:50:27 -0700297 if (rtcp_transport_enabled()) {
298 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
299 << " on " << transport_name << " transport ";
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200300 SetRtcpTransportChannel_n(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200301 transport_controller_->CreateTransportChannel_n(
guoweis46383312015-12-17 16:45:59 -0800302 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP),
303 false /* update_writablity */);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200304 if (!rtcp_transport_channel_) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000305 return false;
306 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000307 }
308
guoweis46383312015-12-17 16:45:59 -0800309 // We're not updating the writablity during the transition state.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200310 SetTransportChannel_n(transport_controller_->CreateTransportChannel_n(
guoweis46383312015-12-17 16:45:59 -0800311 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200312 if (!transport_channel_) {
guoweis46383312015-12-17 16:45:59 -0800313 return false;
314 }
315
316 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
317 if (rtcp_transport_enabled()) {
318 // We can only update the RTCP ready to send after set_transport_channel has
319 // handled channel writability.
320 SetReadyToSend(
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200321 true, rtcp_transport_channel_ && rtcp_transport_channel_->writable());
guoweis46383312015-12-17 16:45:59 -0800322 }
deadbeefcbecd352015-09-23 11:50:27 -0700323 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000324 return true;
325}
326
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200327void BaseChannel::SetTransportChannel_n(TransportChannel* new_tc) {
328 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000329
330 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700331 if (!old_tc && !new_tc) {
332 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000333 return;
334 }
deadbeefcbecd352015-09-23 11:50:27 -0700335 ASSERT(old_tc != new_tc);
336
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000337 if (old_tc) {
338 DisconnectFromTransportChannel(old_tc);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200339 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700340 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000341 }
342
343 transport_channel_ = new_tc;
344
345 if (new_tc) {
346 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700347 for (const auto& pair : socket_options_) {
348 new_tc->SetOption(pair.first, pair.second);
349 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000350 }
deadbeefcbecd352015-09-23 11:50:27 -0700351
352 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
353 // setting new channel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200354 UpdateWritableState_n();
deadbeefcbecd352015-09-23 11:50:27 -0700355 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000356}
357
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200358void BaseChannel::SetRtcpTransportChannel_n(TransportChannel* new_tc,
359 bool update_writablity) {
360 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000361
362 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700363 if (!old_tc && !new_tc) {
364 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000365 return;
366 }
deadbeefcbecd352015-09-23 11:50:27 -0700367 ASSERT(old_tc != new_tc);
368
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000369 if (old_tc) {
370 DisconnectFromTransportChannel(old_tc);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200371 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700372 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000373 }
374
375 rtcp_transport_channel_ = new_tc;
376
377 if (new_tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200378 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800379 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
380 << "should never happen.";
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000381 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700382 for (const auto& pair : rtcp_socket_options_) {
383 new_tc->SetOption(pair.first, pair.second);
384 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000385 }
deadbeefcbecd352015-09-23 11:50:27 -0700386
guoweis46383312015-12-17 16:45:59 -0800387 if (update_writablity) {
388 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
389 // setting new channel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200390 UpdateWritableState_n();
guoweis46383312015-12-17 16:45:59 -0800391 SetReadyToSend(true, new_tc && new_tc->writable());
392 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000393}
394
395void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200396 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000397
398 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
399 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
400 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800401 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700402 tc->SignalSelectedCandidatePairChanged.connect(
403 this, &BaseChannel::OnSelectedCandidatePairChanged);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200404 tc->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000405}
406
407void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200408 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000409
410 tc->SignalWritableState.disconnect(this);
411 tc->SignalReadPacket.disconnect(this);
412 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800413 tc->SignalDtlsState.disconnect(this);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200414 tc->SignalSelectedCandidatePairChanged.disconnect(this);
415 tc->SignalSentPacket.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000416}
417
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000419 worker_thread_->Invoke<void>(Bind(
420 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
421 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 return true;
423}
424
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000426 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427}
428
Peter Boström0c4e06b2015-10-07 12:23:21 +0200429bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000430 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431}
432
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000433bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000434 return InvokeOnWorker(
435 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000436}
437
Peter Boström0c4e06b2015-10-07 12:23:21 +0200438bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000439 return InvokeOnWorker(
440 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000441}
442
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000444 ContentAction action,
445 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100446 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000447 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
448 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449}
450
451bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000452 ContentAction action,
453 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100454 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000455 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
456 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457}
458
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000460 // We pass in the BaseChannel instead of the transport_channel_
461 // because if the transport_channel_ changes, the ConnectionMonitor
462 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200463 // We pass in the network thread because on that thread connection monitor
464 // will call BaseChannel::GetConnectionStats which must be called on the
465 // network thread.
466 connection_monitor_.reset(
467 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000468 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000470 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471}
472
473void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000474 if (connection_monitor_) {
475 connection_monitor_->Stop();
476 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 }
478}
479
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000480bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200481 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000482 return transport_channel_->GetStats(infos);
483}
484
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200485bool BaseChannel::IsReadyToReceive_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 // Receive data if we are enabled and have local content,
487 return enabled() && IsReceiveContentDirection(local_content_direction_);
488}
489
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200490bool BaseChannel::IsReadyToSend_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 // Send outgoing data if we are enabled, have local and remote content,
492 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800493 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 IsSendContentDirection(local_content_direction_) &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200495 network_thread_->Invoke<bool>(
496 Bind(&BaseChannel::IsTransportReadyToSend_n, this));
497}
498
499bool BaseChannel::IsTransportReadyToSend_n() const {
500 return was_ever_writable() &&
501 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502}
503
jbaucheec21bd2016-03-20 06:15:43 -0700504bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700505 const rtc::PacketOptions& options) {
506 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507}
508
jbaucheec21bd2016-03-20 06:15:43 -0700509bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700510 const rtc::PacketOptions& options) {
511 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512}
513
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000514int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200516 return network_thread_->Invoke<int>(
517 Bind(&BaseChannel::SetOption_n, this, type, opt, value));
518}
519
520int BaseChannel::SetOption_n(SocketType type,
521 rtc::Socket::Option opt,
522 int value) {
523 RTC_DCHECK(network_thread_->IsCurrent());
524 TransportChannel* channel = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000526 case ST_RTP:
527 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700528 socket_options_.push_back(
529 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000530 break;
531 case ST_RTCP:
532 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700533 rtcp_socket_options_.push_back(
534 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000535 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000537 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538}
539
540void BaseChannel::OnWritableState(TransportChannel* channel) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200541 RTC_DCHECK(channel == transport_channel_ ||
542 channel == rtcp_transport_channel_);
543 RTC_DCHECK(network_thread_->IsCurrent());
544 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545}
546
547void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000548 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000549 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000550 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100551 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200553 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554
555 // When using RTCP multiplexing we might get RTCP packets on the RTP
556 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
557 bool rtcp = PacketIsRtcp(channel, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700558 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000559 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560}
561
562void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700563 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
564 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565}
566
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800567void BaseChannel::OnDtlsState(TransportChannel* channel,
568 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200569 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800570 return;
571 }
572
573 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
574 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
575 // cover other scenarios like the whole channel is writable (not just this
576 // TransportChannel) or when TransportChannel is attached after DTLS is
577 // negotiated.
578 if (state != DTLS_TRANSPORT_CONNECTED) {
579 srtp_filter_.ResetParams();
580 }
581}
582
Honghai Zhangcc411c02016-03-29 17:27:21 -0700583void BaseChannel::OnSelectedCandidatePairChanged(
584 TransportChannel* channel,
Honghai Zhang52dce732016-03-31 12:37:31 -0700585 CandidatePairInterface* selected_candidate_pair,
586 int last_sent_packet_id) {
Honghai Zhangcc411c02016-03-29 17:27:21 -0700587 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200588 RTC_DCHECK(network_thread_->IsCurrent());
589 std::string transport_name = channel->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700590 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700591 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700592 network_route = rtc::NetworkRoute(
593 selected_candidate_pair->local_candidate().network_id(),
594 selected_candidate_pair->remote_candidate().network_id(),
595 last_sent_packet_id);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700596 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200597 invoker_.AsyncInvoke<void>(
598 worker_thread_, Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_,
599 transport_name, network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700600}
601
deadbeefcbecd352015-09-23 11:50:27 -0700602void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200603 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700604 if (rtcp) {
605 rtcp_ready_to_send_ = ready;
606 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 rtp_ready_to_send_ = ready;
608 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200610 bool ready_to_send =
611 (rtp_ready_to_send_ &&
612 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
613 (rtcp_ready_to_send_ || !rtcp_transport_channel_));
614
615 invoker_.AsyncInvoke<void>(
616 worker_thread_,
617 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618}
619
620bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
621 const char* data, size_t len) {
622 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000623 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624}
625
stefanc1aeaf02015-10-15 07:26:07 -0700626bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700627 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700628 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200629 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
630 // If the thread is not our network thread, we will post to our network
631 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 // synchronize access to all the pieces of the send path, including
633 // SRTP and the inner workings of the transport channels.
634 // The only downside is that we can't return a proper failure code if
635 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200636 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200638 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
639 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800640 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700641 data->options = options;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200642 network_thread_->Post(this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 return true;
644 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200645 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646
647 // Now that we are on the correct thread, ensure we have a place to send this
648 // packet before doing anything. (We might get RTCP packets that we don't
649 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
650 // transport.
651 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
652 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000653 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 return false;
655 }
656
657 // Protect ourselves against crazy data.
658 if (!ValidPacket(rtcp, packet)) {
659 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000660 << PacketType(rtcp)
661 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 return false;
663 }
664
stefanc1aeaf02015-10-15 07:26:07 -0700665 rtc::PacketOptions updated_options;
666 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 // Protect if needed.
668 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200669 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200671 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000672 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000674 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
675 // inside libsrtp for a RTP packet. A external HMAC module will be writing
676 // a fake HMAC value. This is ONLY done for a RTP packet.
677 // Socket layer will update rtp sendtime extension header if present in
678 // packet with current time before updating the HMAC.
679#if !defined(ENABLE_EXTERNAL_AUTH)
680 res = srtp_filter_.ProtectRtp(
681 data, len, static_cast<int>(packet->capacity()), &len);
682#else
stefanc1aeaf02015-10-15 07:26:07 -0700683 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000684 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000685 res = srtp_filter_.ProtectRtp(
686 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700687 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000688 // If protection succeeds, let's get auth params from srtp.
689 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200690 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000691 int key_len;
692 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700693 &auth_key, &key_len,
694 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000695 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700696 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
697 updated_options.packet_time_params.srtp_auth_key.assign(
698 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000699 }
700 }
701#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 if (!res) {
703 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200704 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 GetRtpSeqNum(data, len, &seq_num);
706 GetRtpSsrc(data, len, &ssrc);
707 LOG(LS_ERROR) << "Failed to protect " << content_name_
708 << " RTP packet: size=" << len
709 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
710 return false;
711 }
712 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000713 res = srtp_filter_.ProtectRtcp(data, len,
714 static_cast<int>(packet->capacity()),
715 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 if (!res) {
717 int type = -1;
718 GetRtcpType(data, len, &type);
719 LOG(LS_ERROR) << "Failed to protect " << content_name_
720 << " RTCP packet: size=" << len << ", type=" << type;
721 return false;
722 }
723 }
724
725 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000726 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 } else if (secure_required_) {
728 // This is a double check for something that supposedly can't happen.
729 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
730 << " packet when SRTP is inactive and crypto is required";
731
732 ASSERT(false);
733 return false;
734 }
735
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200737 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
738 int ret = channel->SendPacket(packet->data<char>(), packet->size(),
739 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000740 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 if (channel->GetError() == EWOULDBLOCK) {
742 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700743 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 }
745 return false;
746 }
747 return true;
748}
749
jbaucheec21bd2016-03-20 06:15:43 -0700750bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 // Protect ourselves against crazy data.
752 if (!ValidPacket(rtcp, packet)) {
753 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000754 << PacketType(rtcp)
755 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 return false;
757 }
pbos482b12e2015-11-16 10:19:58 -0800758 if (rtcp) {
759 // Permit all (seemingly valid) RTCP packets.
760 return true;
761 }
762 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700763 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764}
765
jbaucheec21bd2016-03-20 06:15:43 -0700766void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000767 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200768 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 if (!WantsPacket(rtcp, packet)) {
770 return;
771 }
772
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000773 // We are only interested in the first rtp packet because that
774 // indicates the media has started flowing.
775 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 has_received_packet_ = true;
777 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
778 }
779
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 // Unprotect the packet, if needed.
781 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200782 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200783 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000784 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 bool res;
786 if (!rtcp) {
787 res = srtp_filter_.UnprotectRtp(data, len, &len);
788 if (!res) {
789 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200790 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 GetRtpSeqNum(data, len, &seq_num);
792 GetRtpSsrc(data, len, &ssrc);
793 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
794 << " RTP packet: size=" << len
795 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
796 return;
797 }
798 } else {
799 res = srtp_filter_.UnprotectRtcp(data, len, &len);
800 if (!res) {
801 int type = -1;
802 GetRtcpType(data, len, &type);
803 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
804 << " RTCP packet: size=" << len << ", type=" << type;
805 return;
806 }
807 }
808
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000809 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 } else if (secure_required_) {
811 // Our session description indicates that SRTP is required, but we got a
812 // packet before our SRTP filter is active. This means either that
813 // a) we got SRTP packets before we received the SDES keys, in which case
814 // we can't decrypt it anyway, or
815 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
816 // channels, so we haven't yet extracted keys, even if DTLS did complete
817 // on the channel that the packets are being sent on. It's really good
818 // practice to wait for both RTP and RTCP to be good to go before sending
819 // media, to prevent weird failure modes, so it's fine for us to just eat
820 // packets here. This is all sidestepped if RTCP mux is used anyway.
821 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
822 << " packet when SRTP is inactive and crypto is required";
823 return;
824 }
825
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200826 invoker_.AsyncInvoke<void>(
827 worker_thread_,
828 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
829}
830
831void BaseChannel::OnPacketReceived(bool rtcp,
832 const rtc::CopyOnWriteBuffer& packet,
833 const rtc::PacketTime& packet_time) {
834 RTC_DCHECK(worker_thread_->IsCurrent());
835 // Need to copy variable because OnRtcpReceived/OnPacketReceived
836 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
837 rtc::CopyOnWriteBuffer data(packet);
838 if (rtcp) {
839 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200841 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 }
843}
844
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000845bool BaseChannel::PushdownLocalDescription(
846 const SessionDescription* local_desc, ContentAction action,
847 std::string* error_desc) {
848 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 const MediaContentDescription* content_desc =
850 GetContentDescription(content_info);
851 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000852 !SetLocalContent(content_desc, action, error_desc)) {
853 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
854 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000856 return true;
857}
858
859bool BaseChannel::PushdownRemoteDescription(
860 const SessionDescription* remote_desc, ContentAction action,
861 std::string* error_desc) {
862 const ContentInfo* content_info = GetFirstContent(remote_desc);
863 const MediaContentDescription* content_desc =
864 GetContentDescription(content_info);
865 if (content_desc && content_info && !content_info->rejected &&
866 !SetRemoteContent(content_desc, action, error_desc)) {
867 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
868 return false;
869 }
870 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871}
872
873void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000874 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 if (enabled_)
876 return;
877
878 LOG(LS_INFO) << "Channel enabled";
879 enabled_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200880 ChangeState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881}
882
883void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000884 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 if (!enabled_)
886 return;
887
888 LOG(LS_INFO) << "Channel disabled";
889 enabled_ = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200890 ChangeState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891}
892
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200893void BaseChannel::UpdateWritableState_n() {
deadbeefcbecd352015-09-23 11:50:27 -0700894 if (transport_channel_ && transport_channel_->writable() &&
895 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200896 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700897 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200898 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700899 }
900}
901
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200902void BaseChannel::ChannelWritable_n() {
903 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800904 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800906 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907
deadbeefcbecd352015-09-23 11:50:27 -0700908 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 << (was_ever_writable_ ? "" : " for the first time");
910
911 std::vector<ConnectionInfo> infos;
912 transport_channel_->GetStats(&infos);
913 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
914 it != infos.end(); ++it) {
915 if (it->best_connection) {
916 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
917 << "->" << it->remote_candidate.ToSensitiveString();
918 break;
919 }
920 }
921
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200923 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 writable_ = true;
925 ChangeState();
926}
927
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200928void BaseChannel::SignalDtlsSetupFailure_n(bool rtcp) {
929 RTC_DCHECK(network_thread_->IsCurrent());
930 invoker_.AsyncInvoke<void>(
931 signaling_thread(),
932 Bind(&BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000933}
934
935void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
936 ASSERT(signaling_thread() == rtc::Thread::Current());
937 SignalDtlsSetupFailure(this, rtcp);
938}
939
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200940bool BaseChannel::SetDtlsSrtpCryptoSuites_n(TransportChannel* tc, bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800941 std::vector<int> crypto_suites;
942 // We always use the default SRTP crypto suites for RTCP, but we may use
943 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200945 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800947 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800949 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950}
951
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200952bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800953 // Since DTLS is applied to all channels, checking RTP should be enough.
954 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955}
956
957// This function returns true if either DTLS-SRTP is not in use
958// *or* DTLS-SRTP is successfully set up.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200959bool BaseChannel::SetupDtlsSrtp_n(bool rtcp_channel) {
960 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 bool ret = false;
962
deadbeefcbecd352015-09-23 11:50:27 -0700963 TransportChannel* channel =
964 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800966 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800968 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800970 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
971 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 return false;
973 }
974
975 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
976 << content_name() << " "
977 << PacketType(rtcp_channel);
978
979 // OK, we're now doing DTLS (RFC 5764)
980 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
981 SRTP_MASTER_KEY_SALT_LEN * 2);
982
983 // RFC 5705 exporter using the RFC 5764 parameters
984 if (!channel->ExportKeyingMaterial(
985 kDtlsSrtpExporterLabel,
986 NULL, 0, false,
987 &dtls_buffer[0], dtls_buffer.size())) {
988 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
989 ASSERT(false); // This should never happen
990 return false;
991 }
992
993 // Sync up the keys with the DTLS-SRTP interface
994 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
995 SRTP_MASTER_KEY_SALT_LEN);
996 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
997 SRTP_MASTER_KEY_SALT_LEN);
998 size_t offset = 0;
999 memcpy(&client_write_key[0], &dtls_buffer[offset],
1000 SRTP_MASTER_KEY_KEY_LEN);
1001 offset += SRTP_MASTER_KEY_KEY_LEN;
1002 memcpy(&server_write_key[0], &dtls_buffer[offset],
1003 SRTP_MASTER_KEY_KEY_LEN);
1004 offset += SRTP_MASTER_KEY_KEY_LEN;
1005 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
1006 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
1007 offset += SRTP_MASTER_KEY_SALT_LEN;
1008 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
1009 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
1010
1011 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001012 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001013 if (!channel->GetSslRole(&role)) {
1014 LOG(LS_WARNING) << "GetSslRole failed";
1015 return false;
1016 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001018 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 send_key = &server_write_key;
1020 recv_key = &client_write_key;
1021 } else {
1022 send_key = &client_write_key;
1023 recv_key = &server_write_key;
1024 }
1025
1026 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001027 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1028 static_cast<int>(send_key->size()),
1029 selected_crypto_suite, &(*recv_key)[0],
1030 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001032 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1033 static_cast<int>(send_key->size()),
1034 selected_crypto_suite, &(*recv_key)[0],
1035 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 }
1037
1038 if (!ret)
1039 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
1040 else
1041 dtls_keyed_ = true;
1042
1043 return ret;
1044}
1045
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001046void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001047 if (srtp_filter_.IsActive()) {
1048 return;
1049 }
1050
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001051 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001052 return;
1053 }
1054
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001055 if (!SetupDtlsSrtp_n(false)) {
1056 SignalDtlsSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001057 return;
1058 }
1059
1060 if (rtcp_transport_channel_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001061 if (!SetupDtlsSrtp_n(true)) {
1062 SignalDtlsSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001063 return;
1064 }
1065 }
1066}
1067
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001068void BaseChannel::ChannelNotWritable_n() {
1069 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 if (!writable_)
1071 return;
1072
deadbeefcbecd352015-09-23 11:50:27 -07001073 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 writable_ = false;
1075 ChangeState();
1076}
1077
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001078bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001079 const MediaContentDescription* content,
1080 ContentAction action,
1081 ContentSource src,
1082 std::string* error_desc) {
1083 if (action == CA_UPDATE) {
1084 // These parameters never get changed by a CA_UDPATE.
1085 return true;
1086 }
1087
1088 // Cache secure_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001089 return network_thread_->Invoke<bool>(
1090 Bind(&BaseChannel::SetRtpTransportParameters_n, this, content, action,
1091 src, error_desc));
1092}
1093
1094bool BaseChannel::SetRtpTransportParameters_n(
1095 const MediaContentDescription* content,
1096 ContentAction action,
1097 ContentSource src,
1098 std::string* error_desc) {
1099 RTC_DCHECK(network_thread_->IsCurrent());
1100
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001101 if (src == CS_LOCAL) {
1102 set_secure_required(content->crypto_required() != CT_NONE);
1103 }
1104
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001105 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001106 return false;
1107 }
1108
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001109 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001110 return false;
1111 }
1112
1113 return true;
1114}
1115
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001116// |dtls| will be set to true if DTLS is active for transport channel and
1117// crypto is empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001118bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1119 bool* dtls,
1120 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001121 *dtls = transport_channel_->IsDtlsActive();
1122 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001123 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001124 return false;
1125 }
1126 return true;
1127}
1128
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001129bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001130 ContentAction action,
1131 ContentSource src,
1132 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001133 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001134 if (action == CA_UPDATE) {
1135 // no crypto params.
1136 return true;
1137 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001139 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001140 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001141 if (!ret) {
1142 return false;
1143 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001144 switch (action) {
1145 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001146 // If DTLS is already active on the channel, we could be renegotiating
1147 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001148 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001149 ret = srtp_filter_.SetOffer(cryptos, src);
1150 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151 break;
1152 case CA_PRANSWER:
1153 // If we're doing DTLS-SRTP, we don't want to update the filter
1154 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001155 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1157 }
1158 break;
1159 case CA_ANSWER:
1160 // If we're doing DTLS-SRTP, we don't want to update the filter
1161 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001162 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 ret = srtp_filter_.SetAnswer(cryptos, src);
1164 }
1165 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 default:
1167 break;
1168 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001169 if (!ret) {
1170 SafeSetError("Failed to setup SRTP filter.", error_desc);
1171 return false;
1172 }
1173 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001174}
1175
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001176void BaseChannel::ActivateRtcpMux() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001177 network_thread_->Invoke<void>(Bind(&BaseChannel::ActivateRtcpMux_n, this));
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001178}
1179
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001180void BaseChannel::ActivateRtcpMux_n() {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001181 if (!rtcp_mux_filter_.IsActive()) {
1182 rtcp_mux_filter_.SetActive();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001183 SetRtcpTransportChannel_n(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001184 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001185 }
1186}
1187
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001188bool BaseChannel::SetRtcpMux_n(bool enable,
1189 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001190 ContentSource src,
1191 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 bool ret = false;
1193 switch (action) {
1194 case CA_OFFER:
1195 ret = rtcp_mux_filter_.SetOffer(enable, src);
1196 break;
1197 case CA_PRANSWER:
1198 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1199 break;
1200 case CA_ANSWER:
1201 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1202 if (ret && rtcp_mux_filter_.IsActive()) {
1203 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001204 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1205 << " by destroying RTCP transport channel for "
1206 << transport_name();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001207 SetRtcpTransportChannel_n(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001208 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 }
1210 break;
1211 case CA_UPDATE:
1212 // No RTCP mux info.
1213 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001214 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 default:
1216 break;
1217 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001218 if (!ret) {
1219 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1220 return false;
1221 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1223 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1224 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001225 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 // If the RTP transport is already writable, then so are we.
1227 if (transport_channel_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001228 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229 }
1230 }
1231
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001232 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233}
1234
1235bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001236 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001237 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238}
1239
Peter Boström0c4e06b2015-10-07 12:23:21 +02001240bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001241 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 return media_channel()->RemoveRecvStream(ssrc);
1243}
1244
1245bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001246 ContentAction action,
1247 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1249 action == CA_PRANSWER || action == CA_UPDATE))
1250 return false;
1251
1252 // If this is an update, streams only contain streams that have changed.
1253 if (action == CA_UPDATE) {
1254 for (StreamParamsVec::const_iterator it = streams.begin();
1255 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001256 const StreamParams* existing_stream =
1257 GetStreamByIds(local_streams_, it->groupid, it->id);
1258 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 if (media_channel()->AddSendStream(*it)) {
1260 local_streams_.push_back(*it);
1261 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1262 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001263 std::ostringstream desc;
1264 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1265 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001266 return false;
1267 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001268 } else if (existing_stream && !it->has_ssrcs()) {
1269 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001270 std::ostringstream desc;
1271 desc << "Failed to remove send stream with ssrc "
1272 << it->first_ssrc() << ".";
1273 SafeSetError(desc.str(), error_desc);
1274 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001276 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 } else {
1278 LOG(LS_WARNING) << "Ignore unsupported stream update";
1279 }
1280 }
1281 return true;
1282 }
1283 // Else streams are all the streams we want to send.
1284
1285 // Check for streams that have been removed.
1286 bool ret = true;
1287 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1288 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001289 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001291 std::ostringstream desc;
1292 desc << "Failed to remove send stream with ssrc "
1293 << it->first_ssrc() << ".";
1294 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 ret = false;
1296 }
1297 }
1298 }
1299 // Check for new streams.
1300 for (StreamParamsVec::const_iterator it = streams.begin();
1301 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001302 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001304 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001306 std::ostringstream desc;
1307 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1308 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 ret = false;
1310 }
1311 }
1312 }
1313 local_streams_ = streams;
1314 return ret;
1315}
1316
1317bool BaseChannel::UpdateRemoteStreams_w(
1318 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001319 ContentAction action,
1320 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1322 action == CA_PRANSWER || action == CA_UPDATE))
1323 return false;
1324
1325 // If this is an update, streams only contain streams that have changed.
1326 if (action == CA_UPDATE) {
1327 for (StreamParamsVec::const_iterator it = streams.begin();
1328 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001329 const StreamParams* existing_stream =
1330 GetStreamByIds(remote_streams_, it->groupid, it->id);
1331 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 if (AddRecvStream_w(*it)) {
1333 remote_streams_.push_back(*it);
1334 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1335 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001336 std::ostringstream desc;
1337 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1338 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339 return false;
1340 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001341 } else if (existing_stream && !it->has_ssrcs()) {
1342 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001343 std::ostringstream desc;
1344 desc << "Failed to remove remote stream with ssrc "
1345 << it->first_ssrc() << ".";
1346 SafeSetError(desc.str(), error_desc);
1347 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001349 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 } else {
1351 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001352 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 << " new stream = " << it->ToString();
1354 }
1355 }
1356 return true;
1357 }
1358 // Else streams are all the streams we want to receive.
1359
1360 // Check for streams that have been removed.
1361 bool ret = true;
1362 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1363 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001364 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001366 std::ostringstream desc;
1367 desc << "Failed to remove remote stream with ssrc "
1368 << it->first_ssrc() << ".";
1369 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370 ret = false;
1371 }
1372 }
1373 }
1374 // Check for new streams.
1375 for (StreamParamsVec::const_iterator it = streams.begin();
1376 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001377 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378 if (AddRecvStream_w(*it)) {
1379 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1380 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001381 std::ostringstream desc;
1382 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1383 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 ret = false;
1385 }
1386 }
1387 }
1388 remote_streams_ = streams;
1389 return ret;
1390}
1391
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001392void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001393 const std::vector<RtpHeaderExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001394// Absolute Send Time extension id is used only with external auth,
1395// so do not bother searching for it and making asyncronious call to set
1396// something that is not used.
1397#if defined(ENABLE_EXTERNAL_AUTH)
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001398 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001399 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001400 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001401 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001402 invoker_.AsyncInvoke<void>(
1403 network_thread_, Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n,
1404 this, rtp_abs_sendtime_extn_id));
1405#endif
1406}
1407
1408void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1409 int rtp_abs_sendtime_extn_id) {
1410 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001411}
1412
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001413void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001414 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001416 case MSG_SEND_RTP_PACKET:
1417 case MSG_SEND_RTCP_PACKET: {
1418 RTC_DCHECK(network_thread_->IsCurrent());
1419 SendPacketMessageData* data =
1420 static_cast<SendPacketMessageData*>(pmsg->pdata);
1421 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1422 SendPacket(rtcp, &data->packet, data->options);
1423 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 break;
1425 }
1426 case MSG_FIRSTPACKETRECEIVED: {
1427 SignalFirstPacketReceived(this);
1428 break;
1429 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430 }
1431}
1432
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001433void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001434 // Flush all remaining RTCP messages. This should only be called in
1435 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001436 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001437 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001438 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1439 for (const auto& message : rtcp_messages) {
1440 network_thread_->Send(this, MSG_SEND_RTCP_PACKET, message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 }
1442}
1443
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001444void BaseChannel::SignalSentPacket_n(TransportChannel* /* channel */,
1445 const rtc::SentPacket& sent_packet) {
1446 RTC_DCHECK(network_thread_->IsCurrent());
1447 invoker_.AsyncInvoke<void>(
1448 worker_thread_,
1449 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1450}
1451
1452void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1453 RTC_DCHECK(worker_thread_->IsCurrent());
1454 SignalSentPacket(sent_packet);
1455}
1456
1457VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1458 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459 MediaEngineInterface* media_engine,
1460 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001461 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001462 const std::string& content_name,
1463 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001464 : BaseChannel(worker_thread,
1465 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001466 media_channel,
1467 transport_controller,
1468 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001470 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001471 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472
1473VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001474 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 StopAudioMonitor();
1476 StopMediaMonitor();
1477 // this can't be done in the base class, since it calls a virtual
1478 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001479 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480}
1481
skvlad6c87a672016-05-17 17:49:52 -07001482bool VoiceChannel::Init_w(const std::string* bundle_transport_name) {
1483 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484 return false;
1485 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486 return true;
1487}
1488
Peter Boström0c4e06b2015-10-07 12:23:21 +02001489bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001490 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001491 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001492 AudioSource* source) {
deadbeefcbecd352015-09-23 11:50:27 -07001493 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001494 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495}
1496
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497// TODO(juberti): Handle early media the right way. We should get an explicit
1498// ringing message telling us to start playing local ringback, which we cancel
1499// if any early media actually arrives. For now, we do the opposite, which is
1500// to wait 1 second for early media, and start playing local ringback if none
1501// arrives.
1502void VoiceChannel::SetEarlyMedia(bool enable) {
1503 if (enable) {
1504 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001505 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1506 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507 } else {
1508 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001509 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 }
1511}
1512
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001514 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1515 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516}
1517
Peter Boström0c4e06b2015-10-07 12:23:21 +02001518bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1519 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001520 int duration) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001521 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
solenberg1d63dd02015-12-02 12:35:09 -08001522 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523}
1524
solenberg4bac9c52015-10-09 02:32:53 -07001525bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1526 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1527 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001529
Tommif888bb52015-12-12 01:37:01 +01001530void VoiceChannel::SetRawAudioSink(
1531 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001532 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1533 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001534 // passing. So we invoke to our own little routine that gets a pointer to
1535 // our local variable. This is OK since we're synchronously invoking.
1536 InvokeOnWorker(Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001537}
1538
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001539webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001540 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001541 Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001542}
1543
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001544webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1545 uint32_t ssrc) const {
1546 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001547}
1548
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001549bool VoiceChannel::SetRtpSendParameters(
1550 uint32_t ssrc,
1551 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001552 return InvokeOnWorker(
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001553 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001554}
1555
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001556bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1557 webrtc::RtpParameters parameters) {
1558 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1559}
1560
1561webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1562 uint32_t ssrc) const {
1563 return worker_thread()->Invoke<webrtc::RtpParameters>(
1564 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1565}
1566
1567webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1568 uint32_t ssrc) const {
1569 return media_channel()->GetRtpReceiveParameters(ssrc);
1570}
1571
1572bool VoiceChannel::SetRtpReceiveParameters(
1573 uint32_t ssrc,
1574 const webrtc::RtpParameters& parameters) {
1575 return InvokeOnWorker(
1576 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1577}
1578
1579bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1580 webrtc::RtpParameters parameters) {
1581 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001582}
1583
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001585 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1586 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587}
1588
1589void VoiceChannel::StartMediaMonitor(int cms) {
1590 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001591 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 media_monitor_->SignalUpdate.connect(
1593 this, &VoiceChannel::OnMediaMonitorUpdate);
1594 media_monitor_->Start(cms);
1595}
1596
1597void VoiceChannel::StopMediaMonitor() {
1598 if (media_monitor_) {
1599 media_monitor_->Stop();
1600 media_monitor_->SignalUpdate.disconnect(this);
1601 media_monitor_.reset();
1602 }
1603}
1604
1605void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001606 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 audio_monitor_
1608 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1609 audio_monitor_->Start(cms);
1610}
1611
1612void VoiceChannel::StopAudioMonitor() {
1613 if (audio_monitor_) {
1614 audio_monitor_->Stop();
1615 audio_monitor_.reset();
1616 }
1617}
1618
1619bool VoiceChannel::IsAudioMonitorRunning() const {
1620 return (audio_monitor_.get() != NULL);
1621}
1622
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001624 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625}
1626
1627int VoiceChannel::GetOutputLevel_w() {
1628 return media_channel()->GetOutputLevel();
1629}
1630
1631void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1632 media_channel()->GetActiveStreams(actives);
1633}
1634
1635void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001636 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001637 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001638 int flags) {
1639 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640
1641 // Set a flag when we've received an RTP packet. If we're waiting for early
1642 // media, this will disable the timeout.
1643 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1644 received_media_ = true;
1645 }
1646}
1647
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001648void BaseChannel::ChangeState() {
1649 RTC_DCHECK(network_thread_->IsCurrent());
1650 invoker_.AsyncInvoke<void>(worker_thread_,
1651 Bind(&BaseChannel::ChangeState_w, this));
1652}
1653
1654void VoiceChannel::ChangeState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 // Render incoming data if we're the active call, and we have the local
1656 // content. We receive data on the default channel and multiplexed streams.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001657 bool recv = IsReadyToReceive_w();
solenberg5b14b422015-10-01 04:10:31 -07001658 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659
1660 // Send outgoing data if we're the active call, we have the remote content,
1661 // and we have had some form of connectivity.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001662 bool send = IsReadyToSend_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001663 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664
1665 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1666}
1667
1668const ContentInfo* VoiceChannel::GetFirstContent(
1669 const SessionDescription* sdesc) {
1670 return GetFirstAudioContent(sdesc);
1671}
1672
1673bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001674 ContentAction action,
1675 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001676 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001677 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 LOG(LS_INFO) << "Setting local voice description";
1679
1680 const AudioContentDescription* audio =
1681 static_cast<const AudioContentDescription*>(content);
1682 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001683 if (!audio) {
1684 SafeSetError("Can't find audio content in local description.", error_desc);
1685 return false;
1686 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001688 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001689 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 }
1691
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001692 AudioRecvParameters recv_params = last_recv_params_;
1693 RtpParametersFromMediaDescription(audio, &recv_params);
1694 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001695 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001696 error_desc);
1697 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001699 for (const AudioCodec& codec : audio->codecs()) {
1700 bundle_filter()->AddPayloadType(codec.id);
1701 }
1702 last_recv_params_ = recv_params;
1703
1704 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1705 // only give it to the media channel once we have a remote
1706 // description too (without a remote description, we won't be able
1707 // to send them anyway).
1708 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1709 SafeSetError("Failed to set local audio description streams.", error_desc);
1710 return false;
1711 }
1712
1713 set_local_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001714 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001715 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716}
1717
1718bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001719 ContentAction action,
1720 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001721 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001722 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 LOG(LS_INFO) << "Setting remote voice description";
1724
1725 const AudioContentDescription* audio =
1726 static_cast<const AudioContentDescription*>(content);
1727 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001728 if (!audio) {
1729 SafeSetError("Can't find audio content in remote description.", error_desc);
1730 return false;
1731 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001733 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001734 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 }
1736
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001737 AudioSendParameters send_params = last_send_params_;
1738 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001739 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001740 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001741 }
skvladdc1c62c2016-03-16 19:07:43 -07001742
1743 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1744 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001745 SafeSetError("Failed to set remote audio description send parameters.",
1746 error_desc);
1747 return false;
1748 }
1749 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001751 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1752 // and only give it to the media channel once we have a local
1753 // description too (without a local description, we won't be able to
1754 // recv them anyway).
1755 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1756 SafeSetError("Failed to set remote audio description streams.", error_desc);
1757 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758 }
1759
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001760 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001761 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001762 }
1763
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001764 set_remote_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001765 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001766 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767}
1768
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769void VoiceChannel::HandleEarlyMediaTimeout() {
1770 // This occurs on the main thread, not the worker thread.
1771 if (!received_media_) {
1772 LOG(LS_INFO) << "No early media received before timeout";
1773 SignalEarlyMediaTimeout(this);
1774 }
1775}
1776
Peter Boström0c4e06b2015-10-07 12:23:21 +02001777bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1778 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001779 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 if (!enabled()) {
1781 return false;
1782 }
solenberg1d63dd02015-12-02 12:35:09 -08001783 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784}
1785
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001786void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 case MSG_EARLYMEDIATIMEOUT:
1789 HandleEarlyMediaTimeout();
1790 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 case MSG_CHANNEL_ERROR: {
1792 VoiceChannelErrorMessageData* data =
1793 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 delete data;
1795 break;
1796 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 default:
1798 BaseChannel::OnMessage(pmsg);
1799 break;
1800 }
1801}
1802
1803void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001804 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 SignalConnectionMonitor(this, infos);
1806}
1807
1808void VoiceChannel::OnMediaMonitorUpdate(
1809 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1810 ASSERT(media_channel == this->media_channel());
1811 SignalMediaMonitor(this, info);
1812}
1813
1814void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1815 const AudioInfo& info) {
1816 SignalAudioMonitor(this, info);
1817}
1818
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001819void VoiceChannel::GetSrtpCryptoSuites_n(
1820 std::vector<int>* crypto_suites) const {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001821 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822}
1823
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001824VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1825 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001827 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001829 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001830 : BaseChannel(worker_thread,
1831 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001832 media_channel,
1833 transport_controller,
1834 content_name,
perkjc11b1842016-03-07 17:34:13 -08001835 rtcp) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836
skvlad6c87a672016-05-17 17:49:52 -07001837bool VideoChannel::Init_w(const std::string* bundle_transport_name) {
1838 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 return false;
1840 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 return true;
1842}
1843
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001845 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 StopMediaMonitor();
1847 // this can't be done in the base class, since it calls a virtual
1848 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001849
1850 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851}
1852
nisse08582ff2016-02-04 01:24:52 -08001853bool VideoChannel::SetSink(uint32_t ssrc,
1854 rtc::VideoSinkInterface<VideoFrame>* sink) {
1855 worker_thread()->Invoke<void>(
1856 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 return true;
1858}
1859
nisse2ded9b12016-04-08 02:23:55 -07001860void VideoChannel::SetSource(
1861 uint32_t ssrc,
1862 rtc::VideoSourceInterface<cricket::VideoFrame>* source) {
1863 worker_thread()->Invoke<void>(
1864 Bind(&VideoMediaChannel::SetSource, media_channel(), ssrc, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865}
1866
Peter Boström0c4e06b2015-10-07 12:23:21 +02001867bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001868 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001869 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001870 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1871 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001872}
1873
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001874webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001875 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001876 Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001877}
1878
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001879webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1880 uint32_t ssrc) const {
1881 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001882}
1883
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001884bool VideoChannel::SetRtpSendParameters(
1885 uint32_t ssrc,
1886 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001887 return InvokeOnWorker(
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001888 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001889}
1890
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001891bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1892 webrtc::RtpParameters parameters) {
1893 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1894}
1895
1896webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1897 uint32_t ssrc) const {
1898 return worker_thread()->Invoke<webrtc::RtpParameters>(
1899 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
1900}
1901
1902webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
1903 uint32_t ssrc) const {
1904 return media_channel()->GetRtpReceiveParameters(ssrc);
1905}
1906
1907bool VideoChannel::SetRtpReceiveParameters(
1908 uint32_t ssrc,
1909 const webrtc::RtpParameters& parameters) {
1910 return InvokeOnWorker(
1911 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1912}
1913
1914bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1915 webrtc::RtpParameters parameters) {
1916 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001917}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001918
1919void VideoChannel::ChangeState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 // Send outgoing data if we're the active call, we have the remote content,
1921 // and we have had some form of connectivity.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001922 bool send = IsReadyToSend_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923 if (!media_channel()->SetSend(send)) {
1924 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1925 // TODO(gangji): Report error back to server.
1926 }
1927
Peter Boström34fbfff2015-09-24 19:20:30 +02001928 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929}
1930
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001931bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1932 return InvokeOnWorker(
1933 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934}
1935
1936void VideoChannel::StartMediaMonitor(int cms) {
1937 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001938 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 media_monitor_->SignalUpdate.connect(
1940 this, &VideoChannel::OnMediaMonitorUpdate);
1941 media_monitor_->Start(cms);
1942}
1943
1944void VideoChannel::StopMediaMonitor() {
1945 if (media_monitor_) {
1946 media_monitor_->Stop();
1947 media_monitor_.reset();
1948 }
1949}
1950
1951const ContentInfo* VideoChannel::GetFirstContent(
1952 const SessionDescription* sdesc) {
1953 return GetFirstVideoContent(sdesc);
1954}
1955
1956bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001957 ContentAction action,
1958 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001959 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001960 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 LOG(LS_INFO) << "Setting local video description";
1962
1963 const VideoContentDescription* video =
1964 static_cast<const VideoContentDescription*>(content);
1965 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001966 if (!video) {
1967 SafeSetError("Can't find video content in local description.", error_desc);
1968 return false;
1969 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001971 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001972 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973 }
1974
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001975 VideoRecvParameters recv_params = last_recv_params_;
1976 RtpParametersFromMediaDescription(video, &recv_params);
1977 if (!media_channel()->SetRecvParameters(recv_params)) {
1978 SafeSetError("Failed to set local video description recv parameters.",
1979 error_desc);
1980 return false;
1981 }
1982 for (const VideoCodec& codec : video->codecs()) {
1983 bundle_filter()->AddPayloadType(codec.id);
1984 }
1985 last_recv_params_ = recv_params;
1986
1987 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1988 // only give it to the media channel once we have a remote
1989 // description too (without a remote description, we won't be able
1990 // to send them anyway).
1991 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1992 SafeSetError("Failed to set local video description streams.", error_desc);
1993 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994 }
1995
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001996 set_local_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001997 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001998 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999}
2000
2001bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002002 ContentAction action,
2003 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002004 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002005 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 LOG(LS_INFO) << "Setting remote video description";
2007
2008 const VideoContentDescription* video =
2009 static_cast<const VideoContentDescription*>(content);
2010 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002011 if (!video) {
2012 SafeSetError("Can't find video content in remote description.", error_desc);
2013 return false;
2014 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002016 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002017 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018 }
2019
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002020 VideoSendParameters send_params = last_send_params_;
2021 RtpSendParametersFromMediaDescription(video, &send_params);
2022 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002023 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002024 }
skvladdc1c62c2016-03-16 19:07:43 -07002025
2026 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2027
2028 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002029 SafeSetError("Failed to set remote video description send parameters.",
2030 error_desc);
2031 return false;
2032 }
2033 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002035 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2036 // and only give it to the media channel once we have a local
2037 // description too (without a local description, we won't be able to
2038 // recv them anyway).
2039 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2040 SafeSetError("Failed to set remote video description streams.", error_desc);
2041 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 }
2043
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002044 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002045 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002047
2048 set_remote_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002049 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002050 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051}
2052
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002053void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055 case MSG_CHANNEL_ERROR: {
2056 const VideoChannelErrorMessageData* data =
2057 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 delete data;
2059 break;
2060 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 default:
2062 BaseChannel::OnMessage(pmsg);
2063 break;
2064 }
2065}
2066
2067void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002068 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069 SignalConnectionMonitor(this, infos);
2070}
2071
2072// TODO(pthatcher): Look into removing duplicate code between
2073// audio, video, and data, perhaps by using templates.
2074void VideoChannel::OnMediaMonitorUpdate(
2075 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2076 ASSERT(media_channel == this->media_channel());
2077 SignalMediaMonitor(this, info);
2078}
2079
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002080void VideoChannel::GetSrtpCryptoSuites_n(
2081 std::vector<int>* crypto_suites) const {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002082 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083}
2084
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002085DataChannel::DataChannel(rtc::Thread* worker_thread,
2086 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002088 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089 const std::string& content_name,
2090 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002091 : BaseChannel(worker_thread,
2092 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002093 media_channel,
2094 transport_controller,
2095 content_name,
2096 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002097 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002098 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099
2100DataChannel::~DataChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08002101 TRACE_EVENT0("webrtc", "DataChannel::~DataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102 StopMediaMonitor();
2103 // this can't be done in the base class, since it calls a virtual
2104 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002105
2106 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107}
2108
skvlad6c87a672016-05-17 17:49:52 -07002109bool DataChannel::Init_w(const std::string* bundle_transport_name) {
2110 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 return false;
2112 }
2113 media_channel()->SignalDataReceived.connect(
2114 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002115 media_channel()->SignalReadyToSend.connect(
2116 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002117 media_channel()->SignalStreamClosedRemotely.connect(
2118 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119 return true;
2120}
2121
2122bool DataChannel::SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07002123 const rtc::CopyOnWriteBuffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002125 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2126 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002127}
2128
2129const ContentInfo* DataChannel::GetFirstContent(
2130 const SessionDescription* sdesc) {
2131 return GetFirstDataContent(sdesc);
2132}
2133
jbaucheec21bd2016-03-20 06:15:43 -07002134bool DataChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 if (data_channel_type_ == DCT_SCTP) {
2136 // TODO(pthatcher): Do this in a more robust way by checking for
2137 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002138 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 } else if (data_channel_type_ == DCT_RTP) {
2140 return BaseChannel::WantsPacket(rtcp, packet);
2141 }
2142 return false;
2143}
2144
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002145bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2146 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147 // It hasn't been set before, so set it now.
2148 if (data_channel_type_ == DCT_NONE) {
2149 data_channel_type_ = new_data_channel_type;
2150 return true;
2151 }
2152
2153 // It's been set before, but doesn't match. That's bad.
2154 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002155 std::ostringstream desc;
2156 desc << "Data channel type mismatch."
2157 << " Expected " << data_channel_type_
2158 << " Got " << new_data_channel_type;
2159 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 return false;
2161 }
2162
2163 // It's hasn't changed. Nothing to do.
2164 return true;
2165}
2166
2167bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002168 const DataContentDescription* content,
2169 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2171 (content->protocol() == kMediaProtocolDtlsSctp));
2172 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002173 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174}
2175
2176bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002177 ContentAction action,
2178 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002179 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002180 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181 LOG(LS_INFO) << "Setting local data description";
2182
2183 const DataContentDescription* data =
2184 static_cast<const DataContentDescription*>(content);
2185 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002186 if (!data) {
2187 SafeSetError("Can't find data content in local description.", error_desc);
2188 return false;
2189 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002190
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002191 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192 return false;
2193 }
2194
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002195 if (data_channel_type_ == DCT_RTP) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002196 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002197 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198 }
2199 }
2200
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002201 // FYI: We send the SCTP port number (not to be confused with the
2202 // underlying UDP port number) as a codec parameter. So even SCTP
2203 // data channels need codecs.
2204 DataRecvParameters recv_params = last_recv_params_;
2205 RtpParametersFromMediaDescription(data, &recv_params);
2206 if (!media_channel()->SetRecvParameters(recv_params)) {
2207 SafeSetError("Failed to set remote data description recv parameters.",
2208 error_desc);
2209 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002211 if (data_channel_type_ == DCT_RTP) {
2212 for (const DataCodec& codec : data->codecs()) {
2213 bundle_filter()->AddPayloadType(codec.id);
2214 }
2215 }
2216 last_recv_params_ = recv_params;
2217
2218 // TODO(pthatcher): Move local streams into DataSendParameters, and
2219 // only give it to the media channel once we have a remote
2220 // description too (without a remote description, we won't be able
2221 // to send them anyway).
2222 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2223 SafeSetError("Failed to set local data description streams.", error_desc);
2224 return false;
2225 }
2226
2227 set_local_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002228 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002229 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230}
2231
2232bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002233 ContentAction action,
2234 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002235 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002236 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237
2238 const DataContentDescription* data =
2239 static_cast<const DataContentDescription*>(content);
2240 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002241 if (!data) {
2242 SafeSetError("Can't find data content in remote description.", error_desc);
2243 return false;
2244 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002246 // If the remote data doesn't have codecs and isn't an update, it
2247 // must be empty, so ignore it.
2248 if (!data->has_codecs() && action != CA_UPDATE) {
2249 return true;
2250 }
2251
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002252 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253 return false;
2254 }
2255
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002256 LOG(LS_INFO) << "Setting remote data description";
2257 if (data_channel_type_ == DCT_RTP &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002258 !SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002259 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002260 }
2261
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002262
2263 DataSendParameters send_params = last_send_params_;
2264 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2265 if (!media_channel()->SetSendParameters(send_params)) {
2266 SafeSetError("Failed to set remote data description send parameters.",
2267 error_desc);
2268 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002269 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002270 last_send_params_ = send_params;
2271
2272 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2273 // and only give it to the media channel once we have a local
2274 // description too (without a local description, we won't be able to
2275 // recv them anyway).
2276 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2277 SafeSetError("Failed to set remote data description streams.",
2278 error_desc);
2279 return false;
2280 }
2281
2282 set_remote_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002283 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002284 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285}
2286
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002287void DataChannel::ChangeState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288 // Render incoming data if we're the active call, and we have the local
2289 // content. We receive data on the default channel and multiplexed streams.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002290 bool recv = IsReadyToReceive_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002291 if (!media_channel()->SetReceive(recv)) {
2292 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2293 }
2294
2295 // Send outgoing data if we're the active call, we have the remote content,
2296 // and we have had some form of connectivity.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002297 bool send = IsReadyToSend_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002298 if (!media_channel()->SetSend(send)) {
2299 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2300 }
2301
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002302 // Trigger SignalReadyToSendData asynchronously.
2303 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002304
2305 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2306}
2307
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002308void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309 switch (pmsg->message_id) {
2310 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002311 DataChannelReadyToSendMessageData* data =
2312 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002313 ready_to_send_data_ = data->data();
2314 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315 delete data;
2316 break;
2317 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002318 case MSG_DATARECEIVED: {
2319 DataReceivedMessageData* data =
2320 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2321 SignalDataReceived(this, data->params, data->payload);
2322 delete data;
2323 break;
2324 }
2325 case MSG_CHANNEL_ERROR: {
2326 const DataChannelErrorMessageData* data =
2327 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328 delete data;
2329 break;
2330 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002331 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002332 rtc::TypedMessageData<uint32_t>* data =
2333 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002334 SignalStreamClosedRemotely(data->data());
2335 delete data;
2336 break;
2337 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002338 default:
2339 BaseChannel::OnMessage(pmsg);
2340 break;
2341 }
2342}
2343
2344void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002345 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002346 SignalConnectionMonitor(this, infos);
2347}
2348
2349void DataChannel::StartMediaMonitor(int cms) {
2350 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002351 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352 media_monitor_->SignalUpdate.connect(
2353 this, &DataChannel::OnMediaMonitorUpdate);
2354 media_monitor_->Start(cms);
2355}
2356
2357void DataChannel::StopMediaMonitor() {
2358 if (media_monitor_) {
2359 media_monitor_->Stop();
2360 media_monitor_->SignalUpdate.disconnect(this);
2361 media_monitor_.reset();
2362 }
2363}
2364
2365void DataChannel::OnMediaMonitorUpdate(
2366 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2367 ASSERT(media_channel == this->media_channel());
2368 SignalMediaMonitor(this, info);
2369}
2370
2371void DataChannel::OnDataReceived(
2372 const ReceiveDataParams& params, const char* data, size_t len) {
2373 DataReceivedMessageData* msg = new DataReceivedMessageData(
2374 params, data, len);
2375 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2376}
2377
Peter Boström0c4e06b2015-10-07 12:23:21 +02002378void DataChannel::OnDataChannelError(uint32_t ssrc,
2379 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002380 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2381 ssrc, err);
2382 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2383}
2384
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002385void DataChannel::OnDataChannelReadyToSend(bool writable) {
2386 // This is usded for congestion control to indicate that the stream is ready
2387 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2388 // that the transport channel is ready.
2389 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2390 new DataChannelReadyToSendMessageData(writable));
2391}
2392
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002393void DataChannel::GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002394 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002395}
2396
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002397bool DataChannel::ShouldSetupDtlsSrtp_n() const {
2398 return data_channel_type_ == DCT_RTP && BaseChannel::ShouldSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002399}
2400
Peter Boström0c4e06b2015-10-07 12:23:21 +02002401void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2402 rtc::TypedMessageData<uint32_t>* message =
2403 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002404 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2405}
2406
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407} // namespace cricket