blob: 7ec536d2a7242460c9a3b498f90bdfee2ec59a12 [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 ";
deadbeef23d947d2016-08-22 16:00:30 -0700303 // TODO(deadbeef): Remove this grossness when we remove non-muxed RTCP.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200304 SetRtcpTransportChannel_n(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200305 transport_controller_->CreateTransportChannel_n(
guoweis46383312015-12-17 16:45:59 -0800306 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP),
307 false /* update_writablity */);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200308 if (!rtcp_transport_channel_) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000309 return false;
310 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000311 }
312
guoweis46383312015-12-17 16:45:59 -0800313 // We're not updating the writablity during the transition state.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200314 SetTransportChannel_n(transport_controller_->CreateTransportChannel_n(
guoweis46383312015-12-17 16:45:59 -0800315 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
deadbeef23d947d2016-08-22 16:00:30 -0700320 // TODO(deadbeef): Remove this grossness when we remove non-muxed RTCP.
321 if (rtcp_transport_channel_) {
guoweis46383312015-12-17 16:45:59 -0800322 // We can only update the RTCP ready to send after set_transport_channel has
323 // handled channel writability.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700324 SetTransportChannelReadyToSend(true, rtcp_transport_channel_->writable());
guoweis46383312015-12-17 16:45:59 -0800325 }
deadbeefcbecd352015-09-23 11:50:27 -0700326 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000327 return true;
328}
329
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200330void BaseChannel::SetTransportChannel_n(TransportChannel* new_tc) {
331 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000332
333 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700334 if (!old_tc && !new_tc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700335 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000336 return;
337 }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700338 RTC_DCHECK(old_tc != new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700339
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000340 if (old_tc) {
341 DisconnectFromTransportChannel(old_tc);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200342 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700343 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000344 }
345
346 transport_channel_ = new_tc;
347
348 if (new_tc) {
349 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700350 for (const auto& pair : socket_options_) {
351 new_tc->SetOption(pair.first, pair.second);
352 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000353 }
deadbeefcbecd352015-09-23 11:50:27 -0700354
355 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700356 // setting new transport channels.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200357 UpdateWritableState_n();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700358 // On setting a new channel, assume it's ready to send if it's writable,
359 // because we have no way of knowing otherwise (the channel doesn't give us
360 // "was last send successful?").
361 //
362 // This won't always be accurate (the last SendPacket call from another
363 // BaseChannel could have resulted in an error), but even so, we'll just
364 // encounter the error again and update "ready to send" accordingly.
365 SetTransportChannelReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000366}
367
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200368void BaseChannel::SetRtcpTransportChannel_n(TransportChannel* new_tc,
369 bool update_writablity) {
370 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000371
372 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700373 if (!old_tc && !new_tc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700374 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000375 return;
376 }
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700377 RTC_DCHECK(old_tc != new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700378
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000379 if (old_tc) {
380 DisconnectFromTransportChannel(old_tc);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200381 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700382 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000383 }
384
385 rtcp_transport_channel_ = new_tc;
386
387 if (new_tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200388 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800389 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
390 << "should never happen.";
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000391 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700392 for (const auto& pair : rtcp_socket_options_) {
393 new_tc->SetOption(pair.first, pair.second);
394 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000395 }
deadbeefcbecd352015-09-23 11:50:27 -0700396
guoweis46383312015-12-17 16:45:59 -0800397 if (update_writablity) {
398 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
399 // setting new channel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200400 UpdateWritableState_n();
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700401 // On setting a new channel, assume it's ready to send if it's writable,
402 // because we have no way of knowing otherwise (the channel doesn't give us
403 // "was last send successful?").
404 //
405 // This won't always be accurate (the last SendPacket call from another
406 // BaseChannel could have resulted in an error), but even so, we'll just
407 // encounter the error again and update "ready to send" accordingly.
408 SetTransportChannelReadyToSend(true, new_tc && new_tc->writable());
guoweis46383312015-12-17 16:45:59 -0800409 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000410}
411
412void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200413 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000414
415 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
416 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
417 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800418 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700419 tc->SignalSelectedCandidatePairChanged.connect(
420 this, &BaseChannel::OnSelectedCandidatePairChanged);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200421 tc->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000422}
423
424void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200425 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000426
427 tc->SignalWritableState.disconnect(this);
428 tc->SignalReadPacket.disconnect(this);
429 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800430 tc->SignalDtlsState.disconnect(this);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200431 tc->SignalSelectedCandidatePairChanged.disconnect(this);
432 tc->SignalSentPacket.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000433}
434
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700436 worker_thread_->Invoke<void>(
437 RTC_FROM_HERE,
438 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
439 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440 return true;
441}
442
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443bool BaseChannel::AddRecvStream(const StreamParams& sp) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700444 return InvokeOnWorker(RTC_FROM_HERE,
445 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446}
447
Peter Boström0c4e06b2015-10-07 12:23:21 +0200448bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700449 return InvokeOnWorker(RTC_FROM_HERE,
450 Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451}
452
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000453bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000454 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700455 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000456}
457
Peter Boström0c4e06b2015-10-07 12:23:21 +0200458bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700459 return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream,
460 media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000461}
462
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000464 ContentAction action,
465 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100466 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700467 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w,
468 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469}
470
471bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000472 ContentAction action,
473 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100474 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700475 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w,
476 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477}
478
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000480 // We pass in the BaseChannel instead of the transport_channel_
481 // because if the transport_channel_ changes, the ConnectionMonitor
482 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200483 // We pass in the network thread because on that thread connection monitor
484 // will call BaseChannel::GetConnectionStats which must be called on the
485 // network thread.
486 connection_monitor_.reset(
487 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000488 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000490 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491}
492
493void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000494 if (connection_monitor_) {
495 connection_monitor_->Stop();
496 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
498}
499
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000500bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200501 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000502 return transport_channel_->GetStats(infos);
503}
504
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700505bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 // Receive data if we are enabled and have local content,
507 return enabled() && IsReceiveContentDirection(local_content_direction_);
508}
509
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700510bool BaseChannel::IsReadyToSendMedia_w() const {
511 // Need to access some state updated on the network thread.
512 return network_thread_->Invoke<bool>(
513 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
514}
515
516bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 // Send outgoing data if we are enabled, have local and remote content,
518 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800519 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700521 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200522 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523}
524
jbaucheec21bd2016-03-20 06:15:43 -0700525bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700526 const rtc::PacketOptions& options) {
527 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528}
529
jbaucheec21bd2016-03-20 06:15:43 -0700530bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700531 const rtc::PacketOptions& options) {
532 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000533}
534
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000535int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200537 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700538 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200539}
540
541int BaseChannel::SetOption_n(SocketType type,
542 rtc::Socket::Option opt,
543 int value) {
544 RTC_DCHECK(network_thread_->IsCurrent());
545 TransportChannel* channel = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000547 case ST_RTP:
548 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700549 socket_options_.push_back(
550 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000551 break;
552 case ST_RTCP:
553 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700554 rtcp_socket_options_.push_back(
555 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000556 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000558 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559}
560
jbauchcb560652016-08-04 05:20:32 -0700561bool BaseChannel::SetCryptoOptions(const rtc::CryptoOptions& crypto_options) {
562 crypto_options_ = crypto_options;
563 return true;
564}
565
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566void BaseChannel::OnWritableState(TransportChannel* channel) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200567 RTC_DCHECK(channel == transport_channel_ ||
568 channel == rtcp_transport_channel_);
569 RTC_DCHECK(network_thread_->IsCurrent());
570 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571}
572
573void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000574 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000575 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000576 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100577 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200579 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580
581 // When using RTCP multiplexing we might get RTCP packets on the RTP
582 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
583 bool rtcp = PacketIsRtcp(channel, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700584 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000585 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586}
587
588void BaseChannel::OnReadyToSend(TransportChannel* channel) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700589 RTC_DCHECK(channel == transport_channel_ ||
590 channel == rtcp_transport_channel_);
591 SetTransportChannelReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592}
593
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800594void BaseChannel::OnDtlsState(TransportChannel* channel,
595 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200596 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800597 return;
598 }
599
600 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
601 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
602 // cover other scenarios like the whole channel is writable (not just this
603 // TransportChannel) or when TransportChannel is attached after DTLS is
604 // negotiated.
605 if (state != DTLS_TRANSPORT_CONNECTED) {
606 srtp_filter_.ResetParams();
607 }
608}
609
Honghai Zhangcc411c02016-03-29 17:27:21 -0700610void BaseChannel::OnSelectedCandidatePairChanged(
611 TransportChannel* channel,
Honghai Zhang52dce732016-03-31 12:37:31 -0700612 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700613 int last_sent_packet_id,
614 bool ready_to_send) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700615 RTC_DCHECK(channel == transport_channel_ ||
616 channel == rtcp_transport_channel_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200617 RTC_DCHECK(network_thread_->IsCurrent());
618 std::string transport_name = channel->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700619 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700620 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700621 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700622 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700623 selected_candidate_pair->remote_candidate().network_id(),
624 last_sent_packet_id);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700625 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200626 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700627 RTC_FROM_HERE, worker_thread_,
628 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
629 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700630}
631
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700632void BaseChannel::SetTransportChannelReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200633 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700634 if (rtcp) {
635 rtcp_ready_to_send_ = ready;
636 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 rtp_ready_to_send_ = ready;
638 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200640 bool ready_to_send =
641 (rtp_ready_to_send_ &&
642 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
643 (rtcp_ready_to_send_ || !rtcp_transport_channel_));
644
645 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700646 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200647 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648}
649
650bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
651 const char* data, size_t len) {
652 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000653 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654}
655
stefanc1aeaf02015-10-15 07:26:07 -0700656bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700657 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700658 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200659 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
660 // If the thread is not our network thread, we will post to our network
661 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 // synchronize access to all the pieces of the send path, including
663 // SRTP and the inner workings of the transport channels.
664 // The only downside is that we can't return a proper failure code if
665 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200666 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200668 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
669 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800670 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700671 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700672 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 return true;
674 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200675 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676
677 // Now that we are on the correct thread, ensure we have a place to send this
678 // packet before doing anything. (We might get RTCP packets that we don't
679 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
680 // transport.
681 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
682 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000683 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 return false;
685 }
686
687 // Protect ourselves against crazy data.
688 if (!ValidPacket(rtcp, packet)) {
689 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000690 << PacketType(rtcp)
691 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 return false;
693 }
694
stefanc1aeaf02015-10-15 07:26:07 -0700695 rtc::PacketOptions updated_options;
696 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 // Protect if needed.
698 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200699 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200701 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000702 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000704 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
705 // inside libsrtp for a RTP packet. A external HMAC module will be writing
706 // a fake HMAC value. This is ONLY done for a RTP packet.
707 // Socket layer will update rtp sendtime extension header if present in
708 // packet with current time before updating the HMAC.
709#if !defined(ENABLE_EXTERNAL_AUTH)
710 res = srtp_filter_.ProtectRtp(
711 data, len, static_cast<int>(packet->capacity()), &len);
712#else
stefanc1aeaf02015-10-15 07:26:07 -0700713 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000714 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000715 res = srtp_filter_.ProtectRtp(
716 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700717 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000718 // If protection succeeds, let's get auth params from srtp.
719 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200720 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000721 int key_len;
722 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700723 &auth_key, &key_len,
724 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000725 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700726 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
727 updated_options.packet_time_params.srtp_auth_key.assign(
728 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000729 }
730 }
731#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 if (!res) {
733 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200734 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 GetRtpSeqNum(data, len, &seq_num);
736 GetRtpSsrc(data, len, &ssrc);
737 LOG(LS_ERROR) << "Failed to protect " << content_name_
738 << " RTP packet: size=" << len
739 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
740 return false;
741 }
742 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000743 res = srtp_filter_.ProtectRtcp(data, len,
744 static_cast<int>(packet->capacity()),
745 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 if (!res) {
747 int type = -1;
748 GetRtcpType(data, len, &type);
749 LOG(LS_ERROR) << "Failed to protect " << content_name_
750 << " RTCP packet: size=" << len << ", type=" << type;
751 return false;
752 }
753 }
754
755 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000756 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 } else if (secure_required_) {
758 // This is a double check for something that supposedly can't happen.
759 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
760 << " packet when SRTP is inactive and crypto is required";
761
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700762 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 return false;
764 }
765
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200767 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
768 int ret = channel->SendPacket(packet->data<char>(), packet->size(),
769 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000770 if (ret != static_cast<int>(packet->size())) {
skvladc309e0e2016-07-28 17:15:20 -0700771 if (channel->GetError() == ENOTCONN) {
772 LOG(LS_WARNING) << "Got ENOTCONN from transport.";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700773 SetTransportChannelReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 }
775 return false;
776 }
777 return true;
778}
779
jbaucheec21bd2016-03-20 06:15:43 -0700780bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 // Protect ourselves against crazy data.
782 if (!ValidPacket(rtcp, packet)) {
783 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000784 << PacketType(rtcp)
785 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 return false;
787 }
pbos482b12e2015-11-16 10:19:58 -0800788 if (rtcp) {
789 // Permit all (seemingly valid) RTCP packets.
790 return true;
791 }
792 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700793 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794}
795
jbaucheec21bd2016-03-20 06:15:43 -0700796void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000797 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200798 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 if (!WantsPacket(rtcp, packet)) {
800 return;
801 }
802
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000803 // We are only interested in the first rtp packet because that
804 // indicates the media has started flowing.
805 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700807 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 }
809
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 // Unprotect the packet, if needed.
811 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200812 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200813 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000814 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 bool res;
816 if (!rtcp) {
817 res = srtp_filter_.UnprotectRtp(data, len, &len);
818 if (!res) {
819 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200820 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 GetRtpSeqNum(data, len, &seq_num);
822 GetRtpSsrc(data, len, &ssrc);
823 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
824 << " RTP packet: size=" << len
825 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
826 return;
827 }
828 } else {
829 res = srtp_filter_.UnprotectRtcp(data, len, &len);
830 if (!res) {
831 int type = -1;
832 GetRtcpType(data, len, &type);
833 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
834 << " RTCP packet: size=" << len << ", type=" << type;
835 return;
836 }
837 }
838
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000839 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 } else if (secure_required_) {
841 // Our session description indicates that SRTP is required, but we got a
842 // packet before our SRTP filter is active. This means either that
843 // a) we got SRTP packets before we received the SDES keys, in which case
844 // we can't decrypt it anyway, or
845 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
846 // channels, so we haven't yet extracted keys, even if DTLS did complete
847 // on the channel that the packets are being sent on. It's really good
848 // practice to wait for both RTP and RTCP to be good to go before sending
849 // media, to prevent weird failure modes, so it's fine for us to just eat
850 // packets here. This is all sidestepped if RTCP mux is used anyway.
851 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
852 << " packet when SRTP is inactive and crypto is required";
853 return;
854 }
855
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200856 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700857 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200858 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
859}
860
861void BaseChannel::OnPacketReceived(bool rtcp,
862 const rtc::CopyOnWriteBuffer& packet,
863 const rtc::PacketTime& packet_time) {
864 RTC_DCHECK(worker_thread_->IsCurrent());
865 // Need to copy variable because OnRtcpReceived/OnPacketReceived
866 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
867 rtc::CopyOnWriteBuffer data(packet);
868 if (rtcp) {
869 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200871 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 }
873}
874
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000875bool BaseChannel::PushdownLocalDescription(
876 const SessionDescription* local_desc, ContentAction action,
877 std::string* error_desc) {
878 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879 const MediaContentDescription* content_desc =
880 GetContentDescription(content_info);
881 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000882 !SetLocalContent(content_desc, action, error_desc)) {
883 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
884 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000886 return true;
887}
888
889bool BaseChannel::PushdownRemoteDescription(
890 const SessionDescription* remote_desc, ContentAction action,
891 std::string* error_desc) {
892 const ContentInfo* content_info = GetFirstContent(remote_desc);
893 const MediaContentDescription* content_desc =
894 GetContentDescription(content_info);
895 if (content_desc && content_info && !content_info->rejected &&
896 !SetRemoteContent(content_desc, action, error_desc)) {
897 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
898 return false;
899 }
900 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901}
902
903void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700904 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 if (enabled_)
906 return;
907
908 LOG(LS_INFO) << "Channel enabled";
909 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700910 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911}
912
913void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700914 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 if (!enabled_)
916 return;
917
918 LOG(LS_INFO) << "Channel disabled";
919 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700920 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921}
922
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200923void BaseChannel::UpdateWritableState_n() {
deadbeefcbecd352015-09-23 11:50:27 -0700924 if (transport_channel_ && transport_channel_->writable() &&
925 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200926 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700927 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200928 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700929 }
930}
931
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200932void BaseChannel::ChannelWritable_n() {
933 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800934 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800936 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937
deadbeefcbecd352015-09-23 11:50:27 -0700938 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 << (was_ever_writable_ ? "" : " for the first time");
940
941 std::vector<ConnectionInfo> infos;
942 transport_channel_->GetStats(&infos);
943 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
944 it != infos.end(); ++it) {
945 if (it->best_connection) {
946 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
947 << "->" << it->remote_candidate.ToSensitiveString();
948 break;
949 }
950 }
951
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200953 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700955 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956}
957
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200958void BaseChannel::SignalDtlsSetupFailure_n(bool rtcp) {
959 RTC_DCHECK(network_thread_->IsCurrent());
960 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700961 RTC_FROM_HERE, signaling_thread(),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200962 Bind(&BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000963}
964
965void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700966 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000967 SignalDtlsSetupFailure(this, rtcp);
968}
969
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200970bool BaseChannel::SetDtlsSrtpCryptoSuites_n(TransportChannel* tc, bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800971 std::vector<int> crypto_suites;
972 // We always use the default SRTP crypto suites for RTCP, but we may use
973 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200975 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 } else {
jbauchcb560652016-08-04 05:20:32 -0700977 GetDefaultSrtpCryptoSuites(crypto_options(), &crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800979 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980}
981
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200982bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800983 // Since DTLS is applied to all channels, checking RTP should be enough.
984 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985}
986
987// This function returns true if either DTLS-SRTP is not in use
988// *or* DTLS-SRTP is successfully set up.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200989bool BaseChannel::SetupDtlsSrtp_n(bool rtcp_channel) {
990 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 bool ret = false;
992
deadbeefcbecd352015-09-23 11:50:27 -0700993 TransportChannel* channel =
994 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800996 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800998 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001000 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
1001 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 return false;
1003 }
1004
1005 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
1006 << content_name() << " "
1007 << PacketType(rtcp_channel);
1008
jbauchcb560652016-08-04 05:20:32 -07001009 int key_len;
1010 int salt_len;
1011 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
1012 &salt_len)) {
1013 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
1014 return false;
1015 }
1016
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -07001018 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019
1020 // RFC 5705 exporter using the RFC 5764 parameters
1021 if (!channel->ExportKeyingMaterial(
1022 kDtlsSrtpExporterLabel,
1023 NULL, 0, false,
1024 &dtls_buffer[0], dtls_buffer.size())) {
1025 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001026 RTC_DCHECK(false); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 return false;
1028 }
1029
1030 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -07001031 std::vector<unsigned char> client_write_key(key_len + salt_len);
1032 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -07001034 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
1035 offset += key_len;
1036 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
1037 offset += key_len;
1038 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
1039 offset += salt_len;
1040 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041
1042 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001043 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001044 if (!channel->GetSslRole(&role)) {
1045 LOG(LS_WARNING) << "GetSslRole failed";
1046 return false;
1047 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001049 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 send_key = &server_write_key;
1051 recv_key = &client_write_key;
1052 } else {
1053 send_key = &client_write_key;
1054 recv_key = &server_write_key;
1055 }
1056
1057 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001058 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1059 static_cast<int>(send_key->size()),
1060 selected_crypto_suite, &(*recv_key)[0],
1061 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001063 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1064 static_cast<int>(send_key->size()),
1065 selected_crypto_suite, &(*recv_key)[0],
1066 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 }
1068
1069 if (!ret)
1070 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
1071 else
1072 dtls_keyed_ = true;
1073
1074 return ret;
1075}
1076
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001077void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001078 if (srtp_filter_.IsActive()) {
1079 return;
1080 }
1081
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001082 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001083 return;
1084 }
1085
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001086 if (!SetupDtlsSrtp_n(false)) {
1087 SignalDtlsSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001088 return;
1089 }
1090
1091 if (rtcp_transport_channel_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001092 if (!SetupDtlsSrtp_n(true)) {
1093 SignalDtlsSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001094 return;
1095 }
1096 }
1097}
1098
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001099void BaseChannel::ChannelNotWritable_n() {
1100 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 if (!writable_)
1102 return;
1103
deadbeefcbecd352015-09-23 11:50:27 -07001104 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001106 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001107}
1108
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001109bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001110 const MediaContentDescription* content,
1111 ContentAction action,
1112 ContentSource src,
1113 std::string* error_desc) {
1114 if (action == CA_UPDATE) {
1115 // These parameters never get changed by a CA_UDPATE.
1116 return true;
1117 }
1118
1119 // Cache secure_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001120 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001121 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1122 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001123}
1124
1125bool BaseChannel::SetRtpTransportParameters_n(
1126 const MediaContentDescription* content,
1127 ContentAction action,
1128 ContentSource src,
1129 std::string* error_desc) {
1130 RTC_DCHECK(network_thread_->IsCurrent());
1131
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001132 if (src == CS_LOCAL) {
1133 set_secure_required(content->crypto_required() != CT_NONE);
1134 }
1135
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001136 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001137 return false;
1138 }
1139
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001140 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001141 return false;
1142 }
1143
1144 return true;
1145}
1146
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001147// |dtls| will be set to true if DTLS is active for transport channel and
1148// crypto is empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001149bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1150 bool* dtls,
1151 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001152 *dtls = transport_channel_->IsDtlsActive();
1153 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001154 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001155 return false;
1156 }
1157 return true;
1158}
1159
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001160bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001161 ContentAction action,
1162 ContentSource src,
1163 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001164 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001165 if (action == CA_UPDATE) {
1166 // no crypto params.
1167 return true;
1168 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001170 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001171 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001172 if (!ret) {
1173 return false;
1174 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 switch (action) {
1176 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001177 // If DTLS is already active on the channel, we could be renegotiating
1178 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001179 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001180 ret = srtp_filter_.SetOffer(cryptos, src);
1181 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 break;
1183 case CA_PRANSWER:
1184 // If we're doing DTLS-SRTP, we don't want to update the filter
1185 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001186 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1188 }
1189 break;
1190 case CA_ANSWER:
1191 // If we're doing DTLS-SRTP, we don't want to update the filter
1192 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001193 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 ret = srtp_filter_.SetAnswer(cryptos, src);
1195 }
1196 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 default:
1198 break;
1199 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001200 if (!ret) {
1201 SafeSetError("Failed to setup SRTP filter.", error_desc);
1202 return false;
1203 }
1204 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205}
1206
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001207void BaseChannel::ActivateRtcpMux() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001208 network_thread_->Invoke<void>(RTC_FROM_HERE,
1209 Bind(&BaseChannel::ActivateRtcpMux_n, this));
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001210}
1211
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001212void BaseChannel::ActivateRtcpMux_n() {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001213 if (!rtcp_mux_filter_.IsActive()) {
1214 rtcp_mux_filter_.SetActive();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001215 SetRtcpTransportChannel_n(nullptr, true);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001216 }
1217}
1218
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001219bool BaseChannel::SetRtcpMux_n(bool enable,
1220 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001221 ContentSource src,
1222 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 bool ret = false;
1224 switch (action) {
1225 case CA_OFFER:
1226 ret = rtcp_mux_filter_.SetOffer(enable, src);
1227 break;
1228 case CA_PRANSWER:
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001229 // This may activate RTCP muxing, but we don't yet destroy the channel
1230 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1232 break;
1233 case CA_ANSWER:
1234 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1235 if (ret && rtcp_mux_filter_.IsActive()) {
1236 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001237 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1238 << " by destroying RTCP transport channel for "
1239 << transport_name();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001240 SetRtcpTransportChannel_n(nullptr, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 }
1242 break;
1243 case CA_UPDATE:
1244 // No RTCP mux info.
1245 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001246 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 default:
1248 break;
1249 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001250 if (!ret) {
1251 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1252 return false;
1253 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1255 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1256 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001257 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 // If the RTP transport is already writable, then so are we.
1259 if (transport_channel_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001260 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 }
1262 }
1263
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001264 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265}
1266
1267bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001268 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001269 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270}
1271
Peter Boström0c4e06b2015-10-07 12:23:21 +02001272bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001273 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 return media_channel()->RemoveRecvStream(ssrc);
1275}
1276
1277bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001278 ContentAction action,
1279 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1281 action == CA_PRANSWER || action == CA_UPDATE))
1282 return false;
1283
1284 // If this is an update, streams only contain streams that have changed.
1285 if (action == CA_UPDATE) {
1286 for (StreamParamsVec::const_iterator it = streams.begin();
1287 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001288 const StreamParams* existing_stream =
1289 GetStreamByIds(local_streams_, it->groupid, it->id);
1290 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 if (media_channel()->AddSendStream(*it)) {
1292 local_streams_.push_back(*it);
1293 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1294 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001295 std::ostringstream desc;
1296 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1297 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 return false;
1299 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001300 } else if (existing_stream && !it->has_ssrcs()) {
1301 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001302 std::ostringstream desc;
1303 desc << "Failed to remove send stream with ssrc "
1304 << it->first_ssrc() << ".";
1305 SafeSetError(desc.str(), error_desc);
1306 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001308 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 } else {
1310 LOG(LS_WARNING) << "Ignore unsupported stream update";
1311 }
1312 }
1313 return true;
1314 }
1315 // Else streams are all the streams we want to send.
1316
1317 // Check for streams that have been removed.
1318 bool ret = true;
1319 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1320 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001321 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001323 std::ostringstream desc;
1324 desc << "Failed to remove send stream with ssrc "
1325 << it->first_ssrc() << ".";
1326 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327 ret = false;
1328 }
1329 }
1330 }
1331 // Check for new streams.
1332 for (StreamParamsVec::const_iterator it = streams.begin();
1333 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001334 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001336 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001338 std::ostringstream desc;
1339 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1340 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341 ret = false;
1342 }
1343 }
1344 }
1345 local_streams_ = streams;
1346 return ret;
1347}
1348
1349bool BaseChannel::UpdateRemoteStreams_w(
1350 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001351 ContentAction action,
1352 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1354 action == CA_PRANSWER || action == CA_UPDATE))
1355 return false;
1356
1357 // If this is an update, streams only contain streams that have changed.
1358 if (action == CA_UPDATE) {
1359 for (StreamParamsVec::const_iterator it = streams.begin();
1360 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001361 const StreamParams* existing_stream =
1362 GetStreamByIds(remote_streams_, it->groupid, it->id);
1363 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 if (AddRecvStream_w(*it)) {
1365 remote_streams_.push_back(*it);
1366 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1367 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001368 std::ostringstream desc;
1369 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1370 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371 return false;
1372 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001373 } else if (existing_stream && !it->has_ssrcs()) {
1374 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001375 std::ostringstream desc;
1376 desc << "Failed to remove remote stream with ssrc "
1377 << it->first_ssrc() << ".";
1378 SafeSetError(desc.str(), error_desc);
1379 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001381 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382 } else {
1383 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001384 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001385 << " new stream = " << it->ToString();
1386 }
1387 }
1388 return true;
1389 }
1390 // Else streams are all the streams we want to receive.
1391
1392 // Check for streams that have been removed.
1393 bool ret = true;
1394 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1395 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001396 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001398 std::ostringstream desc;
1399 desc << "Failed to remove remote stream with ssrc "
1400 << it->first_ssrc() << ".";
1401 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 ret = false;
1403 }
1404 }
1405 }
1406 // Check for new streams.
1407 for (StreamParamsVec::const_iterator it = streams.begin();
1408 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001409 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410 if (AddRecvStream_w(*it)) {
1411 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1412 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001413 std::ostringstream desc;
1414 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1415 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 ret = false;
1417 }
1418 }
1419 }
1420 remote_streams_ = streams;
1421 return ret;
1422}
1423
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001424void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001425 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001426// Absolute Send Time extension id is used only with external auth,
1427// so do not bother searching for it and making asyncronious call to set
1428// something that is not used.
1429#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001430 const webrtc::RtpExtension* send_time_extension =
1431 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001432 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001433 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001434 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001435 RTC_FROM_HERE, network_thread_,
1436 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1437 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001438#endif
1439}
1440
1441void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1442 int rtp_abs_sendtime_extn_id) {
1443 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001444}
1445
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001446void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001447 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001449 case MSG_SEND_RTP_PACKET:
1450 case MSG_SEND_RTCP_PACKET: {
1451 RTC_DCHECK(network_thread_->IsCurrent());
1452 SendPacketMessageData* data =
1453 static_cast<SendPacketMessageData*>(pmsg->pdata);
1454 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1455 SendPacket(rtcp, &data->packet, data->options);
1456 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457 break;
1458 }
1459 case MSG_FIRSTPACKETRECEIVED: {
1460 SignalFirstPacketReceived(this);
1461 break;
1462 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 }
1464}
1465
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001466void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467 // Flush all remaining RTCP messages. This should only be called in
1468 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001469 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001470 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001471 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1472 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001473 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1474 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 }
1476}
1477
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001478void BaseChannel::SignalSentPacket_n(TransportChannel* /* channel */,
1479 const rtc::SentPacket& sent_packet) {
1480 RTC_DCHECK(network_thread_->IsCurrent());
1481 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001482 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001483 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1484}
1485
1486void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1487 RTC_DCHECK(worker_thread_->IsCurrent());
1488 SignalSentPacket(sent_packet);
1489}
1490
1491VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1492 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 MediaEngineInterface* media_engine,
1494 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001495 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 const std::string& content_name,
1497 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001498 : BaseChannel(worker_thread,
1499 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001500 media_channel,
1501 transport_controller,
1502 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001504 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001505 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506
1507VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001508 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 StopAudioMonitor();
1510 StopMediaMonitor();
1511 // this can't be done in the base class, since it calls a virtual
1512 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001513 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514}
1515
skvlad6c87a672016-05-17 17:49:52 -07001516bool VoiceChannel::Init_w(const std::string* bundle_transport_name) {
1517 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518 return false;
1519 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001520 return true;
1521}
1522
Peter Boström0c4e06b2015-10-07 12:23:21 +02001523bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001524 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001525 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001526 AudioSource* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001527 return InvokeOnWorker(RTC_FROM_HERE,
1528 Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001529 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530}
1531
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532// TODO(juberti): Handle early media the right way. We should get an explicit
1533// ringing message telling us to start playing local ringback, which we cancel
1534// if any early media actually arrives. For now, we do the opposite, which is
1535// to wait 1 second for early media, and start playing local ringback if none
1536// arrives.
1537void VoiceChannel::SetEarlyMedia(bool enable) {
1538 if (enable) {
1539 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001540 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1541 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 } else {
1543 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001544 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 }
1546}
1547
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548bool VoiceChannel::CanInsertDtmf() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001549 return InvokeOnWorker(
1550 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551}
1552
Peter Boström0c4e06b2015-10-07 12:23:21 +02001553bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1554 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001555 int duration) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001556 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this,
1557 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558}
1559
solenberg4bac9c52015-10-09 02:32:53 -07001560bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001561 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume,
1562 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001564
Tommif888bb52015-12-12 01:37:01 +01001565void VoiceChannel::SetRawAudioSink(
1566 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001567 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1568 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001569 // passing. So we invoke to our own little routine that gets a pointer to
1570 // our local variable. This is OK since we're synchronously invoking.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001571 InvokeOnWorker(RTC_FROM_HERE,
1572 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001573}
1574
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001575webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001576 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001577 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001578}
1579
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001580webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1581 uint32_t ssrc) const {
1582 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001583}
1584
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001585bool VoiceChannel::SetRtpSendParameters(
1586 uint32_t ssrc,
1587 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001588 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001589 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001590 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001591}
1592
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001593bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1594 webrtc::RtpParameters parameters) {
1595 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1596}
1597
1598webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1599 uint32_t ssrc) const {
1600 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001601 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001602 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1603}
1604
1605webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1606 uint32_t ssrc) const {
1607 return media_channel()->GetRtpReceiveParameters(ssrc);
1608}
1609
1610bool VoiceChannel::SetRtpReceiveParameters(
1611 uint32_t ssrc,
1612 const webrtc::RtpParameters& parameters) {
1613 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001614 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001615 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1616}
1617
1618bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1619 webrtc::RtpParameters parameters) {
1620 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001621}
1622
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001624 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1625 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626}
1627
1628void VoiceChannel::StartMediaMonitor(int cms) {
1629 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001630 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 media_monitor_->SignalUpdate.connect(
1632 this, &VoiceChannel::OnMediaMonitorUpdate);
1633 media_monitor_->Start(cms);
1634}
1635
1636void VoiceChannel::StopMediaMonitor() {
1637 if (media_monitor_) {
1638 media_monitor_->Stop();
1639 media_monitor_->SignalUpdate.disconnect(this);
1640 media_monitor_.reset();
1641 }
1642}
1643
1644void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001645 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 audio_monitor_
1647 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1648 audio_monitor_->Start(cms);
1649}
1650
1651void VoiceChannel::StopAudioMonitor() {
1652 if (audio_monitor_) {
1653 audio_monitor_->Stop();
1654 audio_monitor_.reset();
1655 }
1656}
1657
1658bool VoiceChannel::IsAudioMonitorRunning() const {
1659 return (audio_monitor_.get() != NULL);
1660}
1661
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001663 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664}
1665
1666int VoiceChannel::GetOutputLevel_w() {
1667 return media_channel()->GetOutputLevel();
1668}
1669
1670void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1671 media_channel()->GetActiveStreams(actives);
1672}
1673
1674void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001675 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001676 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001677 int flags) {
1678 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679
1680 // Set a flag when we've received an RTP packet. If we're waiting for early
1681 // media, this will disable the timeout.
1682 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1683 received_media_ = true;
1684 }
1685}
1686
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001687void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001688 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001689 invoker_.AsyncInvoke<void>(
1690 RTC_FROM_HERE, worker_thread_,
1691 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001692}
1693
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001694void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695 // Render incoming data if we're the active call, and we have the local
1696 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001697 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001698 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699
1700 // Send outgoing data if we're the active call, we have the remote content,
1701 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001702 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001703 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704
1705 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1706}
1707
1708const ContentInfo* VoiceChannel::GetFirstContent(
1709 const SessionDescription* sdesc) {
1710 return GetFirstAudioContent(sdesc);
1711}
1712
1713bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001714 ContentAction action,
1715 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001716 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001717 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 LOG(LS_INFO) << "Setting local voice description";
1719
1720 const AudioContentDescription* audio =
1721 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001722 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001723 if (!audio) {
1724 SafeSetError("Can't find audio content in local description.", error_desc);
1725 return false;
1726 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001728 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001729 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 }
1731
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001732 AudioRecvParameters recv_params = last_recv_params_;
1733 RtpParametersFromMediaDescription(audio, &recv_params);
1734 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001735 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001736 error_desc);
1737 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001739 for (const AudioCodec& codec : audio->codecs()) {
1740 bundle_filter()->AddPayloadType(codec.id);
1741 }
1742 last_recv_params_ = recv_params;
1743
1744 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1745 // only give it to the media channel once we have a remote
1746 // description too (without a remote description, we won't be able
1747 // to send them anyway).
1748 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1749 SafeSetError("Failed to set local audio description streams.", error_desc);
1750 return false;
1751 }
1752
1753 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001754 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001755 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756}
1757
1758bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001759 ContentAction action,
1760 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001761 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001762 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 LOG(LS_INFO) << "Setting remote voice description";
1764
1765 const AudioContentDescription* audio =
1766 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001767 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001768 if (!audio) {
1769 SafeSetError("Can't find audio content in remote description.", error_desc);
1770 return false;
1771 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001773 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001774 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 }
1776
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001777 AudioSendParameters send_params = last_send_params_;
1778 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001779 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001780 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001781 }
skvladdc1c62c2016-03-16 19:07:43 -07001782
1783 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1784 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001785 SafeSetError("Failed to set remote audio description send parameters.",
1786 error_desc);
1787 return false;
1788 }
1789 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001791 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1792 // and only give it to the media channel once we have a local
1793 // description too (without a local description, we won't be able to
1794 // recv them anyway).
1795 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1796 SafeSetError("Failed to set remote audio description streams.", error_desc);
1797 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 }
1799
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001800 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001801 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001802 }
1803
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001804 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001805 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001806 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807}
1808
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809void VoiceChannel::HandleEarlyMediaTimeout() {
1810 // This occurs on the main thread, not the worker thread.
1811 if (!received_media_) {
1812 LOG(LS_INFO) << "No early media received before timeout";
1813 SignalEarlyMediaTimeout(this);
1814 }
1815}
1816
Peter Boström0c4e06b2015-10-07 12:23:21 +02001817bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1818 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001819 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 if (!enabled()) {
1821 return false;
1822 }
solenberg1d63dd02015-12-02 12:35:09 -08001823 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824}
1825
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001826void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 case MSG_EARLYMEDIATIMEOUT:
1829 HandleEarlyMediaTimeout();
1830 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 case MSG_CHANNEL_ERROR: {
1832 VoiceChannelErrorMessageData* data =
1833 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 delete data;
1835 break;
1836 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 default:
1838 BaseChannel::OnMessage(pmsg);
1839 break;
1840 }
1841}
1842
1843void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001844 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 SignalConnectionMonitor(this, infos);
1846}
1847
1848void VoiceChannel::OnMediaMonitorUpdate(
1849 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001850 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 SignalMediaMonitor(this, info);
1852}
1853
1854void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1855 const AudioInfo& info) {
1856 SignalAudioMonitor(this, info);
1857}
1858
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001859void VoiceChannel::GetSrtpCryptoSuites_n(
1860 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07001861 GetSupportedAudioCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862}
1863
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001864VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1865 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001867 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001869 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001870 : BaseChannel(worker_thread,
1871 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001872 media_channel,
1873 transport_controller,
1874 content_name,
perkjc11b1842016-03-07 17:34:13 -08001875 rtcp) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876
skvlad6c87a672016-05-17 17:49:52 -07001877bool VideoChannel::Init_w(const std::string* bundle_transport_name) {
1878 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 return false;
1880 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 return true;
1882}
1883
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001885 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 StopMediaMonitor();
1887 // this can't be done in the base class, since it calls a virtual
1888 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001889
1890 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891}
1892
nisse08582ff2016-02-04 01:24:52 -08001893bool VideoChannel::SetSink(uint32_t ssrc,
1894 rtc::VideoSinkInterface<VideoFrame>* sink) {
1895 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001896 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001897 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898 return true;
1899}
1900
deadbeef5a4a75a2016-06-02 16:23:38 -07001901bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001902 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001903 bool mute,
1904 const VideoOptions* options,
nisse2ded9b12016-04-08 02:23:55 -07001905 rtc::VideoSourceInterface<cricket::VideoFrame>* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001906 return InvokeOnWorker(RTC_FROM_HERE,
1907 Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
deadbeef5a4a75a2016-06-02 16:23:38 -07001908 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001909}
1910
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001911webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001912 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001913 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001914}
1915
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001916webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1917 uint32_t ssrc) const {
1918 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001919}
1920
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001921bool VideoChannel::SetRtpSendParameters(
1922 uint32_t ssrc,
1923 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001924 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001925 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001926 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001927}
1928
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001929bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1930 webrtc::RtpParameters parameters) {
1931 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1932}
1933
1934webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1935 uint32_t ssrc) const {
1936 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001937 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001938 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
1939}
1940
1941webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
1942 uint32_t ssrc) const {
1943 return media_channel()->GetRtpReceiveParameters(ssrc);
1944}
1945
1946bool VideoChannel::SetRtpReceiveParameters(
1947 uint32_t ssrc,
1948 const webrtc::RtpParameters& parameters) {
1949 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001950 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001951 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1952}
1953
1954bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1955 webrtc::RtpParameters parameters) {
1956 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001957}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001958
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001959void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 // Send outgoing data if we're the active call, we have the remote content,
1961 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001962 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 if (!media_channel()->SetSend(send)) {
1964 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1965 // TODO(gangji): Report error back to server.
1966 }
1967
Peter Boström34fbfff2015-09-24 19:20:30 +02001968 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969}
1970
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001971bool VideoChannel::GetStats(VideoMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001972 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
1973 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974}
1975
1976void VideoChannel::StartMediaMonitor(int cms) {
1977 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001978 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 media_monitor_->SignalUpdate.connect(
1980 this, &VideoChannel::OnMediaMonitorUpdate);
1981 media_monitor_->Start(cms);
1982}
1983
1984void VideoChannel::StopMediaMonitor() {
1985 if (media_monitor_) {
1986 media_monitor_->Stop();
1987 media_monitor_.reset();
1988 }
1989}
1990
1991const ContentInfo* VideoChannel::GetFirstContent(
1992 const SessionDescription* sdesc) {
1993 return GetFirstVideoContent(sdesc);
1994}
1995
1996bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001997 ContentAction action,
1998 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001999 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002000 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 LOG(LS_INFO) << "Setting local video description";
2002
2003 const VideoContentDescription* video =
2004 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002005 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002006 if (!video) {
2007 SafeSetError("Can't find video content in local description.", error_desc);
2008 return false;
2009 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002011 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002012 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013 }
2014
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002015 VideoRecvParameters recv_params = last_recv_params_;
2016 RtpParametersFromMediaDescription(video, &recv_params);
2017 if (!media_channel()->SetRecvParameters(recv_params)) {
2018 SafeSetError("Failed to set local video description recv parameters.",
2019 error_desc);
2020 return false;
2021 }
2022 for (const VideoCodec& codec : video->codecs()) {
2023 bundle_filter()->AddPayloadType(codec.id);
2024 }
2025 last_recv_params_ = recv_params;
2026
2027 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2028 // only give it to the media channel once we have a remote
2029 // description too (without a remote description, we won't be able
2030 // to send them anyway).
2031 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2032 SafeSetError("Failed to set local video description streams.", error_desc);
2033 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 }
2035
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002036 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002037 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002038 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039}
2040
2041bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002042 ContentAction action,
2043 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002044 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002045 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046 LOG(LS_INFO) << "Setting remote video description";
2047
2048 const VideoContentDescription* video =
2049 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002050 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002051 if (!video) {
2052 SafeSetError("Can't find video content in remote description.", error_desc);
2053 return false;
2054 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002056 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002057 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 }
2059
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002060 VideoSendParameters send_params = last_send_params_;
2061 RtpSendParametersFromMediaDescription(video, &send_params);
2062 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002063 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002064 }
skvladdc1c62c2016-03-16 19:07:43 -07002065
2066 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2067
2068 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002069 SafeSetError("Failed to set remote video description send parameters.",
2070 error_desc);
2071 return false;
2072 }
2073 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002075 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2076 // and only give it to the media channel once we have a local
2077 // description too (without a local description, we won't be able to
2078 // recv them anyway).
2079 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2080 SafeSetError("Failed to set remote video description streams.", error_desc);
2081 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082 }
2083
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002084 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002085 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002087
2088 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002089 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002090 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091}
2092
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002093void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002094 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095 case MSG_CHANNEL_ERROR: {
2096 const VideoChannelErrorMessageData* data =
2097 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 delete data;
2099 break;
2100 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002101 default:
2102 BaseChannel::OnMessage(pmsg);
2103 break;
2104 }
2105}
2106
2107void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002108 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 SignalConnectionMonitor(this, infos);
2110}
2111
2112// TODO(pthatcher): Look into removing duplicate code between
2113// audio, video, and data, perhaps by using templates.
2114void VideoChannel::OnMediaMonitorUpdate(
2115 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002116 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 SignalMediaMonitor(this, info);
2118}
2119
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002120void VideoChannel::GetSrtpCryptoSuites_n(
2121 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002122 GetSupportedVideoCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123}
2124
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002125DataChannel::DataChannel(rtc::Thread* worker_thread,
2126 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002127 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002128 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129 const std::string& content_name,
2130 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002131 : BaseChannel(worker_thread,
2132 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002133 media_channel,
2134 transport_controller,
2135 content_name,
2136 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002137 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002138 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139
2140DataChannel::~DataChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08002141 TRACE_EVENT0("webrtc", "DataChannel::~DataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142 StopMediaMonitor();
2143 // this can't be done in the base class, since it calls a virtual
2144 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002145
2146 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147}
2148
skvlad6c87a672016-05-17 17:49:52 -07002149bool DataChannel::Init_w(const std::string* bundle_transport_name) {
2150 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151 return false;
2152 }
2153 media_channel()->SignalDataReceived.connect(
2154 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002155 media_channel()->SignalReadyToSend.connect(
2156 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002157 media_channel()->SignalStreamClosedRemotely.connect(
2158 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159 return true;
2160}
2161
2162bool DataChannel::SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07002163 const rtc::CopyOnWriteBuffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 SendDataResult* result) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002165 return InvokeOnWorker(
2166 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2167 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168}
2169
2170const ContentInfo* DataChannel::GetFirstContent(
2171 const SessionDescription* sdesc) {
2172 return GetFirstDataContent(sdesc);
2173}
2174
jbaucheec21bd2016-03-20 06:15:43 -07002175bool DataChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176 if (data_channel_type_ == DCT_SCTP) {
2177 // TODO(pthatcher): Do this in a more robust way by checking for
2178 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002179 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180 } else if (data_channel_type_ == DCT_RTP) {
2181 return BaseChannel::WantsPacket(rtcp, packet);
2182 }
2183 return false;
2184}
2185
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002186bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2187 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 // It hasn't been set before, so set it now.
2189 if (data_channel_type_ == DCT_NONE) {
2190 data_channel_type_ = new_data_channel_type;
2191 return true;
2192 }
2193
2194 // It's been set before, but doesn't match. That's bad.
2195 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002196 std::ostringstream desc;
2197 desc << "Data channel type mismatch."
2198 << " Expected " << data_channel_type_
2199 << " Got " << new_data_channel_type;
2200 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201 return false;
2202 }
2203
2204 // It's hasn't changed. Nothing to do.
2205 return true;
2206}
2207
2208bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002209 const DataContentDescription* content,
2210 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2212 (content->protocol() == kMediaProtocolDtlsSctp));
2213 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002214 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215}
2216
2217bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002218 ContentAction action,
2219 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002220 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002221 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222 LOG(LS_INFO) << "Setting local data description";
2223
2224 const DataContentDescription* data =
2225 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002226 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002227 if (!data) {
2228 SafeSetError("Can't find data content in local description.", error_desc);
2229 return false;
2230 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002232 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233 return false;
2234 }
2235
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002236 if (data_channel_type_ == DCT_RTP) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002237 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002238 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002239 }
2240 }
2241
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002242 // FYI: We send the SCTP port number (not to be confused with the
2243 // underlying UDP port number) as a codec parameter. So even SCTP
2244 // data channels need codecs.
2245 DataRecvParameters recv_params = last_recv_params_;
2246 RtpParametersFromMediaDescription(data, &recv_params);
2247 if (!media_channel()->SetRecvParameters(recv_params)) {
2248 SafeSetError("Failed to set remote data description recv parameters.",
2249 error_desc);
2250 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002252 if (data_channel_type_ == DCT_RTP) {
2253 for (const DataCodec& codec : data->codecs()) {
2254 bundle_filter()->AddPayloadType(codec.id);
2255 }
2256 }
2257 last_recv_params_ = recv_params;
2258
2259 // TODO(pthatcher): Move local streams into DataSendParameters, and
2260 // only give it to the media channel once we have a remote
2261 // description too (without a remote description, we won't be able
2262 // to send them anyway).
2263 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2264 SafeSetError("Failed to set local data description streams.", error_desc);
2265 return false;
2266 }
2267
2268 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002269 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002270 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271}
2272
2273bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002274 ContentAction action,
2275 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002276 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002277 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002278
2279 const DataContentDescription* data =
2280 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002281 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002282 if (!data) {
2283 SafeSetError("Can't find data content in remote description.", error_desc);
2284 return false;
2285 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002286
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002287 // If the remote data doesn't have codecs and isn't an update, it
2288 // must be empty, so ignore it.
2289 if (!data->has_codecs() && action != CA_UPDATE) {
2290 return true;
2291 }
2292
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002293 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002294 return false;
2295 }
2296
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002297 LOG(LS_INFO) << "Setting remote data description";
2298 if (data_channel_type_ == DCT_RTP &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002299 !SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002300 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301 }
2302
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002303
2304 DataSendParameters send_params = last_send_params_;
2305 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2306 if (!media_channel()->SetSendParameters(send_params)) {
2307 SafeSetError("Failed to set remote data description send parameters.",
2308 error_desc);
2309 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002311 last_send_params_ = send_params;
2312
2313 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2314 // and only give it to the media channel once we have a local
2315 // description too (without a local description, we won't be able to
2316 // recv them anyway).
2317 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2318 SafeSetError("Failed to set remote data description streams.",
2319 error_desc);
2320 return false;
2321 }
2322
2323 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002324 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002325 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002326}
2327
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002328void DataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 // Render incoming data if we're the active call, and we have the local
2330 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002331 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332 if (!media_channel()->SetReceive(recv)) {
2333 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2334 }
2335
2336 // Send outgoing data if we're the active call, we have the remote content,
2337 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002338 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339 if (!media_channel()->SetSend(send)) {
2340 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2341 }
2342
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002343 // Trigger SignalReadyToSendData asynchronously.
2344 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002345
2346 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2347}
2348
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002349void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350 switch (pmsg->message_id) {
2351 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002352 DataChannelReadyToSendMessageData* data =
2353 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002354 ready_to_send_data_ = data->data();
2355 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356 delete data;
2357 break;
2358 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002359 case MSG_DATARECEIVED: {
2360 DataReceivedMessageData* data =
2361 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2362 SignalDataReceived(this, data->params, data->payload);
2363 delete data;
2364 break;
2365 }
2366 case MSG_CHANNEL_ERROR: {
2367 const DataChannelErrorMessageData* data =
2368 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369 delete data;
2370 break;
2371 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002372 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002373 rtc::TypedMessageData<uint32_t>* data =
2374 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002375 SignalStreamClosedRemotely(data->data());
2376 delete data;
2377 break;
2378 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002379 default:
2380 BaseChannel::OnMessage(pmsg);
2381 break;
2382 }
2383}
2384
2385void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002386 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002387 SignalConnectionMonitor(this, infos);
2388}
2389
2390void DataChannel::StartMediaMonitor(int cms) {
2391 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002392 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393 media_monitor_->SignalUpdate.connect(
2394 this, &DataChannel::OnMediaMonitorUpdate);
2395 media_monitor_->Start(cms);
2396}
2397
2398void DataChannel::StopMediaMonitor() {
2399 if (media_monitor_) {
2400 media_monitor_->Stop();
2401 media_monitor_->SignalUpdate.disconnect(this);
2402 media_monitor_.reset();
2403 }
2404}
2405
2406void DataChannel::OnMediaMonitorUpdate(
2407 DataMediaChannel* media_channel, const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002408 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002409 SignalMediaMonitor(this, info);
2410}
2411
2412void DataChannel::OnDataReceived(
2413 const ReceiveDataParams& params, const char* data, size_t len) {
2414 DataReceivedMessageData* msg = new DataReceivedMessageData(
2415 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002416 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002417}
2418
Peter Boström0c4e06b2015-10-07 12:23:21 +02002419void DataChannel::OnDataChannelError(uint32_t ssrc,
2420 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002421 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2422 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002423 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002424}
2425
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002426void DataChannel::OnDataChannelReadyToSend(bool writable) {
2427 // This is usded for congestion control to indicate that the stream is ready
2428 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2429 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002430 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002431 new DataChannelReadyToSendMessageData(writable));
2432}
2433
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002434void DataChannel::GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002435 GetSupportedDataCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002436}
2437
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002438bool DataChannel::ShouldSetupDtlsSrtp_n() const {
2439 return data_channel_type_ == DCT_RTP && BaseChannel::ShouldSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002440}
2441
Peter Boström0c4e06b2015-10-07 12:23:21 +02002442void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2443 rtc::TypedMessageData<uint32_t>* message =
2444 new rtc::TypedMessageData<uint32_t>(sid);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002445 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_STREAMCLOSEDREMOTELY,
2446 message);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002447}
2448
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002449} // namespace cricket