blob: 68fdabb390d3806e4ddb91601d095815b25dae96 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwiberg0eb15ed2015-12-17 03:04:15 -080011#include <utility>
12
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010013#include "webrtc/pc/channel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
kjellandera69d9732016-08-31 07:33:05 -070015#include "webrtc/api/call/audio_sink.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000016#include "webrtc/base/bind.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000017#include "webrtc/base/byteorder.h"
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -070018#include "webrtc/base/checks.h"
jbaucheec21bd2016-03-20 06:15:43 -070019#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000020#include "webrtc/base/dscp.h"
21#include "webrtc/base/logging.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070022#include "webrtc/base/networkroute.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010023#include "webrtc/base/trace_event.h"
kjellanderf4752772016-03-02 05:42:30 -080024#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080025#include "webrtc/media/base/rtputils.h"
deadbeef5bd5ca32017-02-10 11:31:50 -080026#include "webrtc/p2p/base/packettransportinternal.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010027#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
29namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000030using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000031
deadbeef2d110be2016-01-13 12:00:26 -080032namespace {
kwiberg31022942016-03-11 14:18:21 -080033// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080034bool SetRawAudioSink_w(VoiceMediaChannel* channel,
35 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080036 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
37 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080038 return true;
39}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020040
41struct SendPacketMessageData : public rtc::MessageData {
42 rtc::CopyOnWriteBuffer packet;
43 rtc::PacketOptions options;
44};
45
isheriff6f8d6862016-05-26 11:24:55 -070046#if defined(ENABLE_EXTERNAL_AUTH)
47// Returns the named header extension if found among all extensions,
48// nullptr otherwise.
49const webrtc::RtpExtension* FindHeaderExtension(
50 const std::vector<webrtc::RtpExtension>& extensions,
51 const std::string& uri) {
52 for (const auto& extension : extensions) {
53 if (extension.uri == uri)
54 return &extension;
55 }
56 return nullptr;
57}
58#endif
59
deadbeef2d110be2016-01-13 12:00:26 -080060} // namespace
61
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000063 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020064 MSG_SEND_RTP_PACKET,
65 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 MSG_FIRSTPACKETRECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070};
71
72// Value specified in RFC 5764.
73static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
74
75static const int kAgcMinus10db = -10;
76
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000077static void SafeSetError(const std::string& message, std::string* error_desc) {
78 if (error_desc) {
79 *error_desc = message;
80 }
81}
82
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000083struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020084 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020086 : ssrc(in_ssrc), error(in_error) {}
87 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 VoiceMediaChannel::Error error;
89};
90
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020092 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020094 : ssrc(in_ssrc), error(in_error) {}
95 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 VideoMediaChannel::Error error;
97};
98
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000099struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 : ssrc(in_ssrc), error(in_error) {}
103 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 DataMediaChannel::Error error;
105};
106
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107static const char* PacketType(bool rtcp) {
108 return (!rtcp) ? "RTP" : "RTCP";
109}
110
jbaucheec21bd2016-03-20 06:15:43 -0700111static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 // Check the packet size. We could check the header too if needed.
113 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000114 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
115 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116}
117
118static bool IsReceiveContentDirection(MediaContentDirection direction) {
119 return direction == MD_SENDRECV || direction == MD_RECVONLY;
120}
121
122static bool IsSendContentDirection(MediaContentDirection direction) {
123 return direction == MD_SENDRECV || direction == MD_SENDONLY;
124}
125
126static const MediaContentDescription* GetContentDescription(
127 const ContentInfo* cinfo) {
128 if (cinfo == NULL)
129 return NULL;
130 return static_cast<const MediaContentDescription*>(cinfo->description);
131}
132
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700133template <class Codec>
134void RtpParametersFromMediaDescription(
135 const MediaContentDescriptionImpl<Codec>* desc,
136 RtpParameters<Codec>* params) {
137 // TODO(pthatcher): Remove this once we're sure no one will give us
138 // a description without codecs (currently a CA_UPDATE with just
139 // streams can).
140 if (desc->has_codecs()) {
141 params->codecs = desc->codecs();
142 }
143 // TODO(pthatcher): See if we really need
144 // rtp_header_extensions_set() and remove it if we don't.
145 if (desc->rtp_header_extensions_set()) {
146 params->extensions = desc->rtp_header_extensions();
147 }
deadbeef13871492015-12-09 12:37:51 -0800148 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700149}
150
nisse05103312016-03-16 02:22:50 -0700151template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700152void RtpSendParametersFromMediaDescription(
153 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700154 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700155 RtpParametersFromMediaDescription(desc, send_params);
156 send_params->max_bandwidth_bps = desc->bandwidth();
157}
158
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200159BaseChannel::BaseChannel(rtc::Thread* worker_thread,
160 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800161 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700162 MediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700163 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800164 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800165 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200166 : worker_thread_(worker_thread),
167 network_thread_(network_thread),
zhihuangf5b251b2017-01-12 19:37:48 -0800168 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 content_name_(content_name),
deadbeefac22f702017-01-12 21:59:29 -0800170 rtcp_mux_required_(rtcp_mux_required),
deadbeef7af91dd2016-12-13 11:29:11 -0800171 srtp_required_(srtp_required),
michaelt79e05882016-11-08 02:50:09 -0800172 media_channel_(media_channel),
173 selected_candidate_pair_(nullptr) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700174 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 LOG(LS_INFO) << "Created channel for " << content_name;
176}
177
178BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800179 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700180 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000181 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200183 // Eats any outstanding messages or packets.
184 worker_thread_->Clear(&invoker_);
185 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 // We must destroy the media channel before the transport channel, otherwise
187 // the media channel may try to send on the dead transport channel. NULLing
188 // is not an effective strategy since the sends will come on another thread.
189 delete media_channel_;
zhihuangf5b251b2017-01-12 19:37:48 -0800190 LOG(LS_INFO) << "Destroyed channel: " << content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200191}
192
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200193void BaseChannel::DisconnectTransportChannels_n() {
194 // Send any outstanding RTCP packets.
195 FlushRtcpMessages_n();
196
197 // Stop signals from transport channels, but keep them alive because
198 // media_channel may use them from a different thread.
zhihuangb2cdd932017-01-19 16:54:25 -0800199 if (rtp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800200 DisconnectFromDtlsTransport(rtp_dtls_transport_);
201 } else if (rtp_packet_transport_) {
202 DisconnectFromPacketTransport(rtp_packet_transport_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200203 }
zhihuangb2cdd932017-01-19 16:54:25 -0800204 if (rtcp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800205 DisconnectFromDtlsTransport(rtcp_dtls_transport_);
206 } else if (rtcp_packet_transport_) {
207 DisconnectFromPacketTransport(rtcp_packet_transport_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200208 }
209
210 // Clear pending read packets/messages.
211 network_thread_->Clear(&invoker_);
212 network_thread_->Clear(this);
213}
214
zhihuangb2cdd932017-01-19 16:54:25 -0800215bool BaseChannel::Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800216 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800217 rtc::PacketTransportInternal* rtp_packet_transport,
218 rtc::PacketTransportInternal* rtcp_packet_transport) {
skvlad6c87a672016-05-17 17:49:52 -0700219 if (!network_thread_->Invoke<bool>(
zhihuangb2cdd932017-01-19 16:54:25 -0800220 RTC_FROM_HERE, Bind(&BaseChannel::InitNetwork_n, this,
deadbeeff5346592017-01-24 21:51:21 -0800221 rtp_dtls_transport, rtcp_dtls_transport,
222 rtp_packet_transport, rtcp_packet_transport))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000223 return false;
224 }
deadbeeff5346592017-01-24 21:51:21 -0800225 // Both RTP and RTCP channels should be set, we can call SetInterface on
226 // the media channel and it can set network options.
227 RTC_DCHECK_RUN_ON(worker_thread_);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000228 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 return true;
230}
231
deadbeeff5346592017-01-24 21:51:21 -0800232bool BaseChannel::InitNetwork_n(
233 DtlsTransportInternal* rtp_dtls_transport,
234 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800235 rtc::PacketTransportInternal* rtp_packet_transport,
236 rtc::PacketTransportInternal* rtcp_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200237 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800238 SetTransports_n(rtp_dtls_transport, rtcp_dtls_transport, rtp_packet_transport,
239 rtcp_packet_transport);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200240
deadbeeff5346592017-01-24 21:51:21 -0800241 if (rtp_dtls_transport_ &&
242 !SetDtlsSrtpCryptoSuites_n(rtp_dtls_transport_, false)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200243 return false;
244 }
zhihuangb2cdd932017-01-19 16:54:25 -0800245 if (rtcp_dtls_transport_ &&
246 !SetDtlsSrtpCryptoSuites_n(rtcp_dtls_transport_, true)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200247 return false;
248 }
deadbeefac22f702017-01-12 21:59:29 -0800249 if (rtcp_mux_required_) {
250 rtcp_mux_filter_.SetActive();
251 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200252 return true;
253}
254
wu@webrtc.org78187522013-10-07 23:32:02 +0000255void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200256 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000257 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200258 // Packets arrive on the network thread, processing packets calls virtual
259 // functions, so need to stop this process in Deinit that is called in
260 // derived classes destructor.
261 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700262 RTC_FROM_HERE, Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000263}
264
zhihuangb2cdd932017-01-19 16:54:25 -0800265void BaseChannel::SetTransports(DtlsTransportInternal* rtp_dtls_transport,
266 DtlsTransportInternal* rtcp_dtls_transport) {
deadbeeff5346592017-01-24 21:51:21 -0800267 network_thread_->Invoke<void>(
268 RTC_FROM_HERE,
269 Bind(&BaseChannel::SetTransports_n, this, rtp_dtls_transport,
270 rtcp_dtls_transport, rtp_dtls_transport, rtcp_dtls_transport));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000271}
272
deadbeeff5346592017-01-24 21:51:21 -0800273void BaseChannel::SetTransports(
deadbeef5bd5ca32017-02-10 11:31:50 -0800274 rtc::PacketTransportInternal* rtp_packet_transport,
275 rtc::PacketTransportInternal* rtcp_packet_transport) {
deadbeeff5346592017-01-24 21:51:21 -0800276 network_thread_->Invoke<void>(
277 RTC_FROM_HERE, Bind(&BaseChannel::SetTransports_n, this, nullptr, nullptr,
278 rtp_packet_transport, rtcp_packet_transport));
279}
zhihuangf5b251b2017-01-12 19:37:48 -0800280
deadbeeff5346592017-01-24 21:51:21 -0800281void BaseChannel::SetTransports_n(
282 DtlsTransportInternal* rtp_dtls_transport,
283 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800284 rtc::PacketTransportInternal* rtp_packet_transport,
285 rtc::PacketTransportInternal* rtcp_packet_transport) {
deadbeeff5346592017-01-24 21:51:21 -0800286 RTC_DCHECK(network_thread_->IsCurrent());
287 // Validate some assertions about the input.
288 RTC_DCHECK(rtp_packet_transport);
289 RTC_DCHECK_EQ(NeedsRtcpTransport(), rtcp_packet_transport != nullptr);
290 if (rtp_dtls_transport || rtcp_dtls_transport) {
291 // DTLS/non-DTLS pointers should be to the same object.
292 RTC_DCHECK(rtp_dtls_transport == rtp_packet_transport);
293 RTC_DCHECK(rtcp_dtls_transport == rtcp_packet_transport);
294 // Can't go from non-DTLS to DTLS.
295 RTC_DCHECK(!rtp_packet_transport_ || rtp_dtls_transport_);
296 } else {
297 // Can't go from DTLS to non-DTLS.
298 RTC_DCHECK(!rtp_dtls_transport_);
299 }
300 // Transport names should be the same.
zhihuangb2cdd932017-01-19 16:54:25 -0800301 if (rtp_dtls_transport && rtcp_dtls_transport) {
302 RTC_DCHECK(rtp_dtls_transport->transport_name() ==
303 rtcp_dtls_transport->transport_name());
zhihuangb2cdd932017-01-19 16:54:25 -0800304 }
deadbeeff5346592017-01-24 21:51:21 -0800305 std::string debug_name;
306 if (rtp_dtls_transport) {
307 transport_name_ = rtp_dtls_transport->transport_name();
308 debug_name = transport_name_;
309 } else {
310 debug_name = rtp_packet_transport->debug_name();
311 }
312 if (rtp_packet_transport == rtp_packet_transport_) {
313 // Nothing to do if transport isn't changing.
deadbeefbad5dad2017-01-17 18:32:35 -0800314 return;
deadbeefcbecd352015-09-23 11:50:27 -0700315 }
316
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800317 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
318 // changes and wait until the DTLS handshake is complete to set the newly
319 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200320 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800321 // Set |writable_| to false such that UpdateWritableState_w can set up
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700322 // DTLS-SRTP when |writable_| becomes true again.
guoweis46383312015-12-17 16:45:59 -0800323 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800324 srtp_filter_.ResetParams();
325 }
326
deadbeefac22f702017-01-12 21:59:29 -0800327 // If this BaseChannel doesn't require RTCP mux and we haven't fully
328 // negotiated RTCP mux, we need an RTCP transport.
deadbeeff5346592017-01-24 21:51:21 -0800329 if (rtcp_packet_transport) {
zhihuangf5b251b2017-01-12 19:37:48 -0800330 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on "
deadbeeff5346592017-01-24 21:51:21 -0800331 << debug_name << " transport " << rtcp_packet_transport;
332 SetTransport_n(true, rtcp_dtls_transport, rtcp_packet_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000333 }
334
deadbeeff5346592017-01-24 21:51:21 -0800335 LOG(LS_INFO) << "Setting RTP Transport for " << content_name() << " on "
336 << debug_name << " transport " << rtp_packet_transport;
337 SetTransport_n(false, rtp_dtls_transport, rtp_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800338
deadbeefcbecd352015-09-23 11:50:27 -0700339 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700340 // setting new transport channels.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200341 UpdateWritableState_n();
deadbeef062ce9f2016-08-26 21:42:15 -0700342 // We can only update ready-to-send after updating writability.
343 //
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700344 // On setting a new channel, assume it's ready to send if it's writable,
345 // because we have no way of knowing otherwise (the channel doesn't give us
346 // "was last send successful?").
347 //
348 // This won't always be accurate (the last SendPacket call from another
349 // BaseChannel could have resulted in an error), but even so, we'll just
350 // encounter the error again and update "ready to send" accordingly.
deadbeef062ce9f2016-08-26 21:42:15 -0700351 SetTransportChannelReadyToSend(
deadbeeff5346592017-01-24 21:51:21 -0800352 false, rtp_packet_transport_ && rtp_packet_transport_->writable());
zhihuangb2cdd932017-01-19 16:54:25 -0800353 SetTransportChannelReadyToSend(
deadbeeff5346592017-01-24 21:51:21 -0800354 true, rtcp_packet_transport_ && rtcp_packet_transport_->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000355}
356
deadbeeff5346592017-01-24 21:51:21 -0800357void BaseChannel::SetTransport_n(
358 bool rtcp,
359 DtlsTransportInternal* new_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800360 rtc::PacketTransportInternal* new_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200361 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800362 DtlsTransportInternal*& old_dtls_transport =
zhihuangb2cdd932017-01-19 16:54:25 -0800363 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
deadbeef5bd5ca32017-02-10 11:31:50 -0800364 rtc::PacketTransportInternal*& old_packet_transport =
deadbeeff5346592017-01-24 21:51:21 -0800365 rtcp ? rtcp_packet_transport_ : rtp_packet_transport_;
zhihuangb2cdd932017-01-19 16:54:25 -0800366
deadbeeff5346592017-01-24 21:51:21 -0800367 if (!old_packet_transport && !new_packet_transport) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700368 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000369 return;
370 }
zhihuangb2cdd932017-01-19 16:54:25 -0800371
deadbeeff5346592017-01-24 21:51:21 -0800372 RTC_DCHECK(old_packet_transport != new_packet_transport);
373 if (old_dtls_transport) {
374 DisconnectFromDtlsTransport(old_dtls_transport);
375 } else if (old_packet_transport) {
376 DisconnectFromPacketTransport(old_packet_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000377 }
378
deadbeeff5346592017-01-24 21:51:21 -0800379 old_packet_transport = new_packet_transport;
380 old_dtls_transport = new_dtls_transport;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000381
deadbeeff5346592017-01-24 21:51:21 -0800382 // If there's no new transport, we're done after disconnecting from old one.
383 if (!new_packet_transport) {
384 return;
385 }
386
387 if (rtcp && new_dtls_transport) {
388 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
389 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
390 << "should never happen.";
391 }
392 if (new_dtls_transport) {
393 ConnectToDtlsTransport(new_dtls_transport);
394 } else {
395 ConnectToPacketTransport(new_packet_transport);
396 }
397 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
398 for (const auto& pair : socket_options) {
399 new_packet_transport->SetOption(pair.first, pair.second);
guoweis46383312015-12-17 16:45:59 -0800400 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000401}
402
deadbeeff5346592017-01-24 21:51:21 -0800403void BaseChannel::ConnectToDtlsTransport(DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200404 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000405
zhihuangb2cdd932017-01-19 16:54:25 -0800406 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
407 transport->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
408 transport->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
409 transport->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
410 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
411 transport->ice_transport()->SignalSelectedCandidatePairChanged.connect(
Honghai Zhangcc411c02016-03-29 17:27:21 -0700412 this, &BaseChannel::OnSelectedCandidatePairChanged);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000413}
414
deadbeeff5346592017-01-24 21:51:21 -0800415void BaseChannel::DisconnectFromDtlsTransport(
416 DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200417 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800418 OnSelectedCandidatePairChanged(transport->ice_transport(), nullptr, -1,
419 false);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000420
zhihuangb2cdd932017-01-19 16:54:25 -0800421 transport->SignalWritableState.disconnect(this);
422 transport->SignalReadPacket.disconnect(this);
423 transport->SignalReadyToSend.disconnect(this);
424 transport->SignalDtlsState.disconnect(this);
425 transport->SignalSentPacket.disconnect(this);
426 transport->ice_transport()->SignalSelectedCandidatePairChanged.disconnect(
427 this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000428}
429
deadbeeff5346592017-01-24 21:51:21 -0800430void BaseChannel::ConnectToPacketTransport(
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.connect(this, &BaseChannel::OnWritableState);
434 transport->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
435 transport->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
436 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
437}
438
439void BaseChannel::DisconnectFromPacketTransport(
deadbeef5bd5ca32017-02-10 11:31:50 -0800440 rtc::PacketTransportInternal* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800441 RTC_DCHECK_RUN_ON(network_thread_);
442 transport->SignalWritableState.disconnect(this);
443 transport->SignalReadPacket.disconnect(this);
444 transport->SignalReadyToSend.disconnect(this);
445 transport->SignalSentPacket.disconnect(this);
446}
447
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700449 worker_thread_->Invoke<void>(
450 RTC_FROM_HERE,
451 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
452 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 return true;
454}
455
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456bool BaseChannel::AddRecvStream(const StreamParams& sp) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700457 return InvokeOnWorker(RTC_FROM_HERE,
458 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459}
460
Peter Boström0c4e06b2015-10-07 12:23:21 +0200461bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700462 return InvokeOnWorker(RTC_FROM_HERE,
463 Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464}
465
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000466bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000467 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700468 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000469}
470
Peter Boström0c4e06b2015-10-07 12:23:21 +0200471bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700472 return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream,
473 media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000474}
475
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000477 ContentAction action,
478 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100479 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700480 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w,
481 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482}
483
484bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000485 ContentAction action,
486 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100487 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700488 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w,
489 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490}
491
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492void BaseChannel::StartConnectionMonitor(int cms) {
zhihuangb2cdd932017-01-19 16:54:25 -0800493 // We pass in the BaseChannel instead of the rtp_dtls_transport_
494 // because if the rtp_dtls_transport_ changes, the ConnectionMonitor
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000495 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200496 // We pass in the network thread because on that thread connection monitor
497 // will call BaseChannel::GetConnectionStats which must be called on the
498 // network thread.
499 connection_monitor_.reset(
500 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000501 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000503 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504}
505
506void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000507 if (connection_monitor_) {
508 connection_monitor_->Stop();
509 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 }
511}
512
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000513bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200514 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800515 if (!rtp_dtls_transport_) {
516 return false;
517 }
zhihuangb2cdd932017-01-19 16:54:25 -0800518 return rtp_dtls_transport_->ice_transport()->GetStats(infos);
zhihuangf5b251b2017-01-12 19:37:48 -0800519}
520
521bool BaseChannel::NeedsRtcpTransport() {
deadbeefac22f702017-01-12 21:59:29 -0800522 // If this BaseChannel doesn't require RTCP mux and we haven't fully
523 // negotiated RTCP mux, we need an RTCP transport.
524 return !rtcp_mux_required_ && !rtcp_mux_filter_.IsFullyActive();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000525}
526
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700527bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 // Receive data if we are enabled and have local content,
529 return enabled() && IsReceiveContentDirection(local_content_direction_);
530}
531
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700532bool BaseChannel::IsReadyToSendMedia_w() const {
533 // Need to access some state updated on the network thread.
534 return network_thread_->Invoke<bool>(
535 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
536}
537
538bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 // Send outgoing data if we are enabled, have local and remote content,
540 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800541 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700543 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200544 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545}
546
jbaucheec21bd2016-03-20 06:15:43 -0700547bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700548 const rtc::PacketOptions& options) {
549 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550}
551
jbaucheec21bd2016-03-20 06:15:43 -0700552bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700553 const rtc::PacketOptions& options) {
554 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555}
556
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000557int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200559 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700560 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200561}
562
563int BaseChannel::SetOption_n(SocketType type,
564 rtc::Socket::Option opt,
565 int value) {
566 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef5bd5ca32017-02-10 11:31:50 -0800567 rtc::PacketTransportInternal* transport = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000569 case ST_RTP:
deadbeeff5346592017-01-24 21:51:21 -0800570 transport = rtp_packet_transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700571 socket_options_.push_back(
572 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000573 break;
574 case ST_RTCP:
deadbeeff5346592017-01-24 21:51:21 -0800575 transport = rtcp_packet_transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700576 rtcp_socket_options_.push_back(
577 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000578 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 }
deadbeeff5346592017-01-24 21:51:21 -0800580 return transport ? transport->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581}
582
jbauchcb560652016-08-04 05:20:32 -0700583bool BaseChannel::SetCryptoOptions(const rtc::CryptoOptions& crypto_options) {
584 crypto_options_ = crypto_options;
585 return true;
586}
587
deadbeef5bd5ca32017-02-10 11:31:50 -0800588void BaseChannel::OnWritableState(rtc::PacketTransportInternal* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800589 RTC_DCHECK(transport == rtp_packet_transport_ ||
590 transport == rtcp_packet_transport_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200591 RTC_DCHECK(network_thread_->IsCurrent());
592 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593}
594
deadbeef5bd5ca32017-02-10 11:31:50 -0800595void BaseChannel::OnPacketRead(rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700596 const char* data,
597 size_t len,
598 const rtc::PacketTime& packet_time,
599 int flags) {
600 TRACE_EVENT0("webrtc", "BaseChannel::OnPacketRead");
601 // OnPacketRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200602 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603
604 // When using RTCP multiplexing we might get RTCP packets on the RTP
605 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
johand89ab142016-10-25 10:50:32 -0700606 bool rtcp = PacketIsRtcp(transport, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700607 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000608 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609}
610
deadbeef5bd5ca32017-02-10 11:31:50 -0800611void BaseChannel::OnReadyToSend(rtc::PacketTransportInternal* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800612 RTC_DCHECK(transport == rtp_packet_transport_ ||
613 transport == rtcp_packet_transport_);
614 SetTransportChannelReadyToSend(transport == rtcp_packet_transport_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615}
616
zhihuangb2cdd932017-01-19 16:54:25 -0800617void BaseChannel::OnDtlsState(DtlsTransportInternal* transport,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800618 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200619 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800620 return;
621 }
622
623 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
624 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
zhihuangb2cdd932017-01-19 16:54:25 -0800625 // cover other scenarios like the whole transport is writable (not just this
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800626 // TransportChannel) or when TransportChannel is attached after DTLS is
627 // negotiated.
628 if (state != DTLS_TRANSPORT_CONNECTED) {
629 srtp_filter_.ResetParams();
630 }
631}
632
Honghai Zhangcc411c02016-03-29 17:27:21 -0700633void BaseChannel::OnSelectedCandidatePairChanged(
zhihuangb2cdd932017-01-19 16:54:25 -0800634 IceTransportInternal* ice_transport,
Honghai Zhang52dce732016-03-31 12:37:31 -0700635 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700636 int last_sent_packet_id,
637 bool ready_to_send) {
deadbeeff5346592017-01-24 21:51:21 -0800638 RTC_DCHECK((rtp_dtls_transport_ &&
639 ice_transport == rtp_dtls_transport_->ice_transport()) ||
640 (rtcp_dtls_transport_ &&
641 ice_transport == rtcp_dtls_transport_->ice_transport()));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200642 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800643 selected_candidate_pair_ = selected_candidate_pair;
zhihuangb2cdd932017-01-19 16:54:25 -0800644 std::string transport_name = ice_transport->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700645 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700646 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700647 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700648 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700649 selected_candidate_pair->remote_candidate().network_id(),
650 last_sent_packet_id);
michaelt79e05882016-11-08 02:50:09 -0800651
652 UpdateTransportOverhead();
Honghai Zhangcc411c02016-03-29 17:27:21 -0700653 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200654 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700655 RTC_FROM_HERE, worker_thread_,
656 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
657 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700658}
659
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700660void BaseChannel::SetTransportChannelReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200661 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700662 if (rtcp) {
663 rtcp_ready_to_send_ = ready;
664 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 rtp_ready_to_send_ = ready;
666 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200668 bool ready_to_send =
669 (rtp_ready_to_send_ &&
deadbeeff5346592017-01-24 21:51:21 -0800670 // In the case of rtcp mux |rtcp_packet_transport_| will be null.
671 (rtcp_ready_to_send_ || !rtcp_packet_transport_));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200672
673 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700674 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200675 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676}
677
deadbeef5bd5ca32017-02-10 11:31:50 -0800678bool BaseChannel::PacketIsRtcp(const rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -0700679 const char* data,
680 size_t len) {
deadbeeff5346592017-01-24 21:51:21 -0800681 return (transport == rtcp_packet_transport_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000682 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683}
684
stefanc1aeaf02015-10-15 07:26:07 -0700685bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700686 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700687 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200688 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
689 // If the thread is not our network thread, we will post to our network
690 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 // synchronize access to all the pieces of the send path, including
692 // SRTP and the inner workings of the transport channels.
693 // The only downside is that we can't return a proper failure code if
694 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200695 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200697 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
698 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800699 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700700 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700701 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 return true;
703 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200704 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705
706 // Now that we are on the correct thread, ensure we have a place to send this
707 // packet before doing anything. (We might get RTCP packets that we don't
708 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
709 // transport.
deadbeef5bd5ca32017-02-10 11:31:50 -0800710 rtc::PacketTransportInternal* transport =
deadbeeff5346592017-01-24 21:51:21 -0800711 (!rtcp || rtcp_mux_filter_.IsActive()) ? rtp_packet_transport_
712 : rtcp_packet_transport_;
zhihuangb2cdd932017-01-19 16:54:25 -0800713 if (!transport || !transport->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 return false;
715 }
716
717 // Protect ourselves against crazy data.
718 if (!ValidPacket(rtcp, packet)) {
719 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000720 << PacketType(rtcp)
721 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 return false;
723 }
724
stefanc1aeaf02015-10-15 07:26:07 -0700725 rtc::PacketOptions updated_options;
726 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 // Protect if needed.
728 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200729 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200731 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000732 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000734 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
735 // inside libsrtp for a RTP packet. A external HMAC module will be writing
736 // a fake HMAC value. This is ONLY done for a RTP packet.
737 // Socket layer will update rtp sendtime extension header if present in
738 // packet with current time before updating the HMAC.
739#if !defined(ENABLE_EXTERNAL_AUTH)
740 res = srtp_filter_.ProtectRtp(
741 data, len, static_cast<int>(packet->capacity()), &len);
742#else
jbauchd48f4882017-03-01 15:34:36 -0800743 if (!srtp_filter_.IsExternalAuthActive()) {
744 res = srtp_filter_.ProtectRtp(
745 data, len, static_cast<int>(packet->capacity()), &len);
746 } else {
747 updated_options.packet_time_params.rtp_sendtime_extension_id =
748 rtp_abs_sendtime_extn_id_;
749 res = srtp_filter_.ProtectRtp(
750 data, len, static_cast<int>(packet->capacity()), &len,
751 &updated_options.packet_time_params.srtp_packet_index);
752 // If protection succeeds, let's get auth params from srtp.
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000753 if (res) {
jbauchd48f4882017-03-01 15:34:36 -0800754 uint8_t* auth_key = NULL;
755 int key_len;
756 res = srtp_filter_.GetRtpAuthParams(
757 &auth_key, &key_len,
758 &updated_options.packet_time_params.srtp_auth_tag_len);
759 if (res) {
760 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
761 updated_options.packet_time_params.srtp_auth_key.assign(
762 auth_key, auth_key + key_len);
763 }
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000764 }
765 }
766#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 if (!res) {
768 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200769 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 GetRtpSeqNum(data, len, &seq_num);
771 GetRtpSsrc(data, len, &ssrc);
772 LOG(LS_ERROR) << "Failed to protect " << content_name_
773 << " RTP packet: size=" << len
774 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
775 return false;
776 }
777 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000778 res = srtp_filter_.ProtectRtcp(data, len,
779 static_cast<int>(packet->capacity()),
780 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 if (!res) {
782 int type = -1;
783 GetRtcpType(data, len, &type);
784 LOG(LS_ERROR) << "Failed to protect " << content_name_
785 << " RTCP packet: size=" << len << ", type=" << type;
786 return false;
787 }
788 }
789
790 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000791 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800792 } else if (srtp_required_) {
deadbeef8f425f92016-12-01 12:26:27 -0800793 // The audio/video engines may attempt to send RTCP packets as soon as the
794 // streams are created, so don't treat this as an error for RTCP.
795 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809
796 if (rtcp) {
797 return false;
798 }
799 // However, there shouldn't be any RTP packets sent before SRTP is set up
800 // (and SetSend(true) is called).
801 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
802 << " and crypto is required";
nisseeb4ca4e2017-01-12 02:24:27 -0800803 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 return false;
805 }
806
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200808 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
zhihuangb2cdd932017-01-19 16:54:25 -0800809 int ret = transport->SendPacket(packet->data<char>(), packet->size(),
810 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000811 if (ret != static_cast<int>(packet->size())) {
zhihuangb2cdd932017-01-19 16:54:25 -0800812 if (transport->GetError() == ENOTCONN) {
skvladc309e0e2016-07-28 17:15:20 -0700813 LOG(LS_WARNING) << "Got ENOTCONN from transport.";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700814 SetTransportChannelReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 }
816 return false;
817 }
818 return true;
819}
820
jbaucheec21bd2016-03-20 06:15:43 -0700821bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 // Protect ourselves against crazy data.
823 if (!ValidPacket(rtcp, packet)) {
824 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000825 << PacketType(rtcp)
826 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 return false;
828 }
pbos482b12e2015-11-16 10:19:58 -0800829 if (rtcp) {
830 // Permit all (seemingly valid) RTCP packets.
831 return true;
832 }
833 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700834 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835}
836
jbaucheec21bd2016-03-20 06:15:43 -0700837void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000838 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200839 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 if (!WantsPacket(rtcp, packet)) {
841 return;
842 }
843
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000844 // We are only interested in the first rtp packet because that
845 // indicates the media has started flowing.
846 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700848 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 }
850
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 // Unprotect the packet, if needed.
852 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200853 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200854 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000855 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 bool res;
857 if (!rtcp) {
858 res = srtp_filter_.UnprotectRtp(data, len, &len);
859 if (!res) {
860 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200861 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 GetRtpSeqNum(data, len, &seq_num);
863 GetRtpSsrc(data, len, &ssrc);
864 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
865 << " RTP packet: size=" << len
866 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
867 return;
868 }
869 } else {
870 res = srtp_filter_.UnprotectRtcp(data, len, &len);
871 if (!res) {
872 int type = -1;
873 GetRtcpType(data, len, &type);
874 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
875 << " RTCP packet: size=" << len << ", type=" << type;
876 return;
877 }
878 }
879
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000880 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800881 } else if (srtp_required_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 // Our session description indicates that SRTP is required, but we got a
883 // packet before our SRTP filter is active. This means either that
884 // a) we got SRTP packets before we received the SDES keys, in which case
885 // we can't decrypt it anyway, or
886 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
zhihuangb2cdd932017-01-19 16:54:25 -0800887 // transports, so we haven't yet extracted keys, even if DTLS did
888 // complete on the transport that the packets are being sent on. It's
889 // really good practice to wait for both RTP and RTCP to be good to go
890 // before sending media, to prevent weird failure modes, so it's fine
891 // for us to just eat packets here. This is all sidestepped if RTCP mux
892 // is used anyway.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
894 << " packet when SRTP is inactive and crypto is required";
895 return;
896 }
897
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200898 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700899 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200900 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
901}
902
903void BaseChannel::OnPacketReceived(bool rtcp,
904 const rtc::CopyOnWriteBuffer& packet,
905 const rtc::PacketTime& packet_time) {
906 RTC_DCHECK(worker_thread_->IsCurrent());
907 // Need to copy variable because OnRtcpReceived/OnPacketReceived
908 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
909 rtc::CopyOnWriteBuffer data(packet);
910 if (rtcp) {
911 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200913 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
915}
916
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000917bool BaseChannel::PushdownLocalDescription(
918 const SessionDescription* local_desc, ContentAction action,
919 std::string* error_desc) {
920 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 const MediaContentDescription* content_desc =
922 GetContentDescription(content_info);
923 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000924 !SetLocalContent(content_desc, action, error_desc)) {
925 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
926 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000928 return true;
929}
930
931bool BaseChannel::PushdownRemoteDescription(
932 const SessionDescription* remote_desc, ContentAction action,
933 std::string* error_desc) {
934 const ContentInfo* content_info = GetFirstContent(remote_desc);
935 const MediaContentDescription* content_desc =
936 GetContentDescription(content_info);
937 if (content_desc && content_info && !content_info->rejected &&
938 !SetRemoteContent(content_desc, action, error_desc)) {
939 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
940 return false;
941 }
942 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943}
944
945void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700946 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 if (enabled_)
948 return;
949
950 LOG(LS_INFO) << "Channel enabled";
951 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700952 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953}
954
955void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700956 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 if (!enabled_)
958 return;
959
960 LOG(LS_INFO) << "Channel disabled";
961 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700962 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963}
964
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200965void BaseChannel::UpdateWritableState_n() {
deadbeeff5346592017-01-24 21:51:21 -0800966 if (rtp_packet_transport_ && rtp_packet_transport_->writable() &&
967 (!rtcp_packet_transport_ || rtcp_packet_transport_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200968 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700969 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200970 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700971 }
972}
973
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200974void BaseChannel::ChannelWritable_n() {
975 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800976 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800978 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979
deadbeefcbecd352015-09-23 11:50:27 -0700980 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 << (was_ever_writable_ ? "" : " for the first time");
982
michaelt79e05882016-11-08 02:50:09 -0800983 if (selected_candidate_pair_)
984 LOG(LS_INFO)
985 << "Using "
986 << selected_candidate_pair_->local_candidate().ToSensitiveString()
987 << "->"
988 << selected_candidate_pair_->remote_candidate().ToSensitiveString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200991 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700993 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994}
995
deadbeef953c2ce2017-01-09 14:53:41 -0800996void BaseChannel::SignalDtlsSrtpSetupFailure_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200997 RTC_DCHECK(network_thread_->IsCurrent());
998 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700999 RTC_FROM_HERE, signaling_thread(),
deadbeef953c2ce2017-01-09 14:53:41 -08001000 Bind(&BaseChannel::SignalDtlsSrtpSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001001}
1002
deadbeef953c2ce2017-01-09 14:53:41 -08001003void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001004 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
deadbeef953c2ce2017-01-09 14:53:41 -08001005 SignalDtlsSrtpSetupFailure(this, rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001006}
1007
zhihuangb2cdd932017-01-19 16:54:25 -08001008bool BaseChannel::SetDtlsSrtpCryptoSuites_n(DtlsTransportInternal* transport,
1009 bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001010 std::vector<int> crypto_suites;
1011 // We always use the default SRTP crypto suites for RTCP, but we may use
1012 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001014 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 } else {
jbauchcb560652016-08-04 05:20:32 -07001016 GetDefaultSrtpCryptoSuites(crypto_options(), &crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017 }
zhihuangb2cdd932017-01-19 16:54:25 -08001018 return transport->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019}
1020
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001021bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
zhihuangb2cdd932017-01-19 16:54:25 -08001022 // Since DTLS is applied to all transports, checking RTP should be enough.
1023 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024}
1025
1026// This function returns true if either DTLS-SRTP is not in use
1027// *or* DTLS-SRTP is successfully set up.
zhihuangb2cdd932017-01-19 16:54:25 -08001028bool BaseChannel::SetupDtlsSrtp_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001029 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030 bool ret = false;
1031
zhihuangb2cdd932017-01-19 16:54:25 -08001032 DtlsTransportInternal* transport =
1033 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
deadbeeff5346592017-01-24 21:51:21 -08001034 RTC_DCHECK(transport);
zhihuangb2cdd932017-01-19 16:54:25 -08001035 RTC_DCHECK(transport->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001037 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038
zhihuangb2cdd932017-01-19 16:54:25 -08001039 if (!transport->GetSrtpCryptoSuite(&selected_crypto_suite)) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001040 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 return false;
1042 }
1043
zhihuangb2cdd932017-01-19 16:54:25 -08001044 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " << content_name() << " "
1045 << PacketType(rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046
jbauchcb560652016-08-04 05:20:32 -07001047 int key_len;
1048 int salt_len;
1049 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
1050 &salt_len)) {
1051 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
1052 return false;
1053 }
1054
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -07001056 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057
1058 // RFC 5705 exporter using the RFC 5764 parameters
zhihuangb2cdd932017-01-19 16:54:25 -08001059 if (!transport->ExportKeyingMaterial(kDtlsSrtpExporterLabel, NULL, 0, false,
1060 &dtls_buffer[0], dtls_buffer.size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
nisseeb4ca4e2017-01-12 02:24:27 -08001062 RTC_NOTREACHED(); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 return false;
1064 }
1065
1066 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -07001067 std::vector<unsigned char> client_write_key(key_len + salt_len);
1068 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -07001070 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
1071 offset += key_len;
1072 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
1073 offset += key_len;
1074 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
1075 offset += salt_len;
1076 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077
1078 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001079 rtc::SSLRole role;
zhihuangb2cdd932017-01-19 16:54:25 -08001080 if (!transport->GetSslRole(&role)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001081 LOG(LS_WARNING) << "GetSslRole failed";
1082 return false;
1083 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001085 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086 send_key = &server_write_key;
1087 recv_key = &client_write_key;
1088 } else {
1089 send_key = &client_write_key;
1090 recv_key = &server_write_key;
1091 }
1092
zhihuangb2cdd932017-01-19 16:54:25 -08001093 if (rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001094 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1095 static_cast<int>(send_key->size()),
1096 selected_crypto_suite, &(*recv_key)[0],
1097 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001099 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1100 static_cast<int>(send_key->size()),
1101 selected_crypto_suite, &(*recv_key)[0],
1102 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 }
1104
michaelt79e05882016-11-08 02:50:09 -08001105 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
michaelt79e05882016-11-08 02:50:09 -08001107 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 dtls_keyed_ = true;
michaelt79e05882016-11-08 02:50:09 -08001109 UpdateTransportOverhead();
1110 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 return ret;
1112}
1113
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001114void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001115 if (srtp_filter_.IsActive()) {
1116 return;
1117 }
1118
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001119 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001120 return;
1121 }
1122
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001123 if (!SetupDtlsSrtp_n(false)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001124 SignalDtlsSrtpSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001125 return;
1126 }
1127
zhihuangb2cdd932017-01-19 16:54:25 -08001128 if (rtcp_dtls_transport_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001129 if (!SetupDtlsSrtp_n(true)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001130 SignalDtlsSrtpSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001131 return;
1132 }
1133 }
1134}
1135
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001136void BaseChannel::ChannelNotWritable_n() {
1137 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 if (!writable_)
1139 return;
1140
deadbeefcbecd352015-09-23 11:50:27 -07001141 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001143 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001144}
1145
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001146bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001147 const MediaContentDescription* content,
1148 ContentAction action,
1149 ContentSource src,
1150 std::string* error_desc) {
1151 if (action == CA_UPDATE) {
1152 // These parameters never get changed by a CA_UDPATE.
1153 return true;
1154 }
1155
deadbeef7af91dd2016-12-13 11:29:11 -08001156 // Cache srtp_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001157 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001158 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1159 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001160}
1161
1162bool BaseChannel::SetRtpTransportParameters_n(
1163 const MediaContentDescription* content,
1164 ContentAction action,
1165 ContentSource src,
1166 std::string* error_desc) {
1167 RTC_DCHECK(network_thread_->IsCurrent());
1168
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001169 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001170 return false;
1171 }
1172
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001173 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001174 return false;
1175 }
1176
1177 return true;
1178}
1179
zhihuangb2cdd932017-01-19 16:54:25 -08001180// |dtls| will be set to true if DTLS is active for transport and crypto is
1181// empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001182bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1183 bool* dtls,
1184 std::string* error_desc) {
deadbeeff5346592017-01-24 21:51:21 -08001185 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001186 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001187 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001188 return false;
1189 }
1190 return true;
1191}
1192
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001193bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001194 ContentAction action,
1195 ContentSource src,
1196 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001197 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001198 if (action == CA_UPDATE) {
1199 // no crypto params.
1200 return true;
1201 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001203 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001204 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001205 if (!ret) {
1206 return false;
1207 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 switch (action) {
1209 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001210 // If DTLS is already active on the channel, we could be renegotiating
1211 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001212 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001213 ret = srtp_filter_.SetOffer(cryptos, src);
1214 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 break;
1216 case CA_PRANSWER:
1217 // If we're doing DTLS-SRTP, we don't want to update the filter
1218 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001219 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1221 }
1222 break;
1223 case CA_ANSWER:
1224 // If we're doing DTLS-SRTP, we don't want to update the filter
1225 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001226 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 ret = srtp_filter_.SetAnswer(cryptos, src);
1228 }
1229 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 default:
1231 break;
1232 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001233 if (!ret) {
1234 SafeSetError("Failed to setup SRTP filter.", error_desc);
1235 return false;
1236 }
1237 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238}
1239
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001240bool BaseChannel::SetRtcpMux_n(bool enable,
1241 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001242 ContentSource src,
1243 std::string* error_desc) {
deadbeef8e814d72017-01-13 11:34:39 -08001244 // Provide a more specific error message for the RTCP mux "require" policy
1245 // case.
1246 if (rtcp_mux_required_ && !enable) {
1247 SafeSetError(
1248 "rtcpMuxPolicy is 'require', but media description does not "
1249 "contain 'a=rtcp-mux'.",
1250 error_desc);
1251 return false;
1252 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 bool ret = false;
1254 switch (action) {
1255 case CA_OFFER:
1256 ret = rtcp_mux_filter_.SetOffer(enable, src);
1257 break;
1258 case CA_PRANSWER:
zhihuangb2cdd932017-01-19 16:54:25 -08001259 // This may activate RTCP muxing, but we don't yet destroy the transport
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001260 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1262 break;
1263 case CA_ANSWER:
1264 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1265 if (ret && rtcp_mux_filter_.IsActive()) {
deadbeefe814a0d2017-02-25 18:15:09 -08001266 // We permanently activated RTCP muxing; signal that we no longer need
1267 // the RTCP transport.
1268 std::string debug_name = transport_name_.empty()
1269 ? rtp_packet_transport_->debug_name()
1270 : transport_name_;
1271 ;
deadbeefcbecd352015-09-23 11:50:27 -07001272 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
deadbeefe814a0d2017-02-25 18:15:09 -08001273 << "; no longer need RTCP transport for " << debug_name;
deadbeeff5346592017-01-24 21:51:21 -08001274 if (rtcp_packet_transport_) {
1275 SetTransport_n(true, nullptr, nullptr);
1276 SignalRtcpMuxFullyActive(transport_name_);
zhihuangf5b251b2017-01-12 19:37:48 -08001277 }
deadbeef062ce9f2016-08-26 21:42:15 -07001278 UpdateWritableState_n();
1279 SetTransportChannelReadyToSend(true, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 }
1281 break;
1282 case CA_UPDATE:
1283 // No RTCP mux info.
1284 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001285 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 default:
1287 break;
1288 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001289 if (!ret) {
1290 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1291 return false;
1292 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
zhihuangb2cdd932017-01-19 16:54:25 -08001294 // CA_ANSWER, but we only want to tear down the RTCP transport if we received
1295 // a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001296 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 // If the RTP transport is already writable, then so are we.
deadbeeff5346592017-01-24 21:51:21 -08001298 if (rtp_packet_transport_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001299 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 }
1301 }
1302
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001303 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304}
1305
1306bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001307 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001308 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309}
1310
Peter Boström0c4e06b2015-10-07 12:23:21 +02001311bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001312 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 return media_channel()->RemoveRecvStream(ssrc);
1314}
1315
1316bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001317 ContentAction action,
1318 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001319 if (!(action == CA_OFFER || action == CA_ANSWER ||
1320 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 return false;
1322
1323 // If this is an update, streams only contain streams that have changed.
1324 if (action == CA_UPDATE) {
1325 for (StreamParamsVec::const_iterator it = streams.begin();
1326 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001327 const StreamParams* existing_stream =
1328 GetStreamByIds(local_streams_, it->groupid, it->id);
1329 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 if (media_channel()->AddSendStream(*it)) {
1331 local_streams_.push_back(*it);
1332 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1333 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001334 std::ostringstream desc;
1335 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1336 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 return false;
1338 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001339 } else if (existing_stream && !it->has_ssrcs()) {
1340 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001341 std::ostringstream desc;
1342 desc << "Failed to remove send stream with ssrc "
1343 << it->first_ssrc() << ".";
1344 SafeSetError(desc.str(), error_desc);
1345 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001347 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 } else {
1349 LOG(LS_WARNING) << "Ignore unsupported stream update";
1350 }
1351 }
1352 return true;
1353 }
1354 // Else streams are all the streams we want to send.
1355
1356 // Check for streams that have been removed.
1357 bool ret = true;
1358 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1359 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001360 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001362 std::ostringstream desc;
1363 desc << "Failed to remove send stream with ssrc "
1364 << it->first_ssrc() << ".";
1365 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 ret = false;
1367 }
1368 }
1369 }
1370 // Check for new streams.
1371 for (StreamParamsVec::const_iterator it = streams.begin();
1372 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001373 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001375 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001377 std::ostringstream desc;
1378 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1379 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380 ret = false;
1381 }
1382 }
1383 }
1384 local_streams_ = streams;
1385 return ret;
1386}
1387
1388bool BaseChannel::UpdateRemoteStreams_w(
1389 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001390 ContentAction action,
1391 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001392 if (!(action == CA_OFFER || action == CA_ANSWER ||
1393 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001394 return false;
1395
1396 // If this is an update, streams only contain streams that have changed.
1397 if (action == CA_UPDATE) {
1398 for (StreamParamsVec::const_iterator it = streams.begin();
1399 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001400 const StreamParams* existing_stream =
1401 GetStreamByIds(remote_streams_, it->groupid, it->id);
1402 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403 if (AddRecvStream_w(*it)) {
1404 remote_streams_.push_back(*it);
1405 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1406 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001407 std::ostringstream desc;
1408 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1409 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410 return false;
1411 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001412 } else if (existing_stream && !it->has_ssrcs()) {
1413 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001414 std::ostringstream desc;
1415 desc << "Failed to remove remote stream with ssrc "
1416 << it->first_ssrc() << ".";
1417 SafeSetError(desc.str(), error_desc);
1418 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001420 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421 } else {
1422 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001423 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 << " new stream = " << it->ToString();
1425 }
1426 }
1427 return true;
1428 }
1429 // Else streams are all the streams we want to receive.
1430
1431 // Check for streams that have been removed.
1432 bool ret = true;
1433 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1434 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001435 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001437 std::ostringstream desc;
1438 desc << "Failed to remove remote stream with ssrc "
1439 << it->first_ssrc() << ".";
1440 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 ret = false;
1442 }
1443 }
1444 }
1445 // Check for new streams.
1446 for (StreamParamsVec::const_iterator it = streams.begin();
1447 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001448 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001449 if (AddRecvStream_w(*it)) {
1450 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1451 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001452 std::ostringstream desc;
1453 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1454 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 ret = false;
1456 }
1457 }
1458 }
1459 remote_streams_ = streams;
1460 return ret;
1461}
1462
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001463void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001464 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001465// Absolute Send Time extension id is used only with external auth,
1466// so do not bother searching for it and making asyncronious call to set
1467// something that is not used.
1468#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001469 const webrtc::RtpExtension* send_time_extension =
1470 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001471 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001472 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001473 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001474 RTC_FROM_HERE, network_thread_,
1475 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1476 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001477#endif
1478}
1479
1480void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1481 int rtp_abs_sendtime_extn_id) {
1482 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001483}
1484
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001485void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001486 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001488 case MSG_SEND_RTP_PACKET:
1489 case MSG_SEND_RTCP_PACKET: {
1490 RTC_DCHECK(network_thread_->IsCurrent());
1491 SendPacketMessageData* data =
1492 static_cast<SendPacketMessageData*>(pmsg->pdata);
1493 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1494 SendPacket(rtcp, &data->packet, data->options);
1495 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 break;
1497 }
1498 case MSG_FIRSTPACKETRECEIVED: {
1499 SignalFirstPacketReceived(this);
1500 break;
1501 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 }
1503}
1504
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001505void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 // Flush all remaining RTCP messages. This should only be called in
1507 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001508 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001509 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001510 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1511 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001512 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1513 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 }
1515}
1516
johand89ab142016-10-25 10:50:32 -07001517void BaseChannel::SignalSentPacket_n(
deadbeef5bd5ca32017-02-10 11:31:50 -08001518 rtc::PacketTransportInternal* /* transport */,
johand89ab142016-10-25 10:50:32 -07001519 const rtc::SentPacket& sent_packet) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001520 RTC_DCHECK(network_thread_->IsCurrent());
1521 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001522 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001523 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1524}
1525
1526void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1527 RTC_DCHECK(worker_thread_->IsCurrent());
1528 SignalSentPacket(sent_packet);
1529}
1530
1531VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1532 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001533 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534 MediaEngineInterface* media_engine,
1535 VoiceMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001537 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001538 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001539 : BaseChannel(worker_thread,
1540 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001541 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001542 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001543 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001544 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001545 srtp_required),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001546 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001547 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548
1549VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001550 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 StopAudioMonitor();
1552 StopMediaMonitor();
1553 // this can't be done in the base class, since it calls a virtual
1554 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001555 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556}
1557
Peter Boström0c4e06b2015-10-07 12:23:21 +02001558bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001559 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001560 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001561 AudioSource* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001562 return InvokeOnWorker(RTC_FROM_HERE,
1563 Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001564 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565}
1566
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567// TODO(juberti): Handle early media the right way. We should get an explicit
1568// ringing message telling us to start playing local ringback, which we cancel
1569// if any early media actually arrives. For now, we do the opposite, which is
1570// to wait 1 second for early media, and start playing local ringback if none
1571// arrives.
1572void VoiceChannel::SetEarlyMedia(bool enable) {
1573 if (enable) {
1574 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001575 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1576 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577 } else {
1578 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001579 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 }
1581}
1582
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583bool VoiceChannel::CanInsertDtmf() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001584 return InvokeOnWorker(
1585 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586}
1587
Peter Boström0c4e06b2015-10-07 12:23:21 +02001588bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1589 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001590 int duration) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001591 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this,
1592 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593}
1594
solenberg4bac9c52015-10-09 02:32:53 -07001595bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001596 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume,
1597 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001599
Tommif888bb52015-12-12 01:37:01 +01001600void VoiceChannel::SetRawAudioSink(
1601 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001602 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1603 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001604 // passing. So we invoke to our own little routine that gets a pointer to
1605 // our local variable. This is OK since we're synchronously invoking.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001606 InvokeOnWorker(RTC_FROM_HERE,
1607 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001608}
1609
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001610webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001611 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001612 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001613}
1614
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001615webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1616 uint32_t ssrc) const {
1617 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001618}
1619
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001620bool VoiceChannel::SetRtpSendParameters(
1621 uint32_t ssrc,
1622 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001623 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001624 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001625 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001626}
1627
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001628bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1629 webrtc::RtpParameters parameters) {
1630 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1631}
1632
1633webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1634 uint32_t ssrc) const {
1635 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001636 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001637 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1638}
1639
1640webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1641 uint32_t ssrc) const {
1642 return media_channel()->GetRtpReceiveParameters(ssrc);
1643}
1644
1645bool VoiceChannel::SetRtpReceiveParameters(
1646 uint32_t ssrc,
1647 const webrtc::RtpParameters& parameters) {
1648 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001649 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001650 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1651}
1652
1653bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1654 webrtc::RtpParameters parameters) {
1655 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001656}
1657
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001659 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1660 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661}
1662
1663void VoiceChannel::StartMediaMonitor(int cms) {
1664 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001665 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 media_monitor_->SignalUpdate.connect(
1667 this, &VoiceChannel::OnMediaMonitorUpdate);
1668 media_monitor_->Start(cms);
1669}
1670
1671void VoiceChannel::StopMediaMonitor() {
1672 if (media_monitor_) {
1673 media_monitor_->Stop();
1674 media_monitor_->SignalUpdate.disconnect(this);
1675 media_monitor_.reset();
1676 }
1677}
1678
1679void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001680 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 audio_monitor_
1682 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1683 audio_monitor_->Start(cms);
1684}
1685
1686void VoiceChannel::StopAudioMonitor() {
1687 if (audio_monitor_) {
1688 audio_monitor_->Stop();
1689 audio_monitor_.reset();
1690 }
1691}
1692
1693bool VoiceChannel::IsAudioMonitorRunning() const {
1694 return (audio_monitor_.get() != NULL);
1695}
1696
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001698 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699}
1700
1701int VoiceChannel::GetOutputLevel_w() {
1702 return media_channel()->GetOutputLevel();
1703}
1704
1705void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1706 media_channel()->GetActiveStreams(actives);
1707}
1708
deadbeef5bd5ca32017-02-10 11:31:50 -08001709void VoiceChannel::OnPacketRead(rtc::PacketTransportInternal* transport,
johand89ab142016-10-25 10:50:32 -07001710 const char* data,
1711 size_t len,
1712 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001713 int flags) {
johand89ab142016-10-25 10:50:32 -07001714 BaseChannel::OnPacketRead(transport, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715 // Set a flag when we've received an RTP packet. If we're waiting for early
1716 // media, this will disable the timeout.
johand89ab142016-10-25 10:50:32 -07001717 if (!received_media_ && !PacketIsRtcp(transport, data, len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 received_media_ = true;
1719 }
1720}
1721
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001722void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001723 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001724 invoker_.AsyncInvoke<void>(
1725 RTC_FROM_HERE, worker_thread_,
1726 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001727}
1728
michaelt79e05882016-11-08 02:50:09 -08001729int BaseChannel::GetTransportOverheadPerPacket() const {
1730 RTC_DCHECK(network_thread_->IsCurrent());
1731
1732 if (!selected_candidate_pair_)
1733 return 0;
1734
1735 int transport_overhead_per_packet = 0;
1736
1737 constexpr int kIpv4Overhaed = 20;
1738 constexpr int kIpv6Overhaed = 40;
1739 transport_overhead_per_packet +=
1740 selected_candidate_pair_->local_candidate().address().family() == AF_INET
1741 ? kIpv4Overhaed
1742 : kIpv6Overhaed;
1743
1744 constexpr int kUdpOverhaed = 8;
1745 constexpr int kTcpOverhaed = 20;
1746 transport_overhead_per_packet +=
1747 selected_candidate_pair_->local_candidate().protocol() ==
1748 TCP_PROTOCOL_NAME
1749 ? kTcpOverhaed
1750 : kUdpOverhaed;
1751
1752 if (secure()) {
1753 int srtp_overhead = 0;
1754 if (srtp_filter_.GetSrtpOverhead(&srtp_overhead))
1755 transport_overhead_per_packet += srtp_overhead;
1756 }
1757
1758 return transport_overhead_per_packet;
1759}
1760
1761void BaseChannel::UpdateTransportOverhead() {
1762 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1763 if (transport_overhead_per_packet)
1764 invoker_.AsyncInvoke<void>(
1765 RTC_FROM_HERE, worker_thread_,
1766 Bind(&MediaChannel::OnTransportOverheadChanged, media_channel_,
1767 transport_overhead_per_packet));
1768}
1769
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001770void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 // Render incoming data if we're the active call, and we have the local
1772 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001773 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001774 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775
1776 // Send outgoing data if we're the active call, we have the remote content,
1777 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001778 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001779 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780
1781 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1782}
1783
1784const ContentInfo* VoiceChannel::GetFirstContent(
1785 const SessionDescription* sdesc) {
1786 return GetFirstAudioContent(sdesc);
1787}
1788
1789bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001790 ContentAction action,
1791 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001792 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001793 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 LOG(LS_INFO) << "Setting local voice description";
1795
1796 const AudioContentDescription* audio =
1797 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001798 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001799 if (!audio) {
1800 SafeSetError("Can't find audio content in local description.", error_desc);
1801 return false;
1802 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001804 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001805 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806 }
1807
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001808 AudioRecvParameters recv_params = last_recv_params_;
1809 RtpParametersFromMediaDescription(audio, &recv_params);
1810 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001811 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001812 error_desc);
1813 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001815 for (const AudioCodec& codec : audio->codecs()) {
1816 bundle_filter()->AddPayloadType(codec.id);
1817 }
1818 last_recv_params_ = recv_params;
1819
1820 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1821 // only give it to the media channel once we have a remote
1822 // description too (without a remote description, we won't be able
1823 // to send them anyway).
1824 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1825 SafeSetError("Failed to set local audio description streams.", error_desc);
1826 return false;
1827 }
1828
1829 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001830 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001831 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832}
1833
1834bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001835 ContentAction action,
1836 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001837 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001838 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 LOG(LS_INFO) << "Setting remote voice description";
1840
1841 const AudioContentDescription* audio =
1842 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001843 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001844 if (!audio) {
1845 SafeSetError("Can't find audio content in remote description.", error_desc);
1846 return false;
1847 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001849 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001850 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 }
1852
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001853 AudioSendParameters send_params = last_send_params_;
1854 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001855 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001856 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001857 }
skvladdc1c62c2016-03-16 19:07:43 -07001858
1859 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1860 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001861 SafeSetError("Failed to set remote audio description send parameters.",
1862 error_desc);
1863 return false;
1864 }
1865 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001867 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1868 // and only give it to the media channel once we have a local
1869 // description too (without a local description, we won't be able to
1870 // recv them anyway).
1871 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1872 SafeSetError("Failed to set remote audio description streams.", error_desc);
1873 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 }
1875
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001876 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001877 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001878 }
1879
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001880 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001881 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001882 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883}
1884
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885void VoiceChannel::HandleEarlyMediaTimeout() {
1886 // This occurs on the main thread, not the worker thread.
1887 if (!received_media_) {
1888 LOG(LS_INFO) << "No early media received before timeout";
1889 SignalEarlyMediaTimeout(this);
1890 }
1891}
1892
Peter Boström0c4e06b2015-10-07 12:23:21 +02001893bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1894 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001895 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896 if (!enabled()) {
1897 return false;
1898 }
solenberg1d63dd02015-12-02 12:35:09 -08001899 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900}
1901
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001902void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 case MSG_EARLYMEDIATIMEOUT:
1905 HandleEarlyMediaTimeout();
1906 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907 case MSG_CHANNEL_ERROR: {
1908 VoiceChannelErrorMessageData* data =
1909 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 delete data;
1911 break;
1912 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 default:
1914 BaseChannel::OnMessage(pmsg);
1915 break;
1916 }
1917}
1918
1919void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001920 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 SignalConnectionMonitor(this, infos);
1922}
1923
1924void VoiceChannel::OnMediaMonitorUpdate(
1925 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001926 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927 SignalMediaMonitor(this, info);
1928}
1929
1930void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1931 const AudioInfo& info) {
1932 SignalAudioMonitor(this, info);
1933}
1934
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001935void VoiceChannel::GetSrtpCryptoSuites_n(
1936 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07001937 GetSupportedAudioCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938}
1939
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001940VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1941 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001942 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 VideoMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001945 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001946 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001947 : BaseChannel(worker_thread,
1948 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001949 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001950 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001951 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001952 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001953 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001956 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957 StopMediaMonitor();
1958 // this can't be done in the base class, since it calls a virtual
1959 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001960
1961 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962}
1963
nisse08582ff2016-02-04 01:24:52 -08001964bool VideoChannel::SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -08001965 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
nisse08582ff2016-02-04 01:24:52 -08001966 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001967 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001968 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969 return true;
1970}
1971
deadbeef5a4a75a2016-06-02 16:23:38 -07001972bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001973 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001974 bool mute,
1975 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -08001976 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001977 return InvokeOnWorker(RTC_FROM_HERE,
1978 Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
deadbeef5a4a75a2016-06-02 16:23:38 -07001979 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001980}
1981
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001982webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001983 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001984 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001985}
1986
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001987webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1988 uint32_t ssrc) const {
1989 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001990}
1991
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001992bool VideoChannel::SetRtpSendParameters(
1993 uint32_t ssrc,
1994 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001995 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001996 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001997 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001998}
1999
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002000bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
2001 webrtc::RtpParameters parameters) {
2002 return media_channel()->SetRtpSendParameters(ssrc, parameters);
2003}
2004
2005webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
2006 uint32_t ssrc) const {
2007 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002008 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002009 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
2010}
2011
2012webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
2013 uint32_t ssrc) const {
2014 return media_channel()->GetRtpReceiveParameters(ssrc);
2015}
2016
2017bool VideoChannel::SetRtpReceiveParameters(
2018 uint32_t ssrc,
2019 const webrtc::RtpParameters& parameters) {
2020 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002021 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002022 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
2023}
2024
2025bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
2026 webrtc::RtpParameters parameters) {
2027 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07002028}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002029
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002030void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031 // Send outgoing data if we're the active call, we have the remote content,
2032 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002033 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 if (!media_channel()->SetSend(send)) {
2035 LOG(LS_ERROR) << "Failed to SetSend on video channel";
2036 // TODO(gangji): Report error back to server.
2037 }
2038
Peter Boström34fbfff2015-09-24 19:20:30 +02002039 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040}
2041
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00002042bool VideoChannel::GetStats(VideoMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002043 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
2044 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002045}
2046
2047void VideoChannel::StartMediaMonitor(int cms) {
2048 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002049 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050 media_monitor_->SignalUpdate.connect(
2051 this, &VideoChannel::OnMediaMonitorUpdate);
2052 media_monitor_->Start(cms);
2053}
2054
2055void VideoChannel::StopMediaMonitor() {
2056 if (media_monitor_) {
2057 media_monitor_->Stop();
2058 media_monitor_.reset();
2059 }
2060}
2061
2062const ContentInfo* VideoChannel::GetFirstContent(
2063 const SessionDescription* sdesc) {
2064 return GetFirstVideoContent(sdesc);
2065}
2066
2067bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002068 ContentAction action,
2069 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002070 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002071 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072 LOG(LS_INFO) << "Setting local video description";
2073
2074 const VideoContentDescription* video =
2075 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002076 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002077 if (!video) {
2078 SafeSetError("Can't find video content in local description.", error_desc);
2079 return false;
2080 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002081
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002082 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002083 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 }
2085
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002086 VideoRecvParameters recv_params = last_recv_params_;
2087 RtpParametersFromMediaDescription(video, &recv_params);
2088 if (!media_channel()->SetRecvParameters(recv_params)) {
2089 SafeSetError("Failed to set local video description recv parameters.",
2090 error_desc);
2091 return false;
2092 }
2093 for (const VideoCodec& codec : video->codecs()) {
2094 bundle_filter()->AddPayloadType(codec.id);
2095 }
2096 last_recv_params_ = recv_params;
2097
2098 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2099 // only give it to the media channel once we have a remote
2100 // description too (without a remote description, we won't be able
2101 // to send them anyway).
2102 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2103 SafeSetError("Failed to set local video description streams.", error_desc);
2104 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105 }
2106
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002107 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002108 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002109 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002110}
2111
2112bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002113 ContentAction action,
2114 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002115 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002116 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 LOG(LS_INFO) << "Setting remote video description";
2118
2119 const VideoContentDescription* video =
2120 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002121 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002122 if (!video) {
2123 SafeSetError("Can't find video content in remote description.", error_desc);
2124 return false;
2125 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002127 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002128 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129 }
2130
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002131 VideoSendParameters send_params = last_send_params_;
2132 RtpSendParametersFromMediaDescription(video, &send_params);
2133 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002134 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002135 }
skvladdc1c62c2016-03-16 19:07:43 -07002136
2137 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2138
2139 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002140 SafeSetError("Failed to set remote video description send parameters.",
2141 error_desc);
2142 return false;
2143 }
2144 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002146 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2147 // and only give it to the media channel once we have a local
2148 // description too (without a local description, we won't be able to
2149 // recv them anyway).
2150 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2151 SafeSetError("Failed to set remote video description streams.", error_desc);
2152 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153 }
2154
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002155 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002156 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002158
2159 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002160 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002161 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162}
2163
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002164void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166 case MSG_CHANNEL_ERROR: {
2167 const VideoChannelErrorMessageData* data =
2168 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 delete data;
2170 break;
2171 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172 default:
2173 BaseChannel::OnMessage(pmsg);
2174 break;
2175 }
2176}
2177
2178void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002179 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180 SignalConnectionMonitor(this, infos);
2181}
2182
2183// TODO(pthatcher): Look into removing duplicate code between
2184// audio, video, and data, perhaps by using templates.
2185void VideoChannel::OnMediaMonitorUpdate(
2186 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002187 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 SignalMediaMonitor(this, info);
2189}
2190
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002191void VideoChannel::GetSrtpCryptoSuites_n(
2192 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002193 GetSupportedVideoCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194}
2195
deadbeef953c2ce2017-01-09 14:53:41 -08002196RtpDataChannel::RtpDataChannel(rtc::Thread* worker_thread,
2197 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002198 rtc::Thread* signaling_thread,
deadbeef953c2ce2017-01-09 14:53:41 -08002199 DataMediaChannel* media_channel,
deadbeef953c2ce2017-01-09 14:53:41 -08002200 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08002201 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002202 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002203 : BaseChannel(worker_thread,
2204 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002205 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002206 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002207 content_name,
deadbeefac22f702017-01-12 21:59:29 -08002208 rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002209 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210
deadbeef953c2ce2017-01-09 14:53:41 -08002211RtpDataChannel::~RtpDataChannel() {
2212 TRACE_EVENT0("webrtc", "RtpDataChannel::~RtpDataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 StopMediaMonitor();
2214 // this can't be done in the base class, since it calls a virtual
2215 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002216
2217 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218}
2219
deadbeeff5346592017-01-24 21:51:21 -08002220bool RtpDataChannel::Init_w(
2221 DtlsTransportInternal* rtp_dtls_transport,
2222 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -08002223 rtc::PacketTransportInternal* rtp_packet_transport,
2224 rtc::PacketTransportInternal* rtcp_packet_transport) {
deadbeeff5346592017-01-24 21:51:21 -08002225 if (!BaseChannel::Init_w(rtp_dtls_transport, rtcp_dtls_transport,
2226 rtp_packet_transport, rtcp_packet_transport)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002227 return false;
2228 }
deadbeef953c2ce2017-01-09 14:53:41 -08002229 media_channel()->SignalDataReceived.connect(this,
2230 &RtpDataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002231 media_channel()->SignalReadyToSend.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08002232 this, &RtpDataChannel::OnDataChannelReadyToSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233 return true;
2234}
2235
deadbeef953c2ce2017-01-09 14:53:41 -08002236bool RtpDataChannel::SendData(const SendDataParams& params,
2237 const rtc::CopyOnWriteBuffer& payload,
2238 SendDataResult* result) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002239 return InvokeOnWorker(
2240 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2241 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242}
2243
deadbeef953c2ce2017-01-09 14:53:41 -08002244const ContentInfo* RtpDataChannel::GetFirstContent(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245 const SessionDescription* sdesc) {
2246 return GetFirstDataContent(sdesc);
2247}
2248
deadbeef953c2ce2017-01-09 14:53:41 -08002249bool RtpDataChannel::CheckDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002250 const DataContentDescription* content,
2251 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2253 (content->protocol() == kMediaProtocolDtlsSctp));
deadbeef953c2ce2017-01-09 14:53:41 -08002254 // It's been set before, but doesn't match. That's bad.
2255 if (is_sctp) {
2256 SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.",
2257 error_desc);
2258 return false;
2259 }
2260 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002261}
2262
deadbeef953c2ce2017-01-09 14:53:41 -08002263bool RtpDataChannel::SetLocalContent_w(const MediaContentDescription* content,
2264 ContentAction action,
2265 std::string* error_desc) {
2266 TRACE_EVENT0("webrtc", "RtpDataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002267 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002268 LOG(LS_INFO) << "Setting local data description";
2269
2270 const DataContentDescription* data =
2271 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002272 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002273 if (!data) {
2274 SafeSetError("Can't find data content in local description.", error_desc);
2275 return false;
2276 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002277
deadbeef953c2ce2017-01-09 14:53:41 -08002278 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279 return false;
2280 }
2281
deadbeef953c2ce2017-01-09 14:53:41 -08002282 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
2283 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284 }
2285
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002286 DataRecvParameters recv_params = last_recv_params_;
2287 RtpParametersFromMediaDescription(data, &recv_params);
2288 if (!media_channel()->SetRecvParameters(recv_params)) {
2289 SafeSetError("Failed to set remote data description recv parameters.",
2290 error_desc);
2291 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 }
deadbeef953c2ce2017-01-09 14:53:41 -08002293 for (const DataCodec& codec : data->codecs()) {
2294 bundle_filter()->AddPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002295 }
2296 last_recv_params_ = recv_params;
2297
2298 // TODO(pthatcher): Move local streams into DataSendParameters, and
2299 // only give it to the media channel once we have a remote
2300 // description too (without a remote description, we won't be able
2301 // to send them anyway).
2302 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2303 SafeSetError("Failed to set local data description streams.", error_desc);
2304 return false;
2305 }
2306
2307 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002308 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002309 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310}
2311
deadbeef953c2ce2017-01-09 14:53:41 -08002312bool RtpDataChannel::SetRemoteContent_w(const MediaContentDescription* content,
2313 ContentAction action,
2314 std::string* error_desc) {
2315 TRACE_EVENT0("webrtc", "RtpDataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002316 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317
2318 const DataContentDescription* data =
2319 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002320 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002321 if (!data) {
2322 SafeSetError("Can't find data content in remote description.", error_desc);
2323 return false;
2324 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002326 // If the remote data doesn't have codecs and isn't an update, it
2327 // must be empty, so ignore it.
2328 if (!data->has_codecs() && action != CA_UPDATE) {
2329 return true;
2330 }
2331
deadbeef953c2ce2017-01-09 14:53:41 -08002332 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333 return false;
2334 }
2335
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002336 LOG(LS_INFO) << "Setting remote data description";
deadbeef953c2ce2017-01-09 14:53:41 -08002337 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002338 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339 }
2340
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002341 DataSendParameters send_params = last_send_params_;
2342 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2343 if (!media_channel()->SetSendParameters(send_params)) {
2344 SafeSetError("Failed to set remote data description send parameters.",
2345 error_desc);
2346 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002347 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002348 last_send_params_ = send_params;
2349
2350 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2351 // and only give it to the media channel once we have a local
2352 // description too (without a local description, we won't be able to
2353 // recv them anyway).
2354 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2355 SafeSetError("Failed to set remote data description streams.",
2356 error_desc);
2357 return false;
2358 }
2359
2360 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002361 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002362 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002363}
2364
deadbeef953c2ce2017-01-09 14:53:41 -08002365void RtpDataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002366 // Render incoming data if we're the active call, and we have the local
2367 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002368 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369 if (!media_channel()->SetReceive(recv)) {
2370 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2371 }
2372
2373 // Send outgoing data if we're the active call, we have the remote content,
2374 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002375 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002376 if (!media_channel()->SetSend(send)) {
2377 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2378 }
2379
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002380 // Trigger SignalReadyToSendData asynchronously.
2381 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002382
2383 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2384}
2385
deadbeef953c2ce2017-01-09 14:53:41 -08002386void RtpDataChannel::OnMessage(rtc::Message* pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002387 switch (pmsg->message_id) {
2388 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002389 DataChannelReadyToSendMessageData* data =
2390 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002391 ready_to_send_data_ = data->data();
2392 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393 delete data;
2394 break;
2395 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002396 case MSG_DATARECEIVED: {
2397 DataReceivedMessageData* data =
2398 static_cast<DataReceivedMessageData*>(pmsg->pdata);
deadbeef953c2ce2017-01-09 14:53:41 -08002399 SignalDataReceived(data->params, data->payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002400 delete data;
2401 break;
2402 }
2403 case MSG_CHANNEL_ERROR: {
2404 const DataChannelErrorMessageData* data =
2405 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002406 delete data;
2407 break;
2408 }
2409 default:
2410 BaseChannel::OnMessage(pmsg);
2411 break;
2412 }
2413}
2414
deadbeef953c2ce2017-01-09 14:53:41 -08002415void RtpDataChannel::OnConnectionMonitorUpdate(
2416 ConnectionMonitor* monitor,
2417 const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418 SignalConnectionMonitor(this, infos);
2419}
2420
deadbeef953c2ce2017-01-09 14:53:41 -08002421void RtpDataChannel::StartMediaMonitor(int cms) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002422 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002423 rtc::Thread::Current()));
deadbeef953c2ce2017-01-09 14:53:41 -08002424 media_monitor_->SignalUpdate.connect(this,
2425 &RtpDataChannel::OnMediaMonitorUpdate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002426 media_monitor_->Start(cms);
2427}
2428
deadbeef953c2ce2017-01-09 14:53:41 -08002429void RtpDataChannel::StopMediaMonitor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002430 if (media_monitor_) {
2431 media_monitor_->Stop();
2432 media_monitor_->SignalUpdate.disconnect(this);
2433 media_monitor_.reset();
2434 }
2435}
2436
deadbeef953c2ce2017-01-09 14:53:41 -08002437void RtpDataChannel::OnMediaMonitorUpdate(DataMediaChannel* media_channel,
2438 const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002439 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002440 SignalMediaMonitor(this, info);
2441}
2442
deadbeef953c2ce2017-01-09 14:53:41 -08002443void RtpDataChannel::OnDataReceived(const ReceiveDataParams& params,
2444 const char* data,
2445 size_t len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002446 DataReceivedMessageData* msg = new DataReceivedMessageData(
2447 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002448 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002449}
2450
deadbeef953c2ce2017-01-09 14:53:41 -08002451void RtpDataChannel::OnDataChannelError(uint32_t ssrc,
2452 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2454 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002455 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002456}
2457
deadbeef953c2ce2017-01-09 14:53:41 -08002458void RtpDataChannel::OnDataChannelReadyToSend(bool writable) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002459 // This is usded for congestion control to indicate that the stream is ready
2460 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2461 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002462 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002463 new DataChannelReadyToSendMessageData(writable));
2464}
2465
deadbeef953c2ce2017-01-09 14:53:41 -08002466void RtpDataChannel::GetSrtpCryptoSuites_n(
2467 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002468 GetSupportedDataCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002469}
2470
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002471} // namespace cricket