blob: 00f78b36c981e7440ed369b76b1f8ae69d3d5a9a [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"
zhihuang38ede132017-06-15 12:52:32 -070026// Adding 'nogncheck' to disable the gn include headers check to support modular
27// WebRTC build targets.
28#include "webrtc/media/engine/webrtcvoiceengine.h" // nogncheck
deadbeef5bd5ca32017-02-10 11:31:50 -080029#include "webrtc/p2p/base/packettransportinternal.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010030#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031
32namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000033using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000034
deadbeef2d110be2016-01-13 12:00:26 -080035namespace {
kwiberg31022942016-03-11 14:18:21 -080036// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080037bool SetRawAudioSink_w(VoiceMediaChannel* channel,
38 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080039 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
40 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080041 return true;
42}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020043
44struct SendPacketMessageData : public rtc::MessageData {
45 rtc::CopyOnWriteBuffer packet;
46 rtc::PacketOptions options;
47};
48
isheriff6f8d6862016-05-26 11:24:55 -070049#if defined(ENABLE_EXTERNAL_AUTH)
50// Returns the named header extension if found among all extensions,
51// nullptr otherwise.
52const webrtc::RtpExtension* FindHeaderExtension(
53 const std::vector<webrtc::RtpExtension>& extensions,
54 const std::string& uri) {
55 for (const auto& extension : extensions) {
56 if (extension.uri == uri)
57 return &extension;
58 }
59 return nullptr;
60}
61#endif
62
deadbeef2d110be2016-01-13 12:00:26 -080063} // namespace
64
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000066 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020067 MSG_SEND_RTP_PACKET,
68 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 MSG_FIRSTPACKETRECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073};
74
75// Value specified in RFC 5764.
76static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
77
78static const int kAgcMinus10db = -10;
79
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000080static void SafeSetError(const std::string& message, std::string* error_desc) {
81 if (error_desc) {
82 *error_desc = message;
83 }
84}
85
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000086struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 : ssrc(in_ssrc), error(in_error) {}
90 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 VoiceMediaChannel::Error error;
92};
93
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000094struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020095 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020097 : ssrc(in_ssrc), error(in_error) {}
98 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 VideoMediaChannel::Error error;
100};
101
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000102struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200105 : ssrc(in_ssrc), error(in_error) {}
106 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 DataMediaChannel::Error error;
108};
109
jbaucheec21bd2016-03-20 06:15:43 -0700110static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 // Check the packet size. We could check the header too if needed.
zstein3dcf0e92017-06-01 13:22:42 -0700112 return packet && IsValidRtpRtcpPacketSize(rtcp, packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113}
114
115static bool IsReceiveContentDirection(MediaContentDirection direction) {
116 return direction == MD_SENDRECV || direction == MD_RECVONLY;
117}
118
119static bool IsSendContentDirection(MediaContentDirection direction) {
120 return direction == MD_SENDRECV || direction == MD_SENDONLY;
121}
122
123static const MediaContentDescription* GetContentDescription(
124 const ContentInfo* cinfo) {
125 if (cinfo == NULL)
126 return NULL;
127 return static_cast<const MediaContentDescription*>(cinfo->description);
128}
129
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700130template <class Codec>
131void RtpParametersFromMediaDescription(
132 const MediaContentDescriptionImpl<Codec>* desc,
133 RtpParameters<Codec>* params) {
134 // TODO(pthatcher): Remove this once we're sure no one will give us
135 // a description without codecs (currently a CA_UPDATE with just
136 // streams can).
137 if (desc->has_codecs()) {
138 params->codecs = desc->codecs();
139 }
140 // TODO(pthatcher): See if we really need
141 // rtp_header_extensions_set() and remove it if we don't.
142 if (desc->rtp_header_extensions_set()) {
143 params->extensions = desc->rtp_header_extensions();
144 }
deadbeef13871492015-12-09 12:37:51 -0800145 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700146}
147
nisse05103312016-03-16 02:22:50 -0700148template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700149void RtpSendParametersFromMediaDescription(
150 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700151 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700152 RtpParametersFromMediaDescription(desc, send_params);
153 send_params->max_bandwidth_bps = desc->bandwidth();
154}
155
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200156BaseChannel::BaseChannel(rtc::Thread* worker_thread,
157 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800158 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700159 MediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700160 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800161 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800162 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200163 : worker_thread_(worker_thread),
164 network_thread_(network_thread),
zhihuangf5b251b2017-01-12 19:37:48 -0800165 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 content_name_(content_name),
zstein56162b92017-04-24 16:54:35 -0700167 rtcp_mux_required_(rtcp_mux_required),
zsteind48dbda2017-04-04 19:45:57 -0700168 rtp_transport_(rtcp_mux_required),
deadbeef7af91dd2016-12-13 11:29:11 -0800169 srtp_required_(srtp_required),
michaelt79e05882016-11-08 02:50:09 -0800170 media_channel_(media_channel),
171 selected_candidate_pair_(nullptr) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700172 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
jbauchdfcab722017-03-06 00:14:10 -0800173#if defined(ENABLE_EXTERNAL_AUTH)
174 srtp_filter_.EnableExternalAuth();
175#endif
zstein56162b92017-04-24 16:54:35 -0700176 rtp_transport_.SignalReadyToSend.connect(
177 this, &BaseChannel::OnTransportReadyToSend);
zstein3dcf0e92017-06-01 13:22:42 -0700178 // TODO(zstein): RtpTransport::SignalPacketReceived will probably be replaced
179 // with a callback interface later so that the demuxer can select which
180 // channel to signal.
181 rtp_transport_.SignalPacketReceived.connect(this,
182 &BaseChannel::OnPacketReceived);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 LOG(LS_INFO) << "Created channel for " << content_name;
184}
185
186BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800187 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700188 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000189 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200191 // Eats any outstanding messages or packets.
192 worker_thread_->Clear(&invoker_);
193 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 // We must destroy the media channel before the transport channel, otherwise
195 // the media channel may try to send on the dead transport channel. NULLing
196 // is not an effective strategy since the sends will come on another thread.
197 delete media_channel_;
zhihuangf5b251b2017-01-12 19:37:48 -0800198 LOG(LS_INFO) << "Destroyed channel: " << content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200199}
200
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200201void BaseChannel::DisconnectTransportChannels_n() {
202 // Send any outstanding RTCP packets.
203 FlushRtcpMessages_n();
204
205 // Stop signals from transport channels, but keep them alive because
206 // media_channel may use them from a different thread.
zhihuangb2cdd932017-01-19 16:54:25 -0800207 if (rtp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800208 DisconnectFromDtlsTransport(rtp_dtls_transport_);
zsteind48dbda2017-04-04 19:45:57 -0700209 } else if (rtp_transport_.rtp_packet_transport()) {
210 DisconnectFromPacketTransport(rtp_transport_.rtp_packet_transport());
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200211 }
zhihuangb2cdd932017-01-19 16:54:25 -0800212 if (rtcp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800213 DisconnectFromDtlsTransport(rtcp_dtls_transport_);
zsteind48dbda2017-04-04 19:45:57 -0700214 } else if (rtp_transport_.rtcp_packet_transport()) {
215 DisconnectFromPacketTransport(rtp_transport_.rtcp_packet_transport());
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200216 }
217
zstein3dcf0e92017-06-01 13:22:42 -0700218 rtp_transport_.SetRtpPacketTransport(nullptr);
219 rtp_transport_.SetRtcpPacketTransport(nullptr);
220
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200221 // Clear pending read packets/messages.
222 network_thread_->Clear(&invoker_);
223 network_thread_->Clear(this);
224}
225
zhihuangb2cdd932017-01-19 16:54:25 -0800226bool BaseChannel::Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800227 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800228 rtc::PacketTransportInternal* rtp_packet_transport,
229 rtc::PacketTransportInternal* rtcp_packet_transport) {
skvlad6c87a672016-05-17 17:49:52 -0700230 if (!network_thread_->Invoke<bool>(
zhihuangb2cdd932017-01-19 16:54:25 -0800231 RTC_FROM_HERE, Bind(&BaseChannel::InitNetwork_n, this,
deadbeeff5346592017-01-24 21:51:21 -0800232 rtp_dtls_transport, rtcp_dtls_transport,
233 rtp_packet_transport, rtcp_packet_transport))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000234 return false;
235 }
deadbeeff5346592017-01-24 21:51:21 -0800236 // Both RTP and RTCP channels should be set, we can call SetInterface on
237 // the media channel and it can set network options.
238 RTC_DCHECK_RUN_ON(worker_thread_);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000239 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 return true;
241}
242
deadbeeff5346592017-01-24 21:51:21 -0800243bool BaseChannel::InitNetwork_n(
244 DtlsTransportInternal* rtp_dtls_transport,
245 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800246 rtc::PacketTransportInternal* rtp_packet_transport,
247 rtc::PacketTransportInternal* rtcp_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200248 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800249 SetTransports_n(rtp_dtls_transport, rtcp_dtls_transport, rtp_packet_transport,
250 rtcp_packet_transport);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200251
zstein56162b92017-04-24 16:54:35 -0700252 if (rtcp_mux_required_) {
deadbeefac22f702017-01-12 21:59:29 -0800253 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.
zsteind48dbda2017-04-04 19:45:57 -0700298 RTC_DCHECK(!rtp_transport_.rtp_packet_transport() || rtp_dtls_transport_);
deadbeeff5346592017-01-24 21:51:21 -0800299 } 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 }
zsteind48dbda2017-04-04 19:45:57 -0700315 if (rtp_packet_transport == rtp_transport_.rtp_packet_transport()) {
deadbeeff5346592017-01-24 21:51:21 -0800316 // 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();
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000345}
346
deadbeeff5346592017-01-24 21:51:21 -0800347void BaseChannel::SetTransport_n(
348 bool rtcp,
349 DtlsTransportInternal* new_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800350 rtc::PacketTransportInternal* new_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200351 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800352 DtlsTransportInternal*& old_dtls_transport =
zhihuangb2cdd932017-01-19 16:54:25 -0800353 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
zsteind48dbda2017-04-04 19:45:57 -0700354 rtc::PacketTransportInternal* old_packet_transport =
355 rtcp ? rtp_transport_.rtcp_packet_transport()
356 : rtp_transport_.rtp_packet_transport();
zhihuangb2cdd932017-01-19 16:54:25 -0800357
deadbeeff5346592017-01-24 21:51:21 -0800358 if (!old_packet_transport && !new_packet_transport) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700359 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000360 return;
361 }
zhihuangb2cdd932017-01-19 16:54:25 -0800362
deadbeeff5346592017-01-24 21:51:21 -0800363 RTC_DCHECK(old_packet_transport != new_packet_transport);
364 if (old_dtls_transport) {
365 DisconnectFromDtlsTransport(old_dtls_transport);
366 } else if (old_packet_transport) {
367 DisconnectFromPacketTransport(old_packet_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000368 }
369
zsteind48dbda2017-04-04 19:45:57 -0700370 if (rtcp) {
zstein56162b92017-04-24 16:54:35 -0700371 rtp_transport_.SetRtcpPacketTransport(new_packet_transport);
zsteind48dbda2017-04-04 19:45:57 -0700372 } else {
zstein56162b92017-04-24 16:54:35 -0700373 rtp_transport_.SetRtpPacketTransport(new_packet_transport);
zsteind48dbda2017-04-04 19:45:57 -0700374 }
deadbeeff5346592017-01-24 21:51:21 -0800375 old_dtls_transport = new_dtls_transport;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000376
deadbeeff5346592017-01-24 21:51:21 -0800377 // If there's no new transport, we're done after disconnecting from old one.
378 if (!new_packet_transport) {
379 return;
380 }
381
382 if (rtcp && new_dtls_transport) {
383 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
384 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
385 << "should never happen.";
386 }
zstein56162b92017-04-24 16:54:35 -0700387
deadbeeff5346592017-01-24 21:51:21 -0800388 if (new_dtls_transport) {
389 ConnectToDtlsTransport(new_dtls_transport);
390 } else {
391 ConnectToPacketTransport(new_packet_transport);
392 }
393 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
394 for (const auto& pair : socket_options) {
395 new_packet_transport->SetOption(pair.first, pair.second);
guoweis46383312015-12-17 16:45:59 -0800396 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000397}
398
deadbeeff5346592017-01-24 21:51:21 -0800399void BaseChannel::ConnectToDtlsTransport(DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200400 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000401
zstein56162b92017-04-24 16:54:35 -0700402 // TODO(zstein): de-dup with ConnectToPacketTransport
zhihuangb2cdd932017-01-19 16:54:25 -0800403 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
zhihuangb2cdd932017-01-19 16:54:25 -0800404 transport->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
405 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
406 transport->ice_transport()->SignalSelectedCandidatePairChanged.connect(
Honghai Zhangcc411c02016-03-29 17:27:21 -0700407 this, &BaseChannel::OnSelectedCandidatePairChanged);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000408}
409
deadbeeff5346592017-01-24 21:51:21 -0800410void BaseChannel::DisconnectFromDtlsTransport(
411 DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200412 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800413 OnSelectedCandidatePairChanged(transport->ice_transport(), nullptr, -1,
414 false);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000415
zhihuangb2cdd932017-01-19 16:54:25 -0800416 transport->SignalWritableState.disconnect(this);
zhihuangb2cdd932017-01-19 16:54:25 -0800417 transport->SignalDtlsState.disconnect(this);
418 transport->SignalSentPacket.disconnect(this);
419 transport->ice_transport()->SignalSelectedCandidatePairChanged.disconnect(
420 this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000421}
422
deadbeeff5346592017-01-24 21:51:21 -0800423void BaseChannel::ConnectToPacketTransport(
deadbeef5bd5ca32017-02-10 11:31:50 -0800424 rtc::PacketTransportInternal* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800425 RTC_DCHECK_RUN_ON(network_thread_);
426 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
deadbeeff5346592017-01-24 21:51:21 -0800427 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
428}
429
430void BaseChannel::DisconnectFromPacketTransport(
deadbeef5bd5ca32017-02-10 11:31:50 -0800431 rtc::PacketTransportInternal* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800432 RTC_DCHECK_RUN_ON(network_thread_);
433 transport->SignalWritableState.disconnect(this);
deadbeeff5346592017-01-24 21:51:21 -0800434 transport->SignalSentPacket.disconnect(this);
435}
436
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700438 worker_thread_->Invoke<void>(
439 RTC_FROM_HERE,
440 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
441 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 return true;
443}
444
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445bool BaseChannel::AddRecvStream(const StreamParams& sp) {
stefanf79ade12017-06-02 06:44:03 -0700446 return InvokeOnWorker<bool>(RTC_FROM_HERE,
447 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448}
449
Peter Boström0c4e06b2015-10-07 12:23:21 +0200450bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
stefanf79ade12017-06-02 06:44:03 -0700451 return InvokeOnWorker<bool>(
452 RTC_FROM_HERE, Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453}
454
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000455bool BaseChannel::AddSendStream(const StreamParams& sp) {
stefanf79ade12017-06-02 06:44:03 -0700456 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700457 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000458}
459
Peter Boström0c4e06b2015-10-07 12:23:21 +0200460bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
stefanf79ade12017-06-02 06:44:03 -0700461 return InvokeOnWorker<bool>(
462 RTC_FROM_HERE,
463 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000464}
465
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000467 ContentAction action,
468 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100469 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
stefanf79ade12017-06-02 06:44:03 -0700470 return InvokeOnWorker<bool>(
471 RTC_FROM_HERE,
472 Bind(&BaseChannel::SetLocalContent_w, this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473}
474
475bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000476 ContentAction action,
477 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100478 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
stefanf79ade12017-06-02 06:44:03 -0700479 return InvokeOnWorker<bool>(
480 RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w, this, content,
481 action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482}
483
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484void BaseChannel::StartConnectionMonitor(int cms) {
zhihuangb2cdd932017-01-19 16:54:25 -0800485 // We pass in the BaseChannel instead of the rtp_dtls_transport_
486 // because if the rtp_dtls_transport_ changes, the ConnectionMonitor
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000487 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200488 // We pass in the network thread because on that thread connection monitor
489 // will call BaseChannel::GetConnectionStats which must be called on the
490 // network thread.
491 connection_monitor_.reset(
492 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000493 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000495 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496}
497
498void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000499 if (connection_monitor_) {
500 connection_monitor_->Stop();
501 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 }
503}
504
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000505bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200506 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800507 if (!rtp_dtls_transport_) {
508 return false;
509 }
zhihuangb2cdd932017-01-19 16:54:25 -0800510 return rtp_dtls_transport_->ice_transport()->GetStats(infos);
zhihuangf5b251b2017-01-12 19:37:48 -0800511}
512
513bool BaseChannel::NeedsRtcpTransport() {
deadbeefac22f702017-01-12 21:59:29 -0800514 // If this BaseChannel doesn't require RTCP mux and we haven't fully
515 // negotiated RTCP mux, we need an RTCP transport.
zstein56162b92017-04-24 16:54:35 -0700516 return !rtcp_mux_required_ && !rtcp_mux_filter_.IsFullyActive();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000517}
518
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700519bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 // Receive data if we are enabled and have local content,
521 return enabled() && IsReceiveContentDirection(local_content_direction_);
522}
523
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700524bool BaseChannel::IsReadyToSendMedia_w() const {
525 // Need to access some state updated on the network thread.
526 return network_thread_->Invoke<bool>(
527 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
528}
529
530bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 // Send outgoing data if we are enabled, have local and remote content,
532 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800533 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700535 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200536 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537}
538
jbaucheec21bd2016-03-20 06:15:43 -0700539bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700540 const rtc::PacketOptions& options) {
541 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542}
543
jbaucheec21bd2016-03-20 06:15:43 -0700544bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700545 const rtc::PacketOptions& options) {
546 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547}
548
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000549int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200551 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700552 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200553}
554
555int BaseChannel::SetOption_n(SocketType type,
556 rtc::Socket::Option opt,
557 int value) {
558 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef5bd5ca32017-02-10 11:31:50 -0800559 rtc::PacketTransportInternal* transport = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000561 case ST_RTP:
zsteind48dbda2017-04-04 19:45:57 -0700562 transport = rtp_transport_.rtp_packet_transport();
deadbeefcbecd352015-09-23 11:50:27 -0700563 socket_options_.push_back(
564 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000565 break;
566 case ST_RTCP:
zsteind48dbda2017-04-04 19:45:57 -0700567 transport = rtp_transport_.rtcp_packet_transport();
deadbeefcbecd352015-09-23 11:50:27 -0700568 rtcp_socket_options_.push_back(
569 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000570 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 }
deadbeeff5346592017-01-24 21:51:21 -0800572 return transport ? transport->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573}
574
deadbeef5bd5ca32017-02-10 11:31:50 -0800575void BaseChannel::OnWritableState(rtc::PacketTransportInternal* transport) {
zsteind48dbda2017-04-04 19:45:57 -0700576 RTC_DCHECK(transport == rtp_transport_.rtp_packet_transport() ||
577 transport == rtp_transport_.rtcp_packet_transport());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200578 RTC_DCHECK(network_thread_->IsCurrent());
579 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580}
581
zhihuangb2cdd932017-01-19 16:54:25 -0800582void BaseChannel::OnDtlsState(DtlsTransportInternal* transport,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800583 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200584 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800585 return;
586 }
587
588 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
589 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
zhihuangb2cdd932017-01-19 16:54:25 -0800590 // cover other scenarios like the whole transport is writable (not just this
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800591 // TransportChannel) or when TransportChannel is attached after DTLS is
592 // negotiated.
593 if (state != DTLS_TRANSPORT_CONNECTED) {
594 srtp_filter_.ResetParams();
595 }
596}
597
Honghai Zhangcc411c02016-03-29 17:27:21 -0700598void BaseChannel::OnSelectedCandidatePairChanged(
zhihuangb2cdd932017-01-19 16:54:25 -0800599 IceTransportInternal* ice_transport,
Honghai Zhang52dce732016-03-31 12:37:31 -0700600 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700601 int last_sent_packet_id,
602 bool ready_to_send) {
deadbeeff5346592017-01-24 21:51:21 -0800603 RTC_DCHECK((rtp_dtls_transport_ &&
604 ice_transport == rtp_dtls_transport_->ice_transport()) ||
605 (rtcp_dtls_transport_ &&
606 ice_transport == rtcp_dtls_transport_->ice_transport()));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200607 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800608 selected_candidate_pair_ = selected_candidate_pair;
zhihuangb2cdd932017-01-19 16:54:25 -0800609 std::string transport_name = ice_transport->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700610 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700611 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700612 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700613 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700614 selected_candidate_pair->remote_candidate().network_id(),
615 last_sent_packet_id);
michaelt79e05882016-11-08 02:50:09 -0800616
617 UpdateTransportOverhead();
Honghai Zhangcc411c02016-03-29 17:27:21 -0700618 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200619 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700620 RTC_FROM_HERE, worker_thread_,
621 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
622 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700623}
624
zstein56162b92017-04-24 16:54:35 -0700625void BaseChannel::OnTransportReadyToSend(bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200626 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700627 RTC_FROM_HERE, worker_thread_,
zstein56162b92017-04-24 16:54:35 -0700628 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629}
630
stefanc1aeaf02015-10-15 07:26:07 -0700631bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700632 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700633 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200634 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
635 // If the thread is not our network thread, we will post to our network
636 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 // synchronize access to all the pieces of the send path, including
638 // SRTP and the inner workings of the transport channels.
639 // The only downside is that we can't return a proper failure code if
640 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200641 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200643 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
644 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800645 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700646 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700647 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 return true;
649 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200650 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651
652 // Now that we are on the correct thread, ensure we have a place to send this
653 // packet before doing anything. (We might get RTCP packets that we don't
654 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
655 // transport.
zstein56162b92017-04-24 16:54:35 -0700656 if (!rtp_transport_.IsWritable(rtcp)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 return false;
658 }
659
660 // Protect ourselves against crazy data.
661 if (!ValidPacket(rtcp, packet)) {
662 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
zstein3dcf0e92017-06-01 13:22:42 -0700663 << RtpRtcpStringLiteral(rtcp)
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000664 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 return false;
666 }
667
stefanc1aeaf02015-10-15 07:26:07 -0700668 rtc::PacketOptions updated_options;
669 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 // Protect if needed.
671 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200672 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200674 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000675 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000677 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
678 // inside libsrtp for a RTP packet. A external HMAC module will be writing
679 // a fake HMAC value. This is ONLY done for a RTP packet.
680 // Socket layer will update rtp sendtime extension header if present in
681 // packet with current time before updating the HMAC.
682#if !defined(ENABLE_EXTERNAL_AUTH)
683 res = srtp_filter_.ProtectRtp(
684 data, len, static_cast<int>(packet->capacity()), &len);
685#else
jbauchd48f4882017-03-01 15:34:36 -0800686 if (!srtp_filter_.IsExternalAuthActive()) {
687 res = srtp_filter_.ProtectRtp(
688 data, len, static_cast<int>(packet->capacity()), &len);
689 } else {
690 updated_options.packet_time_params.rtp_sendtime_extension_id =
691 rtp_abs_sendtime_extn_id_;
692 res = srtp_filter_.ProtectRtp(
693 data, len, static_cast<int>(packet->capacity()), &len,
694 &updated_options.packet_time_params.srtp_packet_index);
695 // If protection succeeds, let's get auth params from srtp.
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000696 if (res) {
jbauchd48f4882017-03-01 15:34:36 -0800697 uint8_t* auth_key = NULL;
698 int key_len;
699 res = srtp_filter_.GetRtpAuthParams(
700 &auth_key, &key_len,
701 &updated_options.packet_time_params.srtp_auth_tag_len);
702 if (res) {
703 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
704 updated_options.packet_time_params.srtp_auth_key.assign(
705 auth_key, auth_key + key_len);
706 }
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000707 }
708 }
709#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 if (!res) {
711 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200712 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 GetRtpSeqNum(data, len, &seq_num);
714 GetRtpSsrc(data, len, &ssrc);
715 LOG(LS_ERROR) << "Failed to protect " << content_name_
716 << " RTP packet: size=" << len
717 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
718 return false;
719 }
720 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000721 res = srtp_filter_.ProtectRtcp(data, len,
722 static_cast<int>(packet->capacity()),
723 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 if (!res) {
725 int type = -1;
726 GetRtcpType(data, len, &type);
727 LOG(LS_ERROR) << "Failed to protect " << content_name_
728 << " RTCP packet: size=" << len << ", type=" << type;
729 return false;
730 }
731 }
732
733 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000734 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800735 } else if (srtp_required_) {
deadbeef8f425f92016-12-01 12:26:27 -0800736 // The audio/video engines may attempt to send RTCP packets as soon as the
737 // streams are created, so don't treat this as an error for RTCP.
738 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809
739 if (rtcp) {
740 return false;
741 }
742 // However, there shouldn't be any RTP packets sent before SRTP is set up
743 // (and SetSend(true) is called).
744 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
745 << " and crypto is required";
nisseeb4ca4e2017-01-12 02:24:27 -0800746 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 return false;
748 }
749
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200751 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
zstein56162b92017-04-24 16:54:35 -0700752 return rtp_transport_.SendPacket(rtcp, packet, updated_options, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753}
754
zstein3dcf0e92017-06-01 13:22:42 -0700755bool BaseChannel::HandlesPayloadType(int packet_type) const {
756 return rtp_transport_.HandlesPayloadType(packet_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757}
758
zstein3dcf0e92017-06-01 13:22:42 -0700759void BaseChannel::OnPacketReceived(bool rtcp,
760 rtc::CopyOnWriteBuffer& packet,
761 const rtc::PacketTime& packet_time) {
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000762 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700764 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 }
766
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 // Unprotect the packet, if needed.
768 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200769 TRACE_EVENT0("webrtc", "SRTP Decode");
zstein3dcf0e92017-06-01 13:22:42 -0700770 char* data = packet.data<char>();
771 int len = static_cast<int>(packet.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 bool res;
773 if (!rtcp) {
774 res = srtp_filter_.UnprotectRtp(data, len, &len);
775 if (!res) {
776 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200777 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 GetRtpSeqNum(data, len, &seq_num);
779 GetRtpSsrc(data, len, &ssrc);
780 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
zstein3dcf0e92017-06-01 13:22:42 -0700781 << " RTP packet: size=" << len << ", seqnum=" << seq_num
782 << ", SSRC=" << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 return;
784 }
785 } else {
786 res = srtp_filter_.UnprotectRtcp(data, len, &len);
787 if (!res) {
788 int type = -1;
789 GetRtcpType(data, len, &type);
790 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
791 << " RTCP packet: size=" << len << ", type=" << type;
792 return;
793 }
794 }
795
zstein3dcf0e92017-06-01 13:22:42 -0700796 packet.SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800797 } else if (srtp_required_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 // Our session description indicates that SRTP is required, but we got a
799 // packet before our SRTP filter is active. This means either that
800 // a) we got SRTP packets before we received the SDES keys, in which case
801 // we can't decrypt it anyway, or
802 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
zhihuangb2cdd932017-01-19 16:54:25 -0800803 // transports, so we haven't yet extracted keys, even if DTLS did
804 // complete on the transport that the packets are being sent on. It's
805 // really good practice to wait for both RTP and RTCP to be good to go
806 // before sending media, to prevent weird failure modes, so it's fine
807 // for us to just eat packets here. This is all sidestepped if RTCP mux
808 // is used anyway.
zstein3dcf0e92017-06-01 13:22:42 -0700809 LOG(LS_WARNING) << "Can't process incoming " << RtpRtcpStringLiteral(rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 << " packet when SRTP is inactive and crypto is required";
811 return;
812 }
813
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200814 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700815 RTC_FROM_HERE, worker_thread_,
zstein3dcf0e92017-06-01 13:22:42 -0700816 Bind(&BaseChannel::ProcessPacket, this, rtcp, packet, packet_time));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200817}
818
zstein3dcf0e92017-06-01 13:22:42 -0700819void BaseChannel::ProcessPacket(bool rtcp,
820 const rtc::CopyOnWriteBuffer& packet,
821 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200822 RTC_DCHECK(worker_thread_->IsCurrent());
zstein3dcf0e92017-06-01 13:22:42 -0700823
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200824 // Need to copy variable because OnRtcpReceived/OnPacketReceived
825 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
826 rtc::CopyOnWriteBuffer data(packet);
827 if (rtcp) {
828 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200830 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831 }
832}
833
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000834bool BaseChannel::PushdownLocalDescription(
835 const SessionDescription* local_desc, ContentAction action,
836 std::string* error_desc) {
837 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 const MediaContentDescription* content_desc =
839 GetContentDescription(content_info);
840 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000841 !SetLocalContent(content_desc, action, error_desc)) {
842 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
843 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000845 return true;
846}
847
848bool BaseChannel::PushdownRemoteDescription(
849 const SessionDescription* remote_desc, ContentAction action,
850 std::string* error_desc) {
851 const ContentInfo* content_info = GetFirstContent(remote_desc);
852 const MediaContentDescription* content_desc =
853 GetContentDescription(content_info);
854 if (content_desc && content_info && !content_info->rejected &&
855 !SetRemoteContent(content_desc, action, error_desc)) {
856 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
857 return false;
858 }
859 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860}
861
862void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700863 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 if (enabled_)
865 return;
866
867 LOG(LS_INFO) << "Channel enabled";
868 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700869 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870}
871
872void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700873 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 if (!enabled_)
875 return;
876
877 LOG(LS_INFO) << "Channel disabled";
878 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700879 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880}
881
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200882void BaseChannel::UpdateWritableState_n() {
zsteind48dbda2017-04-04 19:45:57 -0700883 rtc::PacketTransportInternal* rtp_packet_transport =
884 rtp_transport_.rtp_packet_transport();
885 rtc::PacketTransportInternal* rtcp_packet_transport =
886 rtp_transport_.rtcp_packet_transport();
887 if (rtp_packet_transport && rtp_packet_transport->writable() &&
888 (!rtcp_packet_transport || rtcp_packet_transport->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200889 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700890 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200891 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700892 }
893}
894
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200895void BaseChannel::ChannelWritable_n() {
896 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800897 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800899 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900
deadbeefcbecd352015-09-23 11:50:27 -0700901 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 << (was_ever_writable_ ? "" : " for the first time");
903
michaelt79e05882016-11-08 02:50:09 -0800904 if (selected_candidate_pair_)
905 LOG(LS_INFO)
906 << "Using "
907 << selected_candidate_pair_->local_candidate().ToSensitiveString()
908 << "->"
909 << selected_candidate_pair_->remote_candidate().ToSensitiveString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200912 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700914 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915}
916
deadbeef953c2ce2017-01-09 14:53:41 -0800917void BaseChannel::SignalDtlsSrtpSetupFailure_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200918 RTC_DCHECK(network_thread_->IsCurrent());
919 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700920 RTC_FROM_HERE, signaling_thread(),
deadbeef953c2ce2017-01-09 14:53:41 -0800921 Bind(&BaseChannel::SignalDtlsSrtpSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000922}
923
deadbeef953c2ce2017-01-09 14:53:41 -0800924void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700925 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
deadbeef953c2ce2017-01-09 14:53:41 -0800926 SignalDtlsSrtpSetupFailure(this, rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000927}
928
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200929bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
zhihuangb2cdd932017-01-19 16:54:25 -0800930 // Since DTLS is applied to all transports, checking RTP should be enough.
931 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932}
933
934// This function returns true if either DTLS-SRTP is not in use
935// *or* DTLS-SRTP is successfully set up.
zhihuangb2cdd932017-01-19 16:54:25 -0800936bool BaseChannel::SetupDtlsSrtp_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200937 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 bool ret = false;
939
zhihuangb2cdd932017-01-19 16:54:25 -0800940 DtlsTransportInternal* transport =
941 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
deadbeeff5346592017-01-24 21:51:21 -0800942 RTC_DCHECK(transport);
zhihuangb2cdd932017-01-19 16:54:25 -0800943 RTC_DCHECK(transport->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800945 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946
zhihuangb2cdd932017-01-19 16:54:25 -0800947 if (!transport->GetSrtpCryptoSuite(&selected_crypto_suite)) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800948 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 return false;
950 }
951
zhihuangb2cdd932017-01-19 16:54:25 -0800952 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " << content_name() << " "
zstein3dcf0e92017-06-01 13:22:42 -0700953 << RtpRtcpStringLiteral(rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954
jbauchcb560652016-08-04 05:20:32 -0700955 int key_len;
956 int salt_len;
957 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
958 &salt_len)) {
959 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
960 return false;
961 }
962
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -0700964 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965
966 // RFC 5705 exporter using the RFC 5764 parameters
zhihuangb2cdd932017-01-19 16:54:25 -0800967 if (!transport->ExportKeyingMaterial(kDtlsSrtpExporterLabel, NULL, 0, false,
968 &dtls_buffer[0], dtls_buffer.size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
nisseeb4ca4e2017-01-12 02:24:27 -0800970 RTC_NOTREACHED(); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 return false;
972 }
973
974 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -0700975 std::vector<unsigned char> client_write_key(key_len + salt_len);
976 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -0700978 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
979 offset += key_len;
980 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
981 offset += key_len;
982 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
983 offset += salt_len;
984 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985
986 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000987 rtc::SSLRole role;
zhihuangb2cdd932017-01-19 16:54:25 -0800988 if (!transport->GetSslRole(&role)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000989 LOG(LS_WARNING) << "GetSslRole failed";
990 return false;
991 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000993 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 send_key = &server_write_key;
995 recv_key = &client_write_key;
996 } else {
997 send_key = &client_write_key;
998 recv_key = &server_write_key;
999 }
1000
zhihuangb2cdd932017-01-19 16:54:25 -08001001 if (rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001002 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1003 static_cast<int>(send_key->size()),
1004 selected_crypto_suite, &(*recv_key)[0],
1005 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001007 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1008 static_cast<int>(send_key->size()),
1009 selected_crypto_suite, &(*recv_key)[0],
1010 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 }
1012
michaelt79e05882016-11-08 02:50:09 -08001013 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
michaelt79e05882016-11-08 02:50:09 -08001015 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 dtls_keyed_ = true;
michaelt79e05882016-11-08 02:50:09 -08001017 UpdateTransportOverhead();
1018 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 return ret;
1020}
1021
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001022void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001023 if (srtp_filter_.IsActive()) {
1024 return;
1025 }
1026
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001027 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001028 return;
1029 }
1030
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001031 if (!SetupDtlsSrtp_n(false)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001032 SignalDtlsSrtpSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001033 return;
1034 }
1035
zhihuangb2cdd932017-01-19 16:54:25 -08001036 if (rtcp_dtls_transport_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001037 if (!SetupDtlsSrtp_n(true)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001038 SignalDtlsSrtpSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001039 return;
1040 }
1041 }
1042}
1043
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001044void BaseChannel::ChannelNotWritable_n() {
1045 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 if (!writable_)
1047 return;
1048
deadbeefcbecd352015-09-23 11:50:27 -07001049 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001051 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052}
1053
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001054bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001055 const MediaContentDescription* content,
1056 ContentAction action,
1057 ContentSource src,
1058 std::string* error_desc) {
1059 if (action == CA_UPDATE) {
1060 // These parameters never get changed by a CA_UDPATE.
1061 return true;
1062 }
1063
deadbeef7af91dd2016-12-13 11:29:11 -08001064 // Cache srtp_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001065 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001066 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1067 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001068}
1069
1070bool BaseChannel::SetRtpTransportParameters_n(
1071 const MediaContentDescription* content,
1072 ContentAction action,
1073 ContentSource src,
1074 std::string* error_desc) {
1075 RTC_DCHECK(network_thread_->IsCurrent());
1076
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001077 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001078 return false;
1079 }
1080
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001081 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001082 return false;
1083 }
1084
1085 return true;
1086}
1087
zhihuangb2cdd932017-01-19 16:54:25 -08001088// |dtls| will be set to true if DTLS is active for transport and crypto is
1089// empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001090bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1091 bool* dtls,
1092 std::string* error_desc) {
deadbeeff5346592017-01-24 21:51:21 -08001093 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001094 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001095 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001096 return false;
1097 }
1098 return true;
1099}
1100
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001101bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001102 ContentAction action,
1103 ContentSource src,
1104 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001105 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001106 if (action == CA_UPDATE) {
1107 // no crypto params.
1108 return true;
1109 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001111 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001112 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001113 if (!ret) {
1114 return false;
1115 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001116 switch (action) {
1117 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001118 // If DTLS is already active on the channel, we could be renegotiating
1119 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001120 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001121 ret = srtp_filter_.SetOffer(cryptos, src);
1122 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123 break;
1124 case CA_PRANSWER:
1125 // If we're doing DTLS-SRTP, we don't want to update the filter
1126 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001127 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1129 }
1130 break;
1131 case CA_ANSWER:
1132 // If we're doing DTLS-SRTP, we don't want to update the filter
1133 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001134 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 ret = srtp_filter_.SetAnswer(cryptos, src);
1136 }
1137 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 default:
1139 break;
1140 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001141 if (!ret) {
1142 SafeSetError("Failed to setup SRTP filter.", error_desc);
1143 return false;
1144 }
1145 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146}
1147
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001148bool BaseChannel::SetRtcpMux_n(bool enable,
1149 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001150 ContentSource src,
1151 std::string* error_desc) {
deadbeef8e814d72017-01-13 11:34:39 -08001152 // Provide a more specific error message for the RTCP mux "require" policy
1153 // case.
zstein56162b92017-04-24 16:54:35 -07001154 if (rtcp_mux_required_ && !enable) {
deadbeef8e814d72017-01-13 11:34:39 -08001155 SafeSetError(
1156 "rtcpMuxPolicy is 'require', but media description does not "
1157 "contain 'a=rtcp-mux'.",
1158 error_desc);
1159 return false;
1160 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 bool ret = false;
1162 switch (action) {
1163 case CA_OFFER:
1164 ret = rtcp_mux_filter_.SetOffer(enable, src);
1165 break;
1166 case CA_PRANSWER:
zhihuangb2cdd932017-01-19 16:54:25 -08001167 // This may activate RTCP muxing, but we don't yet destroy the transport
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001168 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1170 break;
1171 case CA_ANSWER:
1172 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1173 if (ret && rtcp_mux_filter_.IsActive()) {
deadbeefe814a0d2017-02-25 18:15:09 -08001174 // We permanently activated RTCP muxing; signal that we no longer need
1175 // the RTCP transport.
zsteind48dbda2017-04-04 19:45:57 -07001176 std::string debug_name =
1177 transport_name_.empty()
1178 ? rtp_transport_.rtp_packet_transport()->debug_name()
1179 : transport_name_;
deadbeefe814a0d2017-02-25 18:15:09 -08001180 ;
deadbeefcbecd352015-09-23 11:50:27 -07001181 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
deadbeefe814a0d2017-02-25 18:15:09 -08001182 << "; no longer need RTCP transport for " << debug_name;
zsteind48dbda2017-04-04 19:45:57 -07001183 if (rtp_transport_.rtcp_packet_transport()) {
deadbeeff5346592017-01-24 21:51:21 -08001184 SetTransport_n(true, nullptr, nullptr);
1185 SignalRtcpMuxFullyActive(transport_name_);
zhihuangf5b251b2017-01-12 19:37:48 -08001186 }
deadbeef062ce9f2016-08-26 21:42:15 -07001187 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 }
1189 break;
1190 case CA_UPDATE:
1191 // No RTCP mux info.
1192 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001193 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 default:
1195 break;
1196 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001197 if (!ret) {
1198 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1199 return false;
1200 }
zstein56162b92017-04-24 16:54:35 -07001201 rtp_transport_.SetRtcpMuxEnabled(rtcp_mux_filter_.IsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
zhihuangb2cdd932017-01-19 16:54:25 -08001203 // CA_ANSWER, but we only want to tear down the RTCP transport if we received
1204 // a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001205 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 // If the RTP transport is already writable, then so are we.
zsteind48dbda2017-04-04 19:45:57 -07001207 if (rtp_transport_.rtp_packet_transport()->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001208 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 }
1210 }
1211
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001212 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213}
1214
1215bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001216 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001217 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218}
1219
Peter Boström0c4e06b2015-10-07 12:23:21 +02001220bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001221 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 return media_channel()->RemoveRecvStream(ssrc);
1223}
1224
1225bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001226 ContentAction action,
1227 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001228 if (!(action == CA_OFFER || action == CA_ANSWER ||
1229 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 return false;
1231
1232 // If this is an update, streams only contain streams that have changed.
1233 if (action == CA_UPDATE) {
1234 for (StreamParamsVec::const_iterator it = streams.begin();
1235 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001236 const StreamParams* existing_stream =
1237 GetStreamByIds(local_streams_, it->groupid, it->id);
1238 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 if (media_channel()->AddSendStream(*it)) {
1240 local_streams_.push_back(*it);
1241 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1242 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001243 std::ostringstream desc;
1244 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1245 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 return false;
1247 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001248 } else if (existing_stream && !it->has_ssrcs()) {
1249 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001250 std::ostringstream desc;
1251 desc << "Failed to remove send stream with ssrc "
1252 << it->first_ssrc() << ".";
1253 SafeSetError(desc.str(), error_desc);
1254 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001256 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 } else {
1258 LOG(LS_WARNING) << "Ignore unsupported stream update";
1259 }
1260 }
1261 return true;
1262 }
1263 // Else streams are all the streams we want to send.
1264
1265 // Check for streams that have been removed.
1266 bool ret = true;
1267 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1268 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001269 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001271 std::ostringstream desc;
1272 desc << "Failed to remove send stream with ssrc "
1273 << it->first_ssrc() << ".";
1274 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 ret = false;
1276 }
1277 }
1278 }
1279 // Check for new streams.
1280 for (StreamParamsVec::const_iterator it = streams.begin();
1281 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001282 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001284 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001286 std::ostringstream desc;
1287 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1288 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 ret = false;
1290 }
1291 }
1292 }
1293 local_streams_ = streams;
1294 return ret;
1295}
1296
1297bool BaseChannel::UpdateRemoteStreams_w(
1298 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001299 ContentAction action,
1300 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001301 if (!(action == CA_OFFER || action == CA_ANSWER ||
1302 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 return false;
1304
1305 // If this is an update, streams only contain streams that have changed.
1306 if (action == CA_UPDATE) {
1307 for (StreamParamsVec::const_iterator it = streams.begin();
1308 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001309 const StreamParams* existing_stream =
1310 GetStreamByIds(remote_streams_, it->groupid, it->id);
1311 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 if (AddRecvStream_w(*it)) {
1313 remote_streams_.push_back(*it);
1314 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1315 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001316 std::ostringstream desc;
1317 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1318 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 return false;
1320 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001321 } else if (existing_stream && !it->has_ssrcs()) {
1322 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001323 std::ostringstream desc;
1324 desc << "Failed to remove remote stream with ssrc "
1325 << it->first_ssrc() << ".";
1326 SafeSetError(desc.str(), error_desc);
1327 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001329 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 } else {
1331 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001332 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 << " new stream = " << it->ToString();
1334 }
1335 }
1336 return true;
1337 }
1338 // Else streams are all the streams we want to receive.
1339
1340 // Check for streams that have been removed.
1341 bool ret = true;
1342 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1343 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001344 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001346 std::ostringstream desc;
1347 desc << "Failed to remove remote stream with ssrc "
1348 << it->first_ssrc() << ".";
1349 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 ret = false;
1351 }
1352 }
1353 }
1354 // Check for new streams.
1355 for (StreamParamsVec::const_iterator it = streams.begin();
1356 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001357 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 if (AddRecvStream_w(*it)) {
1359 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1360 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001361 std::ostringstream desc;
1362 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1363 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 ret = false;
1365 }
1366 }
1367 }
1368 remote_streams_ = streams;
1369 return ret;
1370}
1371
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001372void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001373 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001374// Absolute Send Time extension id is used only with external auth,
1375// so do not bother searching for it and making asyncronious call to set
1376// something that is not used.
1377#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001378 const webrtc::RtpExtension* send_time_extension =
1379 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001380 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001381 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001382 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001383 RTC_FROM_HERE, network_thread_,
1384 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1385 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001386#endif
1387}
1388
1389void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1390 int rtp_abs_sendtime_extn_id) {
1391 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001392}
1393
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001394void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001395 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001396 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001397 case MSG_SEND_RTP_PACKET:
1398 case MSG_SEND_RTCP_PACKET: {
1399 RTC_DCHECK(network_thread_->IsCurrent());
1400 SendPacketMessageData* data =
1401 static_cast<SendPacketMessageData*>(pmsg->pdata);
1402 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1403 SendPacket(rtcp, &data->packet, data->options);
1404 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001405 break;
1406 }
1407 case MSG_FIRSTPACKETRECEIVED: {
1408 SignalFirstPacketReceived(this);
1409 break;
1410 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001411 }
1412}
1413
zstein3dcf0e92017-06-01 13:22:42 -07001414void BaseChannel::AddHandledPayloadType(int payload_type) {
1415 rtp_transport_.AddHandledPayloadType(payload_type);
1416}
1417
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001418void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419 // Flush all remaining RTCP messages. This should only be called in
1420 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001421 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001422 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001423 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1424 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001425 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1426 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 }
1428}
1429
johand89ab142016-10-25 10:50:32 -07001430void BaseChannel::SignalSentPacket_n(
deadbeef5bd5ca32017-02-10 11:31:50 -08001431 rtc::PacketTransportInternal* /* transport */,
johand89ab142016-10-25 10:50:32 -07001432 const rtc::SentPacket& sent_packet) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001433 RTC_DCHECK(network_thread_->IsCurrent());
1434 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001435 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001436 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1437}
1438
1439void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1440 RTC_DCHECK(worker_thread_->IsCurrent());
1441 SignalSentPacket(sent_packet);
1442}
1443
1444VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1445 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001446 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447 MediaEngineInterface* media_engine,
1448 VoiceMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001450 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001451 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001452 : BaseChannel(worker_thread,
1453 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001454 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001455 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001456 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001457 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001458 srtp_required),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001459 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001460 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461
1462VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001463 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464 StopAudioMonitor();
1465 StopMediaMonitor();
1466 // this can't be done in the base class, since it calls a virtual
1467 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001468 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469}
1470
Peter Boström0c4e06b2015-10-07 12:23:21 +02001471bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001472 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001473 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001474 AudioSource* source) {
stefanf79ade12017-06-02 06:44:03 -07001475 return InvokeOnWorker<bool>(
1476 RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
1477 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478}
1479
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480// TODO(juberti): Handle early media the right way. We should get an explicit
1481// ringing message telling us to start playing local ringback, which we cancel
1482// if any early media actually arrives. For now, we do the opposite, which is
1483// to wait 1 second for early media, and start playing local ringback if none
1484// arrives.
1485void VoiceChannel::SetEarlyMedia(bool enable) {
1486 if (enable) {
1487 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001488 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1489 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 } else {
1491 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001492 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 }
1494}
1495
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496bool VoiceChannel::CanInsertDtmf() {
stefanf79ade12017-06-02 06:44:03 -07001497 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001498 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499}
1500
Peter Boström0c4e06b2015-10-07 12:23:21 +02001501bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1502 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001503 int duration) {
stefanf79ade12017-06-02 06:44:03 -07001504 return InvokeOnWorker<bool>(
1505 RTC_FROM_HERE,
1506 Bind(&VoiceChannel::InsertDtmf_w, this, ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507}
1508
solenberg4bac9c52015-10-09 02:32:53 -07001509bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
stefanf79ade12017-06-02 06:44:03 -07001510 return InvokeOnWorker<bool>(
1511 RTC_FROM_HERE,
1512 Bind(&VoiceMediaChannel::SetOutputVolume, media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001514
Tommif888bb52015-12-12 01:37:01 +01001515void VoiceChannel::SetRawAudioSink(
1516 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001517 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1518 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001519 // passing. So we invoke to our own little routine that gets a pointer to
1520 // our local variable. This is OK since we're synchronously invoking.
stefanf79ade12017-06-02 06:44:03 -07001521 InvokeOnWorker<bool>(RTC_FROM_HERE,
1522 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001523}
1524
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001525webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001526 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001527 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001528}
1529
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001530webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1531 uint32_t ssrc) const {
1532 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001533}
1534
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001535bool VoiceChannel::SetRtpSendParameters(
1536 uint32_t ssrc,
1537 const webrtc::RtpParameters& parameters) {
stefanf79ade12017-06-02 06:44:03 -07001538 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001539 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001540 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001541}
1542
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001543bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1544 webrtc::RtpParameters parameters) {
1545 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1546}
1547
1548webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1549 uint32_t ssrc) const {
1550 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001551 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001552 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1553}
1554
1555webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1556 uint32_t ssrc) const {
1557 return media_channel()->GetRtpReceiveParameters(ssrc);
1558}
1559
1560bool VoiceChannel::SetRtpReceiveParameters(
1561 uint32_t ssrc,
1562 const webrtc::RtpParameters& parameters) {
stefanf79ade12017-06-02 06:44:03 -07001563 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001564 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001565 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1566}
1567
1568bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1569 webrtc::RtpParameters parameters) {
1570 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001571}
1572
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
stefanf79ade12017-06-02 06:44:03 -07001574 return InvokeOnWorker<bool>(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1575 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576}
1577
hbos8d609f62017-04-10 07:39:05 -07001578std::vector<webrtc::RtpSource> VoiceChannel::GetSources(uint32_t ssrc) const {
1579 return worker_thread()->Invoke<std::vector<webrtc::RtpSource>>(
zhihuang38ede132017-06-15 12:52:32 -07001580 RTC_FROM_HERE, Bind(&VoiceChannel::GetSources_w, this, ssrc));
1581}
1582
1583std::vector<webrtc::RtpSource> VoiceChannel::GetSources_w(uint32_t ssrc) const {
1584 RTC_DCHECK(worker_thread()->IsCurrent());
1585 return media_channel()->GetSources(ssrc);
hbos8d609f62017-04-10 07:39:05 -07001586}
1587
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588void VoiceChannel::StartMediaMonitor(int cms) {
1589 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001590 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 media_monitor_->SignalUpdate.connect(
1592 this, &VoiceChannel::OnMediaMonitorUpdate);
1593 media_monitor_->Start(cms);
1594}
1595
1596void VoiceChannel::StopMediaMonitor() {
1597 if (media_monitor_) {
1598 media_monitor_->Stop();
1599 media_monitor_->SignalUpdate.disconnect(this);
1600 media_monitor_.reset();
1601 }
1602}
1603
1604void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001605 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 audio_monitor_
1607 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1608 audio_monitor_->Start(cms);
1609}
1610
1611void VoiceChannel::StopAudioMonitor() {
1612 if (audio_monitor_) {
1613 audio_monitor_->Stop();
1614 audio_monitor_.reset();
1615 }
1616}
1617
1618bool VoiceChannel::IsAudioMonitorRunning() const {
1619 return (audio_monitor_.get() != NULL);
1620}
1621
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001623 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624}
1625
1626int VoiceChannel::GetOutputLevel_w() {
1627 return media_channel()->GetOutputLevel();
1628}
1629
1630void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1631 media_channel()->GetActiveStreams(actives);
1632}
1633
zstein3dcf0e92017-06-01 13:22:42 -07001634void VoiceChannel::OnPacketReceived(bool rtcp,
1635 rtc::CopyOnWriteBuffer& packet,
1636 const rtc::PacketTime& packet_time) {
1637 BaseChannel::OnPacketReceived(rtcp, packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 // Set a flag when we've received an RTP packet. If we're waiting for early
1639 // media, this will disable the timeout.
zstein3dcf0e92017-06-01 13:22:42 -07001640 if (!received_media_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 received_media_ = true;
1642 }
1643}
1644
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001645void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001646 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001647 invoker_.AsyncInvoke<void>(
1648 RTC_FROM_HERE, worker_thread_,
1649 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001650}
1651
michaelt79e05882016-11-08 02:50:09 -08001652int BaseChannel::GetTransportOverheadPerPacket() const {
1653 RTC_DCHECK(network_thread_->IsCurrent());
1654
1655 if (!selected_candidate_pair_)
1656 return 0;
1657
1658 int transport_overhead_per_packet = 0;
1659
1660 constexpr int kIpv4Overhaed = 20;
1661 constexpr int kIpv6Overhaed = 40;
1662 transport_overhead_per_packet +=
1663 selected_candidate_pair_->local_candidate().address().family() == AF_INET
1664 ? kIpv4Overhaed
1665 : kIpv6Overhaed;
1666
1667 constexpr int kUdpOverhaed = 8;
1668 constexpr int kTcpOverhaed = 20;
1669 transport_overhead_per_packet +=
1670 selected_candidate_pair_->local_candidate().protocol() ==
1671 TCP_PROTOCOL_NAME
1672 ? kTcpOverhaed
1673 : kUdpOverhaed;
1674
1675 if (secure()) {
1676 int srtp_overhead = 0;
1677 if (srtp_filter_.GetSrtpOverhead(&srtp_overhead))
1678 transport_overhead_per_packet += srtp_overhead;
1679 }
1680
1681 return transport_overhead_per_packet;
1682}
1683
1684void BaseChannel::UpdateTransportOverhead() {
1685 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1686 if (transport_overhead_per_packet)
1687 invoker_.AsyncInvoke<void>(
1688 RTC_FROM_HERE, worker_thread_,
1689 Bind(&MediaChannel::OnTransportOverheadChanged, media_channel_,
1690 transport_overhead_per_packet));
1691}
1692
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001693void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 // Render incoming data if we're the active call, and we have the local
1695 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001696 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001697 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698
1699 // Send outgoing data if we're the active call, we have the remote content,
1700 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001701 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001702 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703
1704 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1705}
1706
1707const ContentInfo* VoiceChannel::GetFirstContent(
1708 const SessionDescription* sdesc) {
1709 return GetFirstAudioContent(sdesc);
1710}
1711
1712bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001713 ContentAction action,
1714 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001715 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001716 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 LOG(LS_INFO) << "Setting local voice description";
1718
1719 const AudioContentDescription* audio =
1720 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001721 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001722 if (!audio) {
1723 SafeSetError("Can't find audio content in local description.", error_desc);
1724 return false;
1725 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001727 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001728 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 }
1730
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001731 AudioRecvParameters recv_params = last_recv_params_;
1732 RtpParametersFromMediaDescription(audio, &recv_params);
1733 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001734 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001735 error_desc);
1736 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001738 for (const AudioCodec& codec : audio->codecs()) {
zstein3dcf0e92017-06-01 13:22:42 -07001739 AddHandledPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001740 }
1741 last_recv_params_ = recv_params;
1742
1743 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1744 // only give it to the media channel once we have a remote
1745 // description too (without a remote description, we won't be able
1746 // to send them anyway).
1747 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1748 SafeSetError("Failed to set local audio description streams.", error_desc);
1749 return false;
1750 }
1751
1752 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001753 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001754 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755}
1756
1757bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001758 ContentAction action,
1759 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001760 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001761 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762 LOG(LS_INFO) << "Setting remote voice description";
1763
1764 const AudioContentDescription* audio =
1765 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001766 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001767 if (!audio) {
1768 SafeSetError("Can't find audio content in remote description.", error_desc);
1769 return false;
1770 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001772 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001773 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 }
1775
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001776 AudioSendParameters send_params = last_send_params_;
1777 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001778 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001779 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001780 }
skvladdc1c62c2016-03-16 19:07:43 -07001781
1782 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1783 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001784 SafeSetError("Failed to set remote audio description send parameters.",
1785 error_desc);
1786 return false;
1787 }
1788 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001790 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1791 // and only give it to the media channel once we have a local
1792 // description too (without a local description, we won't be able to
1793 // recv them anyway).
1794 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1795 SafeSetError("Failed to set remote audio description streams.", error_desc);
1796 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 }
1798
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001799 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001800 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001801 }
1802
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001803 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001804 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001805 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806}
1807
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808void VoiceChannel::HandleEarlyMediaTimeout() {
1809 // This occurs on the main thread, not the worker thread.
1810 if (!received_media_) {
1811 LOG(LS_INFO) << "No early media received before timeout";
1812 SignalEarlyMediaTimeout(this);
1813 }
1814}
1815
Peter Boström0c4e06b2015-10-07 12:23:21 +02001816bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1817 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001818 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 if (!enabled()) {
1820 return false;
1821 }
solenberg1d63dd02015-12-02 12:35:09 -08001822 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823}
1824
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001825void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 case MSG_EARLYMEDIATIMEOUT:
1828 HandleEarlyMediaTimeout();
1829 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 case MSG_CHANNEL_ERROR: {
1831 VoiceChannelErrorMessageData* data =
1832 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833 delete data;
1834 break;
1835 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836 default:
1837 BaseChannel::OnMessage(pmsg);
1838 break;
1839 }
1840}
1841
1842void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001843 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 SignalConnectionMonitor(this, infos);
1845}
1846
1847void VoiceChannel::OnMediaMonitorUpdate(
1848 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001849 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 SignalMediaMonitor(this, info);
1851}
1852
1853void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1854 const AudioInfo& info) {
1855 SignalAudioMonitor(this, info);
1856}
1857
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001858VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1859 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001860 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 VideoMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001863 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001864 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001865 : BaseChannel(worker_thread,
1866 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001867 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001868 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001869 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001870 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001871 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001874 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 StopMediaMonitor();
1876 // this can't be done in the base class, since it calls a virtual
1877 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001878
1879 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880}
1881
nisse08582ff2016-02-04 01:24:52 -08001882bool VideoChannel::SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -08001883 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
nisse08582ff2016-02-04 01:24:52 -08001884 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001885 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001886 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 return true;
1888}
1889
deadbeef5a4a75a2016-06-02 16:23:38 -07001890bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001891 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001892 bool mute,
1893 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -08001894 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
stefanf79ade12017-06-02 06:44:03 -07001895 return InvokeOnWorker<bool>(
1896 RTC_FROM_HERE, Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1897 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001898}
1899
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001900webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001901 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001902 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001903}
1904
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001905webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1906 uint32_t ssrc) const {
1907 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001908}
1909
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001910bool VideoChannel::SetRtpSendParameters(
1911 uint32_t ssrc,
1912 const webrtc::RtpParameters& parameters) {
stefanf79ade12017-06-02 06:44:03 -07001913 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001914 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001915 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001916}
1917
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001918bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1919 webrtc::RtpParameters parameters) {
1920 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1921}
1922
1923webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1924 uint32_t ssrc) const {
1925 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001926 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001927 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
1928}
1929
1930webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
1931 uint32_t ssrc) const {
1932 return media_channel()->GetRtpReceiveParameters(ssrc);
1933}
1934
1935bool VideoChannel::SetRtpReceiveParameters(
1936 uint32_t ssrc,
1937 const webrtc::RtpParameters& parameters) {
stefanf79ade12017-06-02 06:44:03 -07001938 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001939 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001940 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1941}
1942
1943bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1944 webrtc::RtpParameters parameters) {
1945 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001946}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001947
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001948void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949 // Send outgoing data if we're the active call, we have the remote content,
1950 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001951 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 if (!media_channel()->SetSend(send)) {
1953 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1954 // TODO(gangji): Report error back to server.
1955 }
1956
Peter Boström34fbfff2015-09-24 19:20:30 +02001957 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958}
1959
stefanf79ade12017-06-02 06:44:03 -07001960void VideoChannel::FillBitrateInfo(BandwidthEstimationInfo* bwe_info) {
1961 InvokeOnWorker<void>(RTC_FROM_HERE, Bind(&VideoMediaChannel::FillBitrateInfo,
1962 media_channel(), bwe_info));
1963}
1964
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001965bool VideoChannel::GetStats(VideoMediaInfo* stats) {
stefanf79ade12017-06-02 06:44:03 -07001966 return InvokeOnWorker<bool>(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
1967 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968}
1969
1970void VideoChannel::StartMediaMonitor(int cms) {
1971 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001972 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973 media_monitor_->SignalUpdate.connect(
1974 this, &VideoChannel::OnMediaMonitorUpdate);
1975 media_monitor_->Start(cms);
1976}
1977
1978void VideoChannel::StopMediaMonitor() {
1979 if (media_monitor_) {
1980 media_monitor_->Stop();
1981 media_monitor_.reset();
1982 }
1983}
1984
1985const ContentInfo* VideoChannel::GetFirstContent(
1986 const SessionDescription* sdesc) {
1987 return GetFirstVideoContent(sdesc);
1988}
1989
1990bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001991 ContentAction action,
1992 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001993 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001994 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995 LOG(LS_INFO) << "Setting local video description";
1996
1997 const VideoContentDescription* video =
1998 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001999 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002000 if (!video) {
2001 SafeSetError("Can't find video content in local description.", error_desc);
2002 return false;
2003 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002005 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002006 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002007 }
2008
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002009 VideoRecvParameters recv_params = last_recv_params_;
2010 RtpParametersFromMediaDescription(video, &recv_params);
2011 if (!media_channel()->SetRecvParameters(recv_params)) {
2012 SafeSetError("Failed to set local video description recv parameters.",
2013 error_desc);
2014 return false;
2015 }
2016 for (const VideoCodec& codec : video->codecs()) {
zstein3dcf0e92017-06-01 13:22:42 -07002017 AddHandledPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002018 }
2019 last_recv_params_ = recv_params;
2020
2021 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2022 // only give it to the media channel once we have a remote
2023 // description too (without a remote description, we won't be able
2024 // to send them anyway).
2025 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2026 SafeSetError("Failed to set local video description streams.", error_desc);
2027 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 }
2029
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002030 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002031 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002032 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033}
2034
2035bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002036 ContentAction action,
2037 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002038 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002039 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040 LOG(LS_INFO) << "Setting remote video description";
2041
2042 const VideoContentDescription* video =
2043 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002044 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002045 if (!video) {
2046 SafeSetError("Can't find video content in remote description.", error_desc);
2047 return false;
2048 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002050 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002051 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 }
2053
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002054 VideoSendParameters send_params = last_send_params_;
2055 RtpSendParametersFromMediaDescription(video, &send_params);
2056 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002057 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002058 }
skvladdc1c62c2016-03-16 19:07:43 -07002059
2060 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2061
2062 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002063 SafeSetError("Failed to set remote video description send parameters.",
2064 error_desc);
2065 return false;
2066 }
2067 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002069 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2070 // and only give it to the media channel once we have a local
2071 // description too (without a local description, we won't be able to
2072 // recv them anyway).
2073 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2074 SafeSetError("Failed to set remote video description streams.", error_desc);
2075 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076 }
2077
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002078 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002079 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002081
2082 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002083 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002084 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002085}
2086
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002087void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089 case MSG_CHANNEL_ERROR: {
2090 const VideoChannelErrorMessageData* data =
2091 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 delete data;
2093 break;
2094 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095 default:
2096 BaseChannel::OnMessage(pmsg);
2097 break;
2098 }
2099}
2100
2101void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002102 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 SignalConnectionMonitor(this, infos);
2104}
2105
2106// TODO(pthatcher): Look into removing duplicate code between
2107// audio, video, and data, perhaps by using templates.
2108void VideoChannel::OnMediaMonitorUpdate(
2109 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002110 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 SignalMediaMonitor(this, info);
2112}
2113
deadbeef953c2ce2017-01-09 14:53:41 -08002114RtpDataChannel::RtpDataChannel(rtc::Thread* worker_thread,
2115 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002116 rtc::Thread* signaling_thread,
deadbeef953c2ce2017-01-09 14:53:41 -08002117 DataMediaChannel* media_channel,
deadbeef953c2ce2017-01-09 14:53:41 -08002118 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08002119 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002120 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002121 : BaseChannel(worker_thread,
2122 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002123 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002124 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002125 content_name,
deadbeefac22f702017-01-12 21:59:29 -08002126 rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002127 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128
deadbeef953c2ce2017-01-09 14:53:41 -08002129RtpDataChannel::~RtpDataChannel() {
2130 TRACE_EVENT0("webrtc", "RtpDataChannel::~RtpDataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002131 StopMediaMonitor();
2132 // this can't be done in the base class, since it calls a virtual
2133 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002134
2135 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136}
2137
deadbeeff5346592017-01-24 21:51:21 -08002138bool RtpDataChannel::Init_w(
2139 DtlsTransportInternal* rtp_dtls_transport,
2140 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -08002141 rtc::PacketTransportInternal* rtp_packet_transport,
2142 rtc::PacketTransportInternal* rtcp_packet_transport) {
deadbeeff5346592017-01-24 21:51:21 -08002143 if (!BaseChannel::Init_w(rtp_dtls_transport, rtcp_dtls_transport,
2144 rtp_packet_transport, rtcp_packet_transport)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145 return false;
2146 }
deadbeef953c2ce2017-01-09 14:53:41 -08002147 media_channel()->SignalDataReceived.connect(this,
2148 &RtpDataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002149 media_channel()->SignalReadyToSend.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08002150 this, &RtpDataChannel::OnDataChannelReadyToSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151 return true;
2152}
2153
deadbeef953c2ce2017-01-09 14:53:41 -08002154bool RtpDataChannel::SendData(const SendDataParams& params,
2155 const rtc::CopyOnWriteBuffer& payload,
2156 SendDataResult* result) {
stefanf79ade12017-06-02 06:44:03 -07002157 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002158 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2159 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160}
2161
deadbeef953c2ce2017-01-09 14:53:41 -08002162const ContentInfo* RtpDataChannel::GetFirstContent(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163 const SessionDescription* sdesc) {
2164 return GetFirstDataContent(sdesc);
2165}
2166
deadbeef953c2ce2017-01-09 14:53:41 -08002167bool RtpDataChannel::CheckDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002168 const DataContentDescription* content,
2169 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2171 (content->protocol() == kMediaProtocolDtlsSctp));
deadbeef953c2ce2017-01-09 14:53:41 -08002172 // It's been set before, but doesn't match. That's bad.
2173 if (is_sctp) {
2174 SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.",
2175 error_desc);
2176 return false;
2177 }
2178 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179}
2180
deadbeef953c2ce2017-01-09 14:53:41 -08002181bool RtpDataChannel::SetLocalContent_w(const MediaContentDescription* content,
2182 ContentAction action,
2183 std::string* error_desc) {
2184 TRACE_EVENT0("webrtc", "RtpDataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002185 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002186 LOG(LS_INFO) << "Setting local data description";
2187
2188 const DataContentDescription* data =
2189 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002190 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002191 if (!data) {
2192 SafeSetError("Can't find data content in local description.", error_desc);
2193 return false;
2194 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195
deadbeef953c2ce2017-01-09 14:53:41 -08002196 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 return false;
2198 }
2199
deadbeef953c2ce2017-01-09 14:53:41 -08002200 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
2201 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 }
2203
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002204 DataRecvParameters recv_params = last_recv_params_;
2205 RtpParametersFromMediaDescription(data, &recv_params);
2206 if (!media_channel()->SetRecvParameters(recv_params)) {
2207 SafeSetError("Failed to set remote data description recv parameters.",
2208 error_desc);
2209 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210 }
deadbeef953c2ce2017-01-09 14:53:41 -08002211 for (const DataCodec& codec : data->codecs()) {
zstein3dcf0e92017-06-01 13:22:42 -07002212 AddHandledPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002213 }
2214 last_recv_params_ = recv_params;
2215
2216 // TODO(pthatcher): Move local streams into DataSendParameters, and
2217 // only give it to the media channel once we have a remote
2218 // description too (without a remote description, we won't be able
2219 // to send them anyway).
2220 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2221 SafeSetError("Failed to set local data description streams.", error_desc);
2222 return false;
2223 }
2224
2225 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002226 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002227 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228}
2229
deadbeef953c2ce2017-01-09 14:53:41 -08002230bool RtpDataChannel::SetRemoteContent_w(const MediaContentDescription* content,
2231 ContentAction action,
2232 std::string* error_desc) {
2233 TRACE_EVENT0("webrtc", "RtpDataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002234 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235
2236 const DataContentDescription* data =
2237 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002238 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002239 if (!data) {
2240 SafeSetError("Can't find data content in remote description.", error_desc);
2241 return false;
2242 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002243
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002244 // If the remote data doesn't have codecs and isn't an update, it
2245 // must be empty, so ignore it.
2246 if (!data->has_codecs() && action != CA_UPDATE) {
2247 return true;
2248 }
2249
deadbeef953c2ce2017-01-09 14:53:41 -08002250 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251 return false;
2252 }
2253
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002254 LOG(LS_INFO) << "Setting remote data description";
deadbeef953c2ce2017-01-09 14:53:41 -08002255 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002256 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002257 }
2258
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002259 DataSendParameters send_params = last_send_params_;
2260 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2261 if (!media_channel()->SetSendParameters(send_params)) {
2262 SafeSetError("Failed to set remote data description send parameters.",
2263 error_desc);
2264 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002265 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002266 last_send_params_ = send_params;
2267
2268 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2269 // and only give it to the media channel once we have a local
2270 // description too (without a local description, we won't be able to
2271 // recv them anyway).
2272 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2273 SafeSetError("Failed to set remote data description streams.",
2274 error_desc);
2275 return false;
2276 }
2277
2278 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002279 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002280 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002281}
2282
deadbeef953c2ce2017-01-09 14:53:41 -08002283void RtpDataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284 // Render incoming data if we're the active call, and we have the local
2285 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002286 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 if (!media_channel()->SetReceive(recv)) {
2288 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2289 }
2290
2291 // Send outgoing data if we're the active call, we have the remote content,
2292 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002293 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002294 if (!media_channel()->SetSend(send)) {
2295 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2296 }
2297
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002298 // Trigger SignalReadyToSendData asynchronously.
2299 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300
2301 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2302}
2303
deadbeef953c2ce2017-01-09 14:53:41 -08002304void RtpDataChannel::OnMessage(rtc::Message* pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002305 switch (pmsg->message_id) {
2306 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002307 DataChannelReadyToSendMessageData* data =
2308 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002309 ready_to_send_data_ = data->data();
2310 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311 delete data;
2312 break;
2313 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002314 case MSG_DATARECEIVED: {
2315 DataReceivedMessageData* data =
2316 static_cast<DataReceivedMessageData*>(pmsg->pdata);
deadbeef953c2ce2017-01-09 14:53:41 -08002317 SignalDataReceived(data->params, data->payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002318 delete data;
2319 break;
2320 }
2321 case MSG_CHANNEL_ERROR: {
2322 const DataChannelErrorMessageData* data =
2323 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 delete data;
2325 break;
2326 }
2327 default:
2328 BaseChannel::OnMessage(pmsg);
2329 break;
2330 }
2331}
2332
deadbeef953c2ce2017-01-09 14:53:41 -08002333void RtpDataChannel::OnConnectionMonitorUpdate(
2334 ConnectionMonitor* monitor,
2335 const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002336 SignalConnectionMonitor(this, infos);
2337}
2338
deadbeef953c2ce2017-01-09 14:53:41 -08002339void RtpDataChannel::StartMediaMonitor(int cms) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002340 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002341 rtc::Thread::Current()));
deadbeef953c2ce2017-01-09 14:53:41 -08002342 media_monitor_->SignalUpdate.connect(this,
2343 &RtpDataChannel::OnMediaMonitorUpdate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002344 media_monitor_->Start(cms);
2345}
2346
deadbeef953c2ce2017-01-09 14:53:41 -08002347void RtpDataChannel::StopMediaMonitor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 if (media_monitor_) {
2349 media_monitor_->Stop();
2350 media_monitor_->SignalUpdate.disconnect(this);
2351 media_monitor_.reset();
2352 }
2353}
2354
deadbeef953c2ce2017-01-09 14:53:41 -08002355void RtpDataChannel::OnMediaMonitorUpdate(DataMediaChannel* media_channel,
2356 const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002357 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002358 SignalMediaMonitor(this, info);
2359}
2360
deadbeef953c2ce2017-01-09 14:53:41 -08002361void RtpDataChannel::OnDataReceived(const ReceiveDataParams& params,
2362 const char* data,
2363 size_t len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364 DataReceivedMessageData* msg = new DataReceivedMessageData(
2365 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002366 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367}
2368
deadbeef953c2ce2017-01-09 14:53:41 -08002369void RtpDataChannel::OnDataChannelError(uint32_t ssrc,
2370 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002371 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2372 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002373 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002374}
2375
deadbeef953c2ce2017-01-09 14:53:41 -08002376void RtpDataChannel::OnDataChannelReadyToSend(bool writable) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002377 // This is usded for congestion control to indicate that the stream is ready
2378 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2379 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002380 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002381 new DataChannelReadyToSendMessageData(writable));
2382}
2383
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384} // namespace cricket