blob: a34e9434ebaf3afc66aa6cc1691424ffcc0cb0dc [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
kjellandera69d9732016-08-31 07:33:05 -070015#include "webrtc/api/call/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"
johand89ab142016-10-25 10:50:32 -070027#include "webrtc/p2p/base/packettransportinterface.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,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071};
72
73// Value specified in RFC 5764.
74static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
75
76static const int kAgcMinus10db = -10;
77
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000078static void SafeSetError(const std::string& message, std::string* error_desc) {
79 if (error_desc) {
80 *error_desc = message;
81 }
82}
83
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 : ssrc(in_ssrc), error(in_error) {}
88 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 VoiceMediaChannel::Error error;
90};
91
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000092struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020093 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020095 : ssrc(in_ssrc), error(in_error) {}
96 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 VideoMediaChannel::Error error;
98};
99
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000100struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 : ssrc(in_ssrc), error(in_error) {}
104 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 DataMediaChannel::Error error;
106};
107
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108static const char* PacketType(bool rtcp) {
109 return (!rtcp) ? "RTP" : "RTCP";
110}
111
jbaucheec21bd2016-03-20 06:15:43 -0700112static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 // Check the packet size. We could check the header too if needed.
114 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000115 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
116 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117}
118
119static bool IsReceiveContentDirection(MediaContentDirection direction) {
120 return direction == MD_SENDRECV || direction == MD_RECVONLY;
121}
122
123static bool IsSendContentDirection(MediaContentDirection direction) {
124 return direction == MD_SENDRECV || direction == MD_SENDONLY;
125}
126
127static const MediaContentDescription* GetContentDescription(
128 const ContentInfo* cinfo) {
129 if (cinfo == NULL)
130 return NULL;
131 return static_cast<const MediaContentDescription*>(cinfo->description);
132}
133
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700134template <class Codec>
135void RtpParametersFromMediaDescription(
136 const MediaContentDescriptionImpl<Codec>* desc,
137 RtpParameters<Codec>* params) {
138 // TODO(pthatcher): Remove this once we're sure no one will give us
139 // a description without codecs (currently a CA_UPDATE with just
140 // streams can).
141 if (desc->has_codecs()) {
142 params->codecs = desc->codecs();
143 }
144 // TODO(pthatcher): See if we really need
145 // rtp_header_extensions_set() and remove it if we don't.
146 if (desc->rtp_header_extensions_set()) {
147 params->extensions = desc->rtp_header_extensions();
148 }
deadbeef13871492015-12-09 12:37:51 -0800149 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700150}
151
nisse05103312016-03-16 02:22:50 -0700152template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700153void RtpSendParametersFromMediaDescription(
154 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700155 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700156 RtpParametersFromMediaDescription(desc, send_params);
157 send_params->max_bandwidth_bps = desc->bandwidth();
158}
159
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200160BaseChannel::BaseChannel(rtc::Thread* worker_thread,
161 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800162 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700163 MediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700164 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800165 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800166 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200167 : worker_thread_(worker_thread),
168 network_thread_(network_thread),
zhihuangf5b251b2017-01-12 19:37:48 -0800169 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 content_name_(content_name),
deadbeefac22f702017-01-12 21:59:29 -0800171 rtcp_mux_required_(rtcp_mux_required),
deadbeef7af91dd2016-12-13 11:29:11 -0800172 srtp_required_(srtp_required),
michaelt79e05882016-11-08 02:50:09 -0800173 media_channel_(media_channel),
174 selected_candidate_pair_(nullptr) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700175 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 LOG(LS_INFO) << "Created channel for " << content_name;
177}
178
179BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800180 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700181 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000182 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200184 // Eats any outstanding messages or packets.
185 worker_thread_->Clear(&invoker_);
186 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 // We must destroy the media channel before the transport channel, otherwise
188 // the media channel may try to send on the dead transport channel. NULLing
189 // is not an effective strategy since the sends will come on another thread.
190 delete media_channel_;
zhihuangf5b251b2017-01-12 19:37:48 -0800191 LOG(LS_INFO) << "Destroyed channel: " << content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200192}
193
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200194void BaseChannel::DisconnectTransportChannels_n() {
195 // Send any outstanding RTCP packets.
196 FlushRtcpMessages_n();
197
198 // Stop signals from transport channels, but keep them alive because
199 // media_channel may use them from a different thread.
zhihuangb2cdd932017-01-19 16:54:25 -0800200 if (rtp_dtls_transport_) {
201 DisconnectFromTransport(rtp_dtls_transport_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200202 }
zhihuangb2cdd932017-01-19 16:54:25 -0800203 if (rtcp_dtls_transport_) {
204 DisconnectFromTransport(rtcp_dtls_transport_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200205 }
206
207 // Clear pending read packets/messages.
208 network_thread_->Clear(&invoker_);
209 network_thread_->Clear(this);
210}
211
zhihuangb2cdd932017-01-19 16:54:25 -0800212bool BaseChannel::Init_w(DtlsTransportInternal* rtp_dtls_transport,
213 DtlsTransportInternal* rtcp_dtls_transport) {
skvlad6c87a672016-05-17 17:49:52 -0700214 if (!network_thread_->Invoke<bool>(
zhihuangb2cdd932017-01-19 16:54:25 -0800215 RTC_FROM_HERE, Bind(&BaseChannel::InitNetwork_n, this,
216 rtp_dtls_transport, rtcp_dtls_transport))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000217 return false;
218 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219
wu@webrtc.orgde305012013-10-31 15:40:38 +0000220 // Both RTP and RTCP channels are set, we can call SetInterface on
221 // media channel and it can set network options.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200222 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.orgde305012013-10-31 15:40:38 +0000223 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 return true;
225}
226
zhihuangb2cdd932017-01-19 16:54:25 -0800227bool BaseChannel::InitNetwork_n(DtlsTransportInternal* rtp_dtls_transport,
228 DtlsTransportInternal* rtcp_dtls_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200229 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800230 SetTransports_n(rtp_dtls_transport, rtcp_dtls_transport);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200231
zhihuangb2cdd932017-01-19 16:54:25 -0800232 if (!SetDtlsSrtpCryptoSuites_n(rtp_dtls_transport_, false)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200233 return false;
234 }
zhihuangb2cdd932017-01-19 16:54:25 -0800235 if (rtcp_dtls_transport_ &&
236 !SetDtlsSrtpCryptoSuites_n(rtcp_dtls_transport_, true)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200237 return false;
238 }
deadbeefac22f702017-01-12 21:59:29 -0800239 if (rtcp_mux_required_) {
240 rtcp_mux_filter_.SetActive();
241 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200242 return true;
243}
244
wu@webrtc.org78187522013-10-07 23:32:02 +0000245void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200246 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000247 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200248 // Packets arrive on the network thread, processing packets calls virtual
249 // functions, so need to stop this process in Deinit that is called in
250 // derived classes destructor.
251 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700252 RTC_FROM_HERE, Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000253}
254
zhihuangb2cdd932017-01-19 16:54:25 -0800255void BaseChannel::SetTransports(DtlsTransportInternal* rtp_dtls_transport,
256 DtlsTransportInternal* rtcp_dtls_transport) {
257 network_thread_->Invoke<void>(RTC_FROM_HERE,
258 Bind(&BaseChannel::SetTransports_n, this,
259 rtp_dtls_transport, rtcp_dtls_transport));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000260}
261
zhihuangb2cdd932017-01-19 16:54:25 -0800262void BaseChannel::SetTransports_n(DtlsTransportInternal* rtp_dtls_transport,
263 DtlsTransportInternal* rtcp_dtls_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200264 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800265 if (!rtp_dtls_transport && !rtcp_dtls_transport) {
266 LOG(LS_ERROR) << "Setting nullptr to RTP Transport and RTCP Transport.";
267 return;
zhihuangf5b251b2017-01-12 19:37:48 -0800268 }
269
zhihuangb2cdd932017-01-19 16:54:25 -0800270 if (rtp_dtls_transport && rtcp_dtls_transport) {
271 RTC_DCHECK(rtp_dtls_transport->transport_name() ==
272 rtcp_dtls_transport->transport_name());
273 RTC_DCHECK(NeedsRtcpTransport());
274 }
275
276 std::string transport_name = rtp_dtls_transport
277 ? rtp_dtls_transport->transport_name()
278 : rtcp_dtls_transport->transport_name();
279 if (transport_name == transport_name_) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700280 // Nothing to do if transport name isn't changing.
deadbeefbad5dad2017-01-17 18:32:35 -0800281 return;
deadbeefcbecd352015-09-23 11:50:27 -0700282 }
283
zhihuangb2cdd932017-01-19 16:54:25 -0800284 transport_name_ = transport_name;
zhihuangf5b251b2017-01-12 19:37:48 -0800285
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800286 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
287 // changes and wait until the DTLS handshake is complete to set the newly
288 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200289 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800290 // Set |writable_| to false such that UpdateWritableState_w can set up
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700291 // DTLS-SRTP when |writable_| becomes true again.
guoweis46383312015-12-17 16:45:59 -0800292 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800293 srtp_filter_.ResetParams();
294 }
295
deadbeefac22f702017-01-12 21:59:29 -0800296 // If this BaseChannel doesn't require RTCP mux and we haven't fully
297 // negotiated RTCP mux, we need an RTCP transport.
zhihuangf5b251b2017-01-12 19:37:48 -0800298 if (NeedsRtcpTransport()) {
299 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on "
zhihuangb2cdd932017-01-19 16:54:25 -0800300 << transport_name << " transport " << rtcp_dtls_transport;
301 SetTransport_n(true, rtcp_dtls_transport);
302 RTC_DCHECK(rtcp_dtls_transport_);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000303 }
304
zhihuangf5b251b2017-01-12 19:37:48 -0800305 LOG(LS_INFO) << "Setting non-RTCP Transport for " << content_name() << " on "
zhihuangb2cdd932017-01-19 16:54:25 -0800306 << transport_name << " transport " << rtp_dtls_transport;
307 SetTransport_n(false, rtp_dtls_transport);
308 RTC_DCHECK(rtp_dtls_transport_);
guoweis46383312015-12-17 16:45:59 -0800309
deadbeefcbecd352015-09-23 11:50:27 -0700310 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700311 // setting new transport channels.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200312 UpdateWritableState_n();
deadbeef062ce9f2016-08-26 21:42:15 -0700313 // We can only update ready-to-send after updating writability.
314 //
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700315 // On setting a new channel, assume it's ready to send if it's writable,
316 // because we have no way of knowing otherwise (the channel doesn't give us
317 // "was last send successful?").
318 //
319 // This won't always be accurate (the last SendPacket call from another
320 // BaseChannel could have resulted in an error), but even so, we'll just
321 // encounter the error again and update "ready to send" accordingly.
deadbeef062ce9f2016-08-26 21:42:15 -0700322 SetTransportChannelReadyToSend(
zhihuangb2cdd932017-01-19 16:54:25 -0800323 false, rtp_dtls_transport_ && rtp_dtls_transport_->writable());
324 SetTransportChannelReadyToSend(
325 true, rtcp_dtls_transport_ && rtcp_dtls_transport_->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000326}
327
zhihuangb2cdd932017-01-19 16:54:25 -0800328void BaseChannel::SetTransport_n(bool rtcp,
329 DtlsTransportInternal* new_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200330 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800331 DtlsTransportInternal*& old_transport =
332 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
333
zhihuangf5b251b2017-01-12 19:37:48 -0800334 if (!old_transport && !new_transport) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700335 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000336 return;
337 }
zhihuangb2cdd932017-01-19 16:54:25 -0800338
zhihuangf5b251b2017-01-12 19:37:48 -0800339 RTC_DCHECK(old_transport != new_transport);
340 if (old_transport) {
zhihuangb2cdd932017-01-19 16:54:25 -0800341 DisconnectFromTransport(old_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000342 }
343
zhihuangf5b251b2017-01-12 19:37:48 -0800344 old_transport = new_transport;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000345
zhihuangf5b251b2017-01-12 19:37:48 -0800346 if (new_transport) {
deadbeef062ce9f2016-08-26 21:42:15 -0700347 if (rtcp) {
348 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
349 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
350 << "should never happen.";
deadbeefcbecd352015-09-23 11:50:27 -0700351 }
zhihuangb2cdd932017-01-19 16:54:25 -0800352 ConnectToTransport(new_transport);
deadbeef062ce9f2016-08-26 21:42:15 -0700353 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
354 for (const auto& pair : socket_options) {
zhihuangf5b251b2017-01-12 19:37:48 -0800355 new_transport->SetOption(pair.first, pair.second);
deadbeef062ce9f2016-08-26 21:42:15 -0700356 }
guoweis46383312015-12-17 16:45:59 -0800357 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000358}
359
zhihuangb2cdd932017-01-19 16:54:25 -0800360void BaseChannel::ConnectToTransport(DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200361 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000362
zhihuangb2cdd932017-01-19 16:54:25 -0800363 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
364 transport->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
365 transport->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
366 transport->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
367 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
368 transport->ice_transport()->SignalSelectedCandidatePairChanged.connect(
Honghai Zhangcc411c02016-03-29 17:27:21 -0700369 this, &BaseChannel::OnSelectedCandidatePairChanged);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000370}
371
zhihuangb2cdd932017-01-19 16:54:25 -0800372void BaseChannel::DisconnectFromTransport(DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200373 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800374 OnSelectedCandidatePairChanged(transport->ice_transport(), nullptr, -1,
375 false);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000376
zhihuangb2cdd932017-01-19 16:54:25 -0800377 transport->SignalWritableState.disconnect(this);
378 transport->SignalReadPacket.disconnect(this);
379 transport->SignalReadyToSend.disconnect(this);
380 transport->SignalDtlsState.disconnect(this);
381 transport->SignalSentPacket.disconnect(this);
382 transport->ice_transport()->SignalSelectedCandidatePairChanged.disconnect(
383 this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000384}
385
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700387 worker_thread_->Invoke<void>(
388 RTC_FROM_HERE,
389 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
390 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391 return true;
392}
393
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394bool BaseChannel::AddRecvStream(const StreamParams& sp) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700395 return InvokeOnWorker(RTC_FROM_HERE,
396 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397}
398
Peter Boström0c4e06b2015-10-07 12:23:21 +0200399bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700400 return InvokeOnWorker(RTC_FROM_HERE,
401 Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402}
403
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000404bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000405 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700406 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000407}
408
Peter Boström0c4e06b2015-10-07 12:23:21 +0200409bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700410 return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream,
411 media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000412}
413
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000415 ContentAction action,
416 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100417 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700418 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w,
419 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420}
421
422bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000423 ContentAction action,
424 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100425 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700426 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w,
427 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428}
429
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430void BaseChannel::StartConnectionMonitor(int cms) {
zhihuangb2cdd932017-01-19 16:54:25 -0800431 // We pass in the BaseChannel instead of the rtp_dtls_transport_
432 // because if the rtp_dtls_transport_ changes, the ConnectionMonitor
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000433 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200434 // We pass in the network thread because on that thread connection monitor
435 // will call BaseChannel::GetConnectionStats which must be called on the
436 // network thread.
437 connection_monitor_.reset(
438 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000439 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000441 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442}
443
444void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000445 if (connection_monitor_) {
446 connection_monitor_->Stop();
447 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 }
449}
450
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000451bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200452 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800453 return rtp_dtls_transport_->ice_transport()->GetStats(infos);
zhihuangf5b251b2017-01-12 19:37:48 -0800454}
455
456bool BaseChannel::NeedsRtcpTransport() {
deadbeefac22f702017-01-12 21:59:29 -0800457 // If this BaseChannel doesn't require RTCP mux and we haven't fully
458 // negotiated RTCP mux, we need an RTCP transport.
459 return !rtcp_mux_required_ && !rtcp_mux_filter_.IsFullyActive();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000460}
461
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700462bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 // Receive data if we are enabled and have local content,
464 return enabled() && IsReceiveContentDirection(local_content_direction_);
465}
466
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700467bool BaseChannel::IsReadyToSendMedia_w() const {
468 // Need to access some state updated on the network thread.
469 return network_thread_->Invoke<bool>(
470 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
471}
472
473bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 // Send outgoing data if we are enabled, have local and remote content,
475 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800476 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700478 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200479 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480}
481
jbaucheec21bd2016-03-20 06:15:43 -0700482bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700483 const rtc::PacketOptions& options) {
484 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485}
486
jbaucheec21bd2016-03-20 06:15:43 -0700487bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700488 const rtc::PacketOptions& options) {
489 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490}
491
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000492int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200494 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700495 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200496}
497
498int BaseChannel::SetOption_n(SocketType type,
499 rtc::Socket::Option opt,
500 int value) {
501 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800502 DtlsTransportInternal* transport = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000504 case ST_RTP:
zhihuangb2cdd932017-01-19 16:54:25 -0800505 transport = rtp_dtls_transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700506 socket_options_.push_back(
507 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000508 break;
509 case ST_RTCP:
zhihuangb2cdd932017-01-19 16:54:25 -0800510 transport = rtcp_dtls_transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700511 rtcp_socket_options_.push_back(
512 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000513 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 }
zhihuangb2cdd932017-01-19 16:54:25 -0800515 return transport ? transport->ice_transport()->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516}
517
jbauchcb560652016-08-04 05:20:32 -0700518bool BaseChannel::SetCryptoOptions(const rtc::CryptoOptions& crypto_options) {
519 crypto_options_ = crypto_options;
520 return true;
521}
522
johand89ab142016-10-25 10:50:32 -0700523void BaseChannel::OnWritableState(rtc::PacketTransportInterface* transport) {
zhihuangb2cdd932017-01-19 16:54:25 -0800524 RTC_DCHECK(transport == rtp_dtls_transport_ ||
525 transport == rtcp_dtls_transport_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200526 RTC_DCHECK(network_thread_->IsCurrent());
527 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528}
529
johand89ab142016-10-25 10:50:32 -0700530void BaseChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
531 const char* data,
532 size_t len,
533 const rtc::PacketTime& packet_time,
534 int flags) {
535 TRACE_EVENT0("webrtc", "BaseChannel::OnPacketRead");
536 // OnPacketRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200537 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538
539 // When using RTCP multiplexing we might get RTCP packets on the RTP
540 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
johand89ab142016-10-25 10:50:32 -0700541 bool rtcp = PacketIsRtcp(transport, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700542 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000543 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544}
545
johand89ab142016-10-25 10:50:32 -0700546void BaseChannel::OnReadyToSend(rtc::PacketTransportInterface* transport) {
zhihuangb2cdd932017-01-19 16:54:25 -0800547 RTC_DCHECK(transport == rtp_dtls_transport_ ||
548 transport == rtcp_dtls_transport_);
549 SetTransportChannelReadyToSend(transport == rtcp_dtls_transport_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550}
551
zhihuangb2cdd932017-01-19 16:54:25 -0800552void BaseChannel::OnDtlsState(DtlsTransportInternal* transport,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800553 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200554 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800555 return;
556 }
557
558 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
559 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
zhihuangb2cdd932017-01-19 16:54:25 -0800560 // cover other scenarios like the whole transport is writable (not just this
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800561 // TransportChannel) or when TransportChannel is attached after DTLS is
562 // negotiated.
563 if (state != DTLS_TRANSPORT_CONNECTED) {
564 srtp_filter_.ResetParams();
565 }
566}
567
Honghai Zhangcc411c02016-03-29 17:27:21 -0700568void BaseChannel::OnSelectedCandidatePairChanged(
zhihuangb2cdd932017-01-19 16:54:25 -0800569 IceTransportInternal* ice_transport,
Honghai Zhang52dce732016-03-31 12:37:31 -0700570 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700571 int last_sent_packet_id,
572 bool ready_to_send) {
zhihuangb2cdd932017-01-19 16:54:25 -0800573 RTC_DCHECK(ice_transport == rtp_dtls_transport_->ice_transport() ||
574 ice_transport == rtcp_dtls_transport_->ice_transport());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200575 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800576 selected_candidate_pair_ = selected_candidate_pair;
zhihuangb2cdd932017-01-19 16:54:25 -0800577 std::string transport_name = ice_transport->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700578 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700579 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700580 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700581 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700582 selected_candidate_pair->remote_candidate().network_id(),
583 last_sent_packet_id);
michaelt79e05882016-11-08 02:50:09 -0800584
585 UpdateTransportOverhead();
Honghai Zhangcc411c02016-03-29 17:27:21 -0700586 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200587 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700588 RTC_FROM_HERE, worker_thread_,
589 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
590 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700591}
592
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700593void BaseChannel::SetTransportChannelReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200594 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700595 if (rtcp) {
596 rtcp_ready_to_send_ = ready;
597 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000598 rtp_ready_to_send_ = ready;
599 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200601 bool ready_to_send =
602 (rtp_ready_to_send_ &&
zhihuangb2cdd932017-01-19 16:54:25 -0800603 // In the case of rtcp mux |rtcp_dtls_transport_| will be null.
604 (rtcp_ready_to_send_ || !rtcp_dtls_transport_));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200605
606 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700607 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200608 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609}
610
johand89ab142016-10-25 10:50:32 -0700611bool BaseChannel::PacketIsRtcp(const rtc::PacketTransportInterface* transport,
612 const char* data,
613 size_t len) {
zhihuangb2cdd932017-01-19 16:54:25 -0800614 return (transport == rtcp_dtls_transport_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000615 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616}
617
stefanc1aeaf02015-10-15 07:26:07 -0700618bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700619 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700620 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200621 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
622 // If the thread is not our network thread, we will post to our network
623 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 // synchronize access to all the pieces of the send path, including
625 // SRTP and the inner workings of the transport channels.
626 // The only downside is that we can't return a proper failure code if
627 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200628 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200630 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
631 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800632 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700633 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700634 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 return true;
636 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200637 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638
639 // Now that we are on the correct thread, ensure we have a place to send this
640 // packet before doing anything. (We might get RTCP packets that we don't
641 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
642 // transport.
zhihuangb2cdd932017-01-19 16:54:25 -0800643 DtlsTransportInternal* transport = (!rtcp || rtcp_mux_filter_.IsActive())
644 ? rtp_dtls_transport_
645 : rtcp_dtls_transport_;
646 if (!transport || !transport->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 return false;
648 }
649
650 // Protect ourselves against crazy data.
651 if (!ValidPacket(rtcp, packet)) {
652 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000653 << PacketType(rtcp)
654 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 return false;
656 }
657
stefanc1aeaf02015-10-15 07:26:07 -0700658 rtc::PacketOptions updated_options;
659 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 // Protect if needed.
661 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200662 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200664 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000665 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000667 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
668 // inside libsrtp for a RTP packet. A external HMAC module will be writing
669 // a fake HMAC value. This is ONLY done for a RTP packet.
670 // Socket layer will update rtp sendtime extension header if present in
671 // packet with current time before updating the HMAC.
672#if !defined(ENABLE_EXTERNAL_AUTH)
673 res = srtp_filter_.ProtectRtp(
674 data, len, static_cast<int>(packet->capacity()), &len);
675#else
stefanc1aeaf02015-10-15 07:26:07 -0700676 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000677 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000678 res = srtp_filter_.ProtectRtp(
679 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700680 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000681 // If protection succeeds, let's get auth params from srtp.
682 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200683 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000684 int key_len;
685 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700686 &auth_key, &key_len,
687 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000688 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700689 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
690 updated_options.packet_time_params.srtp_auth_key.assign(
691 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000692 }
693 }
694#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 if (!res) {
696 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200697 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 GetRtpSeqNum(data, len, &seq_num);
699 GetRtpSsrc(data, len, &ssrc);
700 LOG(LS_ERROR) << "Failed to protect " << content_name_
701 << " RTP packet: size=" << len
702 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
703 return false;
704 }
705 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000706 res = srtp_filter_.ProtectRtcp(data, len,
707 static_cast<int>(packet->capacity()),
708 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 if (!res) {
710 int type = -1;
711 GetRtcpType(data, len, &type);
712 LOG(LS_ERROR) << "Failed to protect " << content_name_
713 << " RTCP packet: size=" << len << ", type=" << type;
714 return false;
715 }
716 }
717
718 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000719 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800720 } else if (srtp_required_) {
deadbeef8f425f92016-12-01 12:26:27 -0800721 // The audio/video engines may attempt to send RTCP packets as soon as the
722 // streams are created, so don't treat this as an error for RTCP.
723 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809
724 if (rtcp) {
725 return false;
726 }
727 // However, there shouldn't be any RTP packets sent before SRTP is set up
728 // (and SetSend(true) is called).
729 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
730 << " and crypto is required";
nisseeb4ca4e2017-01-12 02:24:27 -0800731 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 return false;
733 }
734
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200736 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
zhihuangb2cdd932017-01-19 16:54:25 -0800737 int ret = transport->SendPacket(packet->data<char>(), packet->size(),
738 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000739 if (ret != static_cast<int>(packet->size())) {
zhihuangb2cdd932017-01-19 16:54:25 -0800740 if (transport->GetError() == ENOTCONN) {
skvladc309e0e2016-07-28 17:15:20 -0700741 LOG(LS_WARNING) << "Got ENOTCONN from transport.";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700742 SetTransportChannelReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 }
744 return false;
745 }
746 return true;
747}
748
jbaucheec21bd2016-03-20 06:15:43 -0700749bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 // Protect ourselves against crazy data.
751 if (!ValidPacket(rtcp, packet)) {
752 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000753 << PacketType(rtcp)
754 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 return false;
756 }
pbos482b12e2015-11-16 10:19:58 -0800757 if (rtcp) {
758 // Permit all (seemingly valid) RTCP packets.
759 return true;
760 }
761 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700762 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763}
764
jbaucheec21bd2016-03-20 06:15:43 -0700765void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000766 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200767 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 if (!WantsPacket(rtcp, packet)) {
769 return;
770 }
771
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000772 // We are only interested in the first rtp packet because that
773 // indicates the media has started flowing.
774 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700776 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 }
778
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 // Unprotect the packet, if needed.
780 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200781 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200782 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000783 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 bool res;
785 if (!rtcp) {
786 res = srtp_filter_.UnprotectRtp(data, len, &len);
787 if (!res) {
788 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200789 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 GetRtpSeqNum(data, len, &seq_num);
791 GetRtpSsrc(data, len, &ssrc);
792 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
793 << " RTP packet: size=" << len
794 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
795 return;
796 }
797 } else {
798 res = srtp_filter_.UnprotectRtcp(data, len, &len);
799 if (!res) {
800 int type = -1;
801 GetRtcpType(data, len, &type);
802 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
803 << " RTCP packet: size=" << len << ", type=" << type;
804 return;
805 }
806 }
807
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000808 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800809 } else if (srtp_required_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 // Our session description indicates that SRTP is required, but we got a
811 // packet before our SRTP filter is active. This means either that
812 // a) we got SRTP packets before we received the SDES keys, in which case
813 // we can't decrypt it anyway, or
814 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
zhihuangb2cdd932017-01-19 16:54:25 -0800815 // transports, so we haven't yet extracted keys, even if DTLS did
816 // complete on the transport that the packets are being sent on. It's
817 // really good practice to wait for both RTP and RTCP to be good to go
818 // before sending media, to prevent weird failure modes, so it's fine
819 // for us to just eat packets here. This is all sidestepped if RTCP mux
820 // is used anyway.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000821 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
822 << " packet when SRTP is inactive and crypto is required";
823 return;
824 }
825
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200826 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700827 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200828 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
829}
830
831void BaseChannel::OnPacketReceived(bool rtcp,
832 const rtc::CopyOnWriteBuffer& packet,
833 const rtc::PacketTime& packet_time) {
834 RTC_DCHECK(worker_thread_->IsCurrent());
835 // Need to copy variable because OnRtcpReceived/OnPacketReceived
836 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
837 rtc::CopyOnWriteBuffer data(packet);
838 if (rtcp) {
839 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200841 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 }
843}
844
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000845bool BaseChannel::PushdownLocalDescription(
846 const SessionDescription* local_desc, ContentAction action,
847 std::string* error_desc) {
848 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 const MediaContentDescription* content_desc =
850 GetContentDescription(content_info);
851 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000852 !SetLocalContent(content_desc, action, error_desc)) {
853 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
854 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000856 return true;
857}
858
859bool BaseChannel::PushdownRemoteDescription(
860 const SessionDescription* remote_desc, ContentAction action,
861 std::string* error_desc) {
862 const ContentInfo* content_info = GetFirstContent(remote_desc);
863 const MediaContentDescription* content_desc =
864 GetContentDescription(content_info);
865 if (content_desc && content_info && !content_info->rejected &&
866 !SetRemoteContent(content_desc, action, error_desc)) {
867 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
868 return false;
869 }
870 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871}
872
873void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700874 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 if (enabled_)
876 return;
877
878 LOG(LS_INFO) << "Channel enabled";
879 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700880 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000881}
882
883void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700884 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 if (!enabled_)
886 return;
887
888 LOG(LS_INFO) << "Channel disabled";
889 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700890 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891}
892
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200893void BaseChannel::UpdateWritableState_n() {
zhihuangb2cdd932017-01-19 16:54:25 -0800894 if (rtp_dtls_transport_ && rtp_dtls_transport_->writable() &&
895 (!rtcp_dtls_transport_ || rtcp_dtls_transport_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200896 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700897 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200898 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700899 }
900}
901
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200902void BaseChannel::ChannelWritable_n() {
903 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800904 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800906 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907
deadbeefcbecd352015-09-23 11:50:27 -0700908 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 << (was_ever_writable_ ? "" : " for the first time");
910
michaelt79e05882016-11-08 02:50:09 -0800911 if (selected_candidate_pair_)
912 LOG(LS_INFO)
913 << "Using "
914 << selected_candidate_pair_->local_candidate().ToSensitiveString()
915 << "->"
916 << selected_candidate_pair_->remote_candidate().ToSensitiveString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200919 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700921 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922}
923
deadbeef953c2ce2017-01-09 14:53:41 -0800924void BaseChannel::SignalDtlsSrtpSetupFailure_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200925 RTC_DCHECK(network_thread_->IsCurrent());
926 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700927 RTC_FROM_HERE, signaling_thread(),
deadbeef953c2ce2017-01-09 14:53:41 -0800928 Bind(&BaseChannel::SignalDtlsSrtpSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000929}
930
deadbeef953c2ce2017-01-09 14:53:41 -0800931void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700932 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
deadbeef953c2ce2017-01-09 14:53:41 -0800933 SignalDtlsSrtpSetupFailure(this, rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000934}
935
zhihuangb2cdd932017-01-19 16:54:25 -0800936bool BaseChannel::SetDtlsSrtpCryptoSuites_n(DtlsTransportInternal* transport,
937 bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800938 std::vector<int> crypto_suites;
939 // We always use the default SRTP crypto suites for RTCP, but we may use
940 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200942 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 } else {
jbauchcb560652016-08-04 05:20:32 -0700944 GetDefaultSrtpCryptoSuites(crypto_options(), &crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 }
zhihuangb2cdd932017-01-19 16:54:25 -0800946 return transport->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947}
948
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200949bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
zhihuangb2cdd932017-01-19 16:54:25 -0800950 // Since DTLS is applied to all transports, checking RTP should be enough.
951 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952}
953
954// This function returns true if either DTLS-SRTP is not in use
955// *or* DTLS-SRTP is successfully set up.
zhihuangb2cdd932017-01-19 16:54:25 -0800956bool BaseChannel::SetupDtlsSrtp_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200957 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 bool ret = false;
959
zhihuangb2cdd932017-01-19 16:54:25 -0800960 DtlsTransportInternal* transport =
961 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962
zhihuangb2cdd932017-01-19 16:54:25 -0800963 RTC_DCHECK(transport->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800965 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966
zhihuangb2cdd932017-01-19 16:54:25 -0800967 if (!transport->GetSrtpCryptoSuite(&selected_crypto_suite)) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800968 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 return false;
970 }
971
zhihuangb2cdd932017-01-19 16:54:25 -0800972 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " << content_name() << " "
973 << PacketType(rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974
jbauchcb560652016-08-04 05:20:32 -0700975 int key_len;
976 int salt_len;
977 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
978 &salt_len)) {
979 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
980 return false;
981 }
982
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -0700984 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985
986 // RFC 5705 exporter using the RFC 5764 parameters
zhihuangb2cdd932017-01-19 16:54:25 -0800987 if (!transport->ExportKeyingMaterial(kDtlsSrtpExporterLabel, NULL, 0, false,
988 &dtls_buffer[0], dtls_buffer.size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
nisseeb4ca4e2017-01-12 02:24:27 -0800990 RTC_NOTREACHED(); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 return false;
992 }
993
994 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -0700995 std::vector<unsigned char> client_write_key(key_len + salt_len);
996 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -0700998 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
999 offset += key_len;
1000 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
1001 offset += key_len;
1002 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
1003 offset += salt_len;
1004 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005
1006 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001007 rtc::SSLRole role;
zhihuangb2cdd932017-01-19 16:54:25 -08001008 if (!transport->GetSslRole(&role)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001009 LOG(LS_WARNING) << "GetSslRole failed";
1010 return false;
1011 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001013 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 send_key = &server_write_key;
1015 recv_key = &client_write_key;
1016 } else {
1017 send_key = &client_write_key;
1018 recv_key = &server_write_key;
1019 }
1020
zhihuangb2cdd932017-01-19 16:54:25 -08001021 if (rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001022 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1023 static_cast<int>(send_key->size()),
1024 selected_crypto_suite, &(*recv_key)[0],
1025 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001027 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1028 static_cast<int>(send_key->size()),
1029 selected_crypto_suite, &(*recv_key)[0],
1030 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 }
1032
michaelt79e05882016-11-08 02:50:09 -08001033 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
michaelt79e05882016-11-08 02:50:09 -08001035 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 dtls_keyed_ = true;
michaelt79e05882016-11-08 02:50:09 -08001037 UpdateTransportOverhead();
1038 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039 return ret;
1040}
1041
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001042void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001043 if (srtp_filter_.IsActive()) {
1044 return;
1045 }
1046
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001047 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001048 return;
1049 }
1050
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001051 if (!SetupDtlsSrtp_n(false)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001052 SignalDtlsSrtpSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001053 return;
1054 }
1055
zhihuangb2cdd932017-01-19 16:54:25 -08001056 if (rtcp_dtls_transport_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001057 if (!SetupDtlsSrtp_n(true)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001058 SignalDtlsSrtpSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001059 return;
1060 }
1061 }
1062}
1063
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001064void BaseChannel::ChannelNotWritable_n() {
1065 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 if (!writable_)
1067 return;
1068
deadbeefcbecd352015-09-23 11:50:27 -07001069 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001071 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072}
1073
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001074bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001075 const MediaContentDescription* content,
1076 ContentAction action,
1077 ContentSource src,
1078 std::string* error_desc) {
1079 if (action == CA_UPDATE) {
1080 // These parameters never get changed by a CA_UDPATE.
1081 return true;
1082 }
1083
deadbeef7af91dd2016-12-13 11:29:11 -08001084 // Cache srtp_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001085 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001086 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1087 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001088}
1089
1090bool BaseChannel::SetRtpTransportParameters_n(
1091 const MediaContentDescription* content,
1092 ContentAction action,
1093 ContentSource src,
1094 std::string* error_desc) {
1095 RTC_DCHECK(network_thread_->IsCurrent());
1096
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001097 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001098 return false;
1099 }
1100
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001101 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001102 return false;
1103 }
1104
1105 return true;
1106}
1107
zhihuangb2cdd932017-01-19 16:54:25 -08001108// |dtls| will be set to true if DTLS is active for transport and crypto is
1109// empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001110bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1111 bool* dtls,
1112 std::string* error_desc) {
zhihuangb2cdd932017-01-19 16:54:25 -08001113 *dtls = rtp_dtls_transport_->IsDtlsActive();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001114 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001115 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001116 return false;
1117 }
1118 return true;
1119}
1120
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001121bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001122 ContentAction action,
1123 ContentSource src,
1124 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001125 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001126 if (action == CA_UPDATE) {
1127 // no crypto params.
1128 return true;
1129 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001131 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001132 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001133 if (!ret) {
1134 return false;
1135 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 switch (action) {
1137 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001138 // If DTLS is already active on the channel, we could be renegotiating
1139 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001140 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001141 ret = srtp_filter_.SetOffer(cryptos, src);
1142 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143 break;
1144 case CA_PRANSWER:
1145 // If we're doing DTLS-SRTP, we don't want to update the filter
1146 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001147 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1149 }
1150 break;
1151 case CA_ANSWER:
1152 // If we're doing DTLS-SRTP, we don't want to update the filter
1153 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001154 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001155 ret = srtp_filter_.SetAnswer(cryptos, src);
1156 }
1157 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 default:
1159 break;
1160 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001161 if (!ret) {
1162 SafeSetError("Failed to setup SRTP filter.", error_desc);
1163 return false;
1164 }
1165 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166}
1167
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001168bool BaseChannel::SetRtcpMux_n(bool enable,
1169 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001170 ContentSource src,
1171 std::string* error_desc) {
deadbeef8e814d72017-01-13 11:34:39 -08001172 // Provide a more specific error message for the RTCP mux "require" policy
1173 // case.
1174 if (rtcp_mux_required_ && !enable) {
1175 SafeSetError(
1176 "rtcpMuxPolicy is 'require', but media description does not "
1177 "contain 'a=rtcp-mux'.",
1178 error_desc);
1179 return false;
1180 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 bool ret = false;
1182 switch (action) {
1183 case CA_OFFER:
1184 ret = rtcp_mux_filter_.SetOffer(enable, src);
1185 break;
1186 case CA_PRANSWER:
zhihuangb2cdd932017-01-19 16:54:25 -08001187 // This may activate RTCP muxing, but we don't yet destroy the transport
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001188 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1190 break;
1191 case CA_ANSWER:
1192 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1193 if (ret && rtcp_mux_filter_.IsActive()) {
1194 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001195 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
zhihuangb2cdd932017-01-19 16:54:25 -08001196 << " by destroying RTCP transport for "
deadbeefcbecd352015-09-23 11:50:27 -07001197 << transport_name();
zhihuangb2cdd932017-01-19 16:54:25 -08001198 if (rtcp_dtls_transport()) {
1199 SetTransport_n(true, nullptr);
1200 SignalRtcpMuxFullyActive(rtp_dtls_transport()->transport_name());
zhihuangf5b251b2017-01-12 19:37:48 -08001201 }
deadbeef062ce9f2016-08-26 21:42:15 -07001202 UpdateWritableState_n();
1203 SetTransportChannelReadyToSend(true, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 }
1205 break;
1206 case CA_UPDATE:
1207 // No RTCP mux info.
1208 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001209 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 default:
1211 break;
1212 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001213 if (!ret) {
1214 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1215 return false;
1216 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
zhihuangb2cdd932017-01-19 16:54:25 -08001218 // CA_ANSWER, but we only want to tear down the RTCP transport if we received
1219 // a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001220 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 // If the RTP transport is already writable, then so are we.
zhihuangb2cdd932017-01-19 16:54:25 -08001222 if (rtp_dtls_transport_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001223 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 }
1225 }
1226
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001227 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228}
1229
1230bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001231 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001232 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233}
1234
Peter Boström0c4e06b2015-10-07 12:23:21 +02001235bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001236 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 return media_channel()->RemoveRecvStream(ssrc);
1238}
1239
1240bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001241 ContentAction action,
1242 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1244 action == CA_PRANSWER || action == CA_UPDATE))
1245 return false;
1246
1247 // If this is an update, streams only contain streams that have changed.
1248 if (action == CA_UPDATE) {
1249 for (StreamParamsVec::const_iterator it = streams.begin();
1250 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001251 const StreamParams* existing_stream =
1252 GetStreamByIds(local_streams_, it->groupid, it->id);
1253 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 if (media_channel()->AddSendStream(*it)) {
1255 local_streams_.push_back(*it);
1256 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1257 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001258 std::ostringstream desc;
1259 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1260 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 return false;
1262 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001263 } else if (existing_stream && !it->has_ssrcs()) {
1264 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001265 std::ostringstream desc;
1266 desc << "Failed to remove send stream with ssrc "
1267 << it->first_ssrc() << ".";
1268 SafeSetError(desc.str(), error_desc);
1269 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001271 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 } else {
1273 LOG(LS_WARNING) << "Ignore unsupported stream update";
1274 }
1275 }
1276 return true;
1277 }
1278 // Else streams are all the streams we want to send.
1279
1280 // Check for streams that have been removed.
1281 bool ret = true;
1282 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1283 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001284 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001286 std::ostringstream desc;
1287 desc << "Failed to remove send stream with ssrc "
1288 << it->first_ssrc() << ".";
1289 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290 ret = false;
1291 }
1292 }
1293 }
1294 // Check for new streams.
1295 for (StreamParamsVec::const_iterator it = streams.begin();
1296 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001297 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001299 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001301 std::ostringstream desc;
1302 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1303 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 ret = false;
1305 }
1306 }
1307 }
1308 local_streams_ = streams;
1309 return ret;
1310}
1311
1312bool BaseChannel::UpdateRemoteStreams_w(
1313 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001314 ContentAction action,
1315 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1317 action == CA_PRANSWER || action == CA_UPDATE))
1318 return false;
1319
1320 // If this is an update, streams only contain streams that have changed.
1321 if (action == CA_UPDATE) {
1322 for (StreamParamsVec::const_iterator it = streams.begin();
1323 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001324 const StreamParams* existing_stream =
1325 GetStreamByIds(remote_streams_, it->groupid, it->id);
1326 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327 if (AddRecvStream_w(*it)) {
1328 remote_streams_.push_back(*it);
1329 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1330 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001331 std::ostringstream desc;
1332 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1333 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 return false;
1335 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001336 } else if (existing_stream && !it->has_ssrcs()) {
1337 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001338 std::ostringstream desc;
1339 desc << "Failed to remove remote stream with ssrc "
1340 << it->first_ssrc() << ".";
1341 SafeSetError(desc.str(), error_desc);
1342 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001344 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 } else {
1346 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001347 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 << " new stream = " << it->ToString();
1349 }
1350 }
1351 return true;
1352 }
1353 // Else streams are all the streams we want to receive.
1354
1355 // Check for streams that have been removed.
1356 bool ret = true;
1357 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1358 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001359 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001361 std::ostringstream desc;
1362 desc << "Failed to remove remote stream with ssrc "
1363 << it->first_ssrc() << ".";
1364 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365 ret = false;
1366 }
1367 }
1368 }
1369 // Check for new streams.
1370 for (StreamParamsVec::const_iterator it = streams.begin();
1371 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001372 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373 if (AddRecvStream_w(*it)) {
1374 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1375 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001376 std::ostringstream desc;
1377 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1378 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379 ret = false;
1380 }
1381 }
1382 }
1383 remote_streams_ = streams;
1384 return ret;
1385}
1386
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001387void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001388 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001389// Absolute Send Time extension id is used only with external auth,
1390// so do not bother searching for it and making asyncronious call to set
1391// something that is not used.
1392#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001393 const webrtc::RtpExtension* send_time_extension =
1394 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001395 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001396 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001397 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001398 RTC_FROM_HERE, network_thread_,
1399 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1400 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001401#endif
1402}
1403
1404void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1405 int rtp_abs_sendtime_extn_id) {
1406 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001407}
1408
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001409void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001410 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001411 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001412 case MSG_SEND_RTP_PACKET:
1413 case MSG_SEND_RTCP_PACKET: {
1414 RTC_DCHECK(network_thread_->IsCurrent());
1415 SendPacketMessageData* data =
1416 static_cast<SendPacketMessageData*>(pmsg->pdata);
1417 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1418 SendPacket(rtcp, &data->packet, data->options);
1419 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001420 break;
1421 }
1422 case MSG_FIRSTPACKETRECEIVED: {
1423 SignalFirstPacketReceived(this);
1424 break;
1425 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426 }
1427}
1428
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001429void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430 // Flush all remaining RTCP messages. This should only be called in
1431 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001432 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001433 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001434 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1435 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001436 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1437 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 }
1439}
1440
johand89ab142016-10-25 10:50:32 -07001441void BaseChannel::SignalSentPacket_n(
1442 rtc::PacketTransportInterface* /* transport */,
1443 const rtc::SentPacket& sent_packet) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001444 RTC_DCHECK(network_thread_->IsCurrent());
1445 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001446 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001447 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1448}
1449
1450void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1451 RTC_DCHECK(worker_thread_->IsCurrent());
1452 SignalSentPacket(sent_packet);
1453}
1454
1455VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1456 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001457 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 MediaEngineInterface* media_engine,
1459 VoiceMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001461 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001462 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001463 : BaseChannel(worker_thread,
1464 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001465 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001466 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001467 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001468 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001469 srtp_required),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001470 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001471 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472
1473VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001474 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 StopAudioMonitor();
1476 StopMediaMonitor();
1477 // this can't be done in the base class, since it calls a virtual
1478 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001479 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480}
1481
zhihuangb2cdd932017-01-19 16:54:25 -08001482bool VoiceChannel::Init_w(DtlsTransportInternal* rtp_dtls_transport,
1483 DtlsTransportInternal* rtcp_dtls_transport) {
1484 return BaseChannel::Init_w(rtp_dtls_transport, rtcp_dtls_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485}
1486
Peter Boström0c4e06b2015-10-07 12:23:21 +02001487bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001488 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001489 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001490 AudioSource* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001491 return InvokeOnWorker(RTC_FROM_HERE,
1492 Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001493 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494}
1495
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496// TODO(juberti): Handle early media the right way. We should get an explicit
1497// ringing message telling us to start playing local ringback, which we cancel
1498// if any early media actually arrives. For now, we do the opposite, which is
1499// to wait 1 second for early media, and start playing local ringback if none
1500// arrives.
1501void VoiceChannel::SetEarlyMedia(bool enable) {
1502 if (enable) {
1503 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001504 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1505 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 } else {
1507 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001508 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 }
1510}
1511
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512bool VoiceChannel::CanInsertDtmf() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001513 return InvokeOnWorker(
1514 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515}
1516
Peter Boström0c4e06b2015-10-07 12:23:21 +02001517bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1518 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001519 int duration) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001520 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this,
1521 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522}
1523
solenberg4bac9c52015-10-09 02:32:53 -07001524bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001525 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume,
1526 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001528
Tommif888bb52015-12-12 01:37:01 +01001529void VoiceChannel::SetRawAudioSink(
1530 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001531 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1532 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001533 // passing. So we invoke to our own little routine that gets a pointer to
1534 // our local variable. This is OK since we're synchronously invoking.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001535 InvokeOnWorker(RTC_FROM_HERE,
1536 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001537}
1538
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001539webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001540 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001541 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001542}
1543
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001544webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1545 uint32_t ssrc) const {
1546 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001547}
1548
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001549bool VoiceChannel::SetRtpSendParameters(
1550 uint32_t ssrc,
1551 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001552 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001553 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001554 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001555}
1556
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001557bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1558 webrtc::RtpParameters parameters) {
1559 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1560}
1561
1562webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1563 uint32_t ssrc) const {
1564 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001565 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001566 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1567}
1568
1569webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1570 uint32_t ssrc) const {
1571 return media_channel()->GetRtpReceiveParameters(ssrc);
1572}
1573
1574bool VoiceChannel::SetRtpReceiveParameters(
1575 uint32_t ssrc,
1576 const webrtc::RtpParameters& parameters) {
1577 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001578 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001579 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1580}
1581
1582bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1583 webrtc::RtpParameters parameters) {
1584 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001585}
1586
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001588 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1589 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590}
1591
1592void VoiceChannel::StartMediaMonitor(int cms) {
1593 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001594 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 media_monitor_->SignalUpdate.connect(
1596 this, &VoiceChannel::OnMediaMonitorUpdate);
1597 media_monitor_->Start(cms);
1598}
1599
1600void VoiceChannel::StopMediaMonitor() {
1601 if (media_monitor_) {
1602 media_monitor_->Stop();
1603 media_monitor_->SignalUpdate.disconnect(this);
1604 media_monitor_.reset();
1605 }
1606}
1607
1608void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001609 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 audio_monitor_
1611 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1612 audio_monitor_->Start(cms);
1613}
1614
1615void VoiceChannel::StopAudioMonitor() {
1616 if (audio_monitor_) {
1617 audio_monitor_->Stop();
1618 audio_monitor_.reset();
1619 }
1620}
1621
1622bool VoiceChannel::IsAudioMonitorRunning() const {
1623 return (audio_monitor_.get() != NULL);
1624}
1625
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001627 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628}
1629
1630int VoiceChannel::GetOutputLevel_w() {
1631 return media_channel()->GetOutputLevel();
1632}
1633
1634void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1635 media_channel()->GetActiveStreams(actives);
1636}
1637
johand89ab142016-10-25 10:50:32 -07001638void VoiceChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
1639 const char* data,
1640 size_t len,
1641 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001642 int flags) {
johand89ab142016-10-25 10:50:32 -07001643 BaseChannel::OnPacketRead(transport, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 // Set a flag when we've received an RTP packet. If we're waiting for early
1645 // media, this will disable the timeout.
johand89ab142016-10-25 10:50:32 -07001646 if (!received_media_ && !PacketIsRtcp(transport, data, len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647 received_media_ = true;
1648 }
1649}
1650
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001651void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001652 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001653 invoker_.AsyncInvoke<void>(
1654 RTC_FROM_HERE, worker_thread_,
1655 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001656}
1657
michaelt79e05882016-11-08 02:50:09 -08001658int BaseChannel::GetTransportOverheadPerPacket() const {
1659 RTC_DCHECK(network_thread_->IsCurrent());
1660
1661 if (!selected_candidate_pair_)
1662 return 0;
1663
1664 int transport_overhead_per_packet = 0;
1665
1666 constexpr int kIpv4Overhaed = 20;
1667 constexpr int kIpv6Overhaed = 40;
1668 transport_overhead_per_packet +=
1669 selected_candidate_pair_->local_candidate().address().family() == AF_INET
1670 ? kIpv4Overhaed
1671 : kIpv6Overhaed;
1672
1673 constexpr int kUdpOverhaed = 8;
1674 constexpr int kTcpOverhaed = 20;
1675 transport_overhead_per_packet +=
1676 selected_candidate_pair_->local_candidate().protocol() ==
1677 TCP_PROTOCOL_NAME
1678 ? kTcpOverhaed
1679 : kUdpOverhaed;
1680
1681 if (secure()) {
1682 int srtp_overhead = 0;
1683 if (srtp_filter_.GetSrtpOverhead(&srtp_overhead))
1684 transport_overhead_per_packet += srtp_overhead;
1685 }
1686
1687 return transport_overhead_per_packet;
1688}
1689
1690void BaseChannel::UpdateTransportOverhead() {
1691 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1692 if (transport_overhead_per_packet)
1693 invoker_.AsyncInvoke<void>(
1694 RTC_FROM_HERE, worker_thread_,
1695 Bind(&MediaChannel::OnTransportOverheadChanged, media_channel_,
1696 transport_overhead_per_packet));
1697}
1698
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001699void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700 // Render incoming data if we're the active call, and we have the local
1701 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001702 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001703 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704
1705 // Send outgoing data if we're the active call, we have the remote content,
1706 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001707 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001708 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709
1710 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1711}
1712
1713const ContentInfo* VoiceChannel::GetFirstContent(
1714 const SessionDescription* sdesc) {
1715 return GetFirstAudioContent(sdesc);
1716}
1717
1718bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001719 ContentAction action,
1720 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001721 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001722 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 LOG(LS_INFO) << "Setting local voice description";
1724
1725 const AudioContentDescription* audio =
1726 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001727 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001728 if (!audio) {
1729 SafeSetError("Can't find audio content in local description.", error_desc);
1730 return false;
1731 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001733 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001734 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 }
1736
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001737 AudioRecvParameters recv_params = last_recv_params_;
1738 RtpParametersFromMediaDescription(audio, &recv_params);
1739 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001740 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001741 error_desc);
1742 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001744 for (const AudioCodec& codec : audio->codecs()) {
1745 bundle_filter()->AddPayloadType(codec.id);
1746 }
1747 last_recv_params_ = recv_params;
1748
1749 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1750 // only give it to the media channel once we have a remote
1751 // description too (without a remote description, we won't be able
1752 // to send them anyway).
1753 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1754 SafeSetError("Failed to set local audio description streams.", error_desc);
1755 return false;
1756 }
1757
1758 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001759 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001760 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761}
1762
1763bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001764 ContentAction action,
1765 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001766 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001767 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 LOG(LS_INFO) << "Setting remote voice description";
1769
1770 const AudioContentDescription* audio =
1771 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001772 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001773 if (!audio) {
1774 SafeSetError("Can't find audio content in remote description.", error_desc);
1775 return false;
1776 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001778 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001779 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 }
1781
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001782 AudioSendParameters send_params = last_send_params_;
1783 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001784 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001785 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001786 }
skvladdc1c62c2016-03-16 19:07:43 -07001787
1788 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1789 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001790 SafeSetError("Failed to set remote audio description send parameters.",
1791 error_desc);
1792 return false;
1793 }
1794 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001796 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1797 // and only give it to the media channel once we have a local
1798 // description too (without a local description, we won't be able to
1799 // recv them anyway).
1800 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1801 SafeSetError("Failed to set remote audio description streams.", error_desc);
1802 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803 }
1804
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001805 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001806 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001807 }
1808
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001809 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001810 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001811 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812}
1813
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814void VoiceChannel::HandleEarlyMediaTimeout() {
1815 // This occurs on the main thread, not the worker thread.
1816 if (!received_media_) {
1817 LOG(LS_INFO) << "No early media received before timeout";
1818 SignalEarlyMediaTimeout(this);
1819 }
1820}
1821
Peter Boström0c4e06b2015-10-07 12:23:21 +02001822bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1823 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001824 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 if (!enabled()) {
1826 return false;
1827 }
solenberg1d63dd02015-12-02 12:35:09 -08001828 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829}
1830
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001831void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833 case MSG_EARLYMEDIATIMEOUT:
1834 HandleEarlyMediaTimeout();
1835 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836 case MSG_CHANNEL_ERROR: {
1837 VoiceChannelErrorMessageData* data =
1838 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 delete data;
1840 break;
1841 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 default:
1843 BaseChannel::OnMessage(pmsg);
1844 break;
1845 }
1846}
1847
1848void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001849 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 SignalConnectionMonitor(this, infos);
1851}
1852
1853void VoiceChannel::OnMediaMonitorUpdate(
1854 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001855 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 SignalMediaMonitor(this, info);
1857}
1858
1859void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1860 const AudioInfo& info) {
1861 SignalAudioMonitor(this, info);
1862}
1863
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001864void VoiceChannel::GetSrtpCryptoSuites_n(
1865 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07001866 GetSupportedAudioCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867}
1868
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001869VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1870 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001871 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 VideoMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001874 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001875 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001876 : BaseChannel(worker_thread,
1877 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001878 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001879 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001880 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001881 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001882 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883
zhihuangb2cdd932017-01-19 16:54:25 -08001884bool VideoChannel::Init_w(DtlsTransportInternal* rtp_dtls_transport,
1885 DtlsTransportInternal* rtcp_dtls_transport) {
1886 return BaseChannel::Init_w(rtp_dtls_transport, rtcp_dtls_transport);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887}
1888
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001890 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 StopMediaMonitor();
1892 // this can't be done in the base class, since it calls a virtual
1893 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001894
1895 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896}
1897
nisse08582ff2016-02-04 01:24:52 -08001898bool VideoChannel::SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -08001899 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
nisse08582ff2016-02-04 01:24:52 -08001900 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001901 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001902 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903 return true;
1904}
1905
deadbeef5a4a75a2016-06-02 16:23:38 -07001906bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001907 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001908 bool mute,
1909 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -08001910 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001911 return InvokeOnWorker(RTC_FROM_HERE,
1912 Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
deadbeef5a4a75a2016-06-02 16:23:38 -07001913 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001914}
1915
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001916webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001917 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001918 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001919}
1920
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001921webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1922 uint32_t ssrc) const {
1923 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001924}
1925
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001926bool VideoChannel::SetRtpSendParameters(
1927 uint32_t ssrc,
1928 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001929 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001930 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001931 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001932}
1933
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001934bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1935 webrtc::RtpParameters parameters) {
1936 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1937}
1938
1939webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1940 uint32_t ssrc) const {
1941 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001942 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001943 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
1944}
1945
1946webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
1947 uint32_t ssrc) const {
1948 return media_channel()->GetRtpReceiveParameters(ssrc);
1949}
1950
1951bool VideoChannel::SetRtpReceiveParameters(
1952 uint32_t ssrc,
1953 const webrtc::RtpParameters& parameters) {
1954 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001955 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001956 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1957}
1958
1959bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1960 webrtc::RtpParameters parameters) {
1961 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001962}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001963
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001964void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 // Send outgoing data if we're the active call, we have the remote content,
1966 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001967 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968 if (!media_channel()->SetSend(send)) {
1969 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1970 // TODO(gangji): Report error back to server.
1971 }
1972
Peter Boström34fbfff2015-09-24 19:20:30 +02001973 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974}
1975
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001976bool VideoChannel::GetStats(VideoMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001977 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
1978 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979}
1980
1981void VideoChannel::StartMediaMonitor(int cms) {
1982 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001983 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 media_monitor_->SignalUpdate.connect(
1985 this, &VideoChannel::OnMediaMonitorUpdate);
1986 media_monitor_->Start(cms);
1987}
1988
1989void VideoChannel::StopMediaMonitor() {
1990 if (media_monitor_) {
1991 media_monitor_->Stop();
1992 media_monitor_.reset();
1993 }
1994}
1995
1996const ContentInfo* VideoChannel::GetFirstContent(
1997 const SessionDescription* sdesc) {
1998 return GetFirstVideoContent(sdesc);
1999}
2000
2001bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002002 ContentAction action,
2003 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002004 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002005 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 LOG(LS_INFO) << "Setting local video description";
2007
2008 const VideoContentDescription* video =
2009 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002010 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002011 if (!video) {
2012 SafeSetError("Can't find video content in local description.", error_desc);
2013 return false;
2014 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002016 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002017 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018 }
2019
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002020 VideoRecvParameters recv_params = last_recv_params_;
2021 RtpParametersFromMediaDescription(video, &recv_params);
2022 if (!media_channel()->SetRecvParameters(recv_params)) {
2023 SafeSetError("Failed to set local video description recv parameters.",
2024 error_desc);
2025 return false;
2026 }
2027 for (const VideoCodec& codec : video->codecs()) {
2028 bundle_filter()->AddPayloadType(codec.id);
2029 }
2030 last_recv_params_ = recv_params;
2031
2032 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2033 // only give it to the media channel once we have a remote
2034 // description too (without a remote description, we won't be able
2035 // to send them anyway).
2036 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2037 SafeSetError("Failed to set local video description streams.", error_desc);
2038 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039 }
2040
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002041 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002042 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002043 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044}
2045
2046bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002047 ContentAction action,
2048 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002049 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002050 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051 LOG(LS_INFO) << "Setting remote video description";
2052
2053 const VideoContentDescription* video =
2054 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002055 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002056 if (!video) {
2057 SafeSetError("Can't find video content in remote description.", error_desc);
2058 return false;
2059 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002061 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002062 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063 }
2064
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002065 VideoSendParameters send_params = last_send_params_;
2066 RtpSendParametersFromMediaDescription(video, &send_params);
2067 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002068 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002069 }
skvladdc1c62c2016-03-16 19:07:43 -07002070
2071 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2072
2073 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002074 SafeSetError("Failed to set remote video description send parameters.",
2075 error_desc);
2076 return false;
2077 }
2078 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002080 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2081 // and only give it to the media channel once we have a local
2082 // description too (without a local description, we won't be able to
2083 // recv them anyway).
2084 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2085 SafeSetError("Failed to set remote video description streams.", error_desc);
2086 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 }
2088
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002089 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002090 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002092
2093 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002094 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002095 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096}
2097
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002098void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 case MSG_CHANNEL_ERROR: {
2101 const VideoChannelErrorMessageData* data =
2102 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 delete data;
2104 break;
2105 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 default:
2107 BaseChannel::OnMessage(pmsg);
2108 break;
2109 }
2110}
2111
2112void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002113 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 SignalConnectionMonitor(this, infos);
2115}
2116
2117// TODO(pthatcher): Look into removing duplicate code between
2118// audio, video, and data, perhaps by using templates.
2119void VideoChannel::OnMediaMonitorUpdate(
2120 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002121 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122 SignalMediaMonitor(this, info);
2123}
2124
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002125void VideoChannel::GetSrtpCryptoSuites_n(
2126 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002127 GetSupportedVideoCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128}
2129
deadbeef953c2ce2017-01-09 14:53:41 -08002130RtpDataChannel::RtpDataChannel(rtc::Thread* worker_thread,
2131 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002132 rtc::Thread* signaling_thread,
deadbeef953c2ce2017-01-09 14:53:41 -08002133 DataMediaChannel* media_channel,
deadbeef953c2ce2017-01-09 14:53:41 -08002134 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08002135 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002136 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002137 : BaseChannel(worker_thread,
2138 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002139 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002140 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002141 content_name,
deadbeefac22f702017-01-12 21:59:29 -08002142 rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002143 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144
deadbeef953c2ce2017-01-09 14:53:41 -08002145RtpDataChannel::~RtpDataChannel() {
2146 TRACE_EVENT0("webrtc", "RtpDataChannel::~RtpDataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147 StopMediaMonitor();
2148 // this can't be done in the base class, since it calls a virtual
2149 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002150
2151 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152}
2153
zhihuangb2cdd932017-01-19 16:54:25 -08002154bool RtpDataChannel::Init_w(DtlsTransportInternal* rtp_dtls_transport,
2155 DtlsTransportInternal* rtcp_dtls_transport) {
2156 if (!BaseChannel::Init_w(rtp_dtls_transport, rtcp_dtls_transport)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 return false;
2158 }
deadbeef953c2ce2017-01-09 14:53:41 -08002159 media_channel()->SignalDataReceived.connect(this,
2160 &RtpDataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002161 media_channel()->SignalReadyToSend.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08002162 this, &RtpDataChannel::OnDataChannelReadyToSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163 return true;
2164}
2165
deadbeef953c2ce2017-01-09 14:53:41 -08002166bool RtpDataChannel::SendData(const SendDataParams& params,
2167 const rtc::CopyOnWriteBuffer& payload,
2168 SendDataResult* result) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002169 return InvokeOnWorker(
2170 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2171 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172}
2173
deadbeef953c2ce2017-01-09 14:53:41 -08002174const ContentInfo* RtpDataChannel::GetFirstContent(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175 const SessionDescription* sdesc) {
2176 return GetFirstDataContent(sdesc);
2177}
2178
deadbeef953c2ce2017-01-09 14:53:41 -08002179bool RtpDataChannel::CheckDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002180 const DataContentDescription* content,
2181 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2183 (content->protocol() == kMediaProtocolDtlsSctp));
deadbeef953c2ce2017-01-09 14:53:41 -08002184 // It's been set before, but doesn't match. That's bad.
2185 if (is_sctp) {
2186 SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.",
2187 error_desc);
2188 return false;
2189 }
2190 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002191}
2192
deadbeef953c2ce2017-01-09 14:53:41 -08002193bool RtpDataChannel::SetLocalContent_w(const MediaContentDescription* content,
2194 ContentAction action,
2195 std::string* error_desc) {
2196 TRACE_EVENT0("webrtc", "RtpDataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002197 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198 LOG(LS_INFO) << "Setting local data description";
2199
2200 const DataContentDescription* data =
2201 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002202 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002203 if (!data) {
2204 SafeSetError("Can't find data content in local description.", error_desc);
2205 return false;
2206 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207
deadbeef953c2ce2017-01-09 14:53:41 -08002208 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209 return false;
2210 }
2211
deadbeef953c2ce2017-01-09 14:53:41 -08002212 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
2213 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 }
2215
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002216 DataRecvParameters recv_params = last_recv_params_;
2217 RtpParametersFromMediaDescription(data, &recv_params);
2218 if (!media_channel()->SetRecvParameters(recv_params)) {
2219 SafeSetError("Failed to set remote data description recv parameters.",
2220 error_desc);
2221 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222 }
deadbeef953c2ce2017-01-09 14:53:41 -08002223 for (const DataCodec& codec : data->codecs()) {
2224 bundle_filter()->AddPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002225 }
2226 last_recv_params_ = recv_params;
2227
2228 // TODO(pthatcher): Move local streams into DataSendParameters, and
2229 // only give it to the media channel once we have a remote
2230 // description too (without a remote description, we won't be able
2231 // to send them anyway).
2232 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2233 SafeSetError("Failed to set local data description streams.", error_desc);
2234 return false;
2235 }
2236
2237 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002238 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002239 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240}
2241
deadbeef953c2ce2017-01-09 14:53:41 -08002242bool RtpDataChannel::SetRemoteContent_w(const MediaContentDescription* content,
2243 ContentAction action,
2244 std::string* error_desc) {
2245 TRACE_EVENT0("webrtc", "RtpDataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002246 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247
2248 const DataContentDescription* data =
2249 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002250 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002251 if (!data) {
2252 SafeSetError("Can't find data content in remote description.", error_desc);
2253 return false;
2254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002255
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002256 // If the remote data doesn't have codecs and isn't an update, it
2257 // must be empty, so ignore it.
2258 if (!data->has_codecs() && action != CA_UPDATE) {
2259 return true;
2260 }
2261
deadbeef953c2ce2017-01-09 14:53:41 -08002262 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263 return false;
2264 }
2265
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002266 LOG(LS_INFO) << "Setting remote data description";
deadbeef953c2ce2017-01-09 14:53:41 -08002267 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002268 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002269 }
2270
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002271 DataSendParameters send_params = last_send_params_;
2272 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2273 if (!media_channel()->SetSendParameters(send_params)) {
2274 SafeSetError("Failed to set remote data description send parameters.",
2275 error_desc);
2276 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002277 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002278 last_send_params_ = send_params;
2279
2280 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2281 // and only give it to the media channel once we have a local
2282 // description too (without a local description, we won't be able to
2283 // recv them anyway).
2284 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2285 SafeSetError("Failed to set remote data description streams.",
2286 error_desc);
2287 return false;
2288 }
2289
2290 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002291 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002292 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002293}
2294
deadbeef953c2ce2017-01-09 14:53:41 -08002295void RtpDataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 // Render incoming data if we're the active call, and we have the local
2297 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002298 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299 if (!media_channel()->SetReceive(recv)) {
2300 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2301 }
2302
2303 // Send outgoing data if we're the active call, we have the remote content,
2304 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002305 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002306 if (!media_channel()->SetSend(send)) {
2307 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2308 }
2309
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002310 // Trigger SignalReadyToSendData asynchronously.
2311 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312
2313 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2314}
2315
deadbeef953c2ce2017-01-09 14:53:41 -08002316void RtpDataChannel::OnMessage(rtc::Message* pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317 switch (pmsg->message_id) {
2318 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002319 DataChannelReadyToSendMessageData* data =
2320 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002321 ready_to_send_data_ = data->data();
2322 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 delete data;
2324 break;
2325 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002326 case MSG_DATARECEIVED: {
2327 DataReceivedMessageData* data =
2328 static_cast<DataReceivedMessageData*>(pmsg->pdata);
deadbeef953c2ce2017-01-09 14:53:41 -08002329 SignalDataReceived(data->params, data->payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330 delete data;
2331 break;
2332 }
2333 case MSG_CHANNEL_ERROR: {
2334 const DataChannelErrorMessageData* data =
2335 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002336 delete data;
2337 break;
2338 }
2339 default:
2340 BaseChannel::OnMessage(pmsg);
2341 break;
2342 }
2343}
2344
deadbeef953c2ce2017-01-09 14:53:41 -08002345void RtpDataChannel::OnConnectionMonitorUpdate(
2346 ConnectionMonitor* monitor,
2347 const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 SignalConnectionMonitor(this, infos);
2349}
2350
deadbeef953c2ce2017-01-09 14:53:41 -08002351void RtpDataChannel::StartMediaMonitor(int cms) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002353 rtc::Thread::Current()));
deadbeef953c2ce2017-01-09 14:53:41 -08002354 media_monitor_->SignalUpdate.connect(this,
2355 &RtpDataChannel::OnMediaMonitorUpdate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356 media_monitor_->Start(cms);
2357}
2358
deadbeef953c2ce2017-01-09 14:53:41 -08002359void RtpDataChannel::StopMediaMonitor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 if (media_monitor_) {
2361 media_monitor_->Stop();
2362 media_monitor_->SignalUpdate.disconnect(this);
2363 media_monitor_.reset();
2364 }
2365}
2366
deadbeef953c2ce2017-01-09 14:53:41 -08002367void RtpDataChannel::OnMediaMonitorUpdate(DataMediaChannel* media_channel,
2368 const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002369 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002370 SignalMediaMonitor(this, info);
2371}
2372
deadbeef953c2ce2017-01-09 14:53:41 -08002373void RtpDataChannel::OnDataReceived(const ReceiveDataParams& params,
2374 const char* data,
2375 size_t len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002376 DataReceivedMessageData* msg = new DataReceivedMessageData(
2377 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002378 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002379}
2380
deadbeef953c2ce2017-01-09 14:53:41 -08002381void RtpDataChannel::OnDataChannelError(uint32_t ssrc,
2382 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002383 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2384 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002385 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002386}
2387
deadbeef953c2ce2017-01-09 14:53:41 -08002388void RtpDataChannel::OnDataChannelReadyToSend(bool writable) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002389 // This is usded for congestion control to indicate that the stream is ready
2390 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2391 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002392 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002393 new DataChannelReadyToSendMessageData(writable));
2394}
2395
deadbeef953c2ce2017-01-09 14:53:41 -08002396void RtpDataChannel::GetSrtpCryptoSuites_n(
2397 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002398 GetSupportedDataCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002399}
2400
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002401} // namespace cricket