blob: 3c438f63ef601df25b7507aa25e5092445581a89 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwiberg0eb15ed2015-12-17 03:04:15 -080011#include <utility>
12
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010013#include "webrtc/pc/channel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
kjellandera69d9732016-08-31 07:33:05 -070015#include "webrtc/api/call/audio_sink.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000016#include "webrtc/base/bind.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000017#include "webrtc/base/byteorder.h"
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -070018#include "webrtc/base/checks.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000019#include "webrtc/base/common.h"
jbaucheec21bd2016-03-20 06:15:43 -070020#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000021#include "webrtc/base/dscp.h"
22#include "webrtc/base/logging.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070023#include "webrtc/base/networkroute.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010024#include "webrtc/base/trace_event.h"
kjellanderf4752772016-03-02 05:42:30 -080025#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080026#include "webrtc/media/base/rtputils.h"
johand89ab142016-10-25 10:50:32 -070027#include "webrtc/p2p/base/packettransportinterface.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010028#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
30namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000031using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000032
deadbeef2d110be2016-01-13 12:00:26 -080033namespace {
kwiberg31022942016-03-11 14:18:21 -080034// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080035bool SetRawAudioSink_w(VoiceMediaChannel* channel,
36 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080037 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
38 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080039 return true;
40}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020041
42struct SendPacketMessageData : public rtc::MessageData {
43 rtc::CopyOnWriteBuffer packet;
44 rtc::PacketOptions options;
45};
46
isheriff6f8d6862016-05-26 11:24:55 -070047#if defined(ENABLE_EXTERNAL_AUTH)
48// Returns the named header extension if found among all extensions,
49// nullptr otherwise.
50const webrtc::RtpExtension* FindHeaderExtension(
51 const std::vector<webrtc::RtpExtension>& extensions,
52 const std::string& uri) {
53 for (const auto& extension : extensions) {
54 if (extension.uri == uri)
55 return &extension;
56 }
57 return nullptr;
58}
59#endif
60
deadbeef2d110be2016-01-13 12:00:26 -080061} // namespace
62
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000064 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020065 MSG_SEND_RTP_PACKET,
66 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 MSG_FIRSTPACKETRECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071};
72
73// Value specified in RFC 5764.
74static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
75
76static const int kAgcMinus10db = -10;
77
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000078static void SafeSetError(const std::string& message, std::string* error_desc) {
79 if (error_desc) {
80 *error_desc = message;
81 }
82}
83
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 : ssrc(in_ssrc), error(in_error) {}
88 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 VoiceMediaChannel::Error error;
90};
91
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000092struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020093 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020095 : ssrc(in_ssrc), error(in_error) {}
96 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 VideoMediaChannel::Error error;
98};
99
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000100struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 : ssrc(in_ssrc), error(in_error) {}
104 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 DataMediaChannel::Error error;
106};
107
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108static const char* PacketType(bool rtcp) {
109 return (!rtcp) ? "RTP" : "RTCP";
110}
111
jbaucheec21bd2016-03-20 06:15:43 -0700112static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 // Check the packet size. We could check the header too if needed.
114 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000115 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
116 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117}
118
119static bool IsReceiveContentDirection(MediaContentDirection direction) {
120 return direction == MD_SENDRECV || direction == MD_RECVONLY;
121}
122
123static bool IsSendContentDirection(MediaContentDirection direction) {
124 return direction == MD_SENDRECV || direction == MD_SENDONLY;
125}
126
127static const MediaContentDescription* GetContentDescription(
128 const ContentInfo* cinfo) {
129 if (cinfo == NULL)
130 return NULL;
131 return static_cast<const MediaContentDescription*>(cinfo->description);
132}
133
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700134template <class Codec>
135void RtpParametersFromMediaDescription(
136 const MediaContentDescriptionImpl<Codec>* desc,
137 RtpParameters<Codec>* params) {
138 // TODO(pthatcher): Remove this once we're sure no one will give us
139 // a description without codecs (currently a CA_UPDATE with just
140 // streams can).
141 if (desc->has_codecs()) {
142 params->codecs = desc->codecs();
143 }
144 // TODO(pthatcher): See if we really need
145 // rtp_header_extensions_set() and remove it if we don't.
146 if (desc->rtp_header_extensions_set()) {
147 params->extensions = desc->rtp_header_extensions();
148 }
deadbeef13871492015-12-09 12:37:51 -0800149 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700150}
151
nisse05103312016-03-16 02:22:50 -0700152template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700153void RtpSendParametersFromMediaDescription(
154 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700155 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700156 RtpParametersFromMediaDescription(desc, send_params);
157 send_params->max_bandwidth_bps = desc->bandwidth();
158}
159
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200160BaseChannel::BaseChannel(rtc::Thread* worker_thread,
161 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800162 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700163 MediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700164 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800165 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800166 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200167 : worker_thread_(worker_thread),
168 network_thread_(network_thread),
zhihuangf5b251b2017-01-12 19:37:48 -0800169 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 content_name_(content_name),
deadbeefac22f702017-01-12 21:59:29 -0800171 rtcp_mux_required_(rtcp_mux_required),
deadbeef7af91dd2016-12-13 11:29:11 -0800172 srtp_required_(srtp_required),
michaelt79e05882016-11-08 02:50:09 -0800173 media_channel_(media_channel),
174 selected_candidate_pair_(nullptr) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700175 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 LOG(LS_INFO) << "Created channel for " << content_name;
177}
178
179BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800180 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700181 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000182 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200184 // Eats any outstanding messages or packets.
185 worker_thread_->Clear(&invoker_);
186 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 // We must destroy the media channel before the transport channel, otherwise
188 // the media channel may try to send on the dead transport channel. NULLing
189 // is not an effective strategy since the sends will come on another thread.
190 delete media_channel_;
zhihuangf5b251b2017-01-12 19:37:48 -0800191 LOG(LS_INFO) << "Destroyed channel: " << content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200192}
193
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200194void BaseChannel::DisconnectTransportChannels_n() {
195 // Send any outstanding RTCP packets.
196 FlushRtcpMessages_n();
197
198 // Stop signals from transport channels, but keep them alive because
199 // media_channel may use them from a different thread.
zhihuangb2cdd932017-01-19 16:54:25 -0800200 if (rtp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800201 DisconnectFromDtlsTransport(rtp_dtls_transport_);
202 } else if (rtp_packet_transport_) {
203 DisconnectFromPacketTransport(rtp_packet_transport_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200204 }
zhihuangb2cdd932017-01-19 16:54:25 -0800205 if (rtcp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800206 DisconnectFromDtlsTransport(rtcp_dtls_transport_);
207 } else if (rtcp_packet_transport_) {
208 DisconnectFromPacketTransport(rtcp_packet_transport_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200209 }
210
211 // Clear pending read packets/messages.
212 network_thread_->Clear(&invoker_);
213 network_thread_->Clear(this);
214}
215
zhihuangb2cdd932017-01-19 16:54:25 -0800216bool BaseChannel::Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800217 DtlsTransportInternal* rtcp_dtls_transport,
218 rtc::PacketTransportInterface* rtp_packet_transport,
219 rtc::PacketTransportInterface* rtcp_packet_transport) {
skvlad6c87a672016-05-17 17:49:52 -0700220 if (!network_thread_->Invoke<bool>(
zhihuangb2cdd932017-01-19 16:54:25 -0800221 RTC_FROM_HERE, Bind(&BaseChannel::InitNetwork_n, this,
deadbeeff5346592017-01-24 21:51:21 -0800222 rtp_dtls_transport, rtcp_dtls_transport,
223 rtp_packet_transport, rtcp_packet_transport))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000224 return false;
225 }
deadbeeff5346592017-01-24 21:51:21 -0800226 // Both RTP and RTCP channels should be set, we can call SetInterface on
227 // the media channel and it can set network options.
228 RTC_DCHECK_RUN_ON(worker_thread_);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000229 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 return true;
231}
232
deadbeeff5346592017-01-24 21:51:21 -0800233bool BaseChannel::InitNetwork_n(
234 DtlsTransportInternal* rtp_dtls_transport,
235 DtlsTransportInternal* rtcp_dtls_transport,
236 rtc::PacketTransportInterface* rtp_packet_transport,
237 rtc::PacketTransportInterface* rtcp_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200238 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800239 SetTransports_n(rtp_dtls_transport, rtcp_dtls_transport, rtp_packet_transport,
240 rtcp_packet_transport);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200241
deadbeeff5346592017-01-24 21:51:21 -0800242 if (rtp_dtls_transport_ &&
243 !SetDtlsSrtpCryptoSuites_n(rtp_dtls_transport_, false)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200244 return false;
245 }
zhihuangb2cdd932017-01-19 16:54:25 -0800246 if (rtcp_dtls_transport_ &&
247 !SetDtlsSrtpCryptoSuites_n(rtcp_dtls_transport_, true)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200248 return false;
249 }
deadbeefac22f702017-01-12 21:59:29 -0800250 if (rtcp_mux_required_) {
251 rtcp_mux_filter_.SetActive();
252 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200253 return true;
254}
255
wu@webrtc.org78187522013-10-07 23:32:02 +0000256void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200257 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000258 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200259 // Packets arrive on the network thread, processing packets calls virtual
260 // functions, so need to stop this process in Deinit that is called in
261 // derived classes destructor.
262 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700263 RTC_FROM_HERE, Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000264}
265
zhihuangb2cdd932017-01-19 16:54:25 -0800266void BaseChannel::SetTransports(DtlsTransportInternal* rtp_dtls_transport,
267 DtlsTransportInternal* rtcp_dtls_transport) {
deadbeeff5346592017-01-24 21:51:21 -0800268 network_thread_->Invoke<void>(
269 RTC_FROM_HERE,
270 Bind(&BaseChannel::SetTransports_n, this, rtp_dtls_transport,
271 rtcp_dtls_transport, rtp_dtls_transport, rtcp_dtls_transport));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000272}
273
deadbeeff5346592017-01-24 21:51:21 -0800274void BaseChannel::SetTransports(
275 rtc::PacketTransportInterface* rtp_packet_transport,
276 rtc::PacketTransportInterface* rtcp_packet_transport) {
277 network_thread_->Invoke<void>(
278 RTC_FROM_HERE, Bind(&BaseChannel::SetTransports_n, this, nullptr, nullptr,
279 rtp_packet_transport, rtcp_packet_transport));
280}
zhihuangf5b251b2017-01-12 19:37:48 -0800281
deadbeeff5346592017-01-24 21:51:21 -0800282void BaseChannel::SetTransports_n(
283 DtlsTransportInternal* rtp_dtls_transport,
284 DtlsTransportInternal* rtcp_dtls_transport,
285 rtc::PacketTransportInterface* rtp_packet_transport,
286 rtc::PacketTransportInterface* rtcp_packet_transport) {
287 RTC_DCHECK(network_thread_->IsCurrent());
288 // Validate some assertions about the input.
289 RTC_DCHECK(rtp_packet_transport);
290 RTC_DCHECK_EQ(NeedsRtcpTransport(), rtcp_packet_transport != nullptr);
291 if (rtp_dtls_transport || rtcp_dtls_transport) {
292 // DTLS/non-DTLS pointers should be to the same object.
293 RTC_DCHECK(rtp_dtls_transport == rtp_packet_transport);
294 RTC_DCHECK(rtcp_dtls_transport == rtcp_packet_transport);
295 // Can't go from non-DTLS to DTLS.
296 RTC_DCHECK(!rtp_packet_transport_ || rtp_dtls_transport_);
297 } else {
298 // Can't go from DTLS to non-DTLS.
299 RTC_DCHECK(!rtp_dtls_transport_);
300 }
301 // Transport names should be the same.
zhihuangb2cdd932017-01-19 16:54:25 -0800302 if (rtp_dtls_transport && rtcp_dtls_transport) {
303 RTC_DCHECK(rtp_dtls_transport->transport_name() ==
304 rtcp_dtls_transport->transport_name());
zhihuangb2cdd932017-01-19 16:54:25 -0800305 }
deadbeeff5346592017-01-24 21:51:21 -0800306 std::string debug_name;
307 if (rtp_dtls_transport) {
308 transport_name_ = rtp_dtls_transport->transport_name();
309 debug_name = transport_name_;
310 } else {
311 debug_name = rtp_packet_transport->debug_name();
312 }
313 if (rtp_packet_transport == rtp_packet_transport_) {
314 // Nothing to do if transport isn't changing.
deadbeefbad5dad2017-01-17 18:32:35 -0800315 return;
deadbeefcbecd352015-09-23 11:50:27 -0700316 }
317
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800318 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
319 // changes and wait until the DTLS handshake is complete to set the newly
320 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200321 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800322 // Set |writable_| to false such that UpdateWritableState_w can set up
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700323 // DTLS-SRTP when |writable_| becomes true again.
guoweis46383312015-12-17 16:45:59 -0800324 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800325 srtp_filter_.ResetParams();
326 }
327
deadbeefac22f702017-01-12 21:59:29 -0800328 // If this BaseChannel doesn't require RTCP mux and we haven't fully
329 // negotiated RTCP mux, we need an RTCP transport.
deadbeeff5346592017-01-24 21:51:21 -0800330 if (rtcp_packet_transport) {
zhihuangf5b251b2017-01-12 19:37:48 -0800331 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on "
deadbeeff5346592017-01-24 21:51:21 -0800332 << debug_name << " transport " << rtcp_packet_transport;
333 SetTransport_n(true, rtcp_dtls_transport, rtcp_packet_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000334 }
335
deadbeeff5346592017-01-24 21:51:21 -0800336 LOG(LS_INFO) << "Setting RTP Transport for " << content_name() << " on "
337 << debug_name << " transport " << rtp_packet_transport;
338 SetTransport_n(false, rtp_dtls_transport, rtp_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800339
deadbeefcbecd352015-09-23 11:50:27 -0700340 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700341 // setting new transport channels.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200342 UpdateWritableState_n();
deadbeef062ce9f2016-08-26 21:42:15 -0700343 // We can only update ready-to-send after updating writability.
344 //
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700345 // On setting a new channel, assume it's ready to send if it's writable,
346 // because we have no way of knowing otherwise (the channel doesn't give us
347 // "was last send successful?").
348 //
349 // This won't always be accurate (the last SendPacket call from another
350 // BaseChannel could have resulted in an error), but even so, we'll just
351 // encounter the error again and update "ready to send" accordingly.
deadbeef062ce9f2016-08-26 21:42:15 -0700352 SetTransportChannelReadyToSend(
deadbeeff5346592017-01-24 21:51:21 -0800353 false, rtp_packet_transport_ && rtp_packet_transport_->writable());
zhihuangb2cdd932017-01-19 16:54:25 -0800354 SetTransportChannelReadyToSend(
deadbeeff5346592017-01-24 21:51:21 -0800355 true, rtcp_packet_transport_ && rtcp_packet_transport_->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000356}
357
deadbeeff5346592017-01-24 21:51:21 -0800358void BaseChannel::SetTransport_n(
359 bool rtcp,
360 DtlsTransportInternal* new_dtls_transport,
361 rtc::PacketTransportInterface* new_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200362 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800363 DtlsTransportInternal*& old_dtls_transport =
zhihuangb2cdd932017-01-19 16:54:25 -0800364 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
deadbeeff5346592017-01-24 21:51:21 -0800365 rtc::PacketTransportInterface*& old_packet_transport =
366 rtcp ? rtcp_packet_transport_ : rtp_packet_transport_;
zhihuangb2cdd932017-01-19 16:54:25 -0800367
deadbeeff5346592017-01-24 21:51:21 -0800368 if (!old_packet_transport && !new_packet_transport) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700369 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000370 return;
371 }
zhihuangb2cdd932017-01-19 16:54:25 -0800372
deadbeeff5346592017-01-24 21:51:21 -0800373 RTC_DCHECK(old_packet_transport != new_packet_transport);
374 if (old_dtls_transport) {
375 DisconnectFromDtlsTransport(old_dtls_transport);
376 } else if (old_packet_transport) {
377 DisconnectFromPacketTransport(old_packet_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000378 }
379
deadbeeff5346592017-01-24 21:51:21 -0800380 old_packet_transport = new_packet_transport;
381 old_dtls_transport = new_dtls_transport;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000382
deadbeeff5346592017-01-24 21:51:21 -0800383 // If there's no new transport, we're done after disconnecting from old one.
384 if (!new_packet_transport) {
385 return;
386 }
387
388 if (rtcp && new_dtls_transport) {
389 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
390 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
391 << "should never happen.";
392 }
393 if (new_dtls_transport) {
394 ConnectToDtlsTransport(new_dtls_transport);
395 } else {
396 ConnectToPacketTransport(new_packet_transport);
397 }
398 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
399 for (const auto& pair : socket_options) {
400 new_packet_transport->SetOption(pair.first, pair.second);
guoweis46383312015-12-17 16:45:59 -0800401 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000402}
403
deadbeeff5346592017-01-24 21:51:21 -0800404void BaseChannel::ConnectToDtlsTransport(DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200405 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000406
zhihuangb2cdd932017-01-19 16:54:25 -0800407 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
408 transport->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
409 transport->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
410 transport->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
411 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
412 transport->ice_transport()->SignalSelectedCandidatePairChanged.connect(
Honghai Zhangcc411c02016-03-29 17:27:21 -0700413 this, &BaseChannel::OnSelectedCandidatePairChanged);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000414}
415
deadbeeff5346592017-01-24 21:51:21 -0800416void BaseChannel::DisconnectFromDtlsTransport(
417 DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200418 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800419 OnSelectedCandidatePairChanged(transport->ice_transport(), nullptr, -1,
420 false);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000421
zhihuangb2cdd932017-01-19 16:54:25 -0800422 transport->SignalWritableState.disconnect(this);
423 transport->SignalReadPacket.disconnect(this);
424 transport->SignalReadyToSend.disconnect(this);
425 transport->SignalDtlsState.disconnect(this);
426 transport->SignalSentPacket.disconnect(this);
427 transport->ice_transport()->SignalSelectedCandidatePairChanged.disconnect(
428 this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000429}
430
deadbeeff5346592017-01-24 21:51:21 -0800431void BaseChannel::ConnectToPacketTransport(
432 rtc::PacketTransportInterface* transport) {
433 RTC_DCHECK_RUN_ON(network_thread_);
434 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
435 transport->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
436 transport->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
437 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
438}
439
440void BaseChannel::DisconnectFromPacketTransport(
441 rtc::PacketTransportInterface* transport) {
442 RTC_DCHECK_RUN_ON(network_thread_);
443 transport->SignalWritableState.disconnect(this);
444 transport->SignalReadPacket.disconnect(this);
445 transport->SignalReadyToSend.disconnect(this);
446 transport->SignalSentPacket.disconnect(this);
447}
448
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700450 worker_thread_->Invoke<void>(
451 RTC_FROM_HERE,
452 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
453 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 return true;
455}
456
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457bool BaseChannel::AddRecvStream(const StreamParams& sp) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700458 return InvokeOnWorker(RTC_FROM_HERE,
459 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460}
461
Peter Boström0c4e06b2015-10-07 12:23:21 +0200462bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700463 return InvokeOnWorker(RTC_FROM_HERE,
464 Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465}
466
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000467bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000468 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700469 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000470}
471
Peter Boström0c4e06b2015-10-07 12:23:21 +0200472bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700473 return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream,
474 media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000475}
476
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000478 ContentAction action,
479 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100480 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700481 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w,
482 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483}
484
485bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000486 ContentAction action,
487 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100488 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700489 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w,
490 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491}
492
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493void BaseChannel::StartConnectionMonitor(int cms) {
zhihuangb2cdd932017-01-19 16:54:25 -0800494 // We pass in the BaseChannel instead of the rtp_dtls_transport_
495 // because if the rtp_dtls_transport_ changes, the ConnectionMonitor
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000496 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200497 // We pass in the network thread because on that thread connection monitor
498 // will call BaseChannel::GetConnectionStats which must be called on the
499 // network thread.
500 connection_monitor_.reset(
501 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000502 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000504 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505}
506
507void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000508 if (connection_monitor_) {
509 connection_monitor_->Stop();
510 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 }
512}
513
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000514bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200515 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800516 if (!rtp_dtls_transport_) {
517 return false;
518 }
zhihuangb2cdd932017-01-19 16:54:25 -0800519 return rtp_dtls_transport_->ice_transport()->GetStats(infos);
zhihuangf5b251b2017-01-12 19:37:48 -0800520}
521
522bool BaseChannel::NeedsRtcpTransport() {
deadbeefac22f702017-01-12 21:59:29 -0800523 // If this BaseChannel doesn't require RTCP mux and we haven't fully
524 // negotiated RTCP mux, we need an RTCP transport.
525 return !rtcp_mux_required_ && !rtcp_mux_filter_.IsFullyActive();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000526}
527
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700528bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 // Receive data if we are enabled and have local content,
530 return enabled() && IsReceiveContentDirection(local_content_direction_);
531}
532
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700533bool BaseChannel::IsReadyToSendMedia_w() const {
534 // Need to access some state updated on the network thread.
535 return network_thread_->Invoke<bool>(
536 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
537}
538
539bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540 // Send outgoing data if we are enabled, have local and remote content,
541 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800542 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700544 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200545 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546}
547
jbaucheec21bd2016-03-20 06:15:43 -0700548bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700549 const rtc::PacketOptions& options) {
550 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000551}
552
jbaucheec21bd2016-03-20 06:15:43 -0700553bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700554 const rtc::PacketOptions& options) {
555 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556}
557
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000558int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200560 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700561 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200562}
563
564int BaseChannel::SetOption_n(SocketType type,
565 rtc::Socket::Option opt,
566 int value) {
567 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800568 rtc::PacketTransportInterface* transport = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000570 case ST_RTP:
deadbeeff5346592017-01-24 21:51:21 -0800571 transport = rtp_packet_transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700572 socket_options_.push_back(
573 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000574 break;
575 case ST_RTCP:
deadbeeff5346592017-01-24 21:51:21 -0800576 transport = rtcp_packet_transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700577 rtcp_socket_options_.push_back(
578 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000579 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 }
deadbeeff5346592017-01-24 21:51:21 -0800581 return transport ? transport->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582}
583
jbauchcb560652016-08-04 05:20:32 -0700584bool BaseChannel::SetCryptoOptions(const rtc::CryptoOptions& crypto_options) {
585 crypto_options_ = crypto_options;
586 return true;
587}
588
johand89ab142016-10-25 10:50:32 -0700589void BaseChannel::OnWritableState(rtc::PacketTransportInterface* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800590 RTC_DCHECK(transport == rtp_packet_transport_ ||
591 transport == rtcp_packet_transport_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200592 RTC_DCHECK(network_thread_->IsCurrent());
593 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594}
595
johand89ab142016-10-25 10:50:32 -0700596void BaseChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
597 const char* data,
598 size_t len,
599 const rtc::PacketTime& packet_time,
600 int flags) {
601 TRACE_EVENT0("webrtc", "BaseChannel::OnPacketRead");
602 // OnPacketRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200603 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604
605 // When using RTCP multiplexing we might get RTCP packets on the RTP
606 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
johand89ab142016-10-25 10:50:32 -0700607 bool rtcp = PacketIsRtcp(transport, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700608 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000609 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610}
611
johand89ab142016-10-25 10:50:32 -0700612void BaseChannel::OnReadyToSend(rtc::PacketTransportInterface* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800613 RTC_DCHECK(transport == rtp_packet_transport_ ||
614 transport == rtcp_packet_transport_);
615 SetTransportChannelReadyToSend(transport == rtcp_packet_transport_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616}
617
zhihuangb2cdd932017-01-19 16:54:25 -0800618void BaseChannel::OnDtlsState(DtlsTransportInternal* transport,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800619 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200620 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800621 return;
622 }
623
624 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
625 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
zhihuangb2cdd932017-01-19 16:54:25 -0800626 // cover other scenarios like the whole transport is writable (not just this
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800627 // TransportChannel) or when TransportChannel is attached after DTLS is
628 // negotiated.
629 if (state != DTLS_TRANSPORT_CONNECTED) {
630 srtp_filter_.ResetParams();
631 }
632}
633
Honghai Zhangcc411c02016-03-29 17:27:21 -0700634void BaseChannel::OnSelectedCandidatePairChanged(
zhihuangb2cdd932017-01-19 16:54:25 -0800635 IceTransportInternal* ice_transport,
Honghai Zhang52dce732016-03-31 12:37:31 -0700636 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700637 int last_sent_packet_id,
638 bool ready_to_send) {
deadbeeff5346592017-01-24 21:51:21 -0800639 RTC_DCHECK((rtp_dtls_transport_ &&
640 ice_transport == rtp_dtls_transport_->ice_transport()) ||
641 (rtcp_dtls_transport_ &&
642 ice_transport == rtcp_dtls_transport_->ice_transport()));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200643 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800644 selected_candidate_pair_ = selected_candidate_pair;
zhihuangb2cdd932017-01-19 16:54:25 -0800645 std::string transport_name = ice_transport->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700646 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700647 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700648 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700649 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700650 selected_candidate_pair->remote_candidate().network_id(),
651 last_sent_packet_id);
michaelt79e05882016-11-08 02:50:09 -0800652
653 UpdateTransportOverhead();
Honghai Zhangcc411c02016-03-29 17:27:21 -0700654 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200655 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700656 RTC_FROM_HERE, worker_thread_,
657 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
658 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700659}
660
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700661void BaseChannel::SetTransportChannelReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200662 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700663 if (rtcp) {
664 rtcp_ready_to_send_ = ready;
665 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 rtp_ready_to_send_ = ready;
667 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200669 bool ready_to_send =
670 (rtp_ready_to_send_ &&
deadbeeff5346592017-01-24 21:51:21 -0800671 // In the case of rtcp mux |rtcp_packet_transport_| will be null.
672 (rtcp_ready_to_send_ || !rtcp_packet_transport_));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200673
674 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700675 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200676 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677}
678
johand89ab142016-10-25 10:50:32 -0700679bool BaseChannel::PacketIsRtcp(const rtc::PacketTransportInterface* transport,
680 const char* data,
681 size_t len) {
deadbeeff5346592017-01-24 21:51:21 -0800682 return (transport == rtcp_packet_transport_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000683 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684}
685
stefanc1aeaf02015-10-15 07:26:07 -0700686bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700687 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700688 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200689 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
690 // If the thread is not our network thread, we will post to our network
691 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 // synchronize access to all the pieces of the send path, including
693 // SRTP and the inner workings of the transport channels.
694 // The only downside is that we can't return a proper failure code if
695 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200696 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200698 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
699 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800700 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700701 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700702 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 return true;
704 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200705 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706
707 // Now that we are on the correct thread, ensure we have a place to send this
708 // packet before doing anything. (We might get RTCP packets that we don't
709 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
710 // transport.
deadbeeff5346592017-01-24 21:51:21 -0800711 rtc::PacketTransportInterface* transport =
712 (!rtcp || rtcp_mux_filter_.IsActive()) ? rtp_packet_transport_
713 : rtcp_packet_transport_;
zhihuangb2cdd932017-01-19 16:54:25 -0800714 if (!transport || !transport->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 return false;
716 }
717
718 // Protect ourselves against crazy data.
719 if (!ValidPacket(rtcp, packet)) {
720 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000721 << PacketType(rtcp)
722 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000723 return false;
724 }
725
stefanc1aeaf02015-10-15 07:26:07 -0700726 rtc::PacketOptions updated_options;
727 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728 // Protect if needed.
729 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200730 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200732 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000733 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000735 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
736 // inside libsrtp for a RTP packet. A external HMAC module will be writing
737 // a fake HMAC value. This is ONLY done for a RTP packet.
738 // Socket layer will update rtp sendtime extension header if present in
739 // packet with current time before updating the HMAC.
740#if !defined(ENABLE_EXTERNAL_AUTH)
741 res = srtp_filter_.ProtectRtp(
742 data, len, static_cast<int>(packet->capacity()), &len);
743#else
stefanc1aeaf02015-10-15 07:26:07 -0700744 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000745 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000746 res = srtp_filter_.ProtectRtp(
747 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700748 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000749 // If protection succeeds, let's get auth params from srtp.
750 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200751 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000752 int key_len;
753 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700754 &auth_key, &key_len,
755 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000756 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700757 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
758 updated_options.packet_time_params.srtp_auth_key.assign(
759 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000760 }
761 }
762#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 if (!res) {
764 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200765 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 GetRtpSeqNum(data, len, &seq_num);
767 GetRtpSsrc(data, len, &ssrc);
768 LOG(LS_ERROR) << "Failed to protect " << content_name_
769 << " RTP packet: size=" << len
770 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
771 return false;
772 }
773 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000774 res = srtp_filter_.ProtectRtcp(data, len,
775 static_cast<int>(packet->capacity()),
776 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 if (!res) {
778 int type = -1;
779 GetRtcpType(data, len, &type);
780 LOG(LS_ERROR) << "Failed to protect " << content_name_
781 << " RTCP packet: size=" << len << ", type=" << type;
782 return false;
783 }
784 }
785
786 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000787 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800788 } else if (srtp_required_) {
deadbeef8f425f92016-12-01 12:26:27 -0800789 // The audio/video engines may attempt to send RTCP packets as soon as the
790 // streams are created, so don't treat this as an error for RTCP.
791 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809
792 if (rtcp) {
793 return false;
794 }
795 // However, there shouldn't be any RTP packets sent before SRTP is set up
796 // (and SetSend(true) is called).
797 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
798 << " and crypto is required";
nisseeb4ca4e2017-01-12 02:24:27 -0800799 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 return false;
801 }
802
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200804 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
zhihuangb2cdd932017-01-19 16:54:25 -0800805 int ret = transport->SendPacket(packet->data<char>(), packet->size(),
806 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000807 if (ret != static_cast<int>(packet->size())) {
zhihuangb2cdd932017-01-19 16:54:25 -0800808 if (transport->GetError() == ENOTCONN) {
skvladc309e0e2016-07-28 17:15:20 -0700809 LOG(LS_WARNING) << "Got ENOTCONN from transport.";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700810 SetTransportChannelReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811 }
812 return false;
813 }
814 return true;
815}
816
jbaucheec21bd2016-03-20 06:15:43 -0700817bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 // Protect ourselves against crazy data.
819 if (!ValidPacket(rtcp, packet)) {
820 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000821 << PacketType(rtcp)
822 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000823 return false;
824 }
pbos482b12e2015-11-16 10:19:58 -0800825 if (rtcp) {
826 // Permit all (seemingly valid) RTCP packets.
827 return true;
828 }
829 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700830 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831}
832
jbaucheec21bd2016-03-20 06:15:43 -0700833void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000834 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200835 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 if (!WantsPacket(rtcp, packet)) {
837 return;
838 }
839
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000840 // We are only interested in the first rtp packet because that
841 // indicates the media has started flowing.
842 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700844 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 }
846
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 // Unprotect the packet, if needed.
848 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200849 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200850 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000851 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 bool res;
853 if (!rtcp) {
854 res = srtp_filter_.UnprotectRtp(data, len, &len);
855 if (!res) {
856 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200857 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 GetRtpSeqNum(data, len, &seq_num);
859 GetRtpSsrc(data, len, &ssrc);
860 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
861 << " RTP packet: size=" << len
862 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
863 return;
864 }
865 } else {
866 res = srtp_filter_.UnprotectRtcp(data, len, &len);
867 if (!res) {
868 int type = -1;
869 GetRtcpType(data, len, &type);
870 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
871 << " RTCP packet: size=" << len << ", type=" << type;
872 return;
873 }
874 }
875
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000876 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800877 } else if (srtp_required_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 // Our session description indicates that SRTP is required, but we got a
879 // packet before our SRTP filter is active. This means either that
880 // a) we got SRTP packets before we received the SDES keys, in which case
881 // we can't decrypt it anyway, or
882 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
zhihuangb2cdd932017-01-19 16:54:25 -0800883 // transports, so we haven't yet extracted keys, even if DTLS did
884 // complete on the transport that the packets are being sent on. It's
885 // really good practice to wait for both RTP and RTCP to be good to go
886 // before sending media, to prevent weird failure modes, so it's fine
887 // for us to just eat packets here. This is all sidestepped if RTCP mux
888 // is used anyway.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
890 << " packet when SRTP is inactive and crypto is required";
891 return;
892 }
893
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200894 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700895 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200896 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
897}
898
899void BaseChannel::OnPacketReceived(bool rtcp,
900 const rtc::CopyOnWriteBuffer& packet,
901 const rtc::PacketTime& packet_time) {
902 RTC_DCHECK(worker_thread_->IsCurrent());
903 // Need to copy variable because OnRtcpReceived/OnPacketReceived
904 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
905 rtc::CopyOnWriteBuffer data(packet);
906 if (rtcp) {
907 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200909 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 }
911}
912
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000913bool BaseChannel::PushdownLocalDescription(
914 const SessionDescription* local_desc, ContentAction action,
915 std::string* error_desc) {
916 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 const MediaContentDescription* content_desc =
918 GetContentDescription(content_info);
919 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000920 !SetLocalContent(content_desc, action, error_desc)) {
921 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
922 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000924 return true;
925}
926
927bool BaseChannel::PushdownRemoteDescription(
928 const SessionDescription* remote_desc, ContentAction action,
929 std::string* error_desc) {
930 const ContentInfo* content_info = GetFirstContent(remote_desc);
931 const MediaContentDescription* content_desc =
932 GetContentDescription(content_info);
933 if (content_desc && content_info && !content_info->rejected &&
934 !SetRemoteContent(content_desc, action, error_desc)) {
935 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
936 return false;
937 }
938 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939}
940
941void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700942 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 if (enabled_)
944 return;
945
946 LOG(LS_INFO) << "Channel enabled";
947 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700948 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949}
950
951void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700952 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 if (!enabled_)
954 return;
955
956 LOG(LS_INFO) << "Channel disabled";
957 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700958 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959}
960
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200961void BaseChannel::UpdateWritableState_n() {
deadbeeff5346592017-01-24 21:51:21 -0800962 if (rtp_packet_transport_ && rtp_packet_transport_->writable() &&
963 (!rtcp_packet_transport_ || rtcp_packet_transport_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200964 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700965 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200966 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700967 }
968}
969
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200970void BaseChannel::ChannelWritable_n() {
971 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800972 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800974 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975
deadbeefcbecd352015-09-23 11:50:27 -0700976 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 << (was_ever_writable_ ? "" : " for the first time");
978
michaelt79e05882016-11-08 02:50:09 -0800979 if (selected_candidate_pair_)
980 LOG(LS_INFO)
981 << "Using "
982 << selected_candidate_pair_->local_candidate().ToSensitiveString()
983 << "->"
984 << selected_candidate_pair_->remote_candidate().ToSensitiveString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200987 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700989 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990}
991
deadbeef953c2ce2017-01-09 14:53:41 -0800992void BaseChannel::SignalDtlsSrtpSetupFailure_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200993 RTC_DCHECK(network_thread_->IsCurrent());
994 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700995 RTC_FROM_HERE, signaling_thread(),
deadbeef953c2ce2017-01-09 14:53:41 -0800996 Bind(&BaseChannel::SignalDtlsSrtpSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000997}
998
deadbeef953c2ce2017-01-09 14:53:41 -0800999void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001000 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
deadbeef953c2ce2017-01-09 14:53:41 -08001001 SignalDtlsSrtpSetupFailure(this, rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001002}
1003
zhihuangb2cdd932017-01-19 16:54:25 -08001004bool BaseChannel::SetDtlsSrtpCryptoSuites_n(DtlsTransportInternal* transport,
1005 bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001006 std::vector<int> crypto_suites;
1007 // We always use the default SRTP crypto suites for RTCP, but we may use
1008 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001010 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 } else {
jbauchcb560652016-08-04 05:20:32 -07001012 GetDefaultSrtpCryptoSuites(crypto_options(), &crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 }
zhihuangb2cdd932017-01-19 16:54:25 -08001014 return transport->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015}
1016
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001017bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
zhihuangb2cdd932017-01-19 16:54:25 -08001018 // Since DTLS is applied to all transports, checking RTP should be enough.
1019 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020}
1021
1022// This function returns true if either DTLS-SRTP is not in use
1023// *or* DTLS-SRTP is successfully set up.
zhihuangb2cdd932017-01-19 16:54:25 -08001024bool BaseChannel::SetupDtlsSrtp_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001025 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026 bool ret = false;
1027
zhihuangb2cdd932017-01-19 16:54:25 -08001028 DtlsTransportInternal* transport =
1029 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
deadbeeff5346592017-01-24 21:51:21 -08001030 RTC_DCHECK(transport);
zhihuangb2cdd932017-01-19 16:54:25 -08001031 RTC_DCHECK(transport->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001032
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001033 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034
zhihuangb2cdd932017-01-19 16:54:25 -08001035 if (!transport->GetSrtpCryptoSuite(&selected_crypto_suite)) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001036 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037 return false;
1038 }
1039
zhihuangb2cdd932017-01-19 16:54:25 -08001040 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " << content_name() << " "
1041 << PacketType(rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042
jbauchcb560652016-08-04 05:20:32 -07001043 int key_len;
1044 int salt_len;
1045 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
1046 &salt_len)) {
1047 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
1048 return false;
1049 }
1050
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -07001052 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053
1054 // RFC 5705 exporter using the RFC 5764 parameters
zhihuangb2cdd932017-01-19 16:54:25 -08001055 if (!transport->ExportKeyingMaterial(kDtlsSrtpExporterLabel, NULL, 0, false,
1056 &dtls_buffer[0], dtls_buffer.size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
nisseeb4ca4e2017-01-12 02:24:27 -08001058 RTC_NOTREACHED(); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 return false;
1060 }
1061
1062 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -07001063 std::vector<unsigned char> client_write_key(key_len + salt_len);
1064 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -07001066 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
1067 offset += key_len;
1068 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
1069 offset += key_len;
1070 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
1071 offset += salt_len;
1072 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073
1074 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001075 rtc::SSLRole role;
zhihuangb2cdd932017-01-19 16:54:25 -08001076 if (!transport->GetSslRole(&role)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001077 LOG(LS_WARNING) << "GetSslRole failed";
1078 return false;
1079 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001081 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 send_key = &server_write_key;
1083 recv_key = &client_write_key;
1084 } else {
1085 send_key = &client_write_key;
1086 recv_key = &server_write_key;
1087 }
1088
zhihuangb2cdd932017-01-19 16:54:25 -08001089 if (rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001090 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1091 static_cast<int>(send_key->size()),
1092 selected_crypto_suite, &(*recv_key)[0],
1093 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001095 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1096 static_cast<int>(send_key->size()),
1097 selected_crypto_suite, &(*recv_key)[0],
1098 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 }
1100
michaelt79e05882016-11-08 02:50:09 -08001101 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
michaelt79e05882016-11-08 02:50:09 -08001103 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 dtls_keyed_ = true;
michaelt79e05882016-11-08 02:50:09 -08001105 UpdateTransportOverhead();
1106 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001107 return ret;
1108}
1109
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001110void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001111 if (srtp_filter_.IsActive()) {
1112 return;
1113 }
1114
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001115 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001116 return;
1117 }
1118
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001119 if (!SetupDtlsSrtp_n(false)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001120 SignalDtlsSrtpSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001121 return;
1122 }
1123
zhihuangb2cdd932017-01-19 16:54:25 -08001124 if (rtcp_dtls_transport_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001125 if (!SetupDtlsSrtp_n(true)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001126 SignalDtlsSrtpSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001127 return;
1128 }
1129 }
1130}
1131
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001132void BaseChannel::ChannelNotWritable_n() {
1133 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 if (!writable_)
1135 return;
1136
deadbeefcbecd352015-09-23 11:50:27 -07001137 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001139 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140}
1141
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001142bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001143 const MediaContentDescription* content,
1144 ContentAction action,
1145 ContentSource src,
1146 std::string* error_desc) {
1147 if (action == CA_UPDATE) {
1148 // These parameters never get changed by a CA_UDPATE.
1149 return true;
1150 }
1151
deadbeef7af91dd2016-12-13 11:29:11 -08001152 // Cache srtp_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001153 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001154 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1155 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001156}
1157
1158bool BaseChannel::SetRtpTransportParameters_n(
1159 const MediaContentDescription* content,
1160 ContentAction action,
1161 ContentSource src,
1162 std::string* error_desc) {
1163 RTC_DCHECK(network_thread_->IsCurrent());
1164
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001165 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001166 return false;
1167 }
1168
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001169 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001170 return false;
1171 }
1172
1173 return true;
1174}
1175
zhihuangb2cdd932017-01-19 16:54:25 -08001176// |dtls| will be set to true if DTLS is active for transport and crypto is
1177// empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001178bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1179 bool* dtls,
1180 std::string* error_desc) {
deadbeeff5346592017-01-24 21:51:21 -08001181 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001182 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001183 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001184 return false;
1185 }
1186 return true;
1187}
1188
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001189bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001190 ContentAction action,
1191 ContentSource src,
1192 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001193 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001194 if (action == CA_UPDATE) {
1195 // no crypto params.
1196 return true;
1197 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001199 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001200 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001201 if (!ret) {
1202 return false;
1203 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 switch (action) {
1205 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001206 // If DTLS is already active on the channel, we could be renegotiating
1207 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001208 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001209 ret = srtp_filter_.SetOffer(cryptos, src);
1210 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 break;
1212 case CA_PRANSWER:
1213 // If we're doing DTLS-SRTP, we don't want to update the filter
1214 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001215 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1217 }
1218 break;
1219 case CA_ANSWER:
1220 // If we're doing DTLS-SRTP, we don't want to update the filter
1221 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001222 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 ret = srtp_filter_.SetAnswer(cryptos, src);
1224 }
1225 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 default:
1227 break;
1228 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001229 if (!ret) {
1230 SafeSetError("Failed to setup SRTP filter.", error_desc);
1231 return false;
1232 }
1233 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234}
1235
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001236bool BaseChannel::SetRtcpMux_n(bool enable,
1237 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001238 ContentSource src,
1239 std::string* error_desc) {
deadbeef8e814d72017-01-13 11:34:39 -08001240 // Provide a more specific error message for the RTCP mux "require" policy
1241 // case.
1242 if (rtcp_mux_required_ && !enable) {
1243 SafeSetError(
1244 "rtcpMuxPolicy is 'require', but media description does not "
1245 "contain 'a=rtcp-mux'.",
1246 error_desc);
1247 return false;
1248 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 bool ret = false;
1250 switch (action) {
1251 case CA_OFFER:
1252 ret = rtcp_mux_filter_.SetOffer(enable, src);
1253 break;
1254 case CA_PRANSWER:
zhihuangb2cdd932017-01-19 16:54:25 -08001255 // This may activate RTCP muxing, but we don't yet destroy the transport
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001256 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1258 break;
1259 case CA_ANSWER:
1260 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1261 if (ret && rtcp_mux_filter_.IsActive()) {
1262 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001263 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
zhihuangb2cdd932017-01-19 16:54:25 -08001264 << " by destroying RTCP transport for "
deadbeefcbecd352015-09-23 11:50:27 -07001265 << transport_name();
deadbeeff5346592017-01-24 21:51:21 -08001266 if (rtcp_packet_transport_) {
1267 SetTransport_n(true, nullptr, nullptr);
1268 SignalRtcpMuxFullyActive(transport_name_);
zhihuangf5b251b2017-01-12 19:37:48 -08001269 }
deadbeef062ce9f2016-08-26 21:42:15 -07001270 UpdateWritableState_n();
1271 SetTransportChannelReadyToSend(true, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 }
1273 break;
1274 case CA_UPDATE:
1275 // No RTCP mux info.
1276 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001277 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 default:
1279 break;
1280 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001281 if (!ret) {
1282 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1283 return false;
1284 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
zhihuangb2cdd932017-01-19 16:54:25 -08001286 // CA_ANSWER, but we only want to tear down the RTCP transport if we received
1287 // a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001288 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 // If the RTP transport is already writable, then so are we.
deadbeeff5346592017-01-24 21:51:21 -08001290 if (rtp_packet_transport_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001291 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 }
1293 }
1294
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001295 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296}
1297
1298bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001299 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001300 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301}
1302
Peter Boström0c4e06b2015-10-07 12:23:21 +02001303bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001304 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305 return media_channel()->RemoveRecvStream(ssrc);
1306}
1307
1308bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001309 ContentAction action,
1310 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1312 action == CA_PRANSWER || action == CA_UPDATE))
1313 return false;
1314
1315 // If this is an update, streams only contain streams that have changed.
1316 if (action == CA_UPDATE) {
1317 for (StreamParamsVec::const_iterator it = streams.begin();
1318 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001319 const StreamParams* existing_stream =
1320 GetStreamByIds(local_streams_, it->groupid, it->id);
1321 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 if (media_channel()->AddSendStream(*it)) {
1323 local_streams_.push_back(*it);
1324 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1325 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001326 std::ostringstream desc;
1327 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1328 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329 return false;
1330 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001331 } else if (existing_stream && !it->has_ssrcs()) {
1332 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001333 std::ostringstream desc;
1334 desc << "Failed to remove send stream with ssrc "
1335 << it->first_ssrc() << ".";
1336 SafeSetError(desc.str(), error_desc);
1337 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001339 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 } else {
1341 LOG(LS_WARNING) << "Ignore unsupported stream update";
1342 }
1343 }
1344 return true;
1345 }
1346 // Else streams are all the streams we want to send.
1347
1348 // Check for streams that have been removed.
1349 bool ret = true;
1350 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1351 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001352 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001354 std::ostringstream desc;
1355 desc << "Failed to remove send stream with ssrc "
1356 << it->first_ssrc() << ".";
1357 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 ret = false;
1359 }
1360 }
1361 }
1362 // Check for new streams.
1363 for (StreamParamsVec::const_iterator it = streams.begin();
1364 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001365 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001367 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001369 std::ostringstream desc;
1370 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1371 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372 ret = false;
1373 }
1374 }
1375 }
1376 local_streams_ = streams;
1377 return ret;
1378}
1379
1380bool BaseChannel::UpdateRemoteStreams_w(
1381 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001382 ContentAction action,
1383 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1385 action == CA_PRANSWER || action == CA_UPDATE))
1386 return false;
1387
1388 // If this is an update, streams only contain streams that have changed.
1389 if (action == CA_UPDATE) {
1390 for (StreamParamsVec::const_iterator it = streams.begin();
1391 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001392 const StreamParams* existing_stream =
1393 GetStreamByIds(remote_streams_, it->groupid, it->id);
1394 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395 if (AddRecvStream_w(*it)) {
1396 remote_streams_.push_back(*it);
1397 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1398 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001399 std::ostringstream desc;
1400 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1401 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 return false;
1403 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001404 } else if (existing_stream && !it->has_ssrcs()) {
1405 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001406 std::ostringstream desc;
1407 desc << "Failed to remove remote stream with ssrc "
1408 << it->first_ssrc() << ".";
1409 SafeSetError(desc.str(), error_desc);
1410 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001411 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001412 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413 } else {
1414 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001415 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 << " new stream = " << it->ToString();
1417 }
1418 }
1419 return true;
1420 }
1421 // Else streams are all the streams we want to receive.
1422
1423 // Check for streams that have been removed.
1424 bool ret = true;
1425 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1426 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001427 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001429 std::ostringstream desc;
1430 desc << "Failed to remove remote stream with ssrc "
1431 << it->first_ssrc() << ".";
1432 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001433 ret = false;
1434 }
1435 }
1436 }
1437 // Check for new streams.
1438 for (StreamParamsVec::const_iterator it = streams.begin();
1439 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001440 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 if (AddRecvStream_w(*it)) {
1442 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1443 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001444 std::ostringstream desc;
1445 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1446 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447 ret = false;
1448 }
1449 }
1450 }
1451 remote_streams_ = streams;
1452 return ret;
1453}
1454
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001455void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001456 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001457// Absolute Send Time extension id is used only with external auth,
1458// so do not bother searching for it and making asyncronious call to set
1459// something that is not used.
1460#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001461 const webrtc::RtpExtension* send_time_extension =
1462 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001463 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001464 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001465 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001466 RTC_FROM_HERE, network_thread_,
1467 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1468 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001469#endif
1470}
1471
1472void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1473 int rtp_abs_sendtime_extn_id) {
1474 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001475}
1476
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001477void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001478 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001480 case MSG_SEND_RTP_PACKET:
1481 case MSG_SEND_RTCP_PACKET: {
1482 RTC_DCHECK(network_thread_->IsCurrent());
1483 SendPacketMessageData* data =
1484 static_cast<SendPacketMessageData*>(pmsg->pdata);
1485 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1486 SendPacket(rtcp, &data->packet, data->options);
1487 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 break;
1489 }
1490 case MSG_FIRSTPACKETRECEIVED: {
1491 SignalFirstPacketReceived(this);
1492 break;
1493 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 }
1495}
1496
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001497void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498 // Flush all remaining RTCP messages. This should only be called in
1499 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001500 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001501 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001502 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1503 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001504 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1505 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 }
1507}
1508
johand89ab142016-10-25 10:50:32 -07001509void BaseChannel::SignalSentPacket_n(
1510 rtc::PacketTransportInterface* /* transport */,
1511 const rtc::SentPacket& sent_packet) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001512 RTC_DCHECK(network_thread_->IsCurrent());
1513 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001514 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001515 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1516}
1517
1518void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1519 RTC_DCHECK(worker_thread_->IsCurrent());
1520 SignalSentPacket(sent_packet);
1521}
1522
1523VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1524 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001525 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 MediaEngineInterface* media_engine,
1527 VoiceMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001529 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001530 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001531 : BaseChannel(worker_thread,
1532 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001533 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001534 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001535 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001536 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001537 srtp_required),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001538 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001539 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540
1541VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001542 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 StopAudioMonitor();
1544 StopMediaMonitor();
1545 // this can't be done in the base class, since it calls a virtual
1546 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001547 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548}
1549
Peter Boström0c4e06b2015-10-07 12:23:21 +02001550bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001551 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001552 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001553 AudioSource* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001554 return InvokeOnWorker(RTC_FROM_HERE,
1555 Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001556 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557}
1558
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559// TODO(juberti): Handle early media the right way. We should get an explicit
1560// ringing message telling us to start playing local ringback, which we cancel
1561// if any early media actually arrives. For now, we do the opposite, which is
1562// to wait 1 second for early media, and start playing local ringback if none
1563// arrives.
1564void VoiceChannel::SetEarlyMedia(bool enable) {
1565 if (enable) {
1566 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001567 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1568 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 } else {
1570 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001571 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572 }
1573}
1574
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575bool VoiceChannel::CanInsertDtmf() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001576 return InvokeOnWorker(
1577 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578}
1579
Peter Boström0c4e06b2015-10-07 12:23:21 +02001580bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1581 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001582 int duration) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001583 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this,
1584 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585}
1586
solenberg4bac9c52015-10-09 02:32:53 -07001587bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001588 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume,
1589 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001591
Tommif888bb52015-12-12 01:37:01 +01001592void VoiceChannel::SetRawAudioSink(
1593 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001594 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1595 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001596 // passing. So we invoke to our own little routine that gets a pointer to
1597 // our local variable. This is OK since we're synchronously invoking.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001598 InvokeOnWorker(RTC_FROM_HERE,
1599 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001600}
1601
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001602webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001603 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001604 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001605}
1606
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001607webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1608 uint32_t ssrc) const {
1609 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001610}
1611
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001612bool VoiceChannel::SetRtpSendParameters(
1613 uint32_t ssrc,
1614 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001615 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001616 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001617 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001618}
1619
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001620bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1621 webrtc::RtpParameters parameters) {
1622 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1623}
1624
1625webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1626 uint32_t ssrc) const {
1627 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001628 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001629 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1630}
1631
1632webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1633 uint32_t ssrc) const {
1634 return media_channel()->GetRtpReceiveParameters(ssrc);
1635}
1636
1637bool VoiceChannel::SetRtpReceiveParameters(
1638 uint32_t ssrc,
1639 const webrtc::RtpParameters& parameters) {
1640 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001641 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001642 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1643}
1644
1645bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1646 webrtc::RtpParameters parameters) {
1647 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001648}
1649
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001651 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1652 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653}
1654
1655void VoiceChannel::StartMediaMonitor(int cms) {
1656 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001657 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 media_monitor_->SignalUpdate.connect(
1659 this, &VoiceChannel::OnMediaMonitorUpdate);
1660 media_monitor_->Start(cms);
1661}
1662
1663void VoiceChannel::StopMediaMonitor() {
1664 if (media_monitor_) {
1665 media_monitor_->Stop();
1666 media_monitor_->SignalUpdate.disconnect(this);
1667 media_monitor_.reset();
1668 }
1669}
1670
1671void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001672 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 audio_monitor_
1674 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1675 audio_monitor_->Start(cms);
1676}
1677
1678void VoiceChannel::StopAudioMonitor() {
1679 if (audio_monitor_) {
1680 audio_monitor_->Stop();
1681 audio_monitor_.reset();
1682 }
1683}
1684
1685bool VoiceChannel::IsAudioMonitorRunning() const {
1686 return (audio_monitor_.get() != NULL);
1687}
1688
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001690 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691}
1692
1693int VoiceChannel::GetOutputLevel_w() {
1694 return media_channel()->GetOutputLevel();
1695}
1696
1697void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1698 media_channel()->GetActiveStreams(actives);
1699}
1700
johand89ab142016-10-25 10:50:32 -07001701void VoiceChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
1702 const char* data,
1703 size_t len,
1704 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001705 int flags) {
johand89ab142016-10-25 10:50:32 -07001706 BaseChannel::OnPacketRead(transport, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 // Set a flag when we've received an RTP packet. If we're waiting for early
1708 // media, this will disable the timeout.
johand89ab142016-10-25 10:50:32 -07001709 if (!received_media_ && !PacketIsRtcp(transport, data, len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 received_media_ = true;
1711 }
1712}
1713
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001714void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001715 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001716 invoker_.AsyncInvoke<void>(
1717 RTC_FROM_HERE, worker_thread_,
1718 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001719}
1720
michaelt79e05882016-11-08 02:50:09 -08001721int BaseChannel::GetTransportOverheadPerPacket() const {
1722 RTC_DCHECK(network_thread_->IsCurrent());
1723
1724 if (!selected_candidate_pair_)
1725 return 0;
1726
1727 int transport_overhead_per_packet = 0;
1728
1729 constexpr int kIpv4Overhaed = 20;
1730 constexpr int kIpv6Overhaed = 40;
1731 transport_overhead_per_packet +=
1732 selected_candidate_pair_->local_candidate().address().family() == AF_INET
1733 ? kIpv4Overhaed
1734 : kIpv6Overhaed;
1735
1736 constexpr int kUdpOverhaed = 8;
1737 constexpr int kTcpOverhaed = 20;
1738 transport_overhead_per_packet +=
1739 selected_candidate_pair_->local_candidate().protocol() ==
1740 TCP_PROTOCOL_NAME
1741 ? kTcpOverhaed
1742 : kUdpOverhaed;
1743
1744 if (secure()) {
1745 int srtp_overhead = 0;
1746 if (srtp_filter_.GetSrtpOverhead(&srtp_overhead))
1747 transport_overhead_per_packet += srtp_overhead;
1748 }
1749
1750 return transport_overhead_per_packet;
1751}
1752
1753void BaseChannel::UpdateTransportOverhead() {
1754 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1755 if (transport_overhead_per_packet)
1756 invoker_.AsyncInvoke<void>(
1757 RTC_FROM_HERE, worker_thread_,
1758 Bind(&MediaChannel::OnTransportOverheadChanged, media_channel_,
1759 transport_overhead_per_packet));
1760}
1761
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001762void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 // Render incoming data if we're the active call, and we have the local
1764 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001765 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001766 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767
1768 // Send outgoing data if we're the active call, we have the remote content,
1769 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001770 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001771 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772
1773 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1774}
1775
1776const ContentInfo* VoiceChannel::GetFirstContent(
1777 const SessionDescription* sdesc) {
1778 return GetFirstAudioContent(sdesc);
1779}
1780
1781bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001782 ContentAction action,
1783 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001784 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001785 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786 LOG(LS_INFO) << "Setting local voice description";
1787
1788 const AudioContentDescription* audio =
1789 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001790 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001791 if (!audio) {
1792 SafeSetError("Can't find audio content in local description.", error_desc);
1793 return false;
1794 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001796 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001797 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 }
1799
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001800 AudioRecvParameters recv_params = last_recv_params_;
1801 RtpParametersFromMediaDescription(audio, &recv_params);
1802 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001803 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001804 error_desc);
1805 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001807 for (const AudioCodec& codec : audio->codecs()) {
1808 bundle_filter()->AddPayloadType(codec.id);
1809 }
1810 last_recv_params_ = recv_params;
1811
1812 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1813 // only give it to the media channel once we have a remote
1814 // description too (without a remote description, we won't be able
1815 // to send them anyway).
1816 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1817 SafeSetError("Failed to set local audio description streams.", error_desc);
1818 return false;
1819 }
1820
1821 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001822 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001823 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824}
1825
1826bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001827 ContentAction action,
1828 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001829 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001830 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 LOG(LS_INFO) << "Setting remote voice description";
1832
1833 const AudioContentDescription* audio =
1834 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001835 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001836 if (!audio) {
1837 SafeSetError("Can't find audio content in remote description.", error_desc);
1838 return false;
1839 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001841 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001842 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 }
1844
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001845 AudioSendParameters send_params = last_send_params_;
1846 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001847 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001848 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001849 }
skvladdc1c62c2016-03-16 19:07:43 -07001850
1851 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1852 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001853 SafeSetError("Failed to set remote audio description send parameters.",
1854 error_desc);
1855 return false;
1856 }
1857 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001859 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1860 // and only give it to the media channel once we have a local
1861 // description too (without a local description, we won't be able to
1862 // recv them anyway).
1863 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1864 SafeSetError("Failed to set remote audio description streams.", error_desc);
1865 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 }
1867
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001868 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001869 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001870 }
1871
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001872 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001873 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001874 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875}
1876
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877void VoiceChannel::HandleEarlyMediaTimeout() {
1878 // This occurs on the main thread, not the worker thread.
1879 if (!received_media_) {
1880 LOG(LS_INFO) << "No early media received before timeout";
1881 SignalEarlyMediaTimeout(this);
1882 }
1883}
1884
Peter Boström0c4e06b2015-10-07 12:23:21 +02001885bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1886 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001887 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888 if (!enabled()) {
1889 return false;
1890 }
solenberg1d63dd02015-12-02 12:35:09 -08001891 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892}
1893
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001894void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896 case MSG_EARLYMEDIATIMEOUT:
1897 HandleEarlyMediaTimeout();
1898 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 case MSG_CHANNEL_ERROR: {
1900 VoiceChannelErrorMessageData* data =
1901 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 delete data;
1903 break;
1904 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001905 default:
1906 BaseChannel::OnMessage(pmsg);
1907 break;
1908 }
1909}
1910
1911void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001912 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 SignalConnectionMonitor(this, infos);
1914}
1915
1916void VoiceChannel::OnMediaMonitorUpdate(
1917 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001918 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001919 SignalMediaMonitor(this, info);
1920}
1921
1922void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1923 const AudioInfo& info) {
1924 SignalAudioMonitor(this, info);
1925}
1926
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001927void VoiceChannel::GetSrtpCryptoSuites_n(
1928 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07001929 GetSupportedAudioCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930}
1931
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001932VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1933 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001934 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935 VideoMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001937 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001938 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001939 : BaseChannel(worker_thread,
1940 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001941 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001942 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001943 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001944 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001945 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001948 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949 StopMediaMonitor();
1950 // this can't be done in the base class, since it calls a virtual
1951 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001952
1953 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954}
1955
nisse08582ff2016-02-04 01:24:52 -08001956bool VideoChannel::SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -08001957 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
nisse08582ff2016-02-04 01:24:52 -08001958 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001959 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001960 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 return true;
1962}
1963
deadbeef5a4a75a2016-06-02 16:23:38 -07001964bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001965 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001966 bool mute,
1967 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -08001968 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001969 return InvokeOnWorker(RTC_FROM_HERE,
1970 Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
deadbeef5a4a75a2016-06-02 16:23:38 -07001971 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001972}
1973
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001974webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001975 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001976 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001977}
1978
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001979webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1980 uint32_t ssrc) const {
1981 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001982}
1983
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001984bool VideoChannel::SetRtpSendParameters(
1985 uint32_t ssrc,
1986 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001987 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001988 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001989 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001990}
1991
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001992bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1993 webrtc::RtpParameters parameters) {
1994 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1995}
1996
1997webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1998 uint32_t ssrc) const {
1999 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002000 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002001 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
2002}
2003
2004webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
2005 uint32_t ssrc) const {
2006 return media_channel()->GetRtpReceiveParameters(ssrc);
2007}
2008
2009bool VideoChannel::SetRtpReceiveParameters(
2010 uint32_t ssrc,
2011 const webrtc::RtpParameters& parameters) {
2012 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002013 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002014 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
2015}
2016
2017bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
2018 webrtc::RtpParameters parameters) {
2019 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07002020}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002021
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002022void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 // Send outgoing data if we're the active call, we have the remote content,
2024 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002025 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026 if (!media_channel()->SetSend(send)) {
2027 LOG(LS_ERROR) << "Failed to SetSend on video channel";
2028 // TODO(gangji): Report error back to server.
2029 }
2030
Peter Boström34fbfff2015-09-24 19:20:30 +02002031 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032}
2033
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00002034bool VideoChannel::GetStats(VideoMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002035 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
2036 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037}
2038
2039void VideoChannel::StartMediaMonitor(int cms) {
2040 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002041 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 media_monitor_->SignalUpdate.connect(
2043 this, &VideoChannel::OnMediaMonitorUpdate);
2044 media_monitor_->Start(cms);
2045}
2046
2047void VideoChannel::StopMediaMonitor() {
2048 if (media_monitor_) {
2049 media_monitor_->Stop();
2050 media_monitor_.reset();
2051 }
2052}
2053
2054const ContentInfo* VideoChannel::GetFirstContent(
2055 const SessionDescription* sdesc) {
2056 return GetFirstVideoContent(sdesc);
2057}
2058
2059bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002060 ContentAction action,
2061 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002062 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002063 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 LOG(LS_INFO) << "Setting local video description";
2065
2066 const VideoContentDescription* video =
2067 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002068 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002069 if (!video) {
2070 SafeSetError("Can't find video content in local description.", error_desc);
2071 return false;
2072 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002074 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002075 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076 }
2077
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002078 VideoRecvParameters recv_params = last_recv_params_;
2079 RtpParametersFromMediaDescription(video, &recv_params);
2080 if (!media_channel()->SetRecvParameters(recv_params)) {
2081 SafeSetError("Failed to set local video description recv parameters.",
2082 error_desc);
2083 return false;
2084 }
2085 for (const VideoCodec& codec : video->codecs()) {
2086 bundle_filter()->AddPayloadType(codec.id);
2087 }
2088 last_recv_params_ = recv_params;
2089
2090 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2091 // only give it to the media channel once we have a remote
2092 // description too (without a remote description, we won't be able
2093 // to send them anyway).
2094 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2095 SafeSetError("Failed to set local video description streams.", error_desc);
2096 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 }
2098
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002099 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002100 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002101 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102}
2103
2104bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002105 ContentAction action,
2106 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002107 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002108 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 LOG(LS_INFO) << "Setting remote video description";
2110
2111 const VideoContentDescription* video =
2112 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002113 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002114 if (!video) {
2115 SafeSetError("Can't find video content in remote description.", error_desc);
2116 return false;
2117 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002119 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002120 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121 }
2122
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002123 VideoSendParameters send_params = last_send_params_;
2124 RtpSendParametersFromMediaDescription(video, &send_params);
2125 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002126 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002127 }
skvladdc1c62c2016-03-16 19:07:43 -07002128
2129 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2130
2131 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002132 SafeSetError("Failed to set remote video description send parameters.",
2133 error_desc);
2134 return false;
2135 }
2136 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002138 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2139 // and only give it to the media channel once we have a local
2140 // description too (without a local description, we won't be able to
2141 // recv them anyway).
2142 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2143 SafeSetError("Failed to set remote video description streams.", error_desc);
2144 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145 }
2146
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002147 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002148 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002150
2151 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002152 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002153 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154}
2155
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002156void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002158 case MSG_CHANNEL_ERROR: {
2159 const VideoChannelErrorMessageData* data =
2160 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161 delete data;
2162 break;
2163 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 default:
2165 BaseChannel::OnMessage(pmsg);
2166 break;
2167 }
2168}
2169
2170void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002171 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172 SignalConnectionMonitor(this, infos);
2173}
2174
2175// TODO(pthatcher): Look into removing duplicate code between
2176// audio, video, and data, perhaps by using templates.
2177void VideoChannel::OnMediaMonitorUpdate(
2178 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002179 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180 SignalMediaMonitor(this, info);
2181}
2182
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002183void VideoChannel::GetSrtpCryptoSuites_n(
2184 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002185 GetSupportedVideoCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002186}
2187
deadbeef953c2ce2017-01-09 14:53:41 -08002188RtpDataChannel::RtpDataChannel(rtc::Thread* worker_thread,
2189 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002190 rtc::Thread* signaling_thread,
deadbeef953c2ce2017-01-09 14:53:41 -08002191 DataMediaChannel* media_channel,
deadbeef953c2ce2017-01-09 14:53:41 -08002192 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08002193 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002194 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002195 : BaseChannel(worker_thread,
2196 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002197 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002198 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002199 content_name,
deadbeefac22f702017-01-12 21:59:29 -08002200 rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002201 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202
deadbeef953c2ce2017-01-09 14:53:41 -08002203RtpDataChannel::~RtpDataChannel() {
2204 TRACE_EVENT0("webrtc", "RtpDataChannel::~RtpDataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205 StopMediaMonitor();
2206 // this can't be done in the base class, since it calls a virtual
2207 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002208
2209 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210}
2211
deadbeeff5346592017-01-24 21:51:21 -08002212bool RtpDataChannel::Init_w(
2213 DtlsTransportInternal* rtp_dtls_transport,
2214 DtlsTransportInternal* rtcp_dtls_transport,
2215 rtc::PacketTransportInterface* rtp_packet_transport,
2216 rtc::PacketTransportInterface* rtcp_packet_transport) {
2217 if (!BaseChannel::Init_w(rtp_dtls_transport, rtcp_dtls_transport,
2218 rtp_packet_transport, rtcp_packet_transport)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002219 return false;
2220 }
deadbeef953c2ce2017-01-09 14:53:41 -08002221 media_channel()->SignalDataReceived.connect(this,
2222 &RtpDataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002223 media_channel()->SignalReadyToSend.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08002224 this, &RtpDataChannel::OnDataChannelReadyToSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 return true;
2226}
2227
deadbeef953c2ce2017-01-09 14:53:41 -08002228bool RtpDataChannel::SendData(const SendDataParams& params,
2229 const rtc::CopyOnWriteBuffer& payload,
2230 SendDataResult* result) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002231 return InvokeOnWorker(
2232 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2233 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234}
2235
deadbeef953c2ce2017-01-09 14:53:41 -08002236const ContentInfo* RtpDataChannel::GetFirstContent(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237 const SessionDescription* sdesc) {
2238 return GetFirstDataContent(sdesc);
2239}
2240
deadbeef953c2ce2017-01-09 14:53:41 -08002241bool RtpDataChannel::CheckDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002242 const DataContentDescription* content,
2243 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002244 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2245 (content->protocol() == kMediaProtocolDtlsSctp));
deadbeef953c2ce2017-01-09 14:53:41 -08002246 // It's been set before, but doesn't match. That's bad.
2247 if (is_sctp) {
2248 SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.",
2249 error_desc);
2250 return false;
2251 }
2252 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253}
2254
deadbeef953c2ce2017-01-09 14:53:41 -08002255bool RtpDataChannel::SetLocalContent_w(const MediaContentDescription* content,
2256 ContentAction action,
2257 std::string* error_desc) {
2258 TRACE_EVENT0("webrtc", "RtpDataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002259 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002260 LOG(LS_INFO) << "Setting local data description";
2261
2262 const DataContentDescription* data =
2263 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002264 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002265 if (!data) {
2266 SafeSetError("Can't find data content in local description.", error_desc);
2267 return false;
2268 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002269
deadbeef953c2ce2017-01-09 14:53:41 -08002270 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271 return false;
2272 }
2273
deadbeef953c2ce2017-01-09 14:53:41 -08002274 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
2275 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 }
2277
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002278 DataRecvParameters recv_params = last_recv_params_;
2279 RtpParametersFromMediaDescription(data, &recv_params);
2280 if (!media_channel()->SetRecvParameters(recv_params)) {
2281 SafeSetError("Failed to set remote data description recv parameters.",
2282 error_desc);
2283 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284 }
deadbeef953c2ce2017-01-09 14:53:41 -08002285 for (const DataCodec& codec : data->codecs()) {
2286 bundle_filter()->AddPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002287 }
2288 last_recv_params_ = recv_params;
2289
2290 // TODO(pthatcher): Move local streams into DataSendParameters, and
2291 // only give it to the media channel once we have a remote
2292 // description too (without a remote description, we won't be able
2293 // to send them anyway).
2294 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2295 SafeSetError("Failed to set local data description streams.", error_desc);
2296 return false;
2297 }
2298
2299 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002300 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002301 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002302}
2303
deadbeef953c2ce2017-01-09 14:53:41 -08002304bool RtpDataChannel::SetRemoteContent_w(const MediaContentDescription* content,
2305 ContentAction action,
2306 std::string* error_desc) {
2307 TRACE_EVENT0("webrtc", "RtpDataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002308 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309
2310 const DataContentDescription* data =
2311 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002312 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002313 if (!data) {
2314 SafeSetError("Can't find data content in remote description.", error_desc);
2315 return false;
2316 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002318 // If the remote data doesn't have codecs and isn't an update, it
2319 // must be empty, so ignore it.
2320 if (!data->has_codecs() && action != CA_UPDATE) {
2321 return true;
2322 }
2323
deadbeef953c2ce2017-01-09 14:53:41 -08002324 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325 return false;
2326 }
2327
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002328 LOG(LS_INFO) << "Setting remote data description";
deadbeef953c2ce2017-01-09 14:53:41 -08002329 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002330 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002331 }
2332
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002333 DataSendParameters send_params = last_send_params_;
2334 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2335 if (!media_channel()->SetSendParameters(send_params)) {
2336 SafeSetError("Failed to set remote data description send parameters.",
2337 error_desc);
2338 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002340 last_send_params_ = send_params;
2341
2342 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2343 // and only give it to the media channel once we have a local
2344 // description too (without a local description, we won't be able to
2345 // recv them anyway).
2346 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2347 SafeSetError("Failed to set remote data description streams.",
2348 error_desc);
2349 return false;
2350 }
2351
2352 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002353 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002354 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355}
2356
deadbeef953c2ce2017-01-09 14:53:41 -08002357void RtpDataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002358 // Render incoming data if we're the active call, and we have the local
2359 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002360 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002361 if (!media_channel()->SetReceive(recv)) {
2362 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2363 }
2364
2365 // Send outgoing data if we're the active call, we have the remote content,
2366 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002367 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002368 if (!media_channel()->SetSend(send)) {
2369 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2370 }
2371
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002372 // Trigger SignalReadyToSendData asynchronously.
2373 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002374
2375 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2376}
2377
deadbeef953c2ce2017-01-09 14:53:41 -08002378void RtpDataChannel::OnMessage(rtc::Message* pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002379 switch (pmsg->message_id) {
2380 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002381 DataChannelReadyToSendMessageData* data =
2382 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002383 ready_to_send_data_ = data->data();
2384 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002385 delete data;
2386 break;
2387 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002388 case MSG_DATARECEIVED: {
2389 DataReceivedMessageData* data =
2390 static_cast<DataReceivedMessageData*>(pmsg->pdata);
deadbeef953c2ce2017-01-09 14:53:41 -08002391 SignalDataReceived(data->params, data->payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002392 delete data;
2393 break;
2394 }
2395 case MSG_CHANNEL_ERROR: {
2396 const DataChannelErrorMessageData* data =
2397 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002398 delete data;
2399 break;
2400 }
2401 default:
2402 BaseChannel::OnMessage(pmsg);
2403 break;
2404 }
2405}
2406
deadbeef953c2ce2017-01-09 14:53:41 -08002407void RtpDataChannel::OnConnectionMonitorUpdate(
2408 ConnectionMonitor* monitor,
2409 const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 SignalConnectionMonitor(this, infos);
2411}
2412
deadbeef953c2ce2017-01-09 14:53:41 -08002413void RtpDataChannel::StartMediaMonitor(int cms) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002414 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002415 rtc::Thread::Current()));
deadbeef953c2ce2017-01-09 14:53:41 -08002416 media_monitor_->SignalUpdate.connect(this,
2417 &RtpDataChannel::OnMediaMonitorUpdate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418 media_monitor_->Start(cms);
2419}
2420
deadbeef953c2ce2017-01-09 14:53:41 -08002421void RtpDataChannel::StopMediaMonitor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002422 if (media_monitor_) {
2423 media_monitor_->Stop();
2424 media_monitor_->SignalUpdate.disconnect(this);
2425 media_monitor_.reset();
2426 }
2427}
2428
deadbeef953c2ce2017-01-09 14:53:41 -08002429void RtpDataChannel::OnMediaMonitorUpdate(DataMediaChannel* media_channel,
2430 const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002431 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002432 SignalMediaMonitor(this, info);
2433}
2434
deadbeef953c2ce2017-01-09 14:53:41 -08002435void RtpDataChannel::OnDataReceived(const ReceiveDataParams& params,
2436 const char* data,
2437 size_t len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002438 DataReceivedMessageData* msg = new DataReceivedMessageData(
2439 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002440 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002441}
2442
deadbeef953c2ce2017-01-09 14:53:41 -08002443void RtpDataChannel::OnDataChannelError(uint32_t ssrc,
2444 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002445 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2446 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002447 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002448}
2449
deadbeef953c2ce2017-01-09 14:53:41 -08002450void RtpDataChannel::OnDataChannelReadyToSend(bool writable) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002451 // This is usded for congestion control to indicate that the stream is ready
2452 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2453 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002454 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002455 new DataChannelReadyToSendMessageData(writable));
2456}
2457
deadbeef953c2ce2017-01-09 14:53:41 -08002458void RtpDataChannel::GetSrtpCryptoSuites_n(
2459 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002460 GetSupportedDataCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002461}
2462
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002463} // namespace cricket