blob: 98838dd01cf5f9de9467fe4d59e76a0dc37f894f [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"
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -070018#include "webrtc/base/checks.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000019#include "webrtc/base/common.h"
jbaucheec21bd2016-03-20 06:15:43 -070020#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000021#include "webrtc/base/dscp.h"
22#include "webrtc/base/logging.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070023#include "webrtc/base/networkroute.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010024#include "webrtc/base/trace_event.h"
kjellanderf4752772016-03-02 05:42:30 -080025#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080026#include "webrtc/media/base/rtputils.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010027#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010028#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
30namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000031using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000032
deadbeef2d110be2016-01-13 12:00:26 -080033namespace {
kwiberg31022942016-03-11 14:18:21 -080034// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080035bool SetRawAudioSink_w(VoiceMediaChannel* channel,
36 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080037 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
38 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080039 return true;
40}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020041
42struct SendPacketMessageData : public rtc::MessageData {
43 rtc::CopyOnWriteBuffer packet;
44 rtc::PacketOptions options;
45};
46
isheriff6f8d6862016-05-26 11:24:55 -070047#if defined(ENABLE_EXTERNAL_AUTH)
48// Returns the named header extension if found among all extensions,
49// nullptr otherwise.
50const webrtc::RtpExtension* FindHeaderExtension(
51 const std::vector<webrtc::RtpExtension>& extensions,
52 const std::string& uri) {
53 for (const auto& extension : extensions) {
54 if (extension.uri == uri)
55 return &extension;
56 }
57 return nullptr;
58}
59#endif
60
deadbeef2d110be2016-01-13 12:00:26 -080061} // namespace
62
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000064 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020065 MSG_SEND_RTP_PACKET,
66 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000071 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072};
73
74// Value specified in RFC 5764.
75static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
76
77static const int kAgcMinus10db = -10;
78
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000079static void SafeSetError(const std::string& message, std::string* error_desc) {
80 if (error_desc) {
81 *error_desc = message;
82 }
83}
84
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000085struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020086 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020088 : ssrc(in_ssrc), error(in_error) {}
89 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 VoiceMediaChannel::Error error;
91};
92
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020094 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020096 : ssrc(in_ssrc), error(in_error) {}
97 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 VideoMediaChannel::Error error;
99};
100
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000101struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200104 : ssrc(in_ssrc), error(in_error) {}
105 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 DataMediaChannel::Error error;
107};
108
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109static const char* PacketType(bool rtcp) {
110 return (!rtcp) ? "RTP" : "RTCP";
111}
112
jbaucheec21bd2016-03-20 06:15:43 -0700113static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 // Check the packet size. We could check the header too if needed.
115 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000116 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
117 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118}
119
120static bool IsReceiveContentDirection(MediaContentDirection direction) {
121 return direction == MD_SENDRECV || direction == MD_RECVONLY;
122}
123
124static bool IsSendContentDirection(MediaContentDirection direction) {
125 return direction == MD_SENDRECV || direction == MD_SENDONLY;
126}
127
128static const MediaContentDescription* GetContentDescription(
129 const ContentInfo* cinfo) {
130 if (cinfo == NULL)
131 return NULL;
132 return static_cast<const MediaContentDescription*>(cinfo->description);
133}
134
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700135template <class Codec>
136void RtpParametersFromMediaDescription(
137 const MediaContentDescriptionImpl<Codec>* desc,
138 RtpParameters<Codec>* params) {
139 // TODO(pthatcher): Remove this once we're sure no one will give us
140 // a description without codecs (currently a CA_UPDATE with just
141 // streams can).
142 if (desc->has_codecs()) {
143 params->codecs = desc->codecs();
144 }
145 // TODO(pthatcher): See if we really need
146 // rtp_header_extensions_set() and remove it if we don't.
147 if (desc->rtp_header_extensions_set()) {
148 params->extensions = desc->rtp_header_extensions();
149 }
deadbeef13871492015-12-09 12:37:51 -0800150 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700151}
152
nisse05103312016-03-16 02:22:50 -0700153template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700154void RtpSendParametersFromMediaDescription(
155 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700156 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700157 RtpParametersFromMediaDescription(desc, send_params);
158 send_params->max_bandwidth_bps = desc->bandwidth();
159}
160
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200161BaseChannel::BaseChannel(rtc::Thread* worker_thread,
162 rtc::Thread* network_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700163 MediaChannel* media_channel,
164 TransportController* transport_controller,
165 const std::string& content_name,
166 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200167 : worker_thread_(worker_thread),
168 network_thread_(network_thread),
169
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 content_name_(content_name),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200171
172 transport_controller_(transport_controller),
deadbeef23d947d2016-08-22 16:00:30 -0700173 rtcp_enabled_(rtcp),
174 media_channel_(media_channel) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700175 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200176 if (transport_controller) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200177 RTC_DCHECK_EQ(network_thread, transport_controller->network_thread());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200178 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 LOG(LS_INFO) << "Created channel for " << content_name;
180}
181
182BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800183 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700184 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000185 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200187 // Eats any outstanding messages or packets.
188 worker_thread_->Clear(&invoker_);
189 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 // We must destroy the media channel before the transport channel, otherwise
191 // the media channel may try to send on the dead transport channel. NULLing
192 // is not an effective strategy since the sends will come on another thread.
193 delete media_channel_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200194 // Note that we don't just call SetTransportChannel_n(nullptr) because that
deadbeefcbecd352015-09-23 11:50:27 -0700195 // would call a pure virtual method which we can't do from a destructor.
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200196 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700197 RTC_FROM_HERE, Bind(&BaseChannel::DestroyTransportChannels_n, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200198 LOG(LS_INFO) << "Destroyed channel";
199}
200
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200201void BaseChannel::DisconnectTransportChannels_n() {
202 // Send any outstanding RTCP packets.
203 FlushRtcpMessages_n();
204
205 // Stop signals from transport channels, but keep them alive because
206 // media_channel may use them from a different thread.
deadbeefcbecd352015-09-23 11:50:27 -0700207 if (transport_channel_) {
208 DisconnectFromTransportChannel(transport_channel_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200209 }
210 if (rtcp_transport_channel_) {
211 DisconnectFromTransportChannel(rtcp_transport_channel_);
212 }
213
214 // Clear pending read packets/messages.
215 network_thread_->Clear(&invoker_);
216 network_thread_->Clear(this);
217}
218
219void BaseChannel::DestroyTransportChannels_n() {
220 if (transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200221 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700222 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
223 }
224 if (rtcp_transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200225 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700226 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
227 }
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200228 // Clear pending send packets/messages.
229 network_thread_->Clear(&invoker_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200230 network_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231}
232
skvlad6c87a672016-05-17 17:49:52 -0700233bool BaseChannel::Init_w(const std::string* bundle_transport_name) {
234 if (!network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700235 RTC_FROM_HERE,
skvlad6c87a672016-05-17 17:49:52 -0700236 Bind(&BaseChannel::InitNetwork_n, this, bundle_transport_name))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000237 return false;
238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000239
wu@webrtc.orgde305012013-10-31 15:40:38 +0000240 // Both RTP and RTCP channels are set, we can call SetInterface on
241 // media channel and it can set network options.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200242 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.orgde305012013-10-31 15:40:38 +0000243 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 return true;
245}
246
skvlad6c87a672016-05-17 17:49:52 -0700247bool BaseChannel::InitNetwork_n(const std::string* bundle_transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200248 RTC_DCHECK(network_thread_->IsCurrent());
skvlad6c87a672016-05-17 17:49:52 -0700249 const std::string& transport_name =
250 (bundle_transport_name ? *bundle_transport_name : content_name());
251 if (!SetTransport_n(transport_name)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200252 return false;
253 }
254
255 if (!SetDtlsSrtpCryptoSuites_n(transport_channel_, false)) {
256 return false;
257 }
deadbeef23d947d2016-08-22 16:00:30 -0700258 if (rtcp_transport_channel_ &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200259 !SetDtlsSrtpCryptoSuites_n(rtcp_transport_channel_, true)) {
260 return false;
261 }
262 return true;
263}
264
wu@webrtc.org78187522013-10-07 23:32:02 +0000265void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200266 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000267 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200268 // Packets arrive on the network thread, processing packets calls virtual
269 // functions, so need to stop this process in Deinit that is called in
270 // derived classes destructor.
271 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700272 RTC_FROM_HERE, Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000273}
274
deadbeefcbecd352015-09-23 11:50:27 -0700275bool BaseChannel::SetTransport(const std::string& transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200276 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700277 RTC_FROM_HERE, Bind(&BaseChannel::SetTransport_n, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000278}
279
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200280bool BaseChannel::SetTransport_n(const std::string& transport_name) {
281 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000282
deadbeefcbecd352015-09-23 11:50:27 -0700283 if (transport_name == transport_name_) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700284 // Nothing to do if transport name isn't changing.
deadbeefcbecd352015-09-23 11:50:27 -0700285 return true;
286 }
287
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800288 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
289 // changes and wait until the DTLS handshake is complete to set the newly
290 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200291 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800292 // Set |writable_| to false such that UpdateWritableState_w can set up
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700293 // DTLS-SRTP when |writable_| becomes true again.
guoweis46383312015-12-17 16:45:59 -0800294 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800295 srtp_filter_.ResetParams();
296 }
297
deadbeef23d947d2016-08-22 16:00:30 -0700298 // If this BaseChannel uses RTCP and we haven't fully negotiated RTCP mux,
299 // we need an RTCP channel.
300 if (rtcp_enabled_ && !rtcp_mux_filter_.IsFullyActive()) {
deadbeefcbecd352015-09-23 11:50:27 -0700301 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
302 << " on " << transport_name << " transport ";
deadbeef062ce9f2016-08-26 21:42:15 -0700303 SetTransportChannel_n(
304 true, transport_controller_->CreateTransportChannel_n(
305 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200306 if (!rtcp_transport_channel_) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000307 return false;
308 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000309 }
310
deadbeef062ce9f2016-08-26 21:42:15 -0700311 LOG(LS_INFO) << "Create non-RTCP TransportChannel for " << content_name()
312 << " on " << transport_name << " transport ";
313 SetTransportChannel_n(
314 false, transport_controller_->CreateTransportChannel_n(
315 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200316 if (!transport_channel_) {
guoweis46383312015-12-17 16:45:59 -0800317 return false;
318 }
319
deadbeefcbecd352015-09-23 11:50:27 -0700320 transport_name_ = transport_name;
deadbeefcbecd352015-09-23 11:50:27 -0700321
322 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700323 // setting new transport channels.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200324 UpdateWritableState_n();
deadbeef062ce9f2016-08-26 21:42:15 -0700325 // We can only update ready-to-send after updating writability.
326 //
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700327 // On setting a new channel, assume it's ready to send if it's writable,
328 // because we have no way of knowing otherwise (the channel doesn't give us
329 // "was last send successful?").
330 //
331 // This won't always be accurate (the last SendPacket call from another
332 // BaseChannel could have resulted in an error), but even so, we'll just
333 // encounter the error again and update "ready to send" accordingly.
deadbeef062ce9f2016-08-26 21:42:15 -0700334 SetTransportChannelReadyToSend(
335 false, transport_channel_ && transport_channel_->writable());
336 SetTransportChannelReadyToSend(
337 true, rtcp_transport_channel_ && rtcp_transport_channel_->writable());
338 return true;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000339}
340
deadbeef062ce9f2016-08-26 21:42:15 -0700341void BaseChannel::SetTransportChannel_n(bool rtcp,
342 TransportChannel* new_channel) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200343 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef062ce9f2016-08-26 21:42:15 -0700344 TransportChannel*& old_channel =
345 rtcp ? rtcp_transport_channel_ : transport_channel_;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000346
deadbeef062ce9f2016-08-26 21:42:15 -0700347 if (!old_channel && !new_channel) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700348 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000349 return;
350 }
deadbeef062ce9f2016-08-26 21:42:15 -0700351 RTC_DCHECK(old_channel != new_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700352
deadbeef062ce9f2016-08-26 21:42:15 -0700353 if (old_channel) {
354 DisconnectFromTransportChannel(old_channel);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200355 transport_controller_->DestroyTransportChannel_n(
deadbeef062ce9f2016-08-26 21:42:15 -0700356 transport_name_, rtcp ? cricket::ICE_CANDIDATE_COMPONENT_RTCP
357 : cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000358 }
359
deadbeef062ce9f2016-08-26 21:42:15 -0700360 old_channel = new_channel;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000361
deadbeef062ce9f2016-08-26 21:42:15 -0700362 if (new_channel) {
363 if (rtcp) {
364 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
365 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
366 << "should never happen.";
deadbeefcbecd352015-09-23 11:50:27 -0700367 }
deadbeef062ce9f2016-08-26 21:42:15 -0700368 ConnectToTransportChannel(new_channel);
369 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
370 for (const auto& pair : socket_options) {
371 new_channel->SetOption(pair.first, pair.second);
372 }
guoweis46383312015-12-17 16:45:59 -0800373 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000374}
375
376void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200377 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000378
379 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
380 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
381 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800382 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700383 tc->SignalSelectedCandidatePairChanged.connect(
384 this, &BaseChannel::OnSelectedCandidatePairChanged);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200385 tc->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000386}
387
388void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200389 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000390
391 tc->SignalWritableState.disconnect(this);
392 tc->SignalReadPacket.disconnect(this);
393 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800394 tc->SignalDtlsState.disconnect(this);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200395 tc->SignalSelectedCandidatePairChanged.disconnect(this);
396 tc->SignalSentPacket.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000397}
398
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700400 worker_thread_->Invoke<void>(
401 RTC_FROM_HERE,
402 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
403 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404 return true;
405}
406
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407bool BaseChannel::AddRecvStream(const StreamParams& sp) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700408 return InvokeOnWorker(RTC_FROM_HERE,
409 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410}
411
Peter Boström0c4e06b2015-10-07 12:23:21 +0200412bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700413 return InvokeOnWorker(RTC_FROM_HERE,
414 Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415}
416
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000417bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000418 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700419 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000420}
421
Peter Boström0c4e06b2015-10-07 12:23:21 +0200422bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700423 return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream,
424 media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000425}
426
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000428 ContentAction action,
429 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100430 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700431 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w,
432 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433}
434
435bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000436 ContentAction action,
437 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100438 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700439 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w,
440 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441}
442
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000444 // We pass in the BaseChannel instead of the transport_channel_
445 // because if the transport_channel_ changes, the ConnectionMonitor
446 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200447 // We pass in the network thread because on that thread connection monitor
448 // will call BaseChannel::GetConnectionStats which must be called on the
449 // network thread.
450 connection_monitor_.reset(
451 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000452 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000454 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455}
456
457void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000458 if (connection_monitor_) {
459 connection_monitor_->Stop();
460 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 }
462}
463
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000464bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200465 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000466 return transport_channel_->GetStats(infos);
467}
468
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700469bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 // Receive data if we are enabled and have local content,
471 return enabled() && IsReceiveContentDirection(local_content_direction_);
472}
473
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700474bool BaseChannel::IsReadyToSendMedia_w() const {
475 // Need to access some state updated on the network thread.
476 return network_thread_->Invoke<bool>(
477 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
478}
479
480bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 // Send outgoing data if we are enabled, have local and remote content,
482 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800483 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700485 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200486 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487}
488
jbaucheec21bd2016-03-20 06:15:43 -0700489bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700490 const rtc::PacketOptions& options) {
491 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492}
493
jbaucheec21bd2016-03-20 06:15:43 -0700494bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700495 const rtc::PacketOptions& options) {
496 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497}
498
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000499int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200501 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700502 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200503}
504
505int BaseChannel::SetOption_n(SocketType type,
506 rtc::Socket::Option opt,
507 int value) {
508 RTC_DCHECK(network_thread_->IsCurrent());
509 TransportChannel* channel = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000511 case ST_RTP:
512 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700513 socket_options_.push_back(
514 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000515 break;
516 case ST_RTCP:
517 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700518 rtcp_socket_options_.push_back(
519 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000520 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000522 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523}
524
jbauchcb560652016-08-04 05:20:32 -0700525bool BaseChannel::SetCryptoOptions(const rtc::CryptoOptions& crypto_options) {
526 crypto_options_ = crypto_options;
527 return true;
528}
529
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530void BaseChannel::OnWritableState(TransportChannel* channel) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200531 RTC_DCHECK(channel == transport_channel_ ||
532 channel == rtcp_transport_channel_);
533 RTC_DCHECK(network_thread_->IsCurrent());
534 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535}
536
537void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000538 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000539 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000540 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100541 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200543 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544
545 // When using RTCP multiplexing we might get RTCP packets on the RTP
546 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
547 bool rtcp = PacketIsRtcp(channel, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700548 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000549 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550}
551
552void BaseChannel::OnReadyToSend(TransportChannel* channel) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700553 RTC_DCHECK(channel == transport_channel_ ||
554 channel == rtcp_transport_channel_);
555 SetTransportChannelReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556}
557
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800558void BaseChannel::OnDtlsState(TransportChannel* channel,
559 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200560 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800561 return;
562 }
563
564 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
565 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
566 // cover other scenarios like the whole channel is writable (not just this
567 // TransportChannel) or when TransportChannel is attached after DTLS is
568 // negotiated.
569 if (state != DTLS_TRANSPORT_CONNECTED) {
570 srtp_filter_.ResetParams();
571 }
572}
573
Honghai Zhangcc411c02016-03-29 17:27:21 -0700574void BaseChannel::OnSelectedCandidatePairChanged(
575 TransportChannel* channel,
Honghai Zhang52dce732016-03-31 12:37:31 -0700576 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700577 int last_sent_packet_id,
578 bool ready_to_send) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700579 RTC_DCHECK(channel == transport_channel_ ||
580 channel == rtcp_transport_channel_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200581 RTC_DCHECK(network_thread_->IsCurrent());
582 std::string transport_name = channel->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700583 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700584 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700585 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700586 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700587 selected_candidate_pair->remote_candidate().network_id(),
588 last_sent_packet_id);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700589 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200590 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700591 RTC_FROM_HERE, worker_thread_,
592 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
593 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700594}
595
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700596void BaseChannel::SetTransportChannelReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200597 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700598 if (rtcp) {
599 rtcp_ready_to_send_ = ready;
600 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 rtp_ready_to_send_ = ready;
602 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200604 bool ready_to_send =
605 (rtp_ready_to_send_ &&
606 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
607 (rtcp_ready_to_send_ || !rtcp_transport_channel_));
608
609 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700610 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200611 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612}
613
614bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
615 const char* data, size_t len) {
616 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000617 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618}
619
stefanc1aeaf02015-10-15 07:26:07 -0700620bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700621 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700622 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200623 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
624 // If the thread is not our network thread, we will post to our network
625 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 // synchronize access to all the pieces of the send path, including
627 // SRTP and the inner workings of the transport channels.
628 // The only downside is that we can't return a proper failure code if
629 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200630 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200632 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
633 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800634 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700635 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700636 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 return true;
638 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200639 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640
641 // Now that we are on the correct thread, ensure we have a place to send this
642 // packet before doing anything. (We might get RTCP packets that we don't
643 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
644 // transport.
645 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
646 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000647 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 return false;
649 }
650
651 // Protect ourselves against crazy data.
652 if (!ValidPacket(rtcp, packet)) {
653 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000654 << PacketType(rtcp)
655 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 return false;
657 }
658
stefanc1aeaf02015-10-15 07:26:07 -0700659 rtc::PacketOptions updated_options;
660 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 // Protect if needed.
662 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200663 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200665 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000666 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000668 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
669 // inside libsrtp for a RTP packet. A external HMAC module will be writing
670 // a fake HMAC value. This is ONLY done for a RTP packet.
671 // Socket layer will update rtp sendtime extension header if present in
672 // packet with current time before updating the HMAC.
673#if !defined(ENABLE_EXTERNAL_AUTH)
674 res = srtp_filter_.ProtectRtp(
675 data, len, static_cast<int>(packet->capacity()), &len);
676#else
stefanc1aeaf02015-10-15 07:26:07 -0700677 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000678 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000679 res = srtp_filter_.ProtectRtp(
680 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700681 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000682 // If protection succeeds, let's get auth params from srtp.
683 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200684 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000685 int key_len;
686 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700687 &auth_key, &key_len,
688 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000689 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700690 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
691 updated_options.packet_time_params.srtp_auth_key.assign(
692 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000693 }
694 }
695#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 if (!res) {
697 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200698 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 GetRtpSeqNum(data, len, &seq_num);
700 GetRtpSsrc(data, len, &ssrc);
701 LOG(LS_ERROR) << "Failed to protect " << content_name_
702 << " RTP packet: size=" << len
703 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
704 return false;
705 }
706 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000707 res = srtp_filter_.ProtectRtcp(data, len,
708 static_cast<int>(packet->capacity()),
709 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 if (!res) {
711 int type = -1;
712 GetRtcpType(data, len, &type);
713 LOG(LS_ERROR) << "Failed to protect " << content_name_
714 << " RTCP packet: size=" << len << ", type=" << type;
715 return false;
716 }
717 }
718
719 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000720 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 } else if (secure_required_) {
722 // This is a double check for something that supposedly can't happen.
723 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
724 << " packet when SRTP is inactive and crypto is required";
725
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700726 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 return false;
728 }
729
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200731 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
732 int ret = channel->SendPacket(packet->data<char>(), packet->size(),
733 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000734 if (ret != static_cast<int>(packet->size())) {
skvladc309e0e2016-07-28 17:15:20 -0700735 if (channel->GetError() == ENOTCONN) {
736 LOG(LS_WARNING) << "Got ENOTCONN from transport.";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700737 SetTransportChannelReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 }
739 return false;
740 }
741 return true;
742}
743
jbaucheec21bd2016-03-20 06:15:43 -0700744bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 // Protect ourselves against crazy data.
746 if (!ValidPacket(rtcp, packet)) {
747 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000748 << PacketType(rtcp)
749 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 return false;
751 }
pbos482b12e2015-11-16 10:19:58 -0800752 if (rtcp) {
753 // Permit all (seemingly valid) RTCP packets.
754 return true;
755 }
756 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700757 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758}
759
jbaucheec21bd2016-03-20 06:15:43 -0700760void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000761 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200762 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 if (!WantsPacket(rtcp, packet)) {
764 return;
765 }
766
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000767 // We are only interested in the first rtp packet because that
768 // indicates the media has started flowing.
769 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700771 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 }
773
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 // Unprotect the packet, if needed.
775 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200776 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200777 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000778 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 bool res;
780 if (!rtcp) {
781 res = srtp_filter_.UnprotectRtp(data, len, &len);
782 if (!res) {
783 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200784 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 GetRtpSeqNum(data, len, &seq_num);
786 GetRtpSsrc(data, len, &ssrc);
787 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
788 << " RTP packet: size=" << len
789 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
790 return;
791 }
792 } else {
793 res = srtp_filter_.UnprotectRtcp(data, len, &len);
794 if (!res) {
795 int type = -1;
796 GetRtcpType(data, len, &type);
797 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
798 << " RTCP packet: size=" << len << ", type=" << type;
799 return;
800 }
801 }
802
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000803 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 } else if (secure_required_) {
805 // Our session description indicates that SRTP is required, but we got a
806 // packet before our SRTP filter is active. This means either that
807 // a) we got SRTP packets before we received the SDES keys, in which case
808 // we can't decrypt it anyway, or
809 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
810 // channels, so we haven't yet extracted keys, even if DTLS did complete
811 // on the channel that the packets are being sent on. It's really good
812 // practice to wait for both RTP and RTCP to be good to go before sending
813 // media, to prevent weird failure modes, so it's fine for us to just eat
814 // packets here. This is all sidestepped if RTCP mux is used anyway.
815 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
816 << " packet when SRTP is inactive and crypto is required";
817 return;
818 }
819
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200820 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700821 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200822 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
823}
824
825void BaseChannel::OnPacketReceived(bool rtcp,
826 const rtc::CopyOnWriteBuffer& packet,
827 const rtc::PacketTime& packet_time) {
828 RTC_DCHECK(worker_thread_->IsCurrent());
829 // Need to copy variable because OnRtcpReceived/OnPacketReceived
830 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
831 rtc::CopyOnWriteBuffer data(packet);
832 if (rtcp) {
833 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000834 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200835 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 }
837}
838
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000839bool BaseChannel::PushdownLocalDescription(
840 const SessionDescription* local_desc, ContentAction action,
841 std::string* error_desc) {
842 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 const MediaContentDescription* content_desc =
844 GetContentDescription(content_info);
845 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000846 !SetLocalContent(content_desc, action, error_desc)) {
847 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
848 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000850 return true;
851}
852
853bool BaseChannel::PushdownRemoteDescription(
854 const SessionDescription* remote_desc, ContentAction action,
855 std::string* error_desc) {
856 const ContentInfo* content_info = GetFirstContent(remote_desc);
857 const MediaContentDescription* content_desc =
858 GetContentDescription(content_info);
859 if (content_desc && content_info && !content_info->rejected &&
860 !SetRemoteContent(content_desc, action, error_desc)) {
861 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
862 return false;
863 }
864 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865}
866
867void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700868 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 if (enabled_)
870 return;
871
872 LOG(LS_INFO) << "Channel enabled";
873 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700874 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875}
876
877void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700878 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 if (!enabled_)
880 return;
881
882 LOG(LS_INFO) << "Channel disabled";
883 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700884 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885}
886
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200887void BaseChannel::UpdateWritableState_n() {
deadbeefcbecd352015-09-23 11:50:27 -0700888 if (transport_channel_ && transport_channel_->writable() &&
889 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200890 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700891 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200892 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700893 }
894}
895
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200896void BaseChannel::ChannelWritable_n() {
897 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800898 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800900 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901
deadbeefcbecd352015-09-23 11:50:27 -0700902 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 << (was_ever_writable_ ? "" : " for the first time");
904
905 std::vector<ConnectionInfo> infos;
906 transport_channel_->GetStats(&infos);
907 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
908 it != infos.end(); ++it) {
909 if (it->best_connection) {
910 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
911 << "->" << it->remote_candidate.ToSensitiveString();
912 break;
913 }
914 }
915
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200917 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700919 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920}
921
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200922void BaseChannel::SignalDtlsSetupFailure_n(bool rtcp) {
923 RTC_DCHECK(network_thread_->IsCurrent());
924 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700925 RTC_FROM_HERE, signaling_thread(),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200926 Bind(&BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000927}
928
929void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700930 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000931 SignalDtlsSetupFailure(this, rtcp);
932}
933
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200934bool BaseChannel::SetDtlsSrtpCryptoSuites_n(TransportChannel* tc, bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800935 std::vector<int> crypto_suites;
936 // We always use the default SRTP crypto suites for RTCP, but we may use
937 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200939 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 } else {
jbauchcb560652016-08-04 05:20:32 -0700941 GetDefaultSrtpCryptoSuites(crypto_options(), &crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800943 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944}
945
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200946bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800947 // Since DTLS is applied to all channels, checking RTP should be enough.
948 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949}
950
951// This function returns true if either DTLS-SRTP is not in use
952// *or* DTLS-SRTP is successfully set up.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200953bool BaseChannel::SetupDtlsSrtp_n(bool rtcp_channel) {
954 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 bool ret = false;
956
deadbeefcbecd352015-09-23 11:50:27 -0700957 TransportChannel* channel =
958 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800960 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800962 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800964 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
965 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 return false;
967 }
968
969 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
970 << content_name() << " "
971 << PacketType(rtcp_channel);
972
jbauchcb560652016-08-04 05:20:32 -0700973 int key_len;
974 int salt_len;
975 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
976 &salt_len)) {
977 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
978 return false;
979 }
980
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -0700982 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983
984 // RFC 5705 exporter using the RFC 5764 parameters
985 if (!channel->ExportKeyingMaterial(
986 kDtlsSrtpExporterLabel,
987 NULL, 0, false,
988 &dtls_buffer[0], dtls_buffer.size())) {
989 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700990 RTC_DCHECK(false); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 return false;
992 }
993
994 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -0700995 std::vector<unsigned char> client_write_key(key_len + salt_len);
996 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -0700998 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
999 offset += key_len;
1000 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
1001 offset += key_len;
1002 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
1003 offset += salt_len;
1004 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005
1006 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001007 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001008 if (!channel->GetSslRole(&role)) {
1009 LOG(LS_WARNING) << "GetSslRole failed";
1010 return false;
1011 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001013 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 send_key = &server_write_key;
1015 recv_key = &client_write_key;
1016 } else {
1017 send_key = &client_write_key;
1018 recv_key = &server_write_key;
1019 }
1020
1021 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001022 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1023 static_cast<int>(send_key->size()),
1024 selected_crypto_suite, &(*recv_key)[0],
1025 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001027 ret = srtp_filter_.SetRtpParams(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 }
1032
1033 if (!ret)
1034 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
1035 else
1036 dtls_keyed_ = true;
1037
1038 return ret;
1039}
1040
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001041void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001042 if (srtp_filter_.IsActive()) {
1043 return;
1044 }
1045
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001046 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001047 return;
1048 }
1049
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001050 if (!SetupDtlsSrtp_n(false)) {
1051 SignalDtlsSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001052 return;
1053 }
1054
1055 if (rtcp_transport_channel_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001056 if (!SetupDtlsSrtp_n(true)) {
1057 SignalDtlsSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001058 return;
1059 }
1060 }
1061}
1062
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001063void BaseChannel::ChannelNotWritable_n() {
1064 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 if (!writable_)
1066 return;
1067
deadbeefcbecd352015-09-23 11:50:27 -07001068 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001070 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071}
1072
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001073bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001074 const MediaContentDescription* content,
1075 ContentAction action,
1076 ContentSource src,
1077 std::string* error_desc) {
1078 if (action == CA_UPDATE) {
1079 // These parameters never get changed by a CA_UDPATE.
1080 return true;
1081 }
1082
1083 // Cache secure_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001084 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001085 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1086 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001087}
1088
1089bool BaseChannel::SetRtpTransportParameters_n(
1090 const MediaContentDescription* content,
1091 ContentAction action,
1092 ContentSource src,
1093 std::string* error_desc) {
1094 RTC_DCHECK(network_thread_->IsCurrent());
1095
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001096 if (src == CS_LOCAL) {
1097 set_secure_required(content->crypto_required() != CT_NONE);
1098 }
1099
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001100 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001101 return false;
1102 }
1103
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001104 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001105 return false;
1106 }
1107
1108 return true;
1109}
1110
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001111// |dtls| will be set to true if DTLS is active for transport channel and
1112// crypto is empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001113bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1114 bool* dtls,
1115 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001116 *dtls = transport_channel_->IsDtlsActive();
1117 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001118 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001119 return false;
1120 }
1121 return true;
1122}
1123
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001124bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001125 ContentAction action,
1126 ContentSource src,
1127 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001128 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001129 if (action == CA_UPDATE) {
1130 // no crypto params.
1131 return true;
1132 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001134 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001135 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001136 if (!ret) {
1137 return false;
1138 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 switch (action) {
1140 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001141 // If DTLS is already active on the channel, we could be renegotiating
1142 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001143 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001144 ret = srtp_filter_.SetOffer(cryptos, src);
1145 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 break;
1147 case CA_PRANSWER:
1148 // If we're doing DTLS-SRTP, we don't want to update the filter
1149 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001150 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1152 }
1153 break;
1154 case CA_ANSWER:
1155 // If we're doing DTLS-SRTP, we don't want to update the filter
1156 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001157 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 ret = srtp_filter_.SetAnswer(cryptos, src);
1159 }
1160 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 default:
1162 break;
1163 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001164 if (!ret) {
1165 SafeSetError("Failed to setup SRTP filter.", error_desc);
1166 return false;
1167 }
1168 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169}
1170
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001171void BaseChannel::ActivateRtcpMux() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001172 network_thread_->Invoke<void>(RTC_FROM_HERE,
1173 Bind(&BaseChannel::ActivateRtcpMux_n, this));
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001174}
1175
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001176void BaseChannel::ActivateRtcpMux_n() {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001177 if (!rtcp_mux_filter_.IsActive()) {
1178 rtcp_mux_filter_.SetActive();
deadbeef062ce9f2016-08-26 21:42:15 -07001179 SetTransportChannel_n(true, nullptr);
1180 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
1181 // removing channel.
1182 UpdateWritableState_n();
1183 SetTransportChannelReadyToSend(true, false);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001184 }
1185}
1186
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001187bool BaseChannel::SetRtcpMux_n(bool enable,
1188 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001189 ContentSource src,
1190 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 bool ret = false;
1192 switch (action) {
1193 case CA_OFFER:
1194 ret = rtcp_mux_filter_.SetOffer(enable, src);
1195 break;
1196 case CA_PRANSWER:
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001197 // This may activate RTCP muxing, but we don't yet destroy the channel
1198 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1200 break;
1201 case CA_ANSWER:
1202 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1203 if (ret && rtcp_mux_filter_.IsActive()) {
1204 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001205 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1206 << " by destroying RTCP transport channel for "
1207 << transport_name();
deadbeef062ce9f2016-08-26 21:42:15 -07001208 SetTransportChannel_n(true, nullptr);
1209 UpdateWritableState_n();
1210 SetTransportChannelReadyToSend(true, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 }
1212 break;
1213 case CA_UPDATE:
1214 // No RTCP mux info.
1215 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001216 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 default:
1218 break;
1219 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001220 if (!ret) {
1221 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1222 return false;
1223 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1225 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1226 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001227 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 // If the RTP transport is already writable, then so are we.
1229 if (transport_channel_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001230 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 }
1232 }
1233
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001234 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235}
1236
1237bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001238 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001239 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240}
1241
Peter Boström0c4e06b2015-10-07 12:23:21 +02001242bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001243 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 return media_channel()->RemoveRecvStream(ssrc);
1245}
1246
1247bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001248 ContentAction action,
1249 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1251 action == CA_PRANSWER || action == CA_UPDATE))
1252 return false;
1253
1254 // If this is an update, streams only contain streams that have changed.
1255 if (action == CA_UPDATE) {
1256 for (StreamParamsVec::const_iterator it = streams.begin();
1257 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001258 const StreamParams* existing_stream =
1259 GetStreamByIds(local_streams_, it->groupid, it->id);
1260 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 if (media_channel()->AddSendStream(*it)) {
1262 local_streams_.push_back(*it);
1263 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1264 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001265 std::ostringstream desc;
1266 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1267 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 return false;
1269 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001270 } else if (existing_stream && !it->has_ssrcs()) {
1271 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001272 std::ostringstream desc;
1273 desc << "Failed to remove send stream with ssrc "
1274 << it->first_ssrc() << ".";
1275 SafeSetError(desc.str(), error_desc);
1276 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001278 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 } else {
1280 LOG(LS_WARNING) << "Ignore unsupported stream update";
1281 }
1282 }
1283 return true;
1284 }
1285 // Else streams are all the streams we want to send.
1286
1287 // Check for streams that have been removed.
1288 bool ret = true;
1289 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1290 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001291 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001293 std::ostringstream desc;
1294 desc << "Failed to remove send stream with ssrc "
1295 << it->first_ssrc() << ".";
1296 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 ret = false;
1298 }
1299 }
1300 }
1301 // Check for new streams.
1302 for (StreamParamsVec::const_iterator it = streams.begin();
1303 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001304 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001306 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001308 std::ostringstream desc;
1309 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1310 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 ret = false;
1312 }
1313 }
1314 }
1315 local_streams_ = streams;
1316 return ret;
1317}
1318
1319bool BaseChannel::UpdateRemoteStreams_w(
1320 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001321 ContentAction action,
1322 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1324 action == CA_PRANSWER || action == CA_UPDATE))
1325 return false;
1326
1327 // If this is an update, streams only contain streams that have changed.
1328 if (action == CA_UPDATE) {
1329 for (StreamParamsVec::const_iterator it = streams.begin();
1330 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001331 const StreamParams* existing_stream =
1332 GetStreamByIds(remote_streams_, it->groupid, it->id);
1333 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 if (AddRecvStream_w(*it)) {
1335 remote_streams_.push_back(*it);
1336 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1337 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001338 std::ostringstream desc;
1339 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1340 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341 return false;
1342 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001343 } else if (existing_stream && !it->has_ssrcs()) {
1344 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001345 std::ostringstream desc;
1346 desc << "Failed to remove remote stream with ssrc "
1347 << it->first_ssrc() << ".";
1348 SafeSetError(desc.str(), error_desc);
1349 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001351 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 } else {
1353 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001354 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355 << " new stream = " << it->ToString();
1356 }
1357 }
1358 return true;
1359 }
1360 // Else streams are all the streams we want to receive.
1361
1362 // Check for streams that have been removed.
1363 bool ret = true;
1364 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1365 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001366 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001368 std::ostringstream desc;
1369 desc << "Failed to remove remote stream with ssrc "
1370 << it->first_ssrc() << ".";
1371 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372 ret = false;
1373 }
1374 }
1375 }
1376 // Check for new streams.
1377 for (StreamParamsVec::const_iterator it = streams.begin();
1378 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001379 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380 if (AddRecvStream_w(*it)) {
1381 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1382 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001383 std::ostringstream desc;
1384 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1385 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386 ret = false;
1387 }
1388 }
1389 }
1390 remote_streams_ = streams;
1391 return ret;
1392}
1393
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001394void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001395 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001396// Absolute Send Time extension id is used only with external auth,
1397// so do not bother searching for it and making asyncronious call to set
1398// something that is not used.
1399#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001400 const webrtc::RtpExtension* send_time_extension =
1401 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001402 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001403 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001404 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001405 RTC_FROM_HERE, network_thread_,
1406 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1407 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001408#endif
1409}
1410
1411void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1412 int rtp_abs_sendtime_extn_id) {
1413 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001414}
1415
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001416void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001417 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001419 case MSG_SEND_RTP_PACKET:
1420 case MSG_SEND_RTCP_PACKET: {
1421 RTC_DCHECK(network_thread_->IsCurrent());
1422 SendPacketMessageData* data =
1423 static_cast<SendPacketMessageData*>(pmsg->pdata);
1424 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1425 SendPacket(rtcp, &data->packet, data->options);
1426 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 break;
1428 }
1429 case MSG_FIRSTPACKETRECEIVED: {
1430 SignalFirstPacketReceived(this);
1431 break;
1432 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001433 }
1434}
1435
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001436void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 // Flush all remaining RTCP messages. This should only be called in
1438 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001439 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001440 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001441 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1442 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001443 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1444 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 }
1446}
1447
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001448void BaseChannel::SignalSentPacket_n(TransportChannel* /* channel */,
1449 const rtc::SentPacket& sent_packet) {
1450 RTC_DCHECK(network_thread_->IsCurrent());
1451 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001452 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001453 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1454}
1455
1456void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1457 RTC_DCHECK(worker_thread_->IsCurrent());
1458 SignalSentPacket(sent_packet);
1459}
1460
1461VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1462 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 MediaEngineInterface* media_engine,
1464 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001465 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 const std::string& content_name,
1467 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001468 : BaseChannel(worker_thread,
1469 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001470 media_channel,
1471 transport_controller,
1472 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001474 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001475 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476
1477VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001478 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479 StopAudioMonitor();
1480 StopMediaMonitor();
1481 // this can't be done in the base class, since it calls a virtual
1482 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001483 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484}
1485
skvlad6c87a672016-05-17 17:49:52 -07001486bool VoiceChannel::Init_w(const std::string* bundle_transport_name) {
1487 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 return false;
1489 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 return true;
1491}
1492
Peter Boström0c4e06b2015-10-07 12:23:21 +02001493bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001494 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001495 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001496 AudioSource* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001497 return InvokeOnWorker(RTC_FROM_HERE,
1498 Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001499 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500}
1501
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502// TODO(juberti): Handle early media the right way. We should get an explicit
1503// ringing message telling us to start playing local ringback, which we cancel
1504// if any early media actually arrives. For now, we do the opposite, which is
1505// to wait 1 second for early media, and start playing local ringback if none
1506// arrives.
1507void VoiceChannel::SetEarlyMedia(bool enable) {
1508 if (enable) {
1509 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001510 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1511 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 } else {
1513 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001514 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 }
1516}
1517
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518bool VoiceChannel::CanInsertDtmf() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001519 return InvokeOnWorker(
1520 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521}
1522
Peter Boström0c4e06b2015-10-07 12:23:21 +02001523bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1524 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001525 int duration) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001526 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this,
1527 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528}
1529
solenberg4bac9c52015-10-09 02:32:53 -07001530bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001531 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume,
1532 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001534
Tommif888bb52015-12-12 01:37:01 +01001535void VoiceChannel::SetRawAudioSink(
1536 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001537 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1538 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001539 // passing. So we invoke to our own little routine that gets a pointer to
1540 // our local variable. This is OK since we're synchronously invoking.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001541 InvokeOnWorker(RTC_FROM_HERE,
1542 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001543}
1544
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001545webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001546 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001547 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001548}
1549
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001550webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1551 uint32_t ssrc) const {
1552 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001553}
1554
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001555bool VoiceChannel::SetRtpSendParameters(
1556 uint32_t ssrc,
1557 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001558 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001559 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001560 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001561}
1562
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001563bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1564 webrtc::RtpParameters parameters) {
1565 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1566}
1567
1568webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1569 uint32_t ssrc) const {
1570 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001571 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001572 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1573}
1574
1575webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1576 uint32_t ssrc) const {
1577 return media_channel()->GetRtpReceiveParameters(ssrc);
1578}
1579
1580bool VoiceChannel::SetRtpReceiveParameters(
1581 uint32_t ssrc,
1582 const webrtc::RtpParameters& parameters) {
1583 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001584 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001585 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1586}
1587
1588bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1589 webrtc::RtpParameters parameters) {
1590 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001591}
1592
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001594 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1595 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596}
1597
1598void VoiceChannel::StartMediaMonitor(int cms) {
1599 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001600 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 media_monitor_->SignalUpdate.connect(
1602 this, &VoiceChannel::OnMediaMonitorUpdate);
1603 media_monitor_->Start(cms);
1604}
1605
1606void VoiceChannel::StopMediaMonitor() {
1607 if (media_monitor_) {
1608 media_monitor_->Stop();
1609 media_monitor_->SignalUpdate.disconnect(this);
1610 media_monitor_.reset();
1611 }
1612}
1613
1614void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001615 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 audio_monitor_
1617 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1618 audio_monitor_->Start(cms);
1619}
1620
1621void VoiceChannel::StopAudioMonitor() {
1622 if (audio_monitor_) {
1623 audio_monitor_->Stop();
1624 audio_monitor_.reset();
1625 }
1626}
1627
1628bool VoiceChannel::IsAudioMonitorRunning() const {
1629 return (audio_monitor_.get() != NULL);
1630}
1631
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001633 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634}
1635
1636int VoiceChannel::GetOutputLevel_w() {
1637 return media_channel()->GetOutputLevel();
1638}
1639
1640void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1641 media_channel()->GetActiveStreams(actives);
1642}
1643
1644void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001645 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001646 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001647 int flags) {
1648 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649
1650 // Set a flag when we've received an RTP packet. If we're waiting for early
1651 // media, this will disable the timeout.
1652 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1653 received_media_ = true;
1654 }
1655}
1656
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001657void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001658 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001659 invoker_.AsyncInvoke<void>(
1660 RTC_FROM_HERE, worker_thread_,
1661 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001662}
1663
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001664void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 // Render incoming data if we're the active call, and we have the local
1666 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001667 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001668 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669
1670 // Send outgoing data if we're the active call, we have the remote content,
1671 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001672 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001673 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674
1675 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1676}
1677
1678const ContentInfo* VoiceChannel::GetFirstContent(
1679 const SessionDescription* sdesc) {
1680 return GetFirstAudioContent(sdesc);
1681}
1682
1683bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001684 ContentAction action,
1685 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001686 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001687 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 LOG(LS_INFO) << "Setting local voice description";
1689
1690 const AudioContentDescription* audio =
1691 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001692 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001693 if (!audio) {
1694 SafeSetError("Can't find audio content in local description.", error_desc);
1695 return false;
1696 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001698 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001699 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 }
1701
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001702 AudioRecvParameters recv_params = last_recv_params_;
1703 RtpParametersFromMediaDescription(audio, &recv_params);
1704 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001705 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001706 error_desc);
1707 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001709 for (const AudioCodec& codec : audio->codecs()) {
1710 bundle_filter()->AddPayloadType(codec.id);
1711 }
1712 last_recv_params_ = recv_params;
1713
1714 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1715 // only give it to the media channel once we have a remote
1716 // description too (without a remote description, we won't be able
1717 // to send them anyway).
1718 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1719 SafeSetError("Failed to set local audio description streams.", error_desc);
1720 return false;
1721 }
1722
1723 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001724 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001725 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726}
1727
1728bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001729 ContentAction action,
1730 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001731 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001732 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 LOG(LS_INFO) << "Setting remote voice description";
1734
1735 const AudioContentDescription* audio =
1736 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001737 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001738 if (!audio) {
1739 SafeSetError("Can't find audio content in remote description.", error_desc);
1740 return false;
1741 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001743 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001744 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745 }
1746
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001747 AudioSendParameters send_params = last_send_params_;
1748 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001749 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001750 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001751 }
skvladdc1c62c2016-03-16 19:07:43 -07001752
1753 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1754 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001755 SafeSetError("Failed to set remote audio description send parameters.",
1756 error_desc);
1757 return false;
1758 }
1759 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001761 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1762 // and only give it to the media channel once we have a local
1763 // description too (without a local description, we won't be able to
1764 // recv them anyway).
1765 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1766 SafeSetError("Failed to set remote audio description streams.", error_desc);
1767 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 }
1769
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001770 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001771 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001772 }
1773
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001774 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001775 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001776 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777}
1778
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779void VoiceChannel::HandleEarlyMediaTimeout() {
1780 // This occurs on the main thread, not the worker thread.
1781 if (!received_media_) {
1782 LOG(LS_INFO) << "No early media received before timeout";
1783 SignalEarlyMediaTimeout(this);
1784 }
1785}
1786
Peter Boström0c4e06b2015-10-07 12:23:21 +02001787bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1788 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001789 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 if (!enabled()) {
1791 return false;
1792 }
solenberg1d63dd02015-12-02 12:35:09 -08001793 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794}
1795
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001796void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 case MSG_EARLYMEDIATIMEOUT:
1799 HandleEarlyMediaTimeout();
1800 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 case MSG_CHANNEL_ERROR: {
1802 VoiceChannelErrorMessageData* data =
1803 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 delete data;
1805 break;
1806 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 default:
1808 BaseChannel::OnMessage(pmsg);
1809 break;
1810 }
1811}
1812
1813void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001814 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815 SignalConnectionMonitor(this, infos);
1816}
1817
1818void VoiceChannel::OnMediaMonitorUpdate(
1819 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001820 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821 SignalMediaMonitor(this, info);
1822}
1823
1824void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1825 const AudioInfo& info) {
1826 SignalAudioMonitor(this, info);
1827}
1828
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001829void VoiceChannel::GetSrtpCryptoSuites_n(
1830 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07001831 GetSupportedAudioCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832}
1833
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001834VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1835 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001837 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001838 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001839 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001840 : BaseChannel(worker_thread,
1841 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001842 media_channel,
1843 transport_controller,
1844 content_name,
perkjc11b1842016-03-07 17:34:13 -08001845 rtcp) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846
skvlad6c87a672016-05-17 17:49:52 -07001847bool VideoChannel::Init_w(const std::string* bundle_transport_name) {
1848 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 return false;
1850 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 return true;
1852}
1853
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001855 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 StopMediaMonitor();
1857 // this can't be done in the base class, since it calls a virtual
1858 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001859
1860 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861}
1862
nisse08582ff2016-02-04 01:24:52 -08001863bool VideoChannel::SetSink(uint32_t ssrc,
1864 rtc::VideoSinkInterface<VideoFrame>* sink) {
1865 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001866 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001867 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 return true;
1869}
1870
deadbeef5a4a75a2016-06-02 16:23:38 -07001871bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001872 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001873 bool mute,
1874 const VideoOptions* options,
nisse2ded9b12016-04-08 02:23:55 -07001875 rtc::VideoSourceInterface<cricket::VideoFrame>* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001876 return InvokeOnWorker(RTC_FROM_HERE,
1877 Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
deadbeef5a4a75a2016-06-02 16:23:38 -07001878 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001879}
1880
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001881webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001882 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001883 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001884}
1885
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001886webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1887 uint32_t ssrc) const {
1888 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001889}
1890
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001891bool VideoChannel::SetRtpSendParameters(
1892 uint32_t ssrc,
1893 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001894 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001895 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001896 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001897}
1898
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001899bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1900 webrtc::RtpParameters parameters) {
1901 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1902}
1903
1904webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1905 uint32_t ssrc) const {
1906 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001907 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001908 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
1909}
1910
1911webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
1912 uint32_t ssrc) const {
1913 return media_channel()->GetRtpReceiveParameters(ssrc);
1914}
1915
1916bool VideoChannel::SetRtpReceiveParameters(
1917 uint32_t ssrc,
1918 const webrtc::RtpParameters& parameters) {
1919 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001920 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001921 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1922}
1923
1924bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1925 webrtc::RtpParameters parameters) {
1926 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001927}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001928
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001929void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 // Send outgoing data if we're the active call, we have the remote content,
1931 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001932 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 if (!media_channel()->SetSend(send)) {
1934 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1935 // TODO(gangji): Report error back to server.
1936 }
1937
Peter Boström34fbfff2015-09-24 19:20:30 +02001938 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939}
1940
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001941bool VideoChannel::GetStats(VideoMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001942 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
1943 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944}
1945
1946void VideoChannel::StartMediaMonitor(int cms) {
1947 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001948 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949 media_monitor_->SignalUpdate.connect(
1950 this, &VideoChannel::OnMediaMonitorUpdate);
1951 media_monitor_->Start(cms);
1952}
1953
1954void VideoChannel::StopMediaMonitor() {
1955 if (media_monitor_) {
1956 media_monitor_->Stop();
1957 media_monitor_.reset();
1958 }
1959}
1960
1961const ContentInfo* VideoChannel::GetFirstContent(
1962 const SessionDescription* sdesc) {
1963 return GetFirstVideoContent(sdesc);
1964}
1965
1966bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001967 ContentAction action,
1968 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001969 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001970 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971 LOG(LS_INFO) << "Setting local video description";
1972
1973 const VideoContentDescription* video =
1974 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001975 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001976 if (!video) {
1977 SafeSetError("Can't find video content in local description.", error_desc);
1978 return false;
1979 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001981 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001982 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 }
1984
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001985 VideoRecvParameters recv_params = last_recv_params_;
1986 RtpParametersFromMediaDescription(video, &recv_params);
1987 if (!media_channel()->SetRecvParameters(recv_params)) {
1988 SafeSetError("Failed to set local video description recv parameters.",
1989 error_desc);
1990 return false;
1991 }
1992 for (const VideoCodec& codec : video->codecs()) {
1993 bundle_filter()->AddPayloadType(codec.id);
1994 }
1995 last_recv_params_ = recv_params;
1996
1997 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1998 // only give it to the media channel once we have a remote
1999 // description too (without a remote description, we won't be able
2000 // to send them anyway).
2001 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2002 SafeSetError("Failed to set local video description streams.", error_desc);
2003 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004 }
2005
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002006 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002007 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002008 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009}
2010
2011bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002012 ContentAction action,
2013 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002014 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002015 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016 LOG(LS_INFO) << "Setting remote video description";
2017
2018 const VideoContentDescription* video =
2019 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002020 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002021 if (!video) {
2022 SafeSetError("Can't find video content in remote description.", error_desc);
2023 return false;
2024 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002026 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002027 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 }
2029
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002030 VideoSendParameters send_params = last_send_params_;
2031 RtpSendParametersFromMediaDescription(video, &send_params);
2032 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002033 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002034 }
skvladdc1c62c2016-03-16 19:07:43 -07002035
2036 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2037
2038 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002039 SafeSetError("Failed to set remote video description send parameters.",
2040 error_desc);
2041 return false;
2042 }
2043 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002045 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2046 // and only give it to the media channel once we have a local
2047 // description too (without a local description, we won't be able to
2048 // recv them anyway).
2049 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2050 SafeSetError("Failed to set remote video description streams.", error_desc);
2051 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 }
2053
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002054 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002055 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002057
2058 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002059 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002060 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061}
2062
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002063void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065 case MSG_CHANNEL_ERROR: {
2066 const VideoChannelErrorMessageData* data =
2067 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 delete data;
2069 break;
2070 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002071 default:
2072 BaseChannel::OnMessage(pmsg);
2073 break;
2074 }
2075}
2076
2077void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002078 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079 SignalConnectionMonitor(this, infos);
2080}
2081
2082// TODO(pthatcher): Look into removing duplicate code between
2083// audio, video, and data, perhaps by using templates.
2084void VideoChannel::OnMediaMonitorUpdate(
2085 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002086 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 SignalMediaMonitor(this, info);
2088}
2089
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002090void VideoChannel::GetSrtpCryptoSuites_n(
2091 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002092 GetSupportedVideoCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093}
2094
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002095DataChannel::DataChannel(rtc::Thread* worker_thread,
2096 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002098 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099 const std::string& content_name,
2100 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002101 : BaseChannel(worker_thread,
2102 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002103 media_channel,
2104 transport_controller,
2105 content_name,
2106 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002107 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002108 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109
2110DataChannel::~DataChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08002111 TRACE_EVENT0("webrtc", "DataChannel::~DataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112 StopMediaMonitor();
2113 // this can't be done in the base class, since it calls a virtual
2114 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002115
2116 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117}
2118
skvlad6c87a672016-05-17 17:49:52 -07002119bool DataChannel::Init_w(const std::string* bundle_transport_name) {
2120 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121 return false;
2122 }
2123 media_channel()->SignalDataReceived.connect(
2124 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002125 media_channel()->SignalReadyToSend.connect(
2126 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002127 media_channel()->SignalStreamClosedRemotely.connect(
2128 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129 return true;
2130}
2131
2132bool DataChannel::SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07002133 const rtc::CopyOnWriteBuffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002134 SendDataResult* result) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002135 return InvokeOnWorker(
2136 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2137 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138}
2139
2140const ContentInfo* DataChannel::GetFirstContent(
2141 const SessionDescription* sdesc) {
2142 return GetFirstDataContent(sdesc);
2143}
2144
jbaucheec21bd2016-03-20 06:15:43 -07002145bool DataChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146 if (data_channel_type_ == DCT_SCTP) {
2147 // TODO(pthatcher): Do this in a more robust way by checking for
2148 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002149 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002150 } else if (data_channel_type_ == DCT_RTP) {
2151 return BaseChannel::WantsPacket(rtcp, packet);
2152 }
2153 return false;
2154}
2155
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002156bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2157 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002158 // It hasn't been set before, so set it now.
2159 if (data_channel_type_ == DCT_NONE) {
2160 data_channel_type_ = new_data_channel_type;
2161 return true;
2162 }
2163
2164 // It's been set before, but doesn't match. That's bad.
2165 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002166 std::ostringstream desc;
2167 desc << "Data channel type mismatch."
2168 << " Expected " << data_channel_type_
2169 << " Got " << new_data_channel_type;
2170 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171 return false;
2172 }
2173
2174 // It's hasn't changed. Nothing to do.
2175 return true;
2176}
2177
2178bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002179 const DataContentDescription* content,
2180 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2182 (content->protocol() == kMediaProtocolDtlsSctp));
2183 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002184 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185}
2186
2187bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002188 ContentAction action,
2189 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002190 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002191 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192 LOG(LS_INFO) << "Setting local data description";
2193
2194 const DataContentDescription* data =
2195 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002196 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002197 if (!data) {
2198 SafeSetError("Can't find data content in local description.", error_desc);
2199 return false;
2200 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002202 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203 return false;
2204 }
2205
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002206 if (data_channel_type_ == DCT_RTP) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002207 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002208 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209 }
2210 }
2211
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002212 // FYI: We send the SCTP port number (not to be confused with the
2213 // underlying UDP port number) as a codec parameter. So even SCTP
2214 // data channels need codecs.
2215 DataRecvParameters recv_params = last_recv_params_;
2216 RtpParametersFromMediaDescription(data, &recv_params);
2217 if (!media_channel()->SetRecvParameters(recv_params)) {
2218 SafeSetError("Failed to set remote data description recv parameters.",
2219 error_desc);
2220 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002222 if (data_channel_type_ == DCT_RTP) {
2223 for (const DataCodec& codec : data->codecs()) {
2224 bundle_filter()->AddPayloadType(codec.id);
2225 }
2226 }
2227 last_recv_params_ = recv_params;
2228
2229 // TODO(pthatcher): Move local streams into DataSendParameters, and
2230 // only give it to the media channel once we have a remote
2231 // description too (without a remote description, we won't be able
2232 // to send them anyway).
2233 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2234 SafeSetError("Failed to set local data description streams.", error_desc);
2235 return false;
2236 }
2237
2238 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002239 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002240 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002241}
2242
2243bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002244 ContentAction action,
2245 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002246 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002247 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248
2249 const DataContentDescription* data =
2250 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002251 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002252 if (!data) {
2253 SafeSetError("Can't find data content in remote description.", error_desc);
2254 return false;
2255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002257 // If the remote data doesn't have codecs and isn't an update, it
2258 // must be empty, so ignore it.
2259 if (!data->has_codecs() && action != CA_UPDATE) {
2260 return true;
2261 }
2262
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002263 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002264 return false;
2265 }
2266
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002267 LOG(LS_INFO) << "Setting remote data description";
2268 if (data_channel_type_ == DCT_RTP &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002269 !SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002270 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271 }
2272
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002273
2274 DataSendParameters send_params = last_send_params_;
2275 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2276 if (!media_channel()->SetSendParameters(send_params)) {
2277 SafeSetError("Failed to set remote data description send parameters.",
2278 error_desc);
2279 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002281 last_send_params_ = send_params;
2282
2283 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2284 // and only give it to the media channel once we have a local
2285 // description too (without a local description, we won't be able to
2286 // recv them anyway).
2287 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2288 SafeSetError("Failed to set remote data description streams.",
2289 error_desc);
2290 return false;
2291 }
2292
2293 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002294 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002295 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296}
2297
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002298void DataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299 // Render incoming data if we're the active call, and we have the local
2300 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002301 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002302 if (!media_channel()->SetReceive(recv)) {
2303 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2304 }
2305
2306 // Send outgoing data if we're the active call, we have the remote content,
2307 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002308 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309 if (!media_channel()->SetSend(send)) {
2310 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2311 }
2312
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002313 // Trigger SignalReadyToSendData asynchronously.
2314 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315
2316 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2317}
2318
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002319void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320 switch (pmsg->message_id) {
2321 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002322 DataChannelReadyToSendMessageData* data =
2323 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002324 ready_to_send_data_ = data->data();
2325 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002326 delete data;
2327 break;
2328 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 case MSG_DATARECEIVED: {
2330 DataReceivedMessageData* data =
2331 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2332 SignalDataReceived(this, data->params, data->payload);
2333 delete data;
2334 break;
2335 }
2336 case MSG_CHANNEL_ERROR: {
2337 const DataChannelErrorMessageData* data =
2338 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339 delete data;
2340 break;
2341 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002342 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002343 rtc::TypedMessageData<uint32_t>* data =
2344 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002345 SignalStreamClosedRemotely(data->data());
2346 delete data;
2347 break;
2348 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002349 default:
2350 BaseChannel::OnMessage(pmsg);
2351 break;
2352 }
2353}
2354
2355void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002356 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002357 SignalConnectionMonitor(this, infos);
2358}
2359
2360void DataChannel::StartMediaMonitor(int cms) {
2361 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002362 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363 media_monitor_->SignalUpdate.connect(
2364 this, &DataChannel::OnMediaMonitorUpdate);
2365 media_monitor_->Start(cms);
2366}
2367
2368void DataChannel::StopMediaMonitor() {
2369 if (media_monitor_) {
2370 media_monitor_->Stop();
2371 media_monitor_->SignalUpdate.disconnect(this);
2372 media_monitor_.reset();
2373 }
2374}
2375
2376void DataChannel::OnMediaMonitorUpdate(
2377 DataMediaChannel* media_channel, const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002378 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002379 SignalMediaMonitor(this, info);
2380}
2381
2382void DataChannel::OnDataReceived(
2383 const ReceiveDataParams& params, const char* data, size_t len) {
2384 DataReceivedMessageData* msg = new DataReceivedMessageData(
2385 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002386 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002387}
2388
Peter Boström0c4e06b2015-10-07 12:23:21 +02002389void DataChannel::OnDataChannelError(uint32_t ssrc,
2390 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002391 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2392 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002393 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002394}
2395
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002396void DataChannel::OnDataChannelReadyToSend(bool writable) {
2397 // This is usded for congestion control to indicate that the stream is ready
2398 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2399 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002400 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002401 new DataChannelReadyToSendMessageData(writable));
2402}
2403
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002404void DataChannel::GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002405 GetSupportedDataCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002406}
2407
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002408bool DataChannel::ShouldSetupDtlsSrtp_n() const {
2409 return data_channel_type_ == DCT_RTP && BaseChannel::ShouldSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410}
2411
Peter Boström0c4e06b2015-10-07 12:23:21 +02002412void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2413 rtc::TypedMessageData<uint32_t>* message =
2414 new rtc::TypedMessageData<uint32_t>(sid);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002415 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_STREAMCLOSEDREMOTELY,
2416 message);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002417}
2418
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002419} // namespace cricket