blob: ecc1c9a633c6d4a935a146f048483256ccdc5d49 [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"
jbaucheec21bd2016-03-20 06:15:43 -070019#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000020#include "webrtc/base/dscp.h"
21#include "webrtc/base/logging.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070022#include "webrtc/base/networkroute.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010023#include "webrtc/base/trace_event.h"
kjellanderf4752772016-03-02 05:42:30 -080024#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080025#include "webrtc/media/base/rtputils.h"
deadbeef5bd5ca32017-02-10 11:31:50 -080026#include "webrtc/p2p/base/packettransportinternal.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010027#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
29namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000030using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000031
deadbeef2d110be2016-01-13 12:00:26 -080032namespace {
kwiberg31022942016-03-11 14:18:21 -080033// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080034bool SetRawAudioSink_w(VoiceMediaChannel* channel,
35 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080036 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
37 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080038 return true;
39}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020040
41struct SendPacketMessageData : public rtc::MessageData {
42 rtc::CopyOnWriteBuffer packet;
43 rtc::PacketOptions options;
44};
45
isheriff6f8d6862016-05-26 11:24:55 -070046#if defined(ENABLE_EXTERNAL_AUTH)
47// Returns the named header extension if found among all extensions,
48// nullptr otherwise.
49const webrtc::RtpExtension* FindHeaderExtension(
50 const std::vector<webrtc::RtpExtension>& extensions,
51 const std::string& uri) {
52 for (const auto& extension : extensions) {
53 if (extension.uri == uri)
54 return &extension;
55 }
56 return nullptr;
57}
58#endif
59
deadbeef2d110be2016-01-13 12:00:26 -080060} // namespace
61
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000063 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020064 MSG_SEND_RTP_PACKET,
65 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 MSG_FIRSTPACKETRECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070};
71
72// Value specified in RFC 5764.
73static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
74
75static const int kAgcMinus10db = -10;
76
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000077static void SafeSetError(const std::string& message, std::string* error_desc) {
78 if (error_desc) {
79 *error_desc = message;
80 }
81}
82
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000083struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020084 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020086 : ssrc(in_ssrc), error(in_error) {}
87 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 VoiceMediaChannel::Error error;
89};
90
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020092 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020094 : ssrc(in_ssrc), error(in_error) {}
95 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 VideoMediaChannel::Error error;
97};
98
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000099struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 : ssrc(in_ssrc), error(in_error) {}
103 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 DataMediaChannel::Error error;
105};
106
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107static const char* PacketType(bool rtcp) {
108 return (!rtcp) ? "RTP" : "RTCP";
109}
110
jbaucheec21bd2016-03-20 06:15:43 -0700111static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 // Check the packet size. We could check the header too if needed.
113 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000114 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
115 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116}
117
118static bool IsReceiveContentDirection(MediaContentDirection direction) {
119 return direction == MD_SENDRECV || direction == MD_RECVONLY;
120}
121
122static bool IsSendContentDirection(MediaContentDirection direction) {
123 return direction == MD_SENDRECV || direction == MD_SENDONLY;
124}
125
126static const MediaContentDescription* GetContentDescription(
127 const ContentInfo* cinfo) {
128 if (cinfo == NULL)
129 return NULL;
130 return static_cast<const MediaContentDescription*>(cinfo->description);
131}
132
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700133template <class Codec>
134void RtpParametersFromMediaDescription(
135 const MediaContentDescriptionImpl<Codec>* desc,
136 RtpParameters<Codec>* params) {
137 // TODO(pthatcher): Remove this once we're sure no one will give us
138 // a description without codecs (currently a CA_UPDATE with just
139 // streams can).
140 if (desc->has_codecs()) {
141 params->codecs = desc->codecs();
142 }
143 // TODO(pthatcher): See if we really need
144 // rtp_header_extensions_set() and remove it if we don't.
145 if (desc->rtp_header_extensions_set()) {
146 params->extensions = desc->rtp_header_extensions();
147 }
deadbeef13871492015-12-09 12:37:51 -0800148 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700149}
150
nisse05103312016-03-16 02:22:50 -0700151template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700152void RtpSendParametersFromMediaDescription(
153 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700154 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700155 RtpParametersFromMediaDescription(desc, send_params);
156 send_params->max_bandwidth_bps = desc->bandwidth();
157}
158
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200159BaseChannel::BaseChannel(rtc::Thread* worker_thread,
160 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800161 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700162 MediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700163 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800164 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800165 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200166 : worker_thread_(worker_thread),
167 network_thread_(network_thread),
zhihuangf5b251b2017-01-12 19:37:48 -0800168 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 content_name_(content_name),
deadbeefac22f702017-01-12 21:59:29 -0800170 rtcp_mux_required_(rtcp_mux_required),
deadbeef7af91dd2016-12-13 11:29:11 -0800171 srtp_required_(srtp_required),
michaelt79e05882016-11-08 02:50:09 -0800172 media_channel_(media_channel),
173 selected_candidate_pair_(nullptr) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700174 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
jbauchdfcab722017-03-06 00:14:10 -0800175#if defined(ENABLE_EXTERNAL_AUTH)
176 srtp_filter_.EnableExternalAuth();
177#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 LOG(LS_INFO) << "Created channel for " << content_name;
179}
180
181BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800182 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700183 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000184 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200186 // Eats any outstanding messages or packets.
187 worker_thread_->Clear(&invoker_);
188 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 // We must destroy the media channel before the transport channel, otherwise
190 // the media channel may try to send on the dead transport channel. NULLing
191 // is not an effective strategy since the sends will come on another thread.
192 delete media_channel_;
zhihuangf5b251b2017-01-12 19:37:48 -0800193 LOG(LS_INFO) << "Destroyed channel: " << content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200194}
195
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200196void BaseChannel::DisconnectTransportChannels_n() {
197 // Send any outstanding RTCP packets.
198 FlushRtcpMessages_n();
199
200 // Stop signals from transport channels, but keep them alive because
201 // media_channel may use them from a different thread.
zhihuangb2cdd932017-01-19 16:54:25 -0800202 if (rtp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800203 DisconnectFromDtlsTransport(rtp_dtls_transport_);
204 } else if (rtp_packet_transport_) {
205 DisconnectFromPacketTransport(rtp_packet_transport_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200206 }
zhihuangb2cdd932017-01-19 16:54:25 -0800207 if (rtcp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800208 DisconnectFromDtlsTransport(rtcp_dtls_transport_);
209 } else if (rtcp_packet_transport_) {
210 DisconnectFromPacketTransport(rtcp_packet_transport_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200211 }
212
213 // Clear pending read packets/messages.
214 network_thread_->Clear(&invoker_);
215 network_thread_->Clear(this);
216}
217
zhihuangb2cdd932017-01-19 16:54:25 -0800218bool BaseChannel::Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800219 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800220 rtc::PacketTransportInternal* rtp_packet_transport,
221 rtc::PacketTransportInternal* rtcp_packet_transport) {
skvlad6c87a672016-05-17 17:49:52 -0700222 if (!network_thread_->Invoke<bool>(
zhihuangb2cdd932017-01-19 16:54:25 -0800223 RTC_FROM_HERE, Bind(&BaseChannel::InitNetwork_n, this,
deadbeeff5346592017-01-24 21:51:21 -0800224 rtp_dtls_transport, rtcp_dtls_transport,
225 rtp_packet_transport, rtcp_packet_transport))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000226 return false;
227 }
deadbeeff5346592017-01-24 21:51:21 -0800228 // Both RTP and RTCP channels should be set, we can call SetInterface on
229 // the media channel and it can set network options.
230 RTC_DCHECK_RUN_ON(worker_thread_);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000231 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 return true;
233}
234
deadbeeff5346592017-01-24 21:51:21 -0800235bool BaseChannel::InitNetwork_n(
236 DtlsTransportInternal* rtp_dtls_transport,
237 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800238 rtc::PacketTransportInternal* rtp_packet_transport,
239 rtc::PacketTransportInternal* rtcp_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200240 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800241 SetTransports_n(rtp_dtls_transport, rtcp_dtls_transport, rtp_packet_transport,
242 rtcp_packet_transport);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200243
deadbeeff5346592017-01-24 21:51:21 -0800244 if (rtp_dtls_transport_ &&
245 !SetDtlsSrtpCryptoSuites_n(rtp_dtls_transport_, false)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200246 return false;
247 }
zhihuangb2cdd932017-01-19 16:54:25 -0800248 if (rtcp_dtls_transport_ &&
249 !SetDtlsSrtpCryptoSuites_n(rtcp_dtls_transport_, true)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200250 return false;
251 }
deadbeefac22f702017-01-12 21:59:29 -0800252 if (rtcp_mux_required_) {
253 rtcp_mux_filter_.SetActive();
254 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200255 return true;
256}
257
wu@webrtc.org78187522013-10-07 23:32:02 +0000258void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200259 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000260 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200261 // Packets arrive on the network thread, processing packets calls virtual
262 // functions, so need to stop this process in Deinit that is called in
263 // derived classes destructor.
264 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700265 RTC_FROM_HERE, Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000266}
267
zhihuangb2cdd932017-01-19 16:54:25 -0800268void BaseChannel::SetTransports(DtlsTransportInternal* rtp_dtls_transport,
269 DtlsTransportInternal* rtcp_dtls_transport) {
deadbeeff5346592017-01-24 21:51:21 -0800270 network_thread_->Invoke<void>(
271 RTC_FROM_HERE,
272 Bind(&BaseChannel::SetTransports_n, this, rtp_dtls_transport,
273 rtcp_dtls_transport, rtp_dtls_transport, rtcp_dtls_transport));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000274}
275
deadbeeff5346592017-01-24 21:51:21 -0800276void BaseChannel::SetTransports(
deadbeef5bd5ca32017-02-10 11:31:50 -0800277 rtc::PacketTransportInternal* rtp_packet_transport,
278 rtc::PacketTransportInternal* rtcp_packet_transport) {
deadbeeff5346592017-01-24 21:51:21 -0800279 network_thread_->Invoke<void>(
280 RTC_FROM_HERE, Bind(&BaseChannel::SetTransports_n, this, nullptr, nullptr,
281 rtp_packet_transport, rtcp_packet_transport));
282}
zhihuangf5b251b2017-01-12 19:37:48 -0800283
deadbeeff5346592017-01-24 21:51:21 -0800284void BaseChannel::SetTransports_n(
285 DtlsTransportInternal* rtp_dtls_transport,
286 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800287 rtc::PacketTransportInternal* rtp_packet_transport,
288 rtc::PacketTransportInternal* rtcp_packet_transport) {
deadbeeff5346592017-01-24 21:51:21 -0800289 RTC_DCHECK(network_thread_->IsCurrent());
290 // Validate some assertions about the input.
291 RTC_DCHECK(rtp_packet_transport);
292 RTC_DCHECK_EQ(NeedsRtcpTransport(), rtcp_packet_transport != nullptr);
293 if (rtp_dtls_transport || rtcp_dtls_transport) {
294 // DTLS/non-DTLS pointers should be to the same object.
295 RTC_DCHECK(rtp_dtls_transport == rtp_packet_transport);
296 RTC_DCHECK(rtcp_dtls_transport == rtcp_packet_transport);
297 // Can't go from non-DTLS to DTLS.
298 RTC_DCHECK(!rtp_packet_transport_ || rtp_dtls_transport_);
299 } else {
300 // Can't go from DTLS to non-DTLS.
301 RTC_DCHECK(!rtp_dtls_transport_);
302 }
303 // Transport names should be the same.
zhihuangb2cdd932017-01-19 16:54:25 -0800304 if (rtp_dtls_transport && rtcp_dtls_transport) {
305 RTC_DCHECK(rtp_dtls_transport->transport_name() ==
306 rtcp_dtls_transport->transport_name());
zhihuangb2cdd932017-01-19 16:54:25 -0800307 }
deadbeeff5346592017-01-24 21:51:21 -0800308 std::string debug_name;
309 if (rtp_dtls_transport) {
310 transport_name_ = rtp_dtls_transport->transport_name();
311 debug_name = transport_name_;
312 } else {
313 debug_name = rtp_packet_transport->debug_name();
314 }
315 if (rtp_packet_transport == rtp_packet_transport_) {
316 // Nothing to do if transport isn't changing.
deadbeefbad5dad2017-01-17 18:32:35 -0800317 return;
deadbeefcbecd352015-09-23 11:50:27 -0700318 }
319
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800320 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
321 // changes and wait until the DTLS handshake is complete to set the newly
322 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200323 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800324 // Set |writable_| to false such that UpdateWritableState_w can set up
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700325 // DTLS-SRTP when |writable_| becomes true again.
guoweis46383312015-12-17 16:45:59 -0800326 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800327 srtp_filter_.ResetParams();
328 }
329
deadbeefac22f702017-01-12 21:59:29 -0800330 // If this BaseChannel doesn't require RTCP mux and we haven't fully
331 // negotiated RTCP mux, we need an RTCP transport.
deadbeeff5346592017-01-24 21:51:21 -0800332 if (rtcp_packet_transport) {
zhihuangf5b251b2017-01-12 19:37:48 -0800333 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on "
deadbeeff5346592017-01-24 21:51:21 -0800334 << debug_name << " transport " << rtcp_packet_transport;
335 SetTransport_n(true, rtcp_dtls_transport, rtcp_packet_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000336 }
337
deadbeeff5346592017-01-24 21:51:21 -0800338 LOG(LS_INFO) << "Setting RTP Transport for " << content_name() << " on "
339 << debug_name << " transport " << rtp_packet_transport;
340 SetTransport_n(false, rtp_dtls_transport, rtp_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800341
deadbeefcbecd352015-09-23 11:50:27 -0700342 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700343 // setting new transport channels.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200344 UpdateWritableState_n();
deadbeef062ce9f2016-08-26 21:42:15 -0700345 // We can only update ready-to-send after updating writability.
346 //
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700347 // On setting a new channel, assume it's ready to send if it's writable,
348 // because we have no way of knowing otherwise (the channel doesn't give us
349 // "was last send successful?").
350 //
351 // This won't always be accurate (the last SendPacket call from another
352 // BaseChannel could have resulted in an error), but even so, we'll just
353 // encounter the error again and update "ready to send" accordingly.
deadbeef062ce9f2016-08-26 21:42:15 -0700354 SetTransportChannelReadyToSend(
deadbeeff5346592017-01-24 21:51:21 -0800355 false, rtp_packet_transport_ && rtp_packet_transport_->writable());
zhihuangb2cdd932017-01-19 16:54:25 -0800356 SetTransportChannelReadyToSend(
deadbeeff5346592017-01-24 21:51:21 -0800357 true, rtcp_packet_transport_ && rtcp_packet_transport_->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000358}
359
deadbeeff5346592017-01-24 21:51:21 -0800360void BaseChannel::SetTransport_n(
361 bool rtcp,
362 DtlsTransportInternal* new_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800363 rtc::PacketTransportInternal* new_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200364 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800365 DtlsTransportInternal*& old_dtls_transport =
zhihuangb2cdd932017-01-19 16:54:25 -0800366 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
deadbeef5bd5ca32017-02-10 11:31:50 -0800367 rtc::PacketTransportInternal*& old_packet_transport =
deadbeeff5346592017-01-24 21:51:21 -0800368 rtcp ? rtcp_packet_transport_ : rtp_packet_transport_;
zhihuangb2cdd932017-01-19 16:54:25 -0800369
deadbeeff5346592017-01-24 21:51:21 -0800370 if (!old_packet_transport && !new_packet_transport) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700371 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000372 return;
373 }
zhihuangb2cdd932017-01-19 16:54:25 -0800374
deadbeeff5346592017-01-24 21:51:21 -0800375 RTC_DCHECK(old_packet_transport != new_packet_transport);
376 if (old_dtls_transport) {
377 DisconnectFromDtlsTransport(old_dtls_transport);
378 } else if (old_packet_transport) {
379 DisconnectFromPacketTransport(old_packet_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000380 }
381
deadbeeff5346592017-01-24 21:51:21 -0800382 old_packet_transport = new_packet_transport;
383 old_dtls_transport = new_dtls_transport;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000384
deadbeeff5346592017-01-24 21:51:21 -0800385 // If there's no new transport, we're done after disconnecting from old one.
386 if (!new_packet_transport) {
387 return;
388 }
389
390 if (rtcp && new_dtls_transport) {
391 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
392 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
393 << "should never happen.";
394 }
395 if (new_dtls_transport) {
396 ConnectToDtlsTransport(new_dtls_transport);
397 } else {
398 ConnectToPacketTransport(new_packet_transport);
399 }
400 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
401 for (const auto& pair : socket_options) {
402 new_packet_transport->SetOption(pair.first, pair.second);
guoweis46383312015-12-17 16:45:59 -0800403 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000404}
405
deadbeeff5346592017-01-24 21:51:21 -0800406void BaseChannel::ConnectToDtlsTransport(DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200407 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000408
zhihuangb2cdd932017-01-19 16:54:25 -0800409 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
410 transport->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
411 transport->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
412 transport->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
413 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
414 transport->ice_transport()->SignalSelectedCandidatePairChanged.connect(
Honghai Zhangcc411c02016-03-29 17:27:21 -0700415 this, &BaseChannel::OnSelectedCandidatePairChanged);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000416}
417
deadbeeff5346592017-01-24 21:51:21 -0800418void BaseChannel::DisconnectFromDtlsTransport(
419 DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200420 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800421 OnSelectedCandidatePairChanged(transport->ice_transport(), nullptr, -1,
422 false);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000423
zhihuangb2cdd932017-01-19 16:54:25 -0800424 transport->SignalWritableState.disconnect(this);
425 transport->SignalReadPacket.disconnect(this);
426 transport->SignalReadyToSend.disconnect(this);
427 transport->SignalDtlsState.disconnect(this);
428 transport->SignalSentPacket.disconnect(this);
429 transport->ice_transport()->SignalSelectedCandidatePairChanged.disconnect(
430 this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000431}
432
deadbeeff5346592017-01-24 21:51:21 -0800433void BaseChannel::ConnectToPacketTransport(
deadbeef5bd5ca32017-02-10 11:31:50 -0800434 rtc::PacketTransportInternal* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800435 RTC_DCHECK_RUN_ON(network_thread_);
436 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
437 transport->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
438 transport->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
439 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
440}
441
442void BaseChannel::DisconnectFromPacketTransport(
deadbeef5bd5ca32017-02-10 11:31:50 -0800443 rtc::PacketTransportInternal* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800444 RTC_DCHECK_RUN_ON(network_thread_);
445 transport->SignalWritableState.disconnect(this);
446 transport->SignalReadPacket.disconnect(this);
447 transport->SignalReadyToSend.disconnect(this);
448 transport->SignalSentPacket.disconnect(this);
449}
450
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700452 worker_thread_->Invoke<void>(
453 RTC_FROM_HERE,
454 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
455 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 return true;
457}
458
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459bool BaseChannel::AddRecvStream(const StreamParams& sp) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700460 return InvokeOnWorker(RTC_FROM_HERE,
461 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462}
463
Peter Boström0c4e06b2015-10-07 12:23:21 +0200464bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700465 return InvokeOnWorker(RTC_FROM_HERE,
466 Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467}
468
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000469bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000470 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700471 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000472}
473
Peter Boström0c4e06b2015-10-07 12:23:21 +0200474bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700475 return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream,
476 media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000477}
478
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000480 ContentAction action,
481 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100482 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700483 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w,
484 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485}
486
487bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000488 ContentAction action,
489 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100490 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700491 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w,
492 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493}
494
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495void BaseChannel::StartConnectionMonitor(int cms) {
zhihuangb2cdd932017-01-19 16:54:25 -0800496 // We pass in the BaseChannel instead of the rtp_dtls_transport_
497 // because if the rtp_dtls_transport_ changes, the ConnectionMonitor
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000498 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200499 // We pass in the network thread because on that thread connection monitor
500 // will call BaseChannel::GetConnectionStats which must be called on the
501 // network thread.
502 connection_monitor_.reset(
503 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000504 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000506 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507}
508
509void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000510 if (connection_monitor_) {
511 connection_monitor_->Stop();
512 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 }
514}
515
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000516bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200517 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800518 if (!rtp_dtls_transport_) {
519 return false;
520 }
zhihuangb2cdd932017-01-19 16:54:25 -0800521 return rtp_dtls_transport_->ice_transport()->GetStats(infos);
zhihuangf5b251b2017-01-12 19:37:48 -0800522}
523
524bool BaseChannel::NeedsRtcpTransport() {
deadbeefac22f702017-01-12 21:59:29 -0800525 // If this BaseChannel doesn't require RTCP mux and we haven't fully
526 // negotiated RTCP mux, we need an RTCP transport.
527 return !rtcp_mux_required_ && !rtcp_mux_filter_.IsFullyActive();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000528}
529
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700530bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 // Receive data if we are enabled and have local content,
532 return enabled() && IsReceiveContentDirection(local_content_direction_);
533}
534
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700535bool BaseChannel::IsReadyToSendMedia_w() const {
536 // Need to access some state updated on the network thread.
537 return network_thread_->Invoke<bool>(
538 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
539}
540
541bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 // Send outgoing data if we are enabled, have local and remote content,
543 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800544 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700546 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200547 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548}
549
jbaucheec21bd2016-03-20 06:15:43 -0700550bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700551 const rtc::PacketOptions& options) {
552 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553}
554
jbaucheec21bd2016-03-20 06:15:43 -0700555bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700556 const rtc::PacketOptions& options) {
557 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558}
559
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000560int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200562 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700563 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200564}
565
566int BaseChannel::SetOption_n(SocketType type,
567 rtc::Socket::Option opt,
568 int value) {
569 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef5bd5ca32017-02-10 11:31:50 -0800570 rtc::PacketTransportInternal* transport = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000572 case ST_RTP:
deadbeeff5346592017-01-24 21:51:21 -0800573 transport = rtp_packet_transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700574 socket_options_.push_back(
575 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000576 break;
577 case ST_RTCP:
deadbeeff5346592017-01-24 21:51:21 -0800578 transport = rtcp_packet_transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700579 rtcp_socket_options_.push_back(
580 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000581 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 }
deadbeeff5346592017-01-24 21:51:21 -0800583 return transport ? transport->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584}
585
jbauchcb560652016-08-04 05:20:32 -0700586bool BaseChannel::SetCryptoOptions(const rtc::CryptoOptions& crypto_options) {
587 crypto_options_ = crypto_options;
588 return true;
589}
590
deadbeef5bd5ca32017-02-10 11:31:50 -0800591void BaseChannel::OnWritableState(rtc::PacketTransportInternal* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800592 RTC_DCHECK(transport == rtp_packet_transport_ ||
593 transport == rtcp_packet_transport_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200594 RTC_DCHECK(network_thread_->IsCurrent());
595 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596}
597
deadbeef5bd5ca32017-02-10 11:31:50 -0800598void BaseChannel::OnPacketRead(rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700599 const char* data,
600 size_t len,
601 const rtc::PacketTime& packet_time,
602 int flags) {
603 TRACE_EVENT0("webrtc", "BaseChannel::OnPacketRead");
604 // OnPacketRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200605 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606
607 // When using RTCP multiplexing we might get RTCP packets on the RTP
608 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
johand89ab142016-10-25 10:50:32 -0700609 bool rtcp = PacketIsRtcp(transport, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700610 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000611 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612}
613
deadbeef5bd5ca32017-02-10 11:31:50 -0800614void BaseChannel::OnReadyToSend(rtc::PacketTransportInternal* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800615 RTC_DCHECK(transport == rtp_packet_transport_ ||
616 transport == rtcp_packet_transport_);
617 SetTransportChannelReadyToSend(transport == rtcp_packet_transport_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618}
619
zhihuangb2cdd932017-01-19 16:54:25 -0800620void BaseChannel::OnDtlsState(DtlsTransportInternal* transport,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800621 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200622 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800623 return;
624 }
625
626 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
627 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
zhihuangb2cdd932017-01-19 16:54:25 -0800628 // cover other scenarios like the whole transport is writable (not just this
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800629 // TransportChannel) or when TransportChannel is attached after DTLS is
630 // negotiated.
631 if (state != DTLS_TRANSPORT_CONNECTED) {
632 srtp_filter_.ResetParams();
633 }
634}
635
Honghai Zhangcc411c02016-03-29 17:27:21 -0700636void BaseChannel::OnSelectedCandidatePairChanged(
zhihuangb2cdd932017-01-19 16:54:25 -0800637 IceTransportInternal* ice_transport,
Honghai Zhang52dce732016-03-31 12:37:31 -0700638 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700639 int last_sent_packet_id,
640 bool ready_to_send) {
deadbeeff5346592017-01-24 21:51:21 -0800641 RTC_DCHECK((rtp_dtls_transport_ &&
642 ice_transport == rtp_dtls_transport_->ice_transport()) ||
643 (rtcp_dtls_transport_ &&
644 ice_transport == rtcp_dtls_transport_->ice_transport()));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200645 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800646 selected_candidate_pair_ = selected_candidate_pair;
zhihuangb2cdd932017-01-19 16:54:25 -0800647 std::string transport_name = ice_transport->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700648 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700649 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700650 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700651 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700652 selected_candidate_pair->remote_candidate().network_id(),
653 last_sent_packet_id);
michaelt79e05882016-11-08 02:50:09 -0800654
655 UpdateTransportOverhead();
Honghai Zhangcc411c02016-03-29 17:27:21 -0700656 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200657 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700658 RTC_FROM_HERE, worker_thread_,
659 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
660 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700661}
662
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700663void BaseChannel::SetTransportChannelReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200664 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700665 if (rtcp) {
666 rtcp_ready_to_send_ = ready;
667 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 rtp_ready_to_send_ = ready;
669 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200671 bool ready_to_send =
672 (rtp_ready_to_send_ &&
deadbeeff5346592017-01-24 21:51:21 -0800673 // In the case of rtcp mux |rtcp_packet_transport_| will be null.
674 (rtcp_ready_to_send_ || !rtcp_packet_transport_));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200675
676 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700677 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200678 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679}
680
deadbeef5bd5ca32017-02-10 11:31:50 -0800681bool BaseChannel::PacketIsRtcp(const rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700682 const char* data,
683 size_t len) {
deadbeeff5346592017-01-24 21:51:21 -0800684 return (transport == rtcp_packet_transport_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000685 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686}
687
stefanc1aeaf02015-10-15 07:26:07 -0700688bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700689 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700690 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200691 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
692 // If the thread is not our network thread, we will post to our network
693 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 // synchronize access to all the pieces of the send path, including
695 // SRTP and the inner workings of the transport channels.
696 // The only downside is that we can't return a proper failure code if
697 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200698 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200700 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
701 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800702 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700703 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700704 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 return true;
706 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200707 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708
709 // Now that we are on the correct thread, ensure we have a place to send this
710 // packet before doing anything. (We might get RTCP packets that we don't
711 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
712 // transport.
deadbeef5bd5ca32017-02-10 11:31:50 -0800713 rtc::PacketTransportInternal* transport =
deadbeeff5346592017-01-24 21:51:21 -0800714 (!rtcp || rtcp_mux_filter_.IsActive()) ? rtp_packet_transport_
715 : rtcp_packet_transport_;
zhihuangb2cdd932017-01-19 16:54:25 -0800716 if (!transport || !transport->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 return false;
718 }
719
720 // Protect ourselves against crazy data.
721 if (!ValidPacket(rtcp, packet)) {
722 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000723 << PacketType(rtcp)
724 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 return false;
726 }
727
stefanc1aeaf02015-10-15 07:26:07 -0700728 rtc::PacketOptions updated_options;
729 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 // Protect if needed.
731 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200732 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200734 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000735 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000736 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000737 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
738 // inside libsrtp for a RTP packet. A external HMAC module will be writing
739 // a fake HMAC value. This is ONLY done for a RTP packet.
740 // Socket layer will update rtp sendtime extension header if present in
741 // packet with current time before updating the HMAC.
742#if !defined(ENABLE_EXTERNAL_AUTH)
743 res = srtp_filter_.ProtectRtp(
744 data, len, static_cast<int>(packet->capacity()), &len);
745#else
jbauchd48f4882017-03-01 15:34:36 -0800746 if (!srtp_filter_.IsExternalAuthActive()) {
747 res = srtp_filter_.ProtectRtp(
748 data, len, static_cast<int>(packet->capacity()), &len);
749 } else {
750 updated_options.packet_time_params.rtp_sendtime_extension_id =
751 rtp_abs_sendtime_extn_id_;
752 res = srtp_filter_.ProtectRtp(
753 data, len, static_cast<int>(packet->capacity()), &len,
754 &updated_options.packet_time_params.srtp_packet_index);
755 // If protection succeeds, let's get auth params from srtp.
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000756 if (res) {
jbauchd48f4882017-03-01 15:34:36 -0800757 uint8_t* auth_key = NULL;
758 int key_len;
759 res = srtp_filter_.GetRtpAuthParams(
760 &auth_key, &key_len,
761 &updated_options.packet_time_params.srtp_auth_tag_len);
762 if (res) {
763 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
764 updated_options.packet_time_params.srtp_auth_key.assign(
765 auth_key, auth_key + key_len);
766 }
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000767 }
768 }
769#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 if (!res) {
771 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200772 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 GetRtpSeqNum(data, len, &seq_num);
774 GetRtpSsrc(data, len, &ssrc);
775 LOG(LS_ERROR) << "Failed to protect " << content_name_
776 << " RTP packet: size=" << len
777 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
778 return false;
779 }
780 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000781 res = srtp_filter_.ProtectRtcp(data, len,
782 static_cast<int>(packet->capacity()),
783 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 if (!res) {
785 int type = -1;
786 GetRtcpType(data, len, &type);
787 LOG(LS_ERROR) << "Failed to protect " << content_name_
788 << " RTCP packet: size=" << len << ", type=" << type;
789 return false;
790 }
791 }
792
793 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000794 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800795 } else if (srtp_required_) {
deadbeef8f425f92016-12-01 12:26:27 -0800796 // The audio/video engines may attempt to send RTCP packets as soon as the
797 // streams are created, so don't treat this as an error for RTCP.
798 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809
799 if (rtcp) {
800 return false;
801 }
802 // However, there shouldn't be any RTP packets sent before SRTP is set up
803 // (and SetSend(true) is called).
804 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
805 << " and crypto is required";
nisseeb4ca4e2017-01-12 02:24:27 -0800806 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 return false;
808 }
809
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200811 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
zhihuangb2cdd932017-01-19 16:54:25 -0800812 int ret = transport->SendPacket(packet->data<char>(), packet->size(),
813 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000814 if (ret != static_cast<int>(packet->size())) {
zhihuangb2cdd932017-01-19 16:54:25 -0800815 if (transport->GetError() == ENOTCONN) {
skvladc309e0e2016-07-28 17:15:20 -0700816 LOG(LS_WARNING) << "Got ENOTCONN from transport.";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700817 SetTransportChannelReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 }
819 return false;
820 }
821 return true;
822}
823
jbaucheec21bd2016-03-20 06:15:43 -0700824bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 // Protect ourselves against crazy data.
826 if (!ValidPacket(rtcp, packet)) {
827 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000828 << PacketType(rtcp)
829 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 return false;
831 }
pbos482b12e2015-11-16 10:19:58 -0800832 if (rtcp) {
833 // Permit all (seemingly valid) RTCP packets.
834 return true;
835 }
836 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700837 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838}
839
jbaucheec21bd2016-03-20 06:15:43 -0700840void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000841 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200842 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 if (!WantsPacket(rtcp, packet)) {
844 return;
845 }
846
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000847 // We are only interested in the first rtp packet because that
848 // indicates the media has started flowing.
849 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700851 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 }
853
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 // Unprotect the packet, if needed.
855 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200856 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200857 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000858 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 bool res;
860 if (!rtcp) {
861 res = srtp_filter_.UnprotectRtp(data, len, &len);
862 if (!res) {
863 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200864 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 GetRtpSeqNum(data, len, &seq_num);
866 GetRtpSsrc(data, len, &ssrc);
867 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
868 << " RTP packet: size=" << len
869 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
870 return;
871 }
872 } else {
873 res = srtp_filter_.UnprotectRtcp(data, len, &len);
874 if (!res) {
875 int type = -1;
876 GetRtcpType(data, len, &type);
877 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
878 << " RTCP packet: size=" << len << ", type=" << type;
879 return;
880 }
881 }
882
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000883 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800884 } else if (srtp_required_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 // Our session description indicates that SRTP is required, but we got a
886 // packet before our SRTP filter is active. This means either that
887 // a) we got SRTP packets before we received the SDES keys, in which case
888 // we can't decrypt it anyway, or
889 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
zhihuangb2cdd932017-01-19 16:54:25 -0800890 // transports, so we haven't yet extracted keys, even if DTLS did
891 // complete on the transport that the packets are being sent on. It's
892 // really good practice to wait for both RTP and RTCP to be good to go
893 // before sending media, to prevent weird failure modes, so it's fine
894 // for us to just eat packets here. This is all sidestepped if RTCP mux
895 // is used anyway.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
897 << " packet when SRTP is inactive and crypto is required";
898 return;
899 }
900
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200901 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700902 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200903 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
904}
905
906void BaseChannel::OnPacketReceived(bool rtcp,
907 const rtc::CopyOnWriteBuffer& packet,
908 const rtc::PacketTime& packet_time) {
909 RTC_DCHECK(worker_thread_->IsCurrent());
910 // Need to copy variable because OnRtcpReceived/OnPacketReceived
911 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
912 rtc::CopyOnWriteBuffer data(packet);
913 if (rtcp) {
914 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200916 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 }
918}
919
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000920bool BaseChannel::PushdownLocalDescription(
921 const SessionDescription* local_desc, ContentAction action,
922 std::string* error_desc) {
923 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 const MediaContentDescription* content_desc =
925 GetContentDescription(content_info);
926 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000927 !SetLocalContent(content_desc, action, error_desc)) {
928 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
929 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000931 return true;
932}
933
934bool BaseChannel::PushdownRemoteDescription(
935 const SessionDescription* remote_desc, ContentAction action,
936 std::string* error_desc) {
937 const ContentInfo* content_info = GetFirstContent(remote_desc);
938 const MediaContentDescription* content_desc =
939 GetContentDescription(content_info);
940 if (content_desc && content_info && !content_info->rejected &&
941 !SetRemoteContent(content_desc, action, error_desc)) {
942 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
943 return false;
944 }
945 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946}
947
948void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700949 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 if (enabled_)
951 return;
952
953 LOG(LS_INFO) << "Channel enabled";
954 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700955 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956}
957
958void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700959 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 if (!enabled_)
961 return;
962
963 LOG(LS_INFO) << "Channel disabled";
964 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700965 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966}
967
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200968void BaseChannel::UpdateWritableState_n() {
deadbeeff5346592017-01-24 21:51:21 -0800969 if (rtp_packet_transport_ && rtp_packet_transport_->writable() &&
970 (!rtcp_packet_transport_ || rtcp_packet_transport_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200971 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700972 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200973 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700974 }
975}
976
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200977void BaseChannel::ChannelWritable_n() {
978 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800979 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800981 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982
deadbeefcbecd352015-09-23 11:50:27 -0700983 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 << (was_ever_writable_ ? "" : " for the first time");
985
michaelt79e05882016-11-08 02:50:09 -0800986 if (selected_candidate_pair_)
987 LOG(LS_INFO)
988 << "Using "
989 << selected_candidate_pair_->local_candidate().ToSensitiveString()
990 << "->"
991 << selected_candidate_pair_->remote_candidate().ToSensitiveString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200994 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700996 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997}
998
deadbeef953c2ce2017-01-09 14:53:41 -0800999void BaseChannel::SignalDtlsSrtpSetupFailure_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001000 RTC_DCHECK(network_thread_->IsCurrent());
1001 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001002 RTC_FROM_HERE, signaling_thread(),
deadbeef953c2ce2017-01-09 14:53:41 -08001003 Bind(&BaseChannel::SignalDtlsSrtpSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001004}
1005
deadbeef953c2ce2017-01-09 14:53:41 -08001006void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001007 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
deadbeef953c2ce2017-01-09 14:53:41 -08001008 SignalDtlsSrtpSetupFailure(this, rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001009}
1010
zhihuangb2cdd932017-01-19 16:54:25 -08001011bool BaseChannel::SetDtlsSrtpCryptoSuites_n(DtlsTransportInternal* transport,
1012 bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001013 std::vector<int> crypto_suites;
1014 // We always use the default SRTP crypto suites for RTCP, but we may use
1015 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001017 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018 } else {
jbauchcb560652016-08-04 05:20:32 -07001019 GetDefaultSrtpCryptoSuites(crypto_options(), &crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 }
zhihuangb2cdd932017-01-19 16:54:25 -08001021 return transport->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022}
1023
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001024bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
zhihuangb2cdd932017-01-19 16:54:25 -08001025 // Since DTLS is applied to all transports, checking RTP should be enough.
1026 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027}
1028
1029// This function returns true if either DTLS-SRTP is not in use
1030// *or* DTLS-SRTP is successfully set up.
zhihuangb2cdd932017-01-19 16:54:25 -08001031bool BaseChannel::SetupDtlsSrtp_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001032 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 bool ret = false;
1034
zhihuangb2cdd932017-01-19 16:54:25 -08001035 DtlsTransportInternal* transport =
1036 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
deadbeeff5346592017-01-24 21:51:21 -08001037 RTC_DCHECK(transport);
zhihuangb2cdd932017-01-19 16:54:25 -08001038 RTC_DCHECK(transport->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001040 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041
zhihuangb2cdd932017-01-19 16:54:25 -08001042 if (!transport->GetSrtpCryptoSuite(&selected_crypto_suite)) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001043 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 return false;
1045 }
1046
zhihuangb2cdd932017-01-19 16:54:25 -08001047 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " << content_name() << " "
1048 << PacketType(rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049
jbauchcb560652016-08-04 05:20:32 -07001050 int key_len;
1051 int salt_len;
1052 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
1053 &salt_len)) {
1054 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
1055 return false;
1056 }
1057
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -07001059 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060
1061 // RFC 5705 exporter using the RFC 5764 parameters
zhihuangb2cdd932017-01-19 16:54:25 -08001062 if (!transport->ExportKeyingMaterial(kDtlsSrtpExporterLabel, NULL, 0, false,
1063 &dtls_buffer[0], dtls_buffer.size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
nisseeb4ca4e2017-01-12 02:24:27 -08001065 RTC_NOTREACHED(); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 return false;
1067 }
1068
1069 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -07001070 std::vector<unsigned char> client_write_key(key_len + salt_len);
1071 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -07001073 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
1074 offset += key_len;
1075 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
1076 offset += key_len;
1077 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
1078 offset += salt_len;
1079 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080
1081 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001082 rtc::SSLRole role;
zhihuangb2cdd932017-01-19 16:54:25 -08001083 if (!transport->GetSslRole(&role)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001084 LOG(LS_WARNING) << "GetSslRole failed";
1085 return false;
1086 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001088 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 send_key = &server_write_key;
1090 recv_key = &client_write_key;
1091 } else {
1092 send_key = &client_write_key;
1093 recv_key = &server_write_key;
1094 }
1095
zhihuangb2cdd932017-01-19 16:54:25 -08001096 if (rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001097 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1098 static_cast<int>(send_key->size()),
1099 selected_crypto_suite, &(*recv_key)[0],
1100 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001102 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1103 static_cast<int>(send_key->size()),
1104 selected_crypto_suite, &(*recv_key)[0],
1105 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 }
1107
michaelt79e05882016-11-08 02:50:09 -08001108 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
michaelt79e05882016-11-08 02:50:09 -08001110 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 dtls_keyed_ = true;
michaelt79e05882016-11-08 02:50:09 -08001112 UpdateTransportOverhead();
1113 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114 return ret;
1115}
1116
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001117void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001118 if (srtp_filter_.IsActive()) {
1119 return;
1120 }
1121
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001122 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001123 return;
1124 }
1125
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001126 if (!SetupDtlsSrtp_n(false)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001127 SignalDtlsSrtpSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001128 return;
1129 }
1130
zhihuangb2cdd932017-01-19 16:54:25 -08001131 if (rtcp_dtls_transport_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001132 if (!SetupDtlsSrtp_n(true)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001133 SignalDtlsSrtpSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001134 return;
1135 }
1136 }
1137}
1138
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001139void BaseChannel::ChannelNotWritable_n() {
1140 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 if (!writable_)
1142 return;
1143
deadbeefcbecd352015-09-23 11:50:27 -07001144 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001146 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147}
1148
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001149bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001150 const MediaContentDescription* content,
1151 ContentAction action,
1152 ContentSource src,
1153 std::string* error_desc) {
1154 if (action == CA_UPDATE) {
1155 // These parameters never get changed by a CA_UDPATE.
1156 return true;
1157 }
1158
deadbeef7af91dd2016-12-13 11:29:11 -08001159 // Cache srtp_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001160 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001161 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1162 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001163}
1164
1165bool BaseChannel::SetRtpTransportParameters_n(
1166 const MediaContentDescription* content,
1167 ContentAction action,
1168 ContentSource src,
1169 std::string* error_desc) {
1170 RTC_DCHECK(network_thread_->IsCurrent());
1171
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001172 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001173 return false;
1174 }
1175
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001176 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001177 return false;
1178 }
1179
1180 return true;
1181}
1182
zhihuangb2cdd932017-01-19 16:54:25 -08001183// |dtls| will be set to true if DTLS is active for transport and crypto is
1184// empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001185bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1186 bool* dtls,
1187 std::string* error_desc) {
deadbeeff5346592017-01-24 21:51:21 -08001188 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001189 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001190 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001191 return false;
1192 }
1193 return true;
1194}
1195
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001196bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001197 ContentAction action,
1198 ContentSource src,
1199 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001200 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001201 if (action == CA_UPDATE) {
1202 // no crypto params.
1203 return true;
1204 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001206 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001207 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001208 if (!ret) {
1209 return false;
1210 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 switch (action) {
1212 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001213 // If DTLS is already active on the channel, we could be renegotiating
1214 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001215 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001216 ret = srtp_filter_.SetOffer(cryptos, src);
1217 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 break;
1219 case CA_PRANSWER:
1220 // If we're doing DTLS-SRTP, we don't want to update the filter
1221 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001222 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1224 }
1225 break;
1226 case CA_ANSWER:
1227 // If we're doing DTLS-SRTP, we don't want to update the filter
1228 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001229 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 ret = srtp_filter_.SetAnswer(cryptos, src);
1231 }
1232 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233 default:
1234 break;
1235 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001236 if (!ret) {
1237 SafeSetError("Failed to setup SRTP filter.", error_desc);
1238 return false;
1239 }
1240 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241}
1242
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001243bool BaseChannel::SetRtcpMux_n(bool enable,
1244 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001245 ContentSource src,
1246 std::string* error_desc) {
deadbeef8e814d72017-01-13 11:34:39 -08001247 // Provide a more specific error message for the RTCP mux "require" policy
1248 // case.
1249 if (rtcp_mux_required_ && !enable) {
1250 SafeSetError(
1251 "rtcpMuxPolicy is 'require', but media description does not "
1252 "contain 'a=rtcp-mux'.",
1253 error_desc);
1254 return false;
1255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 bool ret = false;
1257 switch (action) {
1258 case CA_OFFER:
1259 ret = rtcp_mux_filter_.SetOffer(enable, src);
1260 break;
1261 case CA_PRANSWER:
zhihuangb2cdd932017-01-19 16:54:25 -08001262 // This may activate RTCP muxing, but we don't yet destroy the transport
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001263 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1265 break;
1266 case CA_ANSWER:
1267 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1268 if (ret && rtcp_mux_filter_.IsActive()) {
deadbeefe814a0d2017-02-25 18:15:09 -08001269 // We permanently activated RTCP muxing; signal that we no longer need
1270 // the RTCP transport.
1271 std::string debug_name = transport_name_.empty()
1272 ? rtp_packet_transport_->debug_name()
1273 : transport_name_;
1274 ;
deadbeefcbecd352015-09-23 11:50:27 -07001275 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
deadbeefe814a0d2017-02-25 18:15:09 -08001276 << "; no longer need RTCP transport for " << debug_name;
deadbeeff5346592017-01-24 21:51:21 -08001277 if (rtcp_packet_transport_) {
1278 SetTransport_n(true, nullptr, nullptr);
1279 SignalRtcpMuxFullyActive(transport_name_);
zhihuangf5b251b2017-01-12 19:37:48 -08001280 }
deadbeef062ce9f2016-08-26 21:42:15 -07001281 UpdateWritableState_n();
1282 SetTransportChannelReadyToSend(true, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 }
1284 break;
1285 case CA_UPDATE:
1286 // No RTCP mux info.
1287 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001288 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 default:
1290 break;
1291 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001292 if (!ret) {
1293 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1294 return false;
1295 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
zhihuangb2cdd932017-01-19 16:54:25 -08001297 // CA_ANSWER, but we only want to tear down the RTCP transport if we received
1298 // a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001299 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 // If the RTP transport is already writable, then so are we.
deadbeeff5346592017-01-24 21:51:21 -08001301 if (rtp_packet_transport_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001302 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 }
1304 }
1305
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001306 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307}
1308
1309bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001310 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001311 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312}
1313
Peter Boström0c4e06b2015-10-07 12:23:21 +02001314bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001315 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 return media_channel()->RemoveRecvStream(ssrc);
1317}
1318
1319bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001320 ContentAction action,
1321 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001322 if (!(action == CA_OFFER || action == CA_ANSWER ||
1323 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 return false;
1325
1326 // If this is an update, streams only contain streams that have changed.
1327 if (action == CA_UPDATE) {
1328 for (StreamParamsVec::const_iterator it = streams.begin();
1329 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001330 const StreamParams* existing_stream =
1331 GetStreamByIds(local_streams_, it->groupid, it->id);
1332 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 if (media_channel()->AddSendStream(*it)) {
1334 local_streams_.push_back(*it);
1335 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1336 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001337 std::ostringstream desc;
1338 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1339 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 return false;
1341 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001342 } else if (existing_stream && !it->has_ssrcs()) {
1343 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001344 std::ostringstream desc;
1345 desc << "Failed to remove send stream with ssrc "
1346 << it->first_ssrc() << ".";
1347 SafeSetError(desc.str(), error_desc);
1348 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001350 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351 } else {
1352 LOG(LS_WARNING) << "Ignore unsupported stream update";
1353 }
1354 }
1355 return true;
1356 }
1357 // Else streams are all the streams we want to send.
1358
1359 // Check for streams that have been removed.
1360 bool ret = true;
1361 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1362 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001363 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001365 std::ostringstream desc;
1366 desc << "Failed to remove send stream with ssrc "
1367 << it->first_ssrc() << ".";
1368 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 ret = false;
1370 }
1371 }
1372 }
1373 // Check for new streams.
1374 for (StreamParamsVec::const_iterator it = streams.begin();
1375 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001376 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001378 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001380 std::ostringstream desc;
1381 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1382 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383 ret = false;
1384 }
1385 }
1386 }
1387 local_streams_ = streams;
1388 return ret;
1389}
1390
1391bool BaseChannel::UpdateRemoteStreams_w(
1392 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001393 ContentAction action,
1394 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001395 if (!(action == CA_OFFER || action == CA_ANSWER ||
1396 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 return false;
1398
1399 // If this is an update, streams only contain streams that have changed.
1400 if (action == CA_UPDATE) {
1401 for (StreamParamsVec::const_iterator it = streams.begin();
1402 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001403 const StreamParams* existing_stream =
1404 GetStreamByIds(remote_streams_, it->groupid, it->id);
1405 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001406 if (AddRecvStream_w(*it)) {
1407 remote_streams_.push_back(*it);
1408 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1409 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001410 std::ostringstream desc;
1411 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1412 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413 return false;
1414 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001415 } else if (existing_stream && !it->has_ssrcs()) {
1416 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001417 std::ostringstream desc;
1418 desc << "Failed to remove remote stream with ssrc "
1419 << it->first_ssrc() << ".";
1420 SafeSetError(desc.str(), error_desc);
1421 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001423 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 } else {
1425 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001426 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 << " new stream = " << it->ToString();
1428 }
1429 }
1430 return true;
1431 }
1432 // Else streams are all the streams we want to receive.
1433
1434 // Check for streams that have been removed.
1435 bool ret = true;
1436 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1437 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001438 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001440 std::ostringstream desc;
1441 desc << "Failed to remove remote stream with ssrc "
1442 << it->first_ssrc() << ".";
1443 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444 ret = false;
1445 }
1446 }
1447 }
1448 // Check for new streams.
1449 for (StreamParamsVec::const_iterator it = streams.begin();
1450 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001451 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452 if (AddRecvStream_w(*it)) {
1453 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1454 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001455 std::ostringstream desc;
1456 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1457 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 ret = false;
1459 }
1460 }
1461 }
1462 remote_streams_ = streams;
1463 return ret;
1464}
1465
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001466void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001467 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001468// Absolute Send Time extension id is used only with external auth,
1469// so do not bother searching for it and making asyncronious call to set
1470// something that is not used.
1471#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001472 const webrtc::RtpExtension* send_time_extension =
1473 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001474 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001475 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001476 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001477 RTC_FROM_HERE, network_thread_,
1478 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1479 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001480#endif
1481}
1482
1483void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1484 int rtp_abs_sendtime_extn_id) {
1485 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001486}
1487
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001488void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001489 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001491 case MSG_SEND_RTP_PACKET:
1492 case MSG_SEND_RTCP_PACKET: {
1493 RTC_DCHECK(network_thread_->IsCurrent());
1494 SendPacketMessageData* data =
1495 static_cast<SendPacketMessageData*>(pmsg->pdata);
1496 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1497 SendPacket(rtcp, &data->packet, data->options);
1498 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 break;
1500 }
1501 case MSG_FIRSTPACKETRECEIVED: {
1502 SignalFirstPacketReceived(this);
1503 break;
1504 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505 }
1506}
1507
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001508void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 // Flush all remaining RTCP messages. This should only be called in
1510 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001511 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001512 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001513 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1514 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001515 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1516 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001517 }
1518}
1519
johand89ab142016-10-25 10:50:32 -07001520void BaseChannel::SignalSentPacket_n(
deadbeef5bd5ca32017-02-10 11:31:50 -08001521 rtc::PacketTransportInternal* /* transport */,
johand89ab142016-10-25 10:50:32 -07001522 const rtc::SentPacket& sent_packet) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001523 RTC_DCHECK(network_thread_->IsCurrent());
1524 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001525 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001526 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1527}
1528
1529void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1530 RTC_DCHECK(worker_thread_->IsCurrent());
1531 SignalSentPacket(sent_packet);
1532}
1533
1534VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1535 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001536 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537 MediaEngineInterface* media_engine,
1538 VoiceMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001540 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001541 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001542 : BaseChannel(worker_thread,
1543 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001544 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001545 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001546 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001547 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001548 srtp_required),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001549 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001550 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551
1552VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001553 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 StopAudioMonitor();
1555 StopMediaMonitor();
1556 // this can't be done in the base class, since it calls a virtual
1557 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001558 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559}
1560
Peter Boström0c4e06b2015-10-07 12:23:21 +02001561bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001562 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001563 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001564 AudioSource* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001565 return InvokeOnWorker(RTC_FROM_HERE,
1566 Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001567 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568}
1569
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570// TODO(juberti): Handle early media the right way. We should get an explicit
1571// ringing message telling us to start playing local ringback, which we cancel
1572// if any early media actually arrives. For now, we do the opposite, which is
1573// to wait 1 second for early media, and start playing local ringback if none
1574// arrives.
1575void VoiceChannel::SetEarlyMedia(bool enable) {
1576 if (enable) {
1577 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001578 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1579 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 } else {
1581 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001582 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 }
1584}
1585
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586bool VoiceChannel::CanInsertDtmf() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001587 return InvokeOnWorker(
1588 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589}
1590
Peter Boström0c4e06b2015-10-07 12:23:21 +02001591bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1592 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001593 int duration) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001594 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this,
1595 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596}
1597
solenberg4bac9c52015-10-09 02:32:53 -07001598bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001599 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume,
1600 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001602
Tommif888bb52015-12-12 01:37:01 +01001603void VoiceChannel::SetRawAudioSink(
1604 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001605 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1606 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001607 // passing. So we invoke to our own little routine that gets a pointer to
1608 // our local variable. This is OK since we're synchronously invoking.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001609 InvokeOnWorker(RTC_FROM_HERE,
1610 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001611}
1612
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001613webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001614 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001615 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001616}
1617
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001618webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1619 uint32_t ssrc) const {
1620 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001621}
1622
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001623bool VoiceChannel::SetRtpSendParameters(
1624 uint32_t ssrc,
1625 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001626 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001627 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001628 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001629}
1630
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001631bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1632 webrtc::RtpParameters parameters) {
1633 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1634}
1635
1636webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1637 uint32_t ssrc) const {
1638 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001639 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001640 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1641}
1642
1643webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1644 uint32_t ssrc) const {
1645 return media_channel()->GetRtpReceiveParameters(ssrc);
1646}
1647
1648bool VoiceChannel::SetRtpReceiveParameters(
1649 uint32_t ssrc,
1650 const webrtc::RtpParameters& parameters) {
1651 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001652 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001653 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1654}
1655
1656bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1657 webrtc::RtpParameters parameters) {
1658 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001659}
1660
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001662 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1663 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664}
1665
1666void VoiceChannel::StartMediaMonitor(int cms) {
1667 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001668 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 media_monitor_->SignalUpdate.connect(
1670 this, &VoiceChannel::OnMediaMonitorUpdate);
1671 media_monitor_->Start(cms);
1672}
1673
1674void VoiceChannel::StopMediaMonitor() {
1675 if (media_monitor_) {
1676 media_monitor_->Stop();
1677 media_monitor_->SignalUpdate.disconnect(this);
1678 media_monitor_.reset();
1679 }
1680}
1681
1682void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001683 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684 audio_monitor_
1685 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1686 audio_monitor_->Start(cms);
1687}
1688
1689void VoiceChannel::StopAudioMonitor() {
1690 if (audio_monitor_) {
1691 audio_monitor_->Stop();
1692 audio_monitor_.reset();
1693 }
1694}
1695
1696bool VoiceChannel::IsAudioMonitorRunning() const {
1697 return (audio_monitor_.get() != NULL);
1698}
1699
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001701 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702}
1703
1704int VoiceChannel::GetOutputLevel_w() {
1705 return media_channel()->GetOutputLevel();
1706}
1707
1708void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1709 media_channel()->GetActiveStreams(actives);
1710}
1711
deadbeef5bd5ca32017-02-10 11:31:50 -08001712void VoiceChannel::OnPacketRead(rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -07001713 const char* data,
1714 size_t len,
1715 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001716 int flags) {
johand89ab142016-10-25 10:50:32 -07001717 BaseChannel::OnPacketRead(transport, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 // Set a flag when we've received an RTP packet. If we're waiting for early
1719 // media, this will disable the timeout.
johand89ab142016-10-25 10:50:32 -07001720 if (!received_media_ && !PacketIsRtcp(transport, data, len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 received_media_ = true;
1722 }
1723}
1724
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001725void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001726 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001727 invoker_.AsyncInvoke<void>(
1728 RTC_FROM_HERE, worker_thread_,
1729 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001730}
1731
michaelt79e05882016-11-08 02:50:09 -08001732int BaseChannel::GetTransportOverheadPerPacket() const {
1733 RTC_DCHECK(network_thread_->IsCurrent());
1734
1735 if (!selected_candidate_pair_)
1736 return 0;
1737
1738 int transport_overhead_per_packet = 0;
1739
1740 constexpr int kIpv4Overhaed = 20;
1741 constexpr int kIpv6Overhaed = 40;
1742 transport_overhead_per_packet +=
1743 selected_candidate_pair_->local_candidate().address().family() == AF_INET
1744 ? kIpv4Overhaed
1745 : kIpv6Overhaed;
1746
1747 constexpr int kUdpOverhaed = 8;
1748 constexpr int kTcpOverhaed = 20;
1749 transport_overhead_per_packet +=
1750 selected_candidate_pair_->local_candidate().protocol() ==
1751 TCP_PROTOCOL_NAME
1752 ? kTcpOverhaed
1753 : kUdpOverhaed;
1754
1755 if (secure()) {
1756 int srtp_overhead = 0;
1757 if (srtp_filter_.GetSrtpOverhead(&srtp_overhead))
1758 transport_overhead_per_packet += srtp_overhead;
1759 }
1760
1761 return transport_overhead_per_packet;
1762}
1763
1764void BaseChannel::UpdateTransportOverhead() {
1765 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1766 if (transport_overhead_per_packet)
1767 invoker_.AsyncInvoke<void>(
1768 RTC_FROM_HERE, worker_thread_,
1769 Bind(&MediaChannel::OnTransportOverheadChanged, media_channel_,
1770 transport_overhead_per_packet));
1771}
1772
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001773void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 // Render incoming data if we're the active call, and we have the local
1775 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001776 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001777 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778
1779 // Send outgoing data if we're the active call, we have the remote content,
1780 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001781 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001782 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783
1784 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1785}
1786
1787const ContentInfo* VoiceChannel::GetFirstContent(
1788 const SessionDescription* sdesc) {
1789 return GetFirstAudioContent(sdesc);
1790}
1791
1792bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001793 ContentAction action,
1794 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001795 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001796 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 LOG(LS_INFO) << "Setting local voice description";
1798
1799 const AudioContentDescription* audio =
1800 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001801 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001802 if (!audio) {
1803 SafeSetError("Can't find audio content in local description.", error_desc);
1804 return false;
1805 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001807 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001808 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 }
1810
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001811 AudioRecvParameters recv_params = last_recv_params_;
1812 RtpParametersFromMediaDescription(audio, &recv_params);
1813 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001814 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001815 error_desc);
1816 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001818 for (const AudioCodec& codec : audio->codecs()) {
1819 bundle_filter()->AddPayloadType(codec.id);
1820 }
1821 last_recv_params_ = recv_params;
1822
1823 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1824 // only give it to the media channel once we have a remote
1825 // description too (without a remote description, we won't be able
1826 // to send them anyway).
1827 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1828 SafeSetError("Failed to set local audio description streams.", error_desc);
1829 return false;
1830 }
1831
1832 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001833 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001834 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835}
1836
1837bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001838 ContentAction action,
1839 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001840 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001841 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 LOG(LS_INFO) << "Setting remote voice description";
1843
1844 const AudioContentDescription* audio =
1845 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001846 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001847 if (!audio) {
1848 SafeSetError("Can't find audio content in remote description.", error_desc);
1849 return false;
1850 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001852 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001853 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 }
1855
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001856 AudioSendParameters send_params = last_send_params_;
1857 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001858 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001859 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001860 }
skvladdc1c62c2016-03-16 19:07:43 -07001861
1862 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1863 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001864 SafeSetError("Failed to set remote audio description send parameters.",
1865 error_desc);
1866 return false;
1867 }
1868 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001870 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1871 // and only give it to the media channel once we have a local
1872 // description too (without a local description, we won't be able to
1873 // recv them anyway).
1874 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1875 SafeSetError("Failed to set remote audio description streams.", error_desc);
1876 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 }
1878
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001879 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001880 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001881 }
1882
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001883 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001884 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001885 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886}
1887
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888void VoiceChannel::HandleEarlyMediaTimeout() {
1889 // This occurs on the main thread, not the worker thread.
1890 if (!received_media_) {
1891 LOG(LS_INFO) << "No early media received before timeout";
1892 SignalEarlyMediaTimeout(this);
1893 }
1894}
1895
Peter Boström0c4e06b2015-10-07 12:23:21 +02001896bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1897 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001898 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 if (!enabled()) {
1900 return false;
1901 }
solenberg1d63dd02015-12-02 12:35:09 -08001902 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903}
1904
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001905void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907 case MSG_EARLYMEDIATIMEOUT:
1908 HandleEarlyMediaTimeout();
1909 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 case MSG_CHANNEL_ERROR: {
1911 VoiceChannelErrorMessageData* data =
1912 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 delete data;
1914 break;
1915 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916 default:
1917 BaseChannel::OnMessage(pmsg);
1918 break;
1919 }
1920}
1921
1922void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001923 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 SignalConnectionMonitor(this, infos);
1925}
1926
1927void VoiceChannel::OnMediaMonitorUpdate(
1928 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001929 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 SignalMediaMonitor(this, info);
1931}
1932
1933void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1934 const AudioInfo& info) {
1935 SignalAudioMonitor(this, info);
1936}
1937
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001938void VoiceChannel::GetSrtpCryptoSuites_n(
1939 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07001940 GetSupportedAudioCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941}
1942
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001943VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1944 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001945 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946 VideoMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001948 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001949 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001950 : BaseChannel(worker_thread,
1951 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001952 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001953 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001954 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001955 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001956 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001959 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 StopMediaMonitor();
1961 // this can't be done in the base class, since it calls a virtual
1962 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001963
1964 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965}
1966
nisse08582ff2016-02-04 01:24:52 -08001967bool VideoChannel::SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -08001968 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
nisse08582ff2016-02-04 01:24:52 -08001969 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001970 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001971 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972 return true;
1973}
1974
deadbeef5a4a75a2016-06-02 16:23:38 -07001975bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001976 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001977 bool mute,
1978 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -08001979 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001980 return InvokeOnWorker(RTC_FROM_HERE,
1981 Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
deadbeef5a4a75a2016-06-02 16:23:38 -07001982 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001983}
1984
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001985webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001986 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001987 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001988}
1989
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001990webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1991 uint32_t ssrc) const {
1992 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001993}
1994
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001995bool VideoChannel::SetRtpSendParameters(
1996 uint32_t ssrc,
1997 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001998 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001999 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002000 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07002001}
2002
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002003bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
2004 webrtc::RtpParameters parameters) {
2005 return media_channel()->SetRtpSendParameters(ssrc, parameters);
2006}
2007
2008webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
2009 uint32_t ssrc) const {
2010 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002011 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002012 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
2013}
2014
2015webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
2016 uint32_t ssrc) const {
2017 return media_channel()->GetRtpReceiveParameters(ssrc);
2018}
2019
2020bool VideoChannel::SetRtpReceiveParameters(
2021 uint32_t ssrc,
2022 const webrtc::RtpParameters& parameters) {
2023 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002024 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002025 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
2026}
2027
2028bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
2029 webrtc::RtpParameters parameters) {
2030 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07002031}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002032
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002033void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 // Send outgoing data if we're the active call, we have the remote content,
2035 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002036 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037 if (!media_channel()->SetSend(send)) {
2038 LOG(LS_ERROR) << "Failed to SetSend on video channel";
2039 // TODO(gangji): Report error back to server.
2040 }
2041
Peter Boström34fbfff2015-09-24 19:20:30 +02002042 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002043}
2044
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00002045bool VideoChannel::GetStats(VideoMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002046 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
2047 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048}
2049
2050void VideoChannel::StartMediaMonitor(int cms) {
2051 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002052 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053 media_monitor_->SignalUpdate.connect(
2054 this, &VideoChannel::OnMediaMonitorUpdate);
2055 media_monitor_->Start(cms);
2056}
2057
2058void VideoChannel::StopMediaMonitor() {
2059 if (media_monitor_) {
2060 media_monitor_->Stop();
2061 media_monitor_.reset();
2062 }
2063}
2064
2065const ContentInfo* VideoChannel::GetFirstContent(
2066 const SessionDescription* sdesc) {
2067 return GetFirstVideoContent(sdesc);
2068}
2069
2070bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002071 ContentAction action,
2072 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002073 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002074 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002075 LOG(LS_INFO) << "Setting local video description";
2076
2077 const VideoContentDescription* video =
2078 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002079 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002080 if (!video) {
2081 SafeSetError("Can't find video content in local description.", error_desc);
2082 return false;
2083 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002085 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002086 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 }
2088
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002089 VideoRecvParameters recv_params = last_recv_params_;
2090 RtpParametersFromMediaDescription(video, &recv_params);
2091 if (!media_channel()->SetRecvParameters(recv_params)) {
2092 SafeSetError("Failed to set local video description recv parameters.",
2093 error_desc);
2094 return false;
2095 }
2096 for (const VideoCodec& codec : video->codecs()) {
2097 bundle_filter()->AddPayloadType(codec.id);
2098 }
2099 last_recv_params_ = recv_params;
2100
2101 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2102 // only give it to the media channel once we have a remote
2103 // description too (without a remote description, we won't be able
2104 // to send them anyway).
2105 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2106 SafeSetError("Failed to set local video description streams.", error_desc);
2107 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108 }
2109
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002110 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002111 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002112 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113}
2114
2115bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002116 ContentAction action,
2117 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002118 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002119 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 LOG(LS_INFO) << "Setting remote video description";
2121
2122 const VideoContentDescription* video =
2123 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002124 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002125 if (!video) {
2126 SafeSetError("Can't find video content in remote description.", error_desc);
2127 return false;
2128 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002130 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002131 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 }
2133
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002134 VideoSendParameters send_params = last_send_params_;
2135 RtpSendParametersFromMediaDescription(video, &send_params);
2136 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002137 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002138 }
skvladdc1c62c2016-03-16 19:07:43 -07002139
2140 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2141
2142 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002143 SafeSetError("Failed to set remote video description send parameters.",
2144 error_desc);
2145 return false;
2146 }
2147 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002149 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2150 // and only give it to the media channel once we have a local
2151 // description too (without a local description, we won't be able to
2152 // recv them anyway).
2153 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2154 SafeSetError("Failed to set remote video description streams.", error_desc);
2155 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156 }
2157
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002158 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002159 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002161
2162 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002163 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002164 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165}
2166
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002167void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 case MSG_CHANNEL_ERROR: {
2170 const VideoChannelErrorMessageData* data =
2171 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172 delete data;
2173 break;
2174 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175 default:
2176 BaseChannel::OnMessage(pmsg);
2177 break;
2178 }
2179}
2180
2181void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002182 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183 SignalConnectionMonitor(this, infos);
2184}
2185
2186// TODO(pthatcher): Look into removing duplicate code between
2187// audio, video, and data, perhaps by using templates.
2188void VideoChannel::OnMediaMonitorUpdate(
2189 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002190 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002191 SignalMediaMonitor(this, info);
2192}
2193
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002194void VideoChannel::GetSrtpCryptoSuites_n(
2195 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002196 GetSupportedVideoCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197}
2198
deadbeef953c2ce2017-01-09 14:53:41 -08002199RtpDataChannel::RtpDataChannel(rtc::Thread* worker_thread,
2200 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002201 rtc::Thread* signaling_thread,
deadbeef953c2ce2017-01-09 14:53:41 -08002202 DataMediaChannel* media_channel,
deadbeef953c2ce2017-01-09 14:53:41 -08002203 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08002204 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002205 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002206 : BaseChannel(worker_thread,
2207 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002208 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002209 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002210 content_name,
deadbeefac22f702017-01-12 21:59:29 -08002211 rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002212 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213
deadbeef953c2ce2017-01-09 14:53:41 -08002214RtpDataChannel::~RtpDataChannel() {
2215 TRACE_EVENT0("webrtc", "RtpDataChannel::~RtpDataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216 StopMediaMonitor();
2217 // this can't be done in the base class, since it calls a virtual
2218 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002219
2220 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221}
2222
deadbeeff5346592017-01-24 21:51:21 -08002223bool RtpDataChannel::Init_w(
2224 DtlsTransportInternal* rtp_dtls_transport,
2225 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -08002226 rtc::PacketTransportInternal* rtp_packet_transport,
2227 rtc::PacketTransportInternal* rtcp_packet_transport) {
deadbeeff5346592017-01-24 21:51:21 -08002228 if (!BaseChannel::Init_w(rtp_dtls_transport, rtcp_dtls_transport,
2229 rtp_packet_transport, rtcp_packet_transport)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230 return false;
2231 }
deadbeef953c2ce2017-01-09 14:53:41 -08002232 media_channel()->SignalDataReceived.connect(this,
2233 &RtpDataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002234 media_channel()->SignalReadyToSend.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08002235 this, &RtpDataChannel::OnDataChannelReadyToSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 return true;
2237}
2238
deadbeef953c2ce2017-01-09 14:53:41 -08002239bool RtpDataChannel::SendData(const SendDataParams& params,
2240 const rtc::CopyOnWriteBuffer& payload,
2241 SendDataResult* result) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002242 return InvokeOnWorker(
2243 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2244 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245}
2246
deadbeef953c2ce2017-01-09 14:53:41 -08002247const ContentInfo* RtpDataChannel::GetFirstContent(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248 const SessionDescription* sdesc) {
2249 return GetFirstDataContent(sdesc);
2250}
2251
deadbeef953c2ce2017-01-09 14:53:41 -08002252bool RtpDataChannel::CheckDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002253 const DataContentDescription* content,
2254 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002255 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2256 (content->protocol() == kMediaProtocolDtlsSctp));
deadbeef953c2ce2017-01-09 14:53:41 -08002257 // It's been set before, but doesn't match. That's bad.
2258 if (is_sctp) {
2259 SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.",
2260 error_desc);
2261 return false;
2262 }
2263 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002264}
2265
deadbeef953c2ce2017-01-09 14:53:41 -08002266bool RtpDataChannel::SetLocalContent_w(const MediaContentDescription* content,
2267 ContentAction action,
2268 std::string* error_desc) {
2269 TRACE_EVENT0("webrtc", "RtpDataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002270 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271 LOG(LS_INFO) << "Setting local data description";
2272
2273 const DataContentDescription* data =
2274 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002275 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002276 if (!data) {
2277 SafeSetError("Can't find data content in local description.", error_desc);
2278 return false;
2279 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280
deadbeef953c2ce2017-01-09 14:53:41 -08002281 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 return false;
2283 }
2284
deadbeef953c2ce2017-01-09 14:53:41 -08002285 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
2286 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 }
2288
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002289 DataRecvParameters recv_params = last_recv_params_;
2290 RtpParametersFromMediaDescription(data, &recv_params);
2291 if (!media_channel()->SetRecvParameters(recv_params)) {
2292 SafeSetError("Failed to set remote data description recv parameters.",
2293 error_desc);
2294 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002295 }
deadbeef953c2ce2017-01-09 14:53:41 -08002296 for (const DataCodec& codec : data->codecs()) {
2297 bundle_filter()->AddPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002298 }
2299 last_recv_params_ = recv_params;
2300
2301 // TODO(pthatcher): Move local streams into DataSendParameters, and
2302 // only give it to the media channel once we have a remote
2303 // description too (without a remote description, we won't be able
2304 // to send them anyway).
2305 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2306 SafeSetError("Failed to set local data description streams.", error_desc);
2307 return false;
2308 }
2309
2310 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002311 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002312 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313}
2314
deadbeef953c2ce2017-01-09 14:53:41 -08002315bool RtpDataChannel::SetRemoteContent_w(const MediaContentDescription* content,
2316 ContentAction action,
2317 std::string* error_desc) {
2318 TRACE_EVENT0("webrtc", "RtpDataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002319 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320
2321 const DataContentDescription* data =
2322 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002323 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002324 if (!data) {
2325 SafeSetError("Can't find data content in remote description.", error_desc);
2326 return false;
2327 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002329 // If the remote data doesn't have codecs and isn't an update, it
2330 // must be empty, so ignore it.
2331 if (!data->has_codecs() && action != CA_UPDATE) {
2332 return true;
2333 }
2334
deadbeef953c2ce2017-01-09 14:53:41 -08002335 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002336 return false;
2337 }
2338
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002339 LOG(LS_INFO) << "Setting remote data description";
deadbeef953c2ce2017-01-09 14:53:41 -08002340 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002341 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342 }
2343
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002344 DataSendParameters send_params = last_send_params_;
2345 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2346 if (!media_channel()->SetSendParameters(send_params)) {
2347 SafeSetError("Failed to set remote data description send parameters.",
2348 error_desc);
2349 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002351 last_send_params_ = send_params;
2352
2353 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2354 // and only give it to the media channel once we have a local
2355 // description too (without a local description, we won't be able to
2356 // recv them anyway).
2357 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2358 SafeSetError("Failed to set remote data description streams.",
2359 error_desc);
2360 return false;
2361 }
2362
2363 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002364 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002365 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002366}
2367
deadbeef953c2ce2017-01-09 14:53:41 -08002368void RtpDataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369 // Render incoming data if we're the active call, and we have the local
2370 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002371 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002372 if (!media_channel()->SetReceive(recv)) {
2373 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2374 }
2375
2376 // Send outgoing data if we're the active call, we have the remote content,
2377 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002378 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002379 if (!media_channel()->SetSend(send)) {
2380 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2381 }
2382
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002383 // Trigger SignalReadyToSendData asynchronously.
2384 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002385
2386 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2387}
2388
deadbeef953c2ce2017-01-09 14:53:41 -08002389void RtpDataChannel::OnMessage(rtc::Message* pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002390 switch (pmsg->message_id) {
2391 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002392 DataChannelReadyToSendMessageData* data =
2393 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002394 ready_to_send_data_ = data->data();
2395 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002396 delete data;
2397 break;
2398 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002399 case MSG_DATARECEIVED: {
2400 DataReceivedMessageData* data =
2401 static_cast<DataReceivedMessageData*>(pmsg->pdata);
deadbeef953c2ce2017-01-09 14:53:41 -08002402 SignalDataReceived(data->params, data->payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002403 delete data;
2404 break;
2405 }
2406 case MSG_CHANNEL_ERROR: {
2407 const DataChannelErrorMessageData* data =
2408 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002409 delete data;
2410 break;
2411 }
2412 default:
2413 BaseChannel::OnMessage(pmsg);
2414 break;
2415 }
2416}
2417
deadbeef953c2ce2017-01-09 14:53:41 -08002418void RtpDataChannel::OnConnectionMonitorUpdate(
2419 ConnectionMonitor* monitor,
2420 const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002421 SignalConnectionMonitor(this, infos);
2422}
2423
deadbeef953c2ce2017-01-09 14:53:41 -08002424void RtpDataChannel::StartMediaMonitor(int cms) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002425 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002426 rtc::Thread::Current()));
deadbeef953c2ce2017-01-09 14:53:41 -08002427 media_monitor_->SignalUpdate.connect(this,
2428 &RtpDataChannel::OnMediaMonitorUpdate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002429 media_monitor_->Start(cms);
2430}
2431
deadbeef953c2ce2017-01-09 14:53:41 -08002432void RtpDataChannel::StopMediaMonitor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002433 if (media_monitor_) {
2434 media_monitor_->Stop();
2435 media_monitor_->SignalUpdate.disconnect(this);
2436 media_monitor_.reset();
2437 }
2438}
2439
deadbeef953c2ce2017-01-09 14:53:41 -08002440void RtpDataChannel::OnMediaMonitorUpdate(DataMediaChannel* media_channel,
2441 const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002442 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002443 SignalMediaMonitor(this, info);
2444}
2445
deadbeef953c2ce2017-01-09 14:53:41 -08002446void RtpDataChannel::OnDataReceived(const ReceiveDataParams& params,
2447 const char* data,
2448 size_t len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002449 DataReceivedMessageData* msg = new DataReceivedMessageData(
2450 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002451 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002452}
2453
deadbeef953c2ce2017-01-09 14:53:41 -08002454void RtpDataChannel::OnDataChannelError(uint32_t ssrc,
2455 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002456 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2457 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002458 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459}
2460
deadbeef953c2ce2017-01-09 14:53:41 -08002461void RtpDataChannel::OnDataChannelReadyToSend(bool writable) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002462 // This is usded for congestion control to indicate that the stream is ready
2463 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2464 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002465 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002466 new DataChannelReadyToSendMessageData(writable));
2467}
2468
deadbeef953c2ce2017-01-09 14:53:41 -08002469void RtpDataChannel::GetSrtpCryptoSuites_n(
2470 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002471 GetSupportedDataCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002472}
2473
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002474} // namespace cricket