blob: 65b05857285005a0852f43a7c7dbc18de27635fd [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwiberg0eb15ed2015-12-17 03:04:15 -080011#include <utility>
12
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010013#include "webrtc/pc/channel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
kjellandera69d9732016-08-31 07:33:05 -070015#include "webrtc/api/call/audio_sink.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000016#include "webrtc/base/bind.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000017#include "webrtc/base/byteorder.h"
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -070018#include "webrtc/base/checks.h"
jbaucheec21bd2016-03-20 06:15:43 -070019#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000020#include "webrtc/base/dscp.h"
21#include "webrtc/base/logging.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070022#include "webrtc/base/networkroute.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010023#include "webrtc/base/trace_event.h"
kjellanderf4752772016-03-02 05:42:30 -080024#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080025#include "webrtc/media/base/rtputils.h"
johand89ab142016-10-25 10:50:32 -070026#include "webrtc/p2p/base/packettransportinterface.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010027#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
29namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000030using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000031
deadbeef2d110be2016-01-13 12:00:26 -080032namespace {
kwiberg31022942016-03-11 14:18:21 -080033// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080034bool SetRawAudioSink_w(VoiceMediaChannel* channel,
35 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080036 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
37 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080038 return true;
39}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020040
41struct SendPacketMessageData : public rtc::MessageData {
42 rtc::CopyOnWriteBuffer packet;
43 rtc::PacketOptions options;
44};
45
isheriff6f8d6862016-05-26 11:24:55 -070046#if defined(ENABLE_EXTERNAL_AUTH)
47// Returns the named header extension if found among all extensions,
48// nullptr otherwise.
49const webrtc::RtpExtension* FindHeaderExtension(
50 const std::vector<webrtc::RtpExtension>& extensions,
51 const std::string& uri) {
52 for (const auto& extension : extensions) {
53 if (extension.uri == uri)
54 return &extension;
55 }
56 return nullptr;
57}
58#endif
59
deadbeef2d110be2016-01-13 12:00:26 -080060} // namespace
61
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000063 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020064 MSG_SEND_RTP_PACKET,
65 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 MSG_FIRSTPACKETRECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070};
71
72// Value specified in RFC 5764.
73static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
74
75static const int kAgcMinus10db = -10;
76
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000077static void SafeSetError(const std::string& message, std::string* error_desc) {
78 if (error_desc) {
79 *error_desc = message;
80 }
81}
82
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000083struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020084 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020086 : ssrc(in_ssrc), error(in_error) {}
87 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 VoiceMediaChannel::Error error;
89};
90
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020092 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020094 : ssrc(in_ssrc), error(in_error) {}
95 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 VideoMediaChannel::Error error;
97};
98
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000099struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 : ssrc(in_ssrc), error(in_error) {}
103 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 DataMediaChannel::Error error;
105};
106
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107static const char* PacketType(bool rtcp) {
108 return (!rtcp) ? "RTP" : "RTCP";
109}
110
jbaucheec21bd2016-03-20 06:15:43 -0700111static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 // Check the packet size. We could check the header too if needed.
113 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000114 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
115 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116}
117
118static bool IsReceiveContentDirection(MediaContentDirection direction) {
119 return direction == MD_SENDRECV || direction == MD_RECVONLY;
120}
121
122static bool IsSendContentDirection(MediaContentDirection direction) {
123 return direction == MD_SENDRECV || direction == MD_SENDONLY;
124}
125
126static const MediaContentDescription* GetContentDescription(
127 const ContentInfo* cinfo) {
128 if (cinfo == NULL)
129 return NULL;
130 return static_cast<const MediaContentDescription*>(cinfo->description);
131}
132
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700133template <class Codec>
134void RtpParametersFromMediaDescription(
135 const MediaContentDescriptionImpl<Codec>* desc,
136 RtpParameters<Codec>* params) {
137 // TODO(pthatcher): Remove this once we're sure no one will give us
138 // a description without codecs (currently a CA_UPDATE with just
139 // streams can).
140 if (desc->has_codecs()) {
141 params->codecs = desc->codecs();
142 }
143 // TODO(pthatcher): See if we really need
144 // rtp_header_extensions_set() and remove it if we don't.
145 if (desc->rtp_header_extensions_set()) {
146 params->extensions = desc->rtp_header_extensions();
147 }
deadbeef13871492015-12-09 12:37:51 -0800148 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700149}
150
nisse05103312016-03-16 02:22:50 -0700151template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700152void RtpSendParametersFromMediaDescription(
153 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700154 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700155 RtpParametersFromMediaDescription(desc, send_params);
156 send_params->max_bandwidth_bps = desc->bandwidth();
157}
158
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200159BaseChannel::BaseChannel(rtc::Thread* worker_thread,
160 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800161 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700162 MediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700163 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800164 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800165 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200166 : worker_thread_(worker_thread),
167 network_thread_(network_thread),
zhihuangf5b251b2017-01-12 19:37:48 -0800168 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 content_name_(content_name),
deadbeefac22f702017-01-12 21:59:29 -0800170 rtcp_mux_required_(rtcp_mux_required),
deadbeef7af91dd2016-12-13 11:29:11 -0800171 srtp_required_(srtp_required),
michaelt79e05882016-11-08 02:50:09 -0800172 media_channel_(media_channel),
173 selected_candidate_pair_(nullptr) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700174 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 LOG(LS_INFO) << "Created channel for " << content_name;
176}
177
178BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800179 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700180 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000181 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200183 // Eats any outstanding messages or packets.
184 worker_thread_->Clear(&invoker_);
185 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 // We must destroy the media channel before the transport channel, otherwise
187 // the media channel may try to send on the dead transport channel. NULLing
188 // is not an effective strategy since the sends will come on another thread.
189 delete media_channel_;
zhihuangf5b251b2017-01-12 19:37:48 -0800190 LOG(LS_INFO) << "Destroyed channel: " << content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200191}
192
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200193void BaseChannel::DisconnectTransportChannels_n() {
194 // Send any outstanding RTCP packets.
195 FlushRtcpMessages_n();
196
197 // Stop signals from transport channels, but keep them alive because
198 // media_channel may use them from a different thread.
zhihuangb2cdd932017-01-19 16:54:25 -0800199 if (rtp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800200 DisconnectFromDtlsTransport(rtp_dtls_transport_);
201 } else if (rtp_packet_transport_) {
202 DisconnectFromPacketTransport(rtp_packet_transport_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200203 }
zhihuangb2cdd932017-01-19 16:54:25 -0800204 if (rtcp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800205 DisconnectFromDtlsTransport(rtcp_dtls_transport_);
206 } else if (rtcp_packet_transport_) {
207 DisconnectFromPacketTransport(rtcp_packet_transport_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200208 }
209
210 // Clear pending read packets/messages.
211 network_thread_->Clear(&invoker_);
212 network_thread_->Clear(this);
213}
214
zhihuangb2cdd932017-01-19 16:54:25 -0800215bool BaseChannel::Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800216 DtlsTransportInternal* rtcp_dtls_transport,
217 rtc::PacketTransportInterface* rtp_packet_transport,
218 rtc::PacketTransportInterface* rtcp_packet_transport) {
skvlad6c87a672016-05-17 17:49:52 -0700219 if (!network_thread_->Invoke<bool>(
zhihuangb2cdd932017-01-19 16:54:25 -0800220 RTC_FROM_HERE, Bind(&BaseChannel::InitNetwork_n, this,
deadbeeff5346592017-01-24 21:51:21 -0800221 rtp_dtls_transport, rtcp_dtls_transport,
222 rtp_packet_transport, rtcp_packet_transport))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000223 return false;
224 }
deadbeeff5346592017-01-24 21:51:21 -0800225 // Both RTP and RTCP channels should be set, we can call SetInterface on
226 // the media channel and it can set network options.
227 RTC_DCHECK_RUN_ON(worker_thread_);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000228 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 return true;
230}
231
deadbeeff5346592017-01-24 21:51:21 -0800232bool BaseChannel::InitNetwork_n(
233 DtlsTransportInternal* rtp_dtls_transport,
234 DtlsTransportInternal* rtcp_dtls_transport,
235 rtc::PacketTransportInterface* rtp_packet_transport,
236 rtc::PacketTransportInterface* rtcp_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200237 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800238 SetTransports_n(rtp_dtls_transport, rtcp_dtls_transport, rtp_packet_transport,
239 rtcp_packet_transport);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200240
deadbeeff5346592017-01-24 21:51:21 -0800241 if (rtp_dtls_transport_ &&
242 !SetDtlsSrtpCryptoSuites_n(rtp_dtls_transport_, false)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200243 return false;
244 }
zhihuangb2cdd932017-01-19 16:54:25 -0800245 if (rtcp_dtls_transport_ &&
246 !SetDtlsSrtpCryptoSuites_n(rtcp_dtls_transport_, true)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200247 return false;
248 }
deadbeefac22f702017-01-12 21:59:29 -0800249 if (rtcp_mux_required_) {
250 rtcp_mux_filter_.SetActive();
251 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200252 return true;
253}
254
wu@webrtc.org78187522013-10-07 23:32:02 +0000255void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200256 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000257 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200258 // Packets arrive on the network thread, processing packets calls virtual
259 // functions, so need to stop this process in Deinit that is called in
260 // derived classes destructor.
261 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700262 RTC_FROM_HERE, Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000263}
264
zhihuangb2cdd932017-01-19 16:54:25 -0800265void BaseChannel::SetTransports(DtlsTransportInternal* rtp_dtls_transport,
266 DtlsTransportInternal* rtcp_dtls_transport) {
deadbeeff5346592017-01-24 21:51:21 -0800267 network_thread_->Invoke<void>(
268 RTC_FROM_HERE,
269 Bind(&BaseChannel::SetTransports_n, this, rtp_dtls_transport,
270 rtcp_dtls_transport, rtp_dtls_transport, rtcp_dtls_transport));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000271}
272
deadbeeff5346592017-01-24 21:51:21 -0800273void BaseChannel::SetTransports(
274 rtc::PacketTransportInterface* rtp_packet_transport,
275 rtc::PacketTransportInterface* rtcp_packet_transport) {
276 network_thread_->Invoke<void>(
277 RTC_FROM_HERE, Bind(&BaseChannel::SetTransports_n, this, nullptr, nullptr,
278 rtp_packet_transport, rtcp_packet_transport));
279}
zhihuangf5b251b2017-01-12 19:37:48 -0800280
deadbeeff5346592017-01-24 21:51:21 -0800281void BaseChannel::SetTransports_n(
282 DtlsTransportInternal* rtp_dtls_transport,
283 DtlsTransportInternal* rtcp_dtls_transport,
284 rtc::PacketTransportInterface* rtp_packet_transport,
285 rtc::PacketTransportInterface* rtcp_packet_transport) {
286 RTC_DCHECK(network_thread_->IsCurrent());
287 // Validate some assertions about the input.
288 RTC_DCHECK(rtp_packet_transport);
289 RTC_DCHECK_EQ(NeedsRtcpTransport(), rtcp_packet_transport != nullptr);
290 if (rtp_dtls_transport || rtcp_dtls_transport) {
291 // DTLS/non-DTLS pointers should be to the same object.
292 RTC_DCHECK(rtp_dtls_transport == rtp_packet_transport);
293 RTC_DCHECK(rtcp_dtls_transport == rtcp_packet_transport);
294 // Can't go from non-DTLS to DTLS.
295 RTC_DCHECK(!rtp_packet_transport_ || rtp_dtls_transport_);
296 } else {
297 // Can't go from DTLS to non-DTLS.
298 RTC_DCHECK(!rtp_dtls_transport_);
299 }
300 // Transport names should be the same.
zhihuangb2cdd932017-01-19 16:54:25 -0800301 if (rtp_dtls_transport && rtcp_dtls_transport) {
302 RTC_DCHECK(rtp_dtls_transport->transport_name() ==
303 rtcp_dtls_transport->transport_name());
zhihuangb2cdd932017-01-19 16:54:25 -0800304 }
deadbeeff5346592017-01-24 21:51:21 -0800305 std::string debug_name;
306 if (rtp_dtls_transport) {
307 transport_name_ = rtp_dtls_transport->transport_name();
308 debug_name = transport_name_;
309 } else {
310 debug_name = rtp_packet_transport->debug_name();
311 }
312 if (rtp_packet_transport == rtp_packet_transport_) {
313 // Nothing to do if transport isn't changing.
deadbeefbad5dad2017-01-17 18:32:35 -0800314 return;
deadbeefcbecd352015-09-23 11:50:27 -0700315 }
316
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800317 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
318 // changes and wait until the DTLS handshake is complete to set the newly
319 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200320 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800321 // Set |writable_| to false such that UpdateWritableState_w can set up
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700322 // DTLS-SRTP when |writable_| becomes true again.
guoweis46383312015-12-17 16:45:59 -0800323 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800324 srtp_filter_.ResetParams();
325 }
326
deadbeefac22f702017-01-12 21:59:29 -0800327 // If this BaseChannel doesn't require RTCP mux and we haven't fully
328 // negotiated RTCP mux, we need an RTCP transport.
deadbeeff5346592017-01-24 21:51:21 -0800329 if (rtcp_packet_transport) {
zhihuangf5b251b2017-01-12 19:37:48 -0800330 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on "
deadbeeff5346592017-01-24 21:51:21 -0800331 << debug_name << " transport " << rtcp_packet_transport;
332 SetTransport_n(true, rtcp_dtls_transport, rtcp_packet_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000333 }
334
deadbeeff5346592017-01-24 21:51:21 -0800335 LOG(LS_INFO) << "Setting RTP Transport for " << content_name() << " on "
336 << debug_name << " transport " << rtp_packet_transport;
337 SetTransport_n(false, rtp_dtls_transport, rtp_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800338
deadbeefcbecd352015-09-23 11:50:27 -0700339 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700340 // setting new transport channels.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200341 UpdateWritableState_n();
deadbeef062ce9f2016-08-26 21:42:15 -0700342 // We can only update ready-to-send after updating writability.
343 //
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700344 // On setting a new channel, assume it's ready to send if it's writable,
345 // because we have no way of knowing otherwise (the channel doesn't give us
346 // "was last send successful?").
347 //
348 // This won't always be accurate (the last SendPacket call from another
349 // BaseChannel could have resulted in an error), but even so, we'll just
350 // encounter the error again and update "ready to send" accordingly.
deadbeef062ce9f2016-08-26 21:42:15 -0700351 SetTransportChannelReadyToSend(
deadbeeff5346592017-01-24 21:51:21 -0800352 false, rtp_packet_transport_ && rtp_packet_transport_->writable());
zhihuangb2cdd932017-01-19 16:54:25 -0800353 SetTransportChannelReadyToSend(
deadbeeff5346592017-01-24 21:51:21 -0800354 true, rtcp_packet_transport_ && rtcp_packet_transport_->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000355}
356
deadbeeff5346592017-01-24 21:51:21 -0800357void BaseChannel::SetTransport_n(
358 bool rtcp,
359 DtlsTransportInternal* new_dtls_transport,
360 rtc::PacketTransportInterface* new_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200361 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800362 DtlsTransportInternal*& old_dtls_transport =
zhihuangb2cdd932017-01-19 16:54:25 -0800363 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
deadbeeff5346592017-01-24 21:51:21 -0800364 rtc::PacketTransportInterface*& old_packet_transport =
365 rtcp ? rtcp_packet_transport_ : rtp_packet_transport_;
zhihuangb2cdd932017-01-19 16:54:25 -0800366
deadbeeff5346592017-01-24 21:51:21 -0800367 if (!old_packet_transport && !new_packet_transport) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700368 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000369 return;
370 }
zhihuangb2cdd932017-01-19 16:54:25 -0800371
deadbeeff5346592017-01-24 21:51:21 -0800372 RTC_DCHECK(old_packet_transport != new_packet_transport);
373 if (old_dtls_transport) {
374 DisconnectFromDtlsTransport(old_dtls_transport);
375 } else if (old_packet_transport) {
376 DisconnectFromPacketTransport(old_packet_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000377 }
378
deadbeeff5346592017-01-24 21:51:21 -0800379 old_packet_transport = new_packet_transport;
380 old_dtls_transport = new_dtls_transport;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000381
deadbeeff5346592017-01-24 21:51:21 -0800382 // If there's no new transport, we're done after disconnecting from old one.
383 if (!new_packet_transport) {
384 return;
385 }
386
387 if (rtcp && new_dtls_transport) {
388 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
389 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
390 << "should never happen.";
391 }
392 if (new_dtls_transport) {
393 ConnectToDtlsTransport(new_dtls_transport);
394 } else {
395 ConnectToPacketTransport(new_packet_transport);
396 }
397 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
398 for (const auto& pair : socket_options) {
399 new_packet_transport->SetOption(pair.first, pair.second);
guoweis46383312015-12-17 16:45:59 -0800400 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000401}
402
deadbeeff5346592017-01-24 21:51:21 -0800403void BaseChannel::ConnectToDtlsTransport(DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200404 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000405
zhihuangb2cdd932017-01-19 16:54:25 -0800406 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
407 transport->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
408 transport->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
409 transport->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
410 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
411 transport->ice_transport()->SignalSelectedCandidatePairChanged.connect(
Honghai Zhangcc411c02016-03-29 17:27:21 -0700412 this, &BaseChannel::OnSelectedCandidatePairChanged);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000413}
414
deadbeeff5346592017-01-24 21:51:21 -0800415void BaseChannel::DisconnectFromDtlsTransport(
416 DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200417 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800418 OnSelectedCandidatePairChanged(transport->ice_transport(), nullptr, -1,
419 false);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000420
zhihuangb2cdd932017-01-19 16:54:25 -0800421 transport->SignalWritableState.disconnect(this);
422 transport->SignalReadPacket.disconnect(this);
423 transport->SignalReadyToSend.disconnect(this);
424 transport->SignalDtlsState.disconnect(this);
425 transport->SignalSentPacket.disconnect(this);
426 transport->ice_transport()->SignalSelectedCandidatePairChanged.disconnect(
427 this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000428}
429
deadbeeff5346592017-01-24 21:51:21 -0800430void BaseChannel::ConnectToPacketTransport(
431 rtc::PacketTransportInterface* transport) {
432 RTC_DCHECK_RUN_ON(network_thread_);
433 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
434 transport->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
435 transport->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
436 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
437}
438
439void BaseChannel::DisconnectFromPacketTransport(
440 rtc::PacketTransportInterface* transport) {
441 RTC_DCHECK_RUN_ON(network_thread_);
442 transport->SignalWritableState.disconnect(this);
443 transport->SignalReadPacket.disconnect(this);
444 transport->SignalReadyToSend.disconnect(this);
445 transport->SignalSentPacket.disconnect(this);
446}
447
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700449 worker_thread_->Invoke<void>(
450 RTC_FROM_HERE,
451 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
452 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000453 return true;
454}
455
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456bool BaseChannel::AddRecvStream(const StreamParams& sp) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700457 return InvokeOnWorker(RTC_FROM_HERE,
458 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459}
460
Peter Boström0c4e06b2015-10-07 12:23:21 +0200461bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700462 return InvokeOnWorker(RTC_FROM_HERE,
463 Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464}
465
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000466bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000467 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700468 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000469}
470
Peter Boström0c4e06b2015-10-07 12:23:21 +0200471bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700472 return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream,
473 media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000474}
475
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000477 ContentAction action,
478 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100479 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700480 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w,
481 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482}
483
484bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000485 ContentAction action,
486 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100487 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700488 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w,
489 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490}
491
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492void BaseChannel::StartConnectionMonitor(int cms) {
zhihuangb2cdd932017-01-19 16:54:25 -0800493 // We pass in the BaseChannel instead of the rtp_dtls_transport_
494 // because if the rtp_dtls_transport_ changes, the ConnectionMonitor
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000495 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200496 // We pass in the network thread because on that thread connection monitor
497 // will call BaseChannel::GetConnectionStats which must be called on the
498 // network thread.
499 connection_monitor_.reset(
500 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000501 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000503 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504}
505
506void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000507 if (connection_monitor_) {
508 connection_monitor_->Stop();
509 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 }
511}
512
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000513bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200514 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800515 if (!rtp_dtls_transport_) {
516 return false;
517 }
zhihuangb2cdd932017-01-19 16:54:25 -0800518 return rtp_dtls_transport_->ice_transport()->GetStats(infos);
zhihuangf5b251b2017-01-12 19:37:48 -0800519}
520
521bool BaseChannel::NeedsRtcpTransport() {
deadbeefac22f702017-01-12 21:59:29 -0800522 // If this BaseChannel doesn't require RTCP mux and we haven't fully
523 // negotiated RTCP mux, we need an RTCP transport.
524 return !rtcp_mux_required_ && !rtcp_mux_filter_.IsFullyActive();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000525}
526
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700527bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 // Receive data if we are enabled and have local content,
529 return enabled() && IsReceiveContentDirection(local_content_direction_);
530}
531
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700532bool BaseChannel::IsReadyToSendMedia_w() const {
533 // Need to access some state updated on the network thread.
534 return network_thread_->Invoke<bool>(
535 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
536}
537
538bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 // Send outgoing data if we are enabled, have local and remote content,
540 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800541 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700543 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200544 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545}
546
jbaucheec21bd2016-03-20 06:15:43 -0700547bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700548 const rtc::PacketOptions& options) {
549 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550}
551
jbaucheec21bd2016-03-20 06:15:43 -0700552bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700553 const rtc::PacketOptions& options) {
554 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555}
556
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000557int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200559 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700560 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200561}
562
563int BaseChannel::SetOption_n(SocketType type,
564 rtc::Socket::Option opt,
565 int value) {
566 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800567 rtc::PacketTransportInterface* transport = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000569 case ST_RTP:
deadbeeff5346592017-01-24 21:51:21 -0800570 transport = rtp_packet_transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700571 socket_options_.push_back(
572 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000573 break;
574 case ST_RTCP:
deadbeeff5346592017-01-24 21:51:21 -0800575 transport = rtcp_packet_transport_;
deadbeefcbecd352015-09-23 11:50:27 -0700576 rtcp_socket_options_.push_back(
577 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000578 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 }
deadbeeff5346592017-01-24 21:51:21 -0800580 return transport ? transport->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581}
582
jbauchcb560652016-08-04 05:20:32 -0700583bool BaseChannel::SetCryptoOptions(const rtc::CryptoOptions& crypto_options) {
584 crypto_options_ = crypto_options;
585 return true;
586}
587
johand89ab142016-10-25 10:50:32 -0700588void BaseChannel::OnWritableState(rtc::PacketTransportInterface* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800589 RTC_DCHECK(transport == rtp_packet_transport_ ||
590 transport == rtcp_packet_transport_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200591 RTC_DCHECK(network_thread_->IsCurrent());
592 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593}
594
johand89ab142016-10-25 10:50:32 -0700595void BaseChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
596 const char* data,
597 size_t len,
598 const rtc::PacketTime& packet_time,
599 int flags) {
600 TRACE_EVENT0("webrtc", "BaseChannel::OnPacketRead");
601 // OnPacketRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200602 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603
604 // When using RTCP multiplexing we might get RTCP packets on the RTP
605 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
johand89ab142016-10-25 10:50:32 -0700606 bool rtcp = PacketIsRtcp(transport, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700607 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000608 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609}
610
johand89ab142016-10-25 10:50:32 -0700611void BaseChannel::OnReadyToSend(rtc::PacketTransportInterface* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800612 RTC_DCHECK(transport == rtp_packet_transport_ ||
613 transport == rtcp_packet_transport_);
614 SetTransportChannelReadyToSend(transport == rtcp_packet_transport_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615}
616
zhihuangb2cdd932017-01-19 16:54:25 -0800617void BaseChannel::OnDtlsState(DtlsTransportInternal* transport,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800618 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200619 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800620 return;
621 }
622
623 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
624 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
zhihuangb2cdd932017-01-19 16:54:25 -0800625 // cover other scenarios like the whole transport is writable (not just this
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800626 // TransportChannel) or when TransportChannel is attached after DTLS is
627 // negotiated.
628 if (state != DTLS_TRANSPORT_CONNECTED) {
629 srtp_filter_.ResetParams();
630 }
631}
632
Honghai Zhangcc411c02016-03-29 17:27:21 -0700633void BaseChannel::OnSelectedCandidatePairChanged(
zhihuangb2cdd932017-01-19 16:54:25 -0800634 IceTransportInternal* ice_transport,
Honghai Zhang52dce732016-03-31 12:37:31 -0700635 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700636 int last_sent_packet_id,
637 bool ready_to_send) {
deadbeeff5346592017-01-24 21:51:21 -0800638 RTC_DCHECK((rtp_dtls_transport_ &&
639 ice_transport == rtp_dtls_transport_->ice_transport()) ||
640 (rtcp_dtls_transport_ &&
641 ice_transport == rtcp_dtls_transport_->ice_transport()));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200642 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800643 selected_candidate_pair_ = selected_candidate_pair;
zhihuangb2cdd932017-01-19 16:54:25 -0800644 std::string transport_name = ice_transport->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700645 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700646 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700647 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700648 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700649 selected_candidate_pair->remote_candidate().network_id(),
650 last_sent_packet_id);
michaelt79e05882016-11-08 02:50:09 -0800651
652 UpdateTransportOverhead();
Honghai Zhangcc411c02016-03-29 17:27:21 -0700653 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200654 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700655 RTC_FROM_HERE, worker_thread_,
656 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
657 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700658}
659
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700660void BaseChannel::SetTransportChannelReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200661 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700662 if (rtcp) {
663 rtcp_ready_to_send_ = ready;
664 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 rtp_ready_to_send_ = ready;
666 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200668 bool ready_to_send =
669 (rtp_ready_to_send_ &&
deadbeeff5346592017-01-24 21:51:21 -0800670 // In the case of rtcp mux |rtcp_packet_transport_| will be null.
671 (rtcp_ready_to_send_ || !rtcp_packet_transport_));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200672
673 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700674 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200675 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676}
677
johand89ab142016-10-25 10:50:32 -0700678bool BaseChannel::PacketIsRtcp(const rtc::PacketTransportInterface* transport,
679 const char* data,
680 size_t len) {
deadbeeff5346592017-01-24 21:51:21 -0800681 return (transport == rtcp_packet_transport_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000682 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683}
684
stefanc1aeaf02015-10-15 07:26:07 -0700685bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700686 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700687 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200688 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
689 // If the thread is not our network thread, we will post to our network
690 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 // synchronize access to all the pieces of the send path, including
692 // SRTP and the inner workings of the transport channels.
693 // The only downside is that we can't return a proper failure code if
694 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200695 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200697 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
698 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800699 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700700 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700701 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 return true;
703 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200704 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705
706 // Now that we are on the correct thread, ensure we have a place to send this
707 // packet before doing anything. (We might get RTCP packets that we don't
708 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
709 // transport.
deadbeeff5346592017-01-24 21:51:21 -0800710 rtc::PacketTransportInterface* transport =
711 (!rtcp || rtcp_mux_filter_.IsActive()) ? rtp_packet_transport_
712 : rtcp_packet_transport_;
zhihuangb2cdd932017-01-19 16:54:25 -0800713 if (!transport || !transport->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 return false;
715 }
716
717 // Protect ourselves against crazy data.
718 if (!ValidPacket(rtcp, packet)) {
719 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000720 << PacketType(rtcp)
721 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 return false;
723 }
724
stefanc1aeaf02015-10-15 07:26:07 -0700725 rtc::PacketOptions updated_options;
726 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 // Protect if needed.
728 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200729 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200731 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000732 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000734 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
735 // inside libsrtp for a RTP packet. A external HMAC module will be writing
736 // a fake HMAC value. This is ONLY done for a RTP packet.
737 // Socket layer will update rtp sendtime extension header if present in
738 // packet with current time before updating the HMAC.
739#if !defined(ENABLE_EXTERNAL_AUTH)
740 res = srtp_filter_.ProtectRtp(
741 data, len, static_cast<int>(packet->capacity()), &len);
742#else
stefanc1aeaf02015-10-15 07:26:07 -0700743 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000744 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000745 res = srtp_filter_.ProtectRtp(
746 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700747 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000748 // If protection succeeds, let's get auth params from srtp.
749 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200750 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000751 int key_len;
752 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700753 &auth_key, &key_len,
754 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000755 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700756 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
757 updated_options.packet_time_params.srtp_auth_key.assign(
758 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000759 }
760 }
761#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 if (!res) {
763 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200764 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 GetRtpSeqNum(data, len, &seq_num);
766 GetRtpSsrc(data, len, &ssrc);
767 LOG(LS_ERROR) << "Failed to protect " << content_name_
768 << " RTP packet: size=" << len
769 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
770 return false;
771 }
772 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000773 res = srtp_filter_.ProtectRtcp(data, len,
774 static_cast<int>(packet->capacity()),
775 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 if (!res) {
777 int type = -1;
778 GetRtcpType(data, len, &type);
779 LOG(LS_ERROR) << "Failed to protect " << content_name_
780 << " RTCP packet: size=" << len << ", type=" << type;
781 return false;
782 }
783 }
784
785 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000786 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800787 } else if (srtp_required_) {
deadbeef8f425f92016-12-01 12:26:27 -0800788 // The audio/video engines may attempt to send RTCP packets as soon as the
789 // streams are created, so don't treat this as an error for RTCP.
790 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809
791 if (rtcp) {
792 return false;
793 }
794 // However, there shouldn't be any RTP packets sent before SRTP is set up
795 // (and SetSend(true) is called).
796 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
797 << " and crypto is required";
nisseeb4ca4e2017-01-12 02:24:27 -0800798 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 return false;
800 }
801
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200803 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
zhihuangb2cdd932017-01-19 16:54:25 -0800804 int ret = transport->SendPacket(packet->data<char>(), packet->size(),
805 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000806 if (ret != static_cast<int>(packet->size())) {
zhihuangb2cdd932017-01-19 16:54:25 -0800807 if (transport->GetError() == ENOTCONN) {
skvladc309e0e2016-07-28 17:15:20 -0700808 LOG(LS_WARNING) << "Got ENOTCONN from transport.";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700809 SetTransportChannelReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 }
811 return false;
812 }
813 return true;
814}
815
jbaucheec21bd2016-03-20 06:15:43 -0700816bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 // Protect ourselves against crazy data.
818 if (!ValidPacket(rtcp, packet)) {
819 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000820 << PacketType(rtcp)
821 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 return false;
823 }
pbos482b12e2015-11-16 10:19:58 -0800824 if (rtcp) {
825 // Permit all (seemingly valid) RTCP packets.
826 return true;
827 }
828 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700829 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830}
831
jbaucheec21bd2016-03-20 06:15:43 -0700832void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000833 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200834 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 if (!WantsPacket(rtcp, packet)) {
836 return;
837 }
838
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000839 // We are only interested in the first rtp packet because that
840 // indicates the media has started flowing.
841 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700843 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 }
845
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 // Unprotect the packet, if needed.
847 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200848 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200849 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000850 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 bool res;
852 if (!rtcp) {
853 res = srtp_filter_.UnprotectRtp(data, len, &len);
854 if (!res) {
855 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200856 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 GetRtpSeqNum(data, len, &seq_num);
858 GetRtpSsrc(data, len, &ssrc);
859 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
860 << " RTP packet: size=" << len
861 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
862 return;
863 }
864 } else {
865 res = srtp_filter_.UnprotectRtcp(data, len, &len);
866 if (!res) {
867 int type = -1;
868 GetRtcpType(data, len, &type);
869 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
870 << " RTCP packet: size=" << len << ", type=" << type;
871 return;
872 }
873 }
874
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000875 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800876 } else if (srtp_required_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 // Our session description indicates that SRTP is required, but we got a
878 // packet before our SRTP filter is active. This means either that
879 // a) we got SRTP packets before we received the SDES keys, in which case
880 // we can't decrypt it anyway, or
881 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
zhihuangb2cdd932017-01-19 16:54:25 -0800882 // transports, so we haven't yet extracted keys, even if DTLS did
883 // complete on the transport that the packets are being sent on. It's
884 // really good practice to wait for both RTP and RTCP to be good to go
885 // before sending media, to prevent weird failure modes, so it's fine
886 // for us to just eat packets here. This is all sidestepped if RTCP mux
887 // is used anyway.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
889 << " packet when SRTP is inactive and crypto is required";
890 return;
891 }
892
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200893 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700894 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200895 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
896}
897
898void BaseChannel::OnPacketReceived(bool rtcp,
899 const rtc::CopyOnWriteBuffer& packet,
900 const rtc::PacketTime& packet_time) {
901 RTC_DCHECK(worker_thread_->IsCurrent());
902 // Need to copy variable because OnRtcpReceived/OnPacketReceived
903 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
904 rtc::CopyOnWriteBuffer data(packet);
905 if (rtcp) {
906 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200908 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 }
910}
911
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000912bool BaseChannel::PushdownLocalDescription(
913 const SessionDescription* local_desc, ContentAction action,
914 std::string* error_desc) {
915 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 const MediaContentDescription* content_desc =
917 GetContentDescription(content_info);
918 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000919 !SetLocalContent(content_desc, action, error_desc)) {
920 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
921 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000923 return true;
924}
925
926bool BaseChannel::PushdownRemoteDescription(
927 const SessionDescription* remote_desc, ContentAction action,
928 std::string* error_desc) {
929 const ContentInfo* content_info = GetFirstContent(remote_desc);
930 const MediaContentDescription* content_desc =
931 GetContentDescription(content_info);
932 if (content_desc && content_info && !content_info->rejected &&
933 !SetRemoteContent(content_desc, action, error_desc)) {
934 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
935 return false;
936 }
937 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938}
939
940void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700941 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 if (enabled_)
943 return;
944
945 LOG(LS_INFO) << "Channel enabled";
946 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700947 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948}
949
950void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700951 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 if (!enabled_)
953 return;
954
955 LOG(LS_INFO) << "Channel disabled";
956 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700957 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958}
959
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200960void BaseChannel::UpdateWritableState_n() {
deadbeeff5346592017-01-24 21:51:21 -0800961 if (rtp_packet_transport_ && rtp_packet_transport_->writable() &&
962 (!rtcp_packet_transport_ || rtcp_packet_transport_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200963 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700964 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200965 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700966 }
967}
968
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200969void BaseChannel::ChannelWritable_n() {
970 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800971 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800973 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974
deadbeefcbecd352015-09-23 11:50:27 -0700975 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 << (was_ever_writable_ ? "" : " for the first time");
977
michaelt79e05882016-11-08 02:50:09 -0800978 if (selected_candidate_pair_)
979 LOG(LS_INFO)
980 << "Using "
981 << selected_candidate_pair_->local_candidate().ToSensitiveString()
982 << "->"
983 << selected_candidate_pair_->remote_candidate().ToSensitiveString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200986 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700988 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989}
990
deadbeef953c2ce2017-01-09 14:53:41 -0800991void BaseChannel::SignalDtlsSrtpSetupFailure_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200992 RTC_DCHECK(network_thread_->IsCurrent());
993 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700994 RTC_FROM_HERE, signaling_thread(),
deadbeef953c2ce2017-01-09 14:53:41 -0800995 Bind(&BaseChannel::SignalDtlsSrtpSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000996}
997
deadbeef953c2ce2017-01-09 14:53:41 -0800998void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700999 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
deadbeef953c2ce2017-01-09 14:53:41 -08001000 SignalDtlsSrtpSetupFailure(this, rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +00001001}
1002
zhihuangb2cdd932017-01-19 16:54:25 -08001003bool BaseChannel::SetDtlsSrtpCryptoSuites_n(DtlsTransportInternal* transport,
1004 bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001005 std::vector<int> crypto_suites;
1006 // We always use the default SRTP crypto suites for RTCP, but we may use
1007 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001009 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 } else {
jbauchcb560652016-08-04 05:20:32 -07001011 GetDefaultSrtpCryptoSuites(crypto_options(), &crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 }
zhihuangb2cdd932017-01-19 16:54:25 -08001013 return transport->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014}
1015
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001016bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
zhihuangb2cdd932017-01-19 16:54:25 -08001017 // Since DTLS is applied to all transports, checking RTP should be enough.
1018 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019}
1020
1021// This function returns true if either DTLS-SRTP is not in use
1022// *or* DTLS-SRTP is successfully set up.
zhihuangb2cdd932017-01-19 16:54:25 -08001023bool BaseChannel::SetupDtlsSrtp_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001024 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 bool ret = false;
1026
zhihuangb2cdd932017-01-19 16:54:25 -08001027 DtlsTransportInternal* transport =
1028 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
deadbeeff5346592017-01-24 21:51:21 -08001029 RTC_DCHECK(transport);
zhihuangb2cdd932017-01-19 16:54:25 -08001030 RTC_DCHECK(transport->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001032 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033
zhihuangb2cdd932017-01-19 16:54:25 -08001034 if (!transport->GetSrtpCryptoSuite(&selected_crypto_suite)) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001035 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 return false;
1037 }
1038
zhihuangb2cdd932017-01-19 16:54:25 -08001039 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " << content_name() << " "
1040 << PacketType(rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041
jbauchcb560652016-08-04 05:20:32 -07001042 int key_len;
1043 int salt_len;
1044 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
1045 &salt_len)) {
1046 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
1047 return false;
1048 }
1049
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -07001051 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052
1053 // RFC 5705 exporter using the RFC 5764 parameters
zhihuangb2cdd932017-01-19 16:54:25 -08001054 if (!transport->ExportKeyingMaterial(kDtlsSrtpExporterLabel, NULL, 0, false,
1055 &dtls_buffer[0], dtls_buffer.size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
nisseeb4ca4e2017-01-12 02:24:27 -08001057 RTC_NOTREACHED(); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 return false;
1059 }
1060
1061 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -07001062 std::vector<unsigned char> client_write_key(key_len + salt_len);
1063 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -07001065 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
1066 offset += key_len;
1067 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
1068 offset += key_len;
1069 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
1070 offset += salt_len;
1071 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072
1073 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001074 rtc::SSLRole role;
zhihuangb2cdd932017-01-19 16:54:25 -08001075 if (!transport->GetSslRole(&role)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001076 LOG(LS_WARNING) << "GetSslRole failed";
1077 return false;
1078 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001080 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001081 send_key = &server_write_key;
1082 recv_key = &client_write_key;
1083 } else {
1084 send_key = &client_write_key;
1085 recv_key = &server_write_key;
1086 }
1087
zhihuangb2cdd932017-01-19 16:54:25 -08001088 if (rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001089 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1090 static_cast<int>(send_key->size()),
1091 selected_crypto_suite, &(*recv_key)[0],
1092 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001094 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1095 static_cast<int>(send_key->size()),
1096 selected_crypto_suite, &(*recv_key)[0],
1097 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 }
1099
michaelt79e05882016-11-08 02:50:09 -08001100 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
michaelt79e05882016-11-08 02:50:09 -08001102 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 dtls_keyed_ = true;
michaelt79e05882016-11-08 02:50:09 -08001104 UpdateTransportOverhead();
1105 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 return ret;
1107}
1108
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001109void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001110 if (srtp_filter_.IsActive()) {
1111 return;
1112 }
1113
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001114 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001115 return;
1116 }
1117
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001118 if (!SetupDtlsSrtp_n(false)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001119 SignalDtlsSrtpSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001120 return;
1121 }
1122
zhihuangb2cdd932017-01-19 16:54:25 -08001123 if (rtcp_dtls_transport_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001124 if (!SetupDtlsSrtp_n(true)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001125 SignalDtlsSrtpSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001126 return;
1127 }
1128 }
1129}
1130
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001131void BaseChannel::ChannelNotWritable_n() {
1132 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 if (!writable_)
1134 return;
1135
deadbeefcbecd352015-09-23 11:50:27 -07001136 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001138 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139}
1140
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001141bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001142 const MediaContentDescription* content,
1143 ContentAction action,
1144 ContentSource src,
1145 std::string* error_desc) {
1146 if (action == CA_UPDATE) {
1147 // These parameters never get changed by a CA_UDPATE.
1148 return true;
1149 }
1150
deadbeef7af91dd2016-12-13 11:29:11 -08001151 // Cache srtp_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001152 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001153 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1154 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001155}
1156
1157bool BaseChannel::SetRtpTransportParameters_n(
1158 const MediaContentDescription* content,
1159 ContentAction action,
1160 ContentSource src,
1161 std::string* error_desc) {
1162 RTC_DCHECK(network_thread_->IsCurrent());
1163
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001164 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001165 return false;
1166 }
1167
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001168 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001169 return false;
1170 }
1171
1172 return true;
1173}
1174
zhihuangb2cdd932017-01-19 16:54:25 -08001175// |dtls| will be set to true if DTLS is active for transport and crypto is
1176// empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001177bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1178 bool* dtls,
1179 std::string* error_desc) {
deadbeeff5346592017-01-24 21:51:21 -08001180 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001181 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001182 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001183 return false;
1184 }
1185 return true;
1186}
1187
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001188bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001189 ContentAction action,
1190 ContentSource src,
1191 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001192 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001193 if (action == CA_UPDATE) {
1194 // no crypto params.
1195 return true;
1196 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001198 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001199 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001200 if (!ret) {
1201 return false;
1202 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 switch (action) {
1204 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001205 // If DTLS is already active on the channel, we could be renegotiating
1206 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001207 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001208 ret = srtp_filter_.SetOffer(cryptos, src);
1209 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 break;
1211 case CA_PRANSWER:
1212 // If we're doing DTLS-SRTP, we don't want to update the filter
1213 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001214 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1216 }
1217 break;
1218 case CA_ANSWER:
1219 // If we're doing DTLS-SRTP, we don't want to update the filter
1220 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001221 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 ret = srtp_filter_.SetAnswer(cryptos, src);
1223 }
1224 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 default:
1226 break;
1227 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001228 if (!ret) {
1229 SafeSetError("Failed to setup SRTP filter.", error_desc);
1230 return false;
1231 }
1232 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233}
1234
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001235bool BaseChannel::SetRtcpMux_n(bool enable,
1236 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001237 ContentSource src,
1238 std::string* error_desc) {
deadbeef8e814d72017-01-13 11:34:39 -08001239 // Provide a more specific error message for the RTCP mux "require" policy
1240 // case.
1241 if (rtcp_mux_required_ && !enable) {
1242 SafeSetError(
1243 "rtcpMuxPolicy is 'require', but media description does not "
1244 "contain 'a=rtcp-mux'.",
1245 error_desc);
1246 return false;
1247 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 bool ret = false;
1249 switch (action) {
1250 case CA_OFFER:
1251 ret = rtcp_mux_filter_.SetOffer(enable, src);
1252 break;
1253 case CA_PRANSWER:
zhihuangb2cdd932017-01-19 16:54:25 -08001254 // This may activate RTCP muxing, but we don't yet destroy the transport
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001255 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1257 break;
1258 case CA_ANSWER:
1259 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1260 if (ret && rtcp_mux_filter_.IsActive()) {
1261 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001262 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
zhihuangb2cdd932017-01-19 16:54:25 -08001263 << " by destroying RTCP transport for "
deadbeefcbecd352015-09-23 11:50:27 -07001264 << transport_name();
deadbeeff5346592017-01-24 21:51:21 -08001265 if (rtcp_packet_transport_) {
1266 SetTransport_n(true, nullptr, nullptr);
1267 SignalRtcpMuxFullyActive(transport_name_);
zhihuangf5b251b2017-01-12 19:37:48 -08001268 }
deadbeef062ce9f2016-08-26 21:42:15 -07001269 UpdateWritableState_n();
1270 SetTransportChannelReadyToSend(true, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 }
1272 break;
1273 case CA_UPDATE:
1274 // No RTCP mux info.
1275 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001276 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 default:
1278 break;
1279 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001280 if (!ret) {
1281 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1282 return false;
1283 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
zhihuangb2cdd932017-01-19 16:54:25 -08001285 // CA_ANSWER, but we only want to tear down the RTCP transport if we received
1286 // a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001287 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 // If the RTP transport is already writable, then so are we.
deadbeeff5346592017-01-24 21:51:21 -08001289 if (rtp_packet_transport_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001290 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 }
1292 }
1293
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001294 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295}
1296
1297bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001298 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001299 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300}
1301
Peter Boström0c4e06b2015-10-07 12:23:21 +02001302bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001303 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 return media_channel()->RemoveRecvStream(ssrc);
1305}
1306
1307bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001308 ContentAction action,
1309 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001310 if (!(action == CA_OFFER || action == CA_ANSWER ||
1311 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 return false;
1313
1314 // If this is an update, streams only contain streams that have changed.
1315 if (action == CA_UPDATE) {
1316 for (StreamParamsVec::const_iterator it = streams.begin();
1317 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001318 const StreamParams* existing_stream =
1319 GetStreamByIds(local_streams_, it->groupid, it->id);
1320 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 if (media_channel()->AddSendStream(*it)) {
1322 local_streams_.push_back(*it);
1323 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1324 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001325 std::ostringstream desc;
1326 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1327 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 return false;
1329 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001330 } else if (existing_stream && !it->has_ssrcs()) {
1331 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001332 std::ostringstream desc;
1333 desc << "Failed to remove send stream with ssrc "
1334 << it->first_ssrc() << ".";
1335 SafeSetError(desc.str(), error_desc);
1336 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001338 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339 } else {
1340 LOG(LS_WARNING) << "Ignore unsupported stream update";
1341 }
1342 }
1343 return true;
1344 }
1345 // Else streams are all the streams we want to send.
1346
1347 // Check for streams that have been removed.
1348 bool ret = true;
1349 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1350 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001351 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001353 std::ostringstream desc;
1354 desc << "Failed to remove send stream with ssrc "
1355 << it->first_ssrc() << ".";
1356 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357 ret = false;
1358 }
1359 }
1360 }
1361 // Check for new streams.
1362 for (StreamParamsVec::const_iterator it = streams.begin();
1363 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001364 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001366 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001368 std::ostringstream desc;
1369 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1370 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371 ret = false;
1372 }
1373 }
1374 }
1375 local_streams_ = streams;
1376 return ret;
1377}
1378
1379bool BaseChannel::UpdateRemoteStreams_w(
1380 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001381 ContentAction action,
1382 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001383 if (!(action == CA_OFFER || action == CA_ANSWER ||
1384 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001385 return false;
1386
1387 // If this is an update, streams only contain streams that have changed.
1388 if (action == CA_UPDATE) {
1389 for (StreamParamsVec::const_iterator it = streams.begin();
1390 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001391 const StreamParams* existing_stream =
1392 GetStreamByIds(remote_streams_, it->groupid, it->id);
1393 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001394 if (AddRecvStream_w(*it)) {
1395 remote_streams_.push_back(*it);
1396 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1397 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001398 std::ostringstream desc;
1399 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1400 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401 return false;
1402 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001403 } else if (existing_stream && !it->has_ssrcs()) {
1404 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001405 std::ostringstream desc;
1406 desc << "Failed to remove remote stream with ssrc "
1407 << it->first_ssrc() << ".";
1408 SafeSetError(desc.str(), error_desc);
1409 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001411 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001412 } else {
1413 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001414 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415 << " new stream = " << it->ToString();
1416 }
1417 }
1418 return true;
1419 }
1420 // Else streams are all the streams we want to receive.
1421
1422 // Check for streams that have been removed.
1423 bool ret = true;
1424 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1425 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001426 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001428 std::ostringstream desc;
1429 desc << "Failed to remove remote stream with ssrc "
1430 << it->first_ssrc() << ".";
1431 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432 ret = false;
1433 }
1434 }
1435 }
1436 // Check for new streams.
1437 for (StreamParamsVec::const_iterator it = streams.begin();
1438 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001439 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440 if (AddRecvStream_w(*it)) {
1441 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1442 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001443 std::ostringstream desc;
1444 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1445 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446 ret = false;
1447 }
1448 }
1449 }
1450 remote_streams_ = streams;
1451 return ret;
1452}
1453
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001454void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001455 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001456// Absolute Send Time extension id is used only with external auth,
1457// so do not bother searching for it and making asyncronious call to set
1458// something that is not used.
1459#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001460 const webrtc::RtpExtension* send_time_extension =
1461 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001462 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001463 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001464 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001465 RTC_FROM_HERE, network_thread_,
1466 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1467 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001468#endif
1469}
1470
1471void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1472 int rtp_abs_sendtime_extn_id) {
1473 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001474}
1475
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001476void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001477 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001479 case MSG_SEND_RTP_PACKET:
1480 case MSG_SEND_RTCP_PACKET: {
1481 RTC_DCHECK(network_thread_->IsCurrent());
1482 SendPacketMessageData* data =
1483 static_cast<SendPacketMessageData*>(pmsg->pdata);
1484 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1485 SendPacket(rtcp, &data->packet, data->options);
1486 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487 break;
1488 }
1489 case MSG_FIRSTPACKETRECEIVED: {
1490 SignalFirstPacketReceived(this);
1491 break;
1492 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 }
1494}
1495
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001496void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497 // Flush all remaining RTCP messages. This should only be called in
1498 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001499 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001500 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001501 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1502 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001503 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1504 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505 }
1506}
1507
johand89ab142016-10-25 10:50:32 -07001508void BaseChannel::SignalSentPacket_n(
1509 rtc::PacketTransportInterface* /* transport */,
1510 const rtc::SentPacket& sent_packet) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001511 RTC_DCHECK(network_thread_->IsCurrent());
1512 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001513 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001514 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1515}
1516
1517void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1518 RTC_DCHECK(worker_thread_->IsCurrent());
1519 SignalSentPacket(sent_packet);
1520}
1521
1522VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1523 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001524 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001525 MediaEngineInterface* media_engine,
1526 VoiceMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001528 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001529 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001530 : BaseChannel(worker_thread,
1531 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001532 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001533 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001534 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001535 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001536 srtp_required),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001537 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001538 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539
1540VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001541 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 StopAudioMonitor();
1543 StopMediaMonitor();
1544 // this can't be done in the base class, since it calls a virtual
1545 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001546 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547}
1548
Peter Boström0c4e06b2015-10-07 12:23:21 +02001549bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001550 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001551 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001552 AudioSource* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001553 return InvokeOnWorker(RTC_FROM_HERE,
1554 Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001555 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556}
1557
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558// TODO(juberti): Handle early media the right way. We should get an explicit
1559// ringing message telling us to start playing local ringback, which we cancel
1560// if any early media actually arrives. For now, we do the opposite, which is
1561// to wait 1 second for early media, and start playing local ringback if none
1562// arrives.
1563void VoiceChannel::SetEarlyMedia(bool enable) {
1564 if (enable) {
1565 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001566 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1567 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 } else {
1569 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001570 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 }
1572}
1573
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574bool VoiceChannel::CanInsertDtmf() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001575 return InvokeOnWorker(
1576 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577}
1578
Peter Boström0c4e06b2015-10-07 12:23:21 +02001579bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1580 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001581 int duration) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001582 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this,
1583 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584}
1585
solenberg4bac9c52015-10-09 02:32:53 -07001586bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001587 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume,
1588 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001590
Tommif888bb52015-12-12 01:37:01 +01001591void VoiceChannel::SetRawAudioSink(
1592 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001593 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1594 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001595 // passing. So we invoke to our own little routine that gets a pointer to
1596 // our local variable. This is OK since we're synchronously invoking.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001597 InvokeOnWorker(RTC_FROM_HERE,
1598 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001599}
1600
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001601webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001602 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001603 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001604}
1605
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001606webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1607 uint32_t ssrc) const {
1608 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001609}
1610
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001611bool VoiceChannel::SetRtpSendParameters(
1612 uint32_t ssrc,
1613 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001614 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001615 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001616 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001617}
1618
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001619bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1620 webrtc::RtpParameters parameters) {
1621 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1622}
1623
1624webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1625 uint32_t ssrc) const {
1626 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001627 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001628 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1629}
1630
1631webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1632 uint32_t ssrc) const {
1633 return media_channel()->GetRtpReceiveParameters(ssrc);
1634}
1635
1636bool VoiceChannel::SetRtpReceiveParameters(
1637 uint32_t ssrc,
1638 const webrtc::RtpParameters& parameters) {
1639 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001640 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001641 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1642}
1643
1644bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1645 webrtc::RtpParameters parameters) {
1646 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001647}
1648
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001650 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1651 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652}
1653
1654void VoiceChannel::StartMediaMonitor(int cms) {
1655 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001656 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 media_monitor_->SignalUpdate.connect(
1658 this, &VoiceChannel::OnMediaMonitorUpdate);
1659 media_monitor_->Start(cms);
1660}
1661
1662void VoiceChannel::StopMediaMonitor() {
1663 if (media_monitor_) {
1664 media_monitor_->Stop();
1665 media_monitor_->SignalUpdate.disconnect(this);
1666 media_monitor_.reset();
1667 }
1668}
1669
1670void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001671 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 audio_monitor_
1673 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1674 audio_monitor_->Start(cms);
1675}
1676
1677void VoiceChannel::StopAudioMonitor() {
1678 if (audio_monitor_) {
1679 audio_monitor_->Stop();
1680 audio_monitor_.reset();
1681 }
1682}
1683
1684bool VoiceChannel::IsAudioMonitorRunning() const {
1685 return (audio_monitor_.get() != NULL);
1686}
1687
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001689 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690}
1691
1692int VoiceChannel::GetOutputLevel_w() {
1693 return media_channel()->GetOutputLevel();
1694}
1695
1696void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1697 media_channel()->GetActiveStreams(actives);
1698}
1699
johand89ab142016-10-25 10:50:32 -07001700void VoiceChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
1701 const char* data,
1702 size_t len,
1703 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001704 int flags) {
johand89ab142016-10-25 10:50:32 -07001705 BaseChannel::OnPacketRead(transport, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 // Set a flag when we've received an RTP packet. If we're waiting for early
1707 // media, this will disable the timeout.
johand89ab142016-10-25 10:50:32 -07001708 if (!received_media_ && !PacketIsRtcp(transport, data, len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 received_media_ = true;
1710 }
1711}
1712
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001713void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001714 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001715 invoker_.AsyncInvoke<void>(
1716 RTC_FROM_HERE, worker_thread_,
1717 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001718}
1719
michaelt79e05882016-11-08 02:50:09 -08001720int BaseChannel::GetTransportOverheadPerPacket() const {
1721 RTC_DCHECK(network_thread_->IsCurrent());
1722
1723 if (!selected_candidate_pair_)
1724 return 0;
1725
1726 int transport_overhead_per_packet = 0;
1727
1728 constexpr int kIpv4Overhaed = 20;
1729 constexpr int kIpv6Overhaed = 40;
1730 transport_overhead_per_packet +=
1731 selected_candidate_pair_->local_candidate().address().family() == AF_INET
1732 ? kIpv4Overhaed
1733 : kIpv6Overhaed;
1734
1735 constexpr int kUdpOverhaed = 8;
1736 constexpr int kTcpOverhaed = 20;
1737 transport_overhead_per_packet +=
1738 selected_candidate_pair_->local_candidate().protocol() ==
1739 TCP_PROTOCOL_NAME
1740 ? kTcpOverhaed
1741 : kUdpOverhaed;
1742
1743 if (secure()) {
1744 int srtp_overhead = 0;
1745 if (srtp_filter_.GetSrtpOverhead(&srtp_overhead))
1746 transport_overhead_per_packet += srtp_overhead;
1747 }
1748
1749 return transport_overhead_per_packet;
1750}
1751
1752void BaseChannel::UpdateTransportOverhead() {
1753 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1754 if (transport_overhead_per_packet)
1755 invoker_.AsyncInvoke<void>(
1756 RTC_FROM_HERE, worker_thread_,
1757 Bind(&MediaChannel::OnTransportOverheadChanged, media_channel_,
1758 transport_overhead_per_packet));
1759}
1760
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001761void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762 // Render incoming data if we're the active call, and we have the local
1763 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001764 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001765 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766
1767 // Send outgoing data if we're the active call, we have the remote content,
1768 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001769 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001770 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771
1772 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1773}
1774
1775const ContentInfo* VoiceChannel::GetFirstContent(
1776 const SessionDescription* sdesc) {
1777 return GetFirstAudioContent(sdesc);
1778}
1779
1780bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001781 ContentAction action,
1782 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001783 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001784 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 LOG(LS_INFO) << "Setting local voice description";
1786
1787 const AudioContentDescription* audio =
1788 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001789 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001790 if (!audio) {
1791 SafeSetError("Can't find audio content in local description.", error_desc);
1792 return false;
1793 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001795 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001796 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 }
1798
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001799 AudioRecvParameters recv_params = last_recv_params_;
1800 RtpParametersFromMediaDescription(audio, &recv_params);
1801 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001802 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001803 error_desc);
1804 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001806 for (const AudioCodec& codec : audio->codecs()) {
1807 bundle_filter()->AddPayloadType(codec.id);
1808 }
1809 last_recv_params_ = recv_params;
1810
1811 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1812 // only give it to the media channel once we have a remote
1813 // description too (without a remote description, we won't be able
1814 // to send them anyway).
1815 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1816 SafeSetError("Failed to set local audio description streams.", error_desc);
1817 return false;
1818 }
1819
1820 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001821 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001822 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823}
1824
1825bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001826 ContentAction action,
1827 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001828 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001829 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 LOG(LS_INFO) << "Setting remote voice description";
1831
1832 const AudioContentDescription* audio =
1833 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001834 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001835 if (!audio) {
1836 SafeSetError("Can't find audio content in remote description.", error_desc);
1837 return false;
1838 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001840 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001841 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 }
1843
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001844 AudioSendParameters send_params = last_send_params_;
1845 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001846 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001847 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001848 }
skvladdc1c62c2016-03-16 19:07:43 -07001849
1850 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1851 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001852 SafeSetError("Failed to set remote audio description send parameters.",
1853 error_desc);
1854 return false;
1855 }
1856 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001858 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1859 // and only give it to the media channel once we have a local
1860 // description too (without a local description, we won't be able to
1861 // recv them anyway).
1862 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1863 SafeSetError("Failed to set remote audio description streams.", error_desc);
1864 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 }
1866
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001867 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001868 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001869 }
1870
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001871 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001872 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001873 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874}
1875
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876void VoiceChannel::HandleEarlyMediaTimeout() {
1877 // This occurs on the main thread, not the worker thread.
1878 if (!received_media_) {
1879 LOG(LS_INFO) << "No early media received before timeout";
1880 SignalEarlyMediaTimeout(this);
1881 }
1882}
1883
Peter Boström0c4e06b2015-10-07 12:23:21 +02001884bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1885 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001886 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 if (!enabled()) {
1888 return false;
1889 }
solenberg1d63dd02015-12-02 12:35:09 -08001890 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891}
1892
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001893void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 case MSG_EARLYMEDIATIMEOUT:
1896 HandleEarlyMediaTimeout();
1897 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898 case MSG_CHANNEL_ERROR: {
1899 VoiceChannelErrorMessageData* data =
1900 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901 delete data;
1902 break;
1903 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 default:
1905 BaseChannel::OnMessage(pmsg);
1906 break;
1907 }
1908}
1909
1910void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001911 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001912 SignalConnectionMonitor(this, infos);
1913}
1914
1915void VoiceChannel::OnMediaMonitorUpdate(
1916 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001917 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 SignalMediaMonitor(this, info);
1919}
1920
1921void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1922 const AudioInfo& info) {
1923 SignalAudioMonitor(this, info);
1924}
1925
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001926void VoiceChannel::GetSrtpCryptoSuites_n(
1927 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07001928 GetSupportedAudioCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929}
1930
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001931VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1932 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001933 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 VideoMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001936 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001937 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001938 : BaseChannel(worker_thread,
1939 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001940 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001941 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001942 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001943 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001944 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001947 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948 StopMediaMonitor();
1949 // this can't be done in the base class, since it calls a virtual
1950 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001951
1952 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953}
1954
nisse08582ff2016-02-04 01:24:52 -08001955bool VideoChannel::SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -08001956 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
nisse08582ff2016-02-04 01:24:52 -08001957 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001958 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001959 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 return true;
1961}
1962
deadbeef5a4a75a2016-06-02 16:23:38 -07001963bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001964 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001965 bool mute,
1966 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -08001967 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001968 return InvokeOnWorker(RTC_FROM_HERE,
1969 Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
deadbeef5a4a75a2016-06-02 16:23:38 -07001970 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001971}
1972
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001973webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001974 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001975 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001976}
1977
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001978webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1979 uint32_t ssrc) const {
1980 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001981}
1982
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001983bool VideoChannel::SetRtpSendParameters(
1984 uint32_t ssrc,
1985 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001986 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001987 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001988 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001989}
1990
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001991bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1992 webrtc::RtpParameters parameters) {
1993 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1994}
1995
1996webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1997 uint32_t ssrc) const {
1998 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001999 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002000 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
2001}
2002
2003webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
2004 uint32_t ssrc) const {
2005 return media_channel()->GetRtpReceiveParameters(ssrc);
2006}
2007
2008bool VideoChannel::SetRtpReceiveParameters(
2009 uint32_t ssrc,
2010 const webrtc::RtpParameters& parameters) {
2011 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002012 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002013 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
2014}
2015
2016bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
2017 webrtc::RtpParameters parameters) {
2018 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07002019}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002020
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002021void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022 // Send outgoing data if we're the active call, we have the remote content,
2023 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002024 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 if (!media_channel()->SetSend(send)) {
2026 LOG(LS_ERROR) << "Failed to SetSend on video channel";
2027 // TODO(gangji): Report error back to server.
2028 }
2029
Peter Boström34fbfff2015-09-24 19:20:30 +02002030 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031}
2032
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00002033bool VideoChannel::GetStats(VideoMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002034 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
2035 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036}
2037
2038void VideoChannel::StartMediaMonitor(int cms) {
2039 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002040 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002041 media_monitor_->SignalUpdate.connect(
2042 this, &VideoChannel::OnMediaMonitorUpdate);
2043 media_monitor_->Start(cms);
2044}
2045
2046void VideoChannel::StopMediaMonitor() {
2047 if (media_monitor_) {
2048 media_monitor_->Stop();
2049 media_monitor_.reset();
2050 }
2051}
2052
2053const ContentInfo* VideoChannel::GetFirstContent(
2054 const SessionDescription* sdesc) {
2055 return GetFirstVideoContent(sdesc);
2056}
2057
2058bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002059 ContentAction action,
2060 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002061 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002062 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063 LOG(LS_INFO) << "Setting local video description";
2064
2065 const VideoContentDescription* video =
2066 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002067 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002068 if (!video) {
2069 SafeSetError("Can't find video content in local description.", error_desc);
2070 return false;
2071 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002073 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002074 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002075 }
2076
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002077 VideoRecvParameters recv_params = last_recv_params_;
2078 RtpParametersFromMediaDescription(video, &recv_params);
2079 if (!media_channel()->SetRecvParameters(recv_params)) {
2080 SafeSetError("Failed to set local video description recv parameters.",
2081 error_desc);
2082 return false;
2083 }
2084 for (const VideoCodec& codec : video->codecs()) {
2085 bundle_filter()->AddPayloadType(codec.id);
2086 }
2087 last_recv_params_ = recv_params;
2088
2089 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2090 // only give it to the media channel once we have a remote
2091 // description too (without a remote description, we won't be able
2092 // to send them anyway).
2093 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2094 SafeSetError("Failed to set local video description streams.", error_desc);
2095 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 }
2097
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002098 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002099 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002100 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002101}
2102
2103bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002104 ContentAction action,
2105 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002106 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002107 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108 LOG(LS_INFO) << "Setting remote video description";
2109
2110 const VideoContentDescription* video =
2111 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002112 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002113 if (!video) {
2114 SafeSetError("Can't find video content in remote description.", error_desc);
2115 return false;
2116 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002118 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002119 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 }
2121
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002122 VideoSendParameters send_params = last_send_params_;
2123 RtpSendParametersFromMediaDescription(video, &send_params);
2124 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002125 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002126 }
skvladdc1c62c2016-03-16 19:07:43 -07002127
2128 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2129
2130 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002131 SafeSetError("Failed to set remote video description send parameters.",
2132 error_desc);
2133 return false;
2134 }
2135 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002137 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2138 // and only give it to the media channel once we have a local
2139 // description too (without a local description, we won't be able to
2140 // recv them anyway).
2141 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2142 SafeSetError("Failed to set remote video description streams.", error_desc);
2143 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144 }
2145
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002146 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002147 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002149
2150 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002151 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002152 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153}
2154
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002155void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 case MSG_CHANNEL_ERROR: {
2158 const VideoChannelErrorMessageData* data =
2159 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 delete data;
2161 break;
2162 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163 default:
2164 BaseChannel::OnMessage(pmsg);
2165 break;
2166 }
2167}
2168
2169void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002170 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171 SignalConnectionMonitor(this, infos);
2172}
2173
2174// TODO(pthatcher): Look into removing duplicate code between
2175// audio, video, and data, perhaps by using templates.
2176void VideoChannel::OnMediaMonitorUpdate(
2177 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002178 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179 SignalMediaMonitor(this, info);
2180}
2181
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002182void VideoChannel::GetSrtpCryptoSuites_n(
2183 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002184 GetSupportedVideoCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185}
2186
deadbeef953c2ce2017-01-09 14:53:41 -08002187RtpDataChannel::RtpDataChannel(rtc::Thread* worker_thread,
2188 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002189 rtc::Thread* signaling_thread,
deadbeef953c2ce2017-01-09 14:53:41 -08002190 DataMediaChannel* media_channel,
deadbeef953c2ce2017-01-09 14:53:41 -08002191 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08002192 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002193 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002194 : BaseChannel(worker_thread,
2195 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002196 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002197 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002198 content_name,
deadbeefac22f702017-01-12 21:59:29 -08002199 rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002200 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201
deadbeef953c2ce2017-01-09 14:53:41 -08002202RtpDataChannel::~RtpDataChannel() {
2203 TRACE_EVENT0("webrtc", "RtpDataChannel::~RtpDataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204 StopMediaMonitor();
2205 // this can't be done in the base class, since it calls a virtual
2206 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002207
2208 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209}
2210
deadbeeff5346592017-01-24 21:51:21 -08002211bool RtpDataChannel::Init_w(
2212 DtlsTransportInternal* rtp_dtls_transport,
2213 DtlsTransportInternal* rtcp_dtls_transport,
2214 rtc::PacketTransportInterface* rtp_packet_transport,
2215 rtc::PacketTransportInterface* rtcp_packet_transport) {
2216 if (!BaseChannel::Init_w(rtp_dtls_transport, rtcp_dtls_transport,
2217 rtp_packet_transport, rtcp_packet_transport)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 return false;
2219 }
deadbeef953c2ce2017-01-09 14:53:41 -08002220 media_channel()->SignalDataReceived.connect(this,
2221 &RtpDataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002222 media_channel()->SignalReadyToSend.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08002223 this, &RtpDataChannel::OnDataChannelReadyToSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 return true;
2225}
2226
deadbeef953c2ce2017-01-09 14:53:41 -08002227bool RtpDataChannel::SendData(const SendDataParams& params,
2228 const rtc::CopyOnWriteBuffer& payload,
2229 SendDataResult* result) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002230 return InvokeOnWorker(
2231 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2232 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233}
2234
deadbeef953c2ce2017-01-09 14:53:41 -08002235const ContentInfo* RtpDataChannel::GetFirstContent(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 const SessionDescription* sdesc) {
2237 return GetFirstDataContent(sdesc);
2238}
2239
deadbeef953c2ce2017-01-09 14:53:41 -08002240bool RtpDataChannel::CheckDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002241 const DataContentDescription* content,
2242 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002243 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2244 (content->protocol() == kMediaProtocolDtlsSctp));
deadbeef953c2ce2017-01-09 14:53:41 -08002245 // It's been set before, but doesn't match. That's bad.
2246 if (is_sctp) {
2247 SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.",
2248 error_desc);
2249 return false;
2250 }
2251 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252}
2253
deadbeef953c2ce2017-01-09 14:53:41 -08002254bool RtpDataChannel::SetLocalContent_w(const MediaContentDescription* content,
2255 ContentAction action,
2256 std::string* error_desc) {
2257 TRACE_EVENT0("webrtc", "RtpDataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002258 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259 LOG(LS_INFO) << "Setting local data description";
2260
2261 const DataContentDescription* data =
2262 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002263 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002264 if (!data) {
2265 SafeSetError("Can't find data content in local description.", error_desc);
2266 return false;
2267 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002268
deadbeef953c2ce2017-01-09 14:53:41 -08002269 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270 return false;
2271 }
2272
deadbeef953c2ce2017-01-09 14:53:41 -08002273 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
2274 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275 }
2276
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002277 DataRecvParameters recv_params = last_recv_params_;
2278 RtpParametersFromMediaDescription(data, &recv_params);
2279 if (!media_channel()->SetRecvParameters(recv_params)) {
2280 SafeSetError("Failed to set remote data description recv parameters.",
2281 error_desc);
2282 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002283 }
deadbeef953c2ce2017-01-09 14:53:41 -08002284 for (const DataCodec& codec : data->codecs()) {
2285 bundle_filter()->AddPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002286 }
2287 last_recv_params_ = recv_params;
2288
2289 // TODO(pthatcher): Move local streams into DataSendParameters, and
2290 // only give it to the media channel once we have a remote
2291 // description too (without a remote description, we won't be able
2292 // to send them anyway).
2293 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2294 SafeSetError("Failed to set local data description streams.", error_desc);
2295 return false;
2296 }
2297
2298 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002299 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002300 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301}
2302
deadbeef953c2ce2017-01-09 14:53:41 -08002303bool RtpDataChannel::SetRemoteContent_w(const MediaContentDescription* content,
2304 ContentAction action,
2305 std::string* error_desc) {
2306 TRACE_EVENT0("webrtc", "RtpDataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002307 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308
2309 const DataContentDescription* data =
2310 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002311 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002312 if (!data) {
2313 SafeSetError("Can't find data content in remote description.", error_desc);
2314 return false;
2315 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002316
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002317 // If the remote data doesn't have codecs and isn't an update, it
2318 // must be empty, so ignore it.
2319 if (!data->has_codecs() && action != CA_UPDATE) {
2320 return true;
2321 }
2322
deadbeef953c2ce2017-01-09 14:53:41 -08002323 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 return false;
2325 }
2326
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002327 LOG(LS_INFO) << "Setting remote data description";
deadbeef953c2ce2017-01-09 14:53:41 -08002328 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002329 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330 }
2331
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002332 DataSendParameters send_params = last_send_params_;
2333 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2334 if (!media_channel()->SetSendParameters(send_params)) {
2335 SafeSetError("Failed to set remote data description send parameters.",
2336 error_desc);
2337 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002338 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002339 last_send_params_ = send_params;
2340
2341 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2342 // and only give it to the media channel once we have a local
2343 // description too (without a local description, we won't be able to
2344 // recv them anyway).
2345 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2346 SafeSetError("Failed to set remote data description streams.",
2347 error_desc);
2348 return false;
2349 }
2350
2351 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002352 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002353 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354}
2355
deadbeef953c2ce2017-01-09 14:53:41 -08002356void RtpDataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002357 // Render incoming data if we're the active call, and we have the local
2358 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002359 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 if (!media_channel()->SetReceive(recv)) {
2361 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2362 }
2363
2364 // Send outgoing data if we're the active call, we have the remote content,
2365 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002366 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367 if (!media_channel()->SetSend(send)) {
2368 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2369 }
2370
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002371 // Trigger SignalReadyToSendData asynchronously.
2372 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002373
2374 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2375}
2376
deadbeef953c2ce2017-01-09 14:53:41 -08002377void RtpDataChannel::OnMessage(rtc::Message* pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002378 switch (pmsg->message_id) {
2379 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002380 DataChannelReadyToSendMessageData* data =
2381 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002382 ready_to_send_data_ = data->data();
2383 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384 delete data;
2385 break;
2386 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002387 case MSG_DATARECEIVED: {
2388 DataReceivedMessageData* data =
2389 static_cast<DataReceivedMessageData*>(pmsg->pdata);
deadbeef953c2ce2017-01-09 14:53:41 -08002390 SignalDataReceived(data->params, data->payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002391 delete data;
2392 break;
2393 }
2394 case MSG_CHANNEL_ERROR: {
2395 const DataChannelErrorMessageData* data =
2396 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002397 delete data;
2398 break;
2399 }
2400 default:
2401 BaseChannel::OnMessage(pmsg);
2402 break;
2403 }
2404}
2405
deadbeef953c2ce2017-01-09 14:53:41 -08002406void RtpDataChannel::OnConnectionMonitorUpdate(
2407 ConnectionMonitor* monitor,
2408 const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002409 SignalConnectionMonitor(this, infos);
2410}
2411
deadbeef953c2ce2017-01-09 14:53:41 -08002412void RtpDataChannel::StartMediaMonitor(int cms) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002414 rtc::Thread::Current()));
deadbeef953c2ce2017-01-09 14:53:41 -08002415 media_monitor_->SignalUpdate.connect(this,
2416 &RtpDataChannel::OnMediaMonitorUpdate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002417 media_monitor_->Start(cms);
2418}
2419
deadbeef953c2ce2017-01-09 14:53:41 -08002420void RtpDataChannel::StopMediaMonitor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002421 if (media_monitor_) {
2422 media_monitor_->Stop();
2423 media_monitor_->SignalUpdate.disconnect(this);
2424 media_monitor_.reset();
2425 }
2426}
2427
deadbeef953c2ce2017-01-09 14:53:41 -08002428void RtpDataChannel::OnMediaMonitorUpdate(DataMediaChannel* media_channel,
2429 const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002430 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002431 SignalMediaMonitor(this, info);
2432}
2433
deadbeef953c2ce2017-01-09 14:53:41 -08002434void RtpDataChannel::OnDataReceived(const ReceiveDataParams& params,
2435 const char* data,
2436 size_t len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002437 DataReceivedMessageData* msg = new DataReceivedMessageData(
2438 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002439 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002440}
2441
deadbeef953c2ce2017-01-09 14:53:41 -08002442void RtpDataChannel::OnDataChannelError(uint32_t ssrc,
2443 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002444 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2445 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002446 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447}
2448
deadbeef953c2ce2017-01-09 14:53:41 -08002449void RtpDataChannel::OnDataChannelReadyToSend(bool writable) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002450 // This is usded for congestion control to indicate that the stream is ready
2451 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2452 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002453 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002454 new DataChannelReadyToSendMessageData(writable));
2455}
2456
deadbeef953c2ce2017-01-09 14:53:41 -08002457void RtpDataChannel::GetSrtpCryptoSuites_n(
2458 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002459 GetSupportedDataCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002460}
2461
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002462} // namespace cricket