blob: a08d7ae35091b7b78d2e29a758b5ff5d50407463 [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"
hbos8d609f62017-04-10 07:39:05 -070026#include "webrtc/media/engine/webrtcvoiceengine.h"
deadbeef5bd5ca32017-02-10 11:31:50 -080027#include "webrtc/p2p/base/packettransportinternal.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010028#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029
30namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000031using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000032
deadbeef2d110be2016-01-13 12:00:26 -080033namespace {
kwiberg31022942016-03-11 14:18:21 -080034// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080035bool SetRawAudioSink_w(VoiceMediaChannel* channel,
36 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080037 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
38 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080039 return true;
40}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020041
42struct SendPacketMessageData : public rtc::MessageData {
43 rtc::CopyOnWriteBuffer packet;
44 rtc::PacketOptions options;
45};
46
isheriff6f8d6862016-05-26 11:24:55 -070047#if defined(ENABLE_EXTERNAL_AUTH)
48// Returns the named header extension if found among all extensions,
49// nullptr otherwise.
50const webrtc::RtpExtension* FindHeaderExtension(
51 const std::vector<webrtc::RtpExtension>& extensions,
52 const std::string& uri) {
53 for (const auto& extension : extensions) {
54 if (extension.uri == uri)
55 return &extension;
56 }
57 return nullptr;
58}
59#endif
60
deadbeef2d110be2016-01-13 12:00:26 -080061} // namespace
62
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000064 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020065 MSG_SEND_RTP_PACKET,
66 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 MSG_FIRSTPACKETRECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071};
72
73// Value specified in RFC 5764.
74static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
75
76static const int kAgcMinus10db = -10;
77
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000078static void SafeSetError(const std::string& message, std::string* error_desc) {
79 if (error_desc) {
80 *error_desc = message;
81 }
82}
83
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 : ssrc(in_ssrc), error(in_error) {}
88 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 VoiceMediaChannel::Error error;
90};
91
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000092struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020093 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020095 : ssrc(in_ssrc), error(in_error) {}
96 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 VideoMediaChannel::Error error;
98};
99
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000100struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 : ssrc(in_ssrc), error(in_error) {}
104 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 DataMediaChannel::Error error;
106};
107
jbaucheec21bd2016-03-20 06:15:43 -0700108static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 // Check the packet size. We could check the header too if needed.
zstein3dcf0e92017-06-01 13:22:42 -0700110 return packet && IsValidRtpRtcpPacketSize(rtcp, packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111}
112
113static bool IsReceiveContentDirection(MediaContentDirection direction) {
114 return direction == MD_SENDRECV || direction == MD_RECVONLY;
115}
116
117static bool IsSendContentDirection(MediaContentDirection direction) {
118 return direction == MD_SENDRECV || direction == MD_SENDONLY;
119}
120
121static const MediaContentDescription* GetContentDescription(
122 const ContentInfo* cinfo) {
123 if (cinfo == NULL)
124 return NULL;
125 return static_cast<const MediaContentDescription*>(cinfo->description);
126}
127
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700128template <class Codec>
129void RtpParametersFromMediaDescription(
130 const MediaContentDescriptionImpl<Codec>* desc,
131 RtpParameters<Codec>* params) {
132 // TODO(pthatcher): Remove this once we're sure no one will give us
133 // a description without codecs (currently a CA_UPDATE with just
134 // streams can).
135 if (desc->has_codecs()) {
136 params->codecs = desc->codecs();
137 }
138 // TODO(pthatcher): See if we really need
139 // rtp_header_extensions_set() and remove it if we don't.
140 if (desc->rtp_header_extensions_set()) {
141 params->extensions = desc->rtp_header_extensions();
142 }
deadbeef13871492015-12-09 12:37:51 -0800143 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700144}
145
nisse05103312016-03-16 02:22:50 -0700146template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700147void RtpSendParametersFromMediaDescription(
148 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700149 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700150 RtpParametersFromMediaDescription(desc, send_params);
151 send_params->max_bandwidth_bps = desc->bandwidth();
152}
153
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200154BaseChannel::BaseChannel(rtc::Thread* worker_thread,
155 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -0800156 rtc::Thread* signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700157 MediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -0700158 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -0800159 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -0800160 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200161 : worker_thread_(worker_thread),
162 network_thread_(network_thread),
zhihuangf5b251b2017-01-12 19:37:48 -0800163 signaling_thread_(signaling_thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 content_name_(content_name),
zstein56162b92017-04-24 16:54:35 -0700165 rtcp_mux_required_(rtcp_mux_required),
zsteind48dbda2017-04-04 19:45:57 -0700166 rtp_transport_(rtcp_mux_required),
deadbeef7af91dd2016-12-13 11:29:11 -0800167 srtp_required_(srtp_required),
michaelt79e05882016-11-08 02:50:09 -0800168 media_channel_(media_channel),
169 selected_candidate_pair_(nullptr) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700170 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
jbauchdfcab722017-03-06 00:14:10 -0800171#if defined(ENABLE_EXTERNAL_AUTH)
172 srtp_filter_.EnableExternalAuth();
173#endif
zstein56162b92017-04-24 16:54:35 -0700174 rtp_transport_.SignalReadyToSend.connect(
175 this, &BaseChannel::OnTransportReadyToSend);
zstein3dcf0e92017-06-01 13:22:42 -0700176 // TODO(zstein): RtpTransport::SignalPacketReceived will probably be replaced
177 // with a callback interface later so that the demuxer can select which
178 // channel to signal.
179 rtp_transport_.SignalPacketReceived.connect(this,
180 &BaseChannel::OnPacketReceived);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 LOG(LS_INFO) << "Created channel for " << content_name;
182}
183
184BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800185 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700186 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000187 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200189 // Eats any outstanding messages or packets.
190 worker_thread_->Clear(&invoker_);
191 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 // We must destroy the media channel before the transport channel, otherwise
193 // the media channel may try to send on the dead transport channel. NULLing
194 // is not an effective strategy since the sends will come on another thread.
195 delete media_channel_;
zhihuangf5b251b2017-01-12 19:37:48 -0800196 LOG(LS_INFO) << "Destroyed channel: " << content_name_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200197}
198
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200199void BaseChannel::DisconnectTransportChannels_n() {
200 // Send any outstanding RTCP packets.
201 FlushRtcpMessages_n();
202
203 // Stop signals from transport channels, but keep them alive because
204 // media_channel may use them from a different thread.
zhihuangb2cdd932017-01-19 16:54:25 -0800205 if (rtp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800206 DisconnectFromDtlsTransport(rtp_dtls_transport_);
zsteind48dbda2017-04-04 19:45:57 -0700207 } else if (rtp_transport_.rtp_packet_transport()) {
208 DisconnectFromPacketTransport(rtp_transport_.rtp_packet_transport());
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200209 }
zhihuangb2cdd932017-01-19 16:54:25 -0800210 if (rtcp_dtls_transport_) {
deadbeeff5346592017-01-24 21:51:21 -0800211 DisconnectFromDtlsTransport(rtcp_dtls_transport_);
zsteind48dbda2017-04-04 19:45:57 -0700212 } else if (rtp_transport_.rtcp_packet_transport()) {
213 DisconnectFromPacketTransport(rtp_transport_.rtcp_packet_transport());
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200214 }
215
zstein3dcf0e92017-06-01 13:22:42 -0700216 rtp_transport_.SetRtpPacketTransport(nullptr);
217 rtp_transport_.SetRtcpPacketTransport(nullptr);
218
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200219 // Clear pending read packets/messages.
220 network_thread_->Clear(&invoker_);
221 network_thread_->Clear(this);
222}
223
zhihuangb2cdd932017-01-19 16:54:25 -0800224bool BaseChannel::Init_w(DtlsTransportInternal* rtp_dtls_transport,
deadbeeff5346592017-01-24 21:51:21 -0800225 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800226 rtc::PacketTransportInternal* rtp_packet_transport,
227 rtc::PacketTransportInternal* rtcp_packet_transport) {
skvlad6c87a672016-05-17 17:49:52 -0700228 if (!network_thread_->Invoke<bool>(
zhihuangb2cdd932017-01-19 16:54:25 -0800229 RTC_FROM_HERE, Bind(&BaseChannel::InitNetwork_n, this,
deadbeeff5346592017-01-24 21:51:21 -0800230 rtp_dtls_transport, rtcp_dtls_transport,
231 rtp_packet_transport, rtcp_packet_transport))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000232 return false;
233 }
deadbeeff5346592017-01-24 21:51:21 -0800234 // Both RTP and RTCP channels should be set, we can call SetInterface on
235 // the media channel and it can set network options.
236 RTC_DCHECK_RUN_ON(worker_thread_);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000237 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238 return true;
239}
240
deadbeeff5346592017-01-24 21:51:21 -0800241bool BaseChannel::InitNetwork_n(
242 DtlsTransportInternal* rtp_dtls_transport,
243 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800244 rtc::PacketTransportInternal* rtp_packet_transport,
245 rtc::PacketTransportInternal* rtcp_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200246 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800247 SetTransports_n(rtp_dtls_transport, rtcp_dtls_transport, rtp_packet_transport,
248 rtcp_packet_transport);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200249
zstein56162b92017-04-24 16:54:35 -0700250 if (rtcp_mux_required_) {
deadbeefac22f702017-01-12 21:59:29 -0800251 rtcp_mux_filter_.SetActive();
252 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200253 return true;
254}
255
wu@webrtc.org78187522013-10-07 23:32:02 +0000256void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200257 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000258 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200259 // Packets arrive on the network thread, processing packets calls virtual
260 // functions, so need to stop this process in Deinit that is called in
261 // derived classes destructor.
262 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700263 RTC_FROM_HERE, Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000264}
265
zhihuangb2cdd932017-01-19 16:54:25 -0800266void BaseChannel::SetTransports(DtlsTransportInternal* rtp_dtls_transport,
267 DtlsTransportInternal* rtcp_dtls_transport) {
deadbeeff5346592017-01-24 21:51:21 -0800268 network_thread_->Invoke<void>(
269 RTC_FROM_HERE,
270 Bind(&BaseChannel::SetTransports_n, this, rtp_dtls_transport,
271 rtcp_dtls_transport, rtp_dtls_transport, rtcp_dtls_transport));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000272}
273
deadbeeff5346592017-01-24 21:51:21 -0800274void BaseChannel::SetTransports(
deadbeef5bd5ca32017-02-10 11:31:50 -0800275 rtc::PacketTransportInternal* rtp_packet_transport,
276 rtc::PacketTransportInternal* rtcp_packet_transport) {
deadbeeff5346592017-01-24 21:51:21 -0800277 network_thread_->Invoke<void>(
278 RTC_FROM_HERE, Bind(&BaseChannel::SetTransports_n, this, nullptr, nullptr,
279 rtp_packet_transport, rtcp_packet_transport));
280}
zhihuangf5b251b2017-01-12 19:37:48 -0800281
deadbeeff5346592017-01-24 21:51:21 -0800282void BaseChannel::SetTransports_n(
283 DtlsTransportInternal* rtp_dtls_transport,
284 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800285 rtc::PacketTransportInternal* rtp_packet_transport,
286 rtc::PacketTransportInternal* rtcp_packet_transport) {
deadbeeff5346592017-01-24 21:51:21 -0800287 RTC_DCHECK(network_thread_->IsCurrent());
288 // Validate some assertions about the input.
289 RTC_DCHECK(rtp_packet_transport);
290 RTC_DCHECK_EQ(NeedsRtcpTransport(), rtcp_packet_transport != nullptr);
291 if (rtp_dtls_transport || rtcp_dtls_transport) {
292 // DTLS/non-DTLS pointers should be to the same object.
293 RTC_DCHECK(rtp_dtls_transport == rtp_packet_transport);
294 RTC_DCHECK(rtcp_dtls_transport == rtcp_packet_transport);
295 // Can't go from non-DTLS to DTLS.
zsteind48dbda2017-04-04 19:45:57 -0700296 RTC_DCHECK(!rtp_transport_.rtp_packet_transport() || rtp_dtls_transport_);
deadbeeff5346592017-01-24 21:51:21 -0800297 } else {
298 // Can't go from DTLS to non-DTLS.
299 RTC_DCHECK(!rtp_dtls_transport_);
300 }
301 // Transport names should be the same.
zhihuangb2cdd932017-01-19 16:54:25 -0800302 if (rtp_dtls_transport && rtcp_dtls_transport) {
303 RTC_DCHECK(rtp_dtls_transport->transport_name() ==
304 rtcp_dtls_transport->transport_name());
zhihuangb2cdd932017-01-19 16:54:25 -0800305 }
deadbeeff5346592017-01-24 21:51:21 -0800306 std::string debug_name;
307 if (rtp_dtls_transport) {
308 transport_name_ = rtp_dtls_transport->transport_name();
309 debug_name = transport_name_;
310 } else {
311 debug_name = rtp_packet_transport->debug_name();
312 }
zsteind48dbda2017-04-04 19:45:57 -0700313 if (rtp_packet_transport == rtp_transport_.rtp_packet_transport()) {
deadbeeff5346592017-01-24 21:51:21 -0800314 // Nothing to do if transport isn't changing.
deadbeefbad5dad2017-01-17 18:32:35 -0800315 return;
deadbeefcbecd352015-09-23 11:50:27 -0700316 }
317
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800318 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
319 // changes and wait until the DTLS handshake is complete to set the newly
320 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200321 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800322 // Set |writable_| to false such that UpdateWritableState_w can set up
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700323 // DTLS-SRTP when |writable_| becomes true again.
guoweis46383312015-12-17 16:45:59 -0800324 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800325 srtp_filter_.ResetParams();
326 }
327
deadbeefac22f702017-01-12 21:59:29 -0800328 // If this BaseChannel doesn't require RTCP mux and we haven't fully
329 // negotiated RTCP mux, we need an RTCP transport.
deadbeeff5346592017-01-24 21:51:21 -0800330 if (rtcp_packet_transport) {
zhihuangf5b251b2017-01-12 19:37:48 -0800331 LOG(LS_INFO) << "Setting RTCP Transport for " << content_name() << " on "
deadbeeff5346592017-01-24 21:51:21 -0800332 << debug_name << " transport " << rtcp_packet_transport;
333 SetTransport_n(true, rtcp_dtls_transport, rtcp_packet_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000334 }
335
deadbeeff5346592017-01-24 21:51:21 -0800336 LOG(LS_INFO) << "Setting RTP Transport for " << content_name() << " on "
337 << debug_name << " transport " << rtp_packet_transport;
338 SetTransport_n(false, rtp_dtls_transport, rtp_packet_transport);
guoweis46383312015-12-17 16:45:59 -0800339
deadbeefcbecd352015-09-23 11:50:27 -0700340 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700341 // setting new transport channels.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200342 UpdateWritableState_n();
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000343}
344
deadbeeff5346592017-01-24 21:51:21 -0800345void BaseChannel::SetTransport_n(
346 bool rtcp,
347 DtlsTransportInternal* new_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -0800348 rtc::PacketTransportInternal* new_packet_transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200349 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800350 DtlsTransportInternal*& old_dtls_transport =
zhihuangb2cdd932017-01-19 16:54:25 -0800351 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
zsteind48dbda2017-04-04 19:45:57 -0700352 rtc::PacketTransportInternal* old_packet_transport =
353 rtcp ? rtp_transport_.rtcp_packet_transport()
354 : rtp_transport_.rtp_packet_transport();
zhihuangb2cdd932017-01-19 16:54:25 -0800355
deadbeeff5346592017-01-24 21:51:21 -0800356 if (!old_packet_transport && !new_packet_transport) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700357 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000358 return;
359 }
zhihuangb2cdd932017-01-19 16:54:25 -0800360
deadbeeff5346592017-01-24 21:51:21 -0800361 RTC_DCHECK(old_packet_transport != new_packet_transport);
362 if (old_dtls_transport) {
363 DisconnectFromDtlsTransport(old_dtls_transport);
364 } else if (old_packet_transport) {
365 DisconnectFromPacketTransport(old_packet_transport);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000366 }
367
zsteind48dbda2017-04-04 19:45:57 -0700368 if (rtcp) {
zstein56162b92017-04-24 16:54:35 -0700369 rtp_transport_.SetRtcpPacketTransport(new_packet_transport);
zsteind48dbda2017-04-04 19:45:57 -0700370 } else {
zstein56162b92017-04-24 16:54:35 -0700371 rtp_transport_.SetRtpPacketTransport(new_packet_transport);
zsteind48dbda2017-04-04 19:45:57 -0700372 }
deadbeeff5346592017-01-24 21:51:21 -0800373 old_dtls_transport = new_dtls_transport;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000374
deadbeeff5346592017-01-24 21:51:21 -0800375 // If there's no new transport, we're done after disconnecting from old one.
376 if (!new_packet_transport) {
377 return;
378 }
379
380 if (rtcp && new_dtls_transport) {
381 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
382 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
383 << "should never happen.";
384 }
zstein56162b92017-04-24 16:54:35 -0700385
deadbeeff5346592017-01-24 21:51:21 -0800386 if (new_dtls_transport) {
387 ConnectToDtlsTransport(new_dtls_transport);
388 } else {
389 ConnectToPacketTransport(new_packet_transport);
390 }
391 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
392 for (const auto& pair : socket_options) {
393 new_packet_transport->SetOption(pair.first, pair.second);
guoweis46383312015-12-17 16:45:59 -0800394 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000395}
396
deadbeeff5346592017-01-24 21:51:21 -0800397void BaseChannel::ConnectToDtlsTransport(DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200398 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000399
zstein56162b92017-04-24 16:54:35 -0700400 // TODO(zstein): de-dup with ConnectToPacketTransport
zhihuangb2cdd932017-01-19 16:54:25 -0800401 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
zhihuangb2cdd932017-01-19 16:54:25 -0800402 transport->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
403 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
404 transport->ice_transport()->SignalSelectedCandidatePairChanged.connect(
Honghai Zhangcc411c02016-03-29 17:27:21 -0700405 this, &BaseChannel::OnSelectedCandidatePairChanged);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000406}
407
deadbeeff5346592017-01-24 21:51:21 -0800408void BaseChannel::DisconnectFromDtlsTransport(
409 DtlsTransportInternal* transport) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200410 RTC_DCHECK(network_thread_->IsCurrent());
zhihuangb2cdd932017-01-19 16:54:25 -0800411 OnSelectedCandidatePairChanged(transport->ice_transport(), nullptr, -1,
412 false);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000413
zhihuangb2cdd932017-01-19 16:54:25 -0800414 transport->SignalWritableState.disconnect(this);
zhihuangb2cdd932017-01-19 16:54:25 -0800415 transport->SignalDtlsState.disconnect(this);
416 transport->SignalSentPacket.disconnect(this);
417 transport->ice_transport()->SignalSelectedCandidatePairChanged.disconnect(
418 this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000419}
420
deadbeeff5346592017-01-24 21:51:21 -0800421void BaseChannel::ConnectToPacketTransport(
deadbeef5bd5ca32017-02-10 11:31:50 -0800422 rtc::PacketTransportInternal* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800423 RTC_DCHECK_RUN_ON(network_thread_);
424 transport->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
deadbeeff5346592017-01-24 21:51:21 -0800425 transport->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
426}
427
428void BaseChannel::DisconnectFromPacketTransport(
deadbeef5bd5ca32017-02-10 11:31:50 -0800429 rtc::PacketTransportInternal* transport) {
deadbeeff5346592017-01-24 21:51:21 -0800430 RTC_DCHECK_RUN_ON(network_thread_);
431 transport->SignalWritableState.disconnect(this);
deadbeeff5346592017-01-24 21:51:21 -0800432 transport->SignalSentPacket.disconnect(this);
433}
434
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700436 worker_thread_->Invoke<void>(
437 RTC_FROM_HERE,
438 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
439 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440 return true;
441}
442
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443bool BaseChannel::AddRecvStream(const StreamParams& sp) {
charujaind72098a2017-06-01 08:54:47 -0700444 return InvokeOnWorker(RTC_FROM_HERE,
445 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446}
447
Peter Boström0c4e06b2015-10-07 12:23:21 +0200448bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
charujaind72098a2017-06-01 08:54:47 -0700449 return InvokeOnWorker(RTC_FROM_HERE,
450 Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451}
452
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000453bool BaseChannel::AddSendStream(const StreamParams& sp) {
charujaind72098a2017-06-01 08:54:47 -0700454 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700455 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000456}
457
Peter Boström0c4e06b2015-10-07 12:23:21 +0200458bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
charujaind72098a2017-06-01 08:54:47 -0700459 return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream,
460 media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000461}
462
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000464 ContentAction action,
465 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100466 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
charujaind72098a2017-06-01 08:54:47 -0700467 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w,
468 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469}
470
471bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000472 ContentAction action,
473 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100474 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
charujaind72098a2017-06-01 08:54:47 -0700475 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w,
476 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477}
478
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479void BaseChannel::StartConnectionMonitor(int cms) {
zhihuangb2cdd932017-01-19 16:54:25 -0800480 // We pass in the BaseChannel instead of the rtp_dtls_transport_
481 // because if the rtp_dtls_transport_ changes, the ConnectionMonitor
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000482 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200483 // We pass in the network thread because on that thread connection monitor
484 // will call BaseChannel::GetConnectionStats which must be called on the
485 // network thread.
486 connection_monitor_.reset(
487 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000488 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000490 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491}
492
493void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000494 if (connection_monitor_) {
495 connection_monitor_->Stop();
496 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497 }
498}
499
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000500bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200501 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800502 if (!rtp_dtls_transport_) {
503 return false;
504 }
zhihuangb2cdd932017-01-19 16:54:25 -0800505 return rtp_dtls_transport_->ice_transport()->GetStats(infos);
zhihuangf5b251b2017-01-12 19:37:48 -0800506}
507
508bool BaseChannel::NeedsRtcpTransport() {
deadbeefac22f702017-01-12 21:59:29 -0800509 // If this BaseChannel doesn't require RTCP mux and we haven't fully
510 // negotiated RTCP mux, we need an RTCP transport.
zstein56162b92017-04-24 16:54:35 -0700511 return !rtcp_mux_required_ && !rtcp_mux_filter_.IsFullyActive();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000512}
513
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700514bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 // Receive data if we are enabled and have local content,
516 return enabled() && IsReceiveContentDirection(local_content_direction_);
517}
518
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700519bool BaseChannel::IsReadyToSendMedia_w() const {
520 // Need to access some state updated on the network thread.
521 return network_thread_->Invoke<bool>(
522 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
523}
524
525bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 // Send outgoing data if we are enabled, have local and remote content,
527 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800528 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700530 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200531 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532}
533
jbaucheec21bd2016-03-20 06:15:43 -0700534bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700535 const rtc::PacketOptions& options) {
536 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537}
538
jbaucheec21bd2016-03-20 06:15:43 -0700539bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700540 const rtc::PacketOptions& options) {
541 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542}
543
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000544int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200546 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700547 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200548}
549
550int BaseChannel::SetOption_n(SocketType type,
551 rtc::Socket::Option opt,
552 int value) {
553 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef5bd5ca32017-02-10 11:31:50 -0800554 rtc::PacketTransportInternal* transport = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000556 case ST_RTP:
zsteind48dbda2017-04-04 19:45:57 -0700557 transport = rtp_transport_.rtp_packet_transport();
deadbeefcbecd352015-09-23 11:50:27 -0700558 socket_options_.push_back(
559 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000560 break;
561 case ST_RTCP:
zsteind48dbda2017-04-04 19:45:57 -0700562 transport = rtp_transport_.rtcp_packet_transport();
deadbeefcbecd352015-09-23 11:50:27 -0700563 rtcp_socket_options_.push_back(
564 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000565 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 }
deadbeeff5346592017-01-24 21:51:21 -0800567 return transport ? transport->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568}
569
deadbeef5bd5ca32017-02-10 11:31:50 -0800570void BaseChannel::OnWritableState(rtc::PacketTransportInternal* transport) {
zsteind48dbda2017-04-04 19:45:57 -0700571 RTC_DCHECK(transport == rtp_transport_.rtp_packet_transport() ||
572 transport == rtp_transport_.rtcp_packet_transport());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200573 RTC_DCHECK(network_thread_->IsCurrent());
574 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575}
576
zhihuangb2cdd932017-01-19 16:54:25 -0800577void BaseChannel::OnDtlsState(DtlsTransportInternal* transport,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800578 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200579 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800580 return;
581 }
582
583 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
584 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
zhihuangb2cdd932017-01-19 16:54:25 -0800585 // cover other scenarios like the whole transport is writable (not just this
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800586 // TransportChannel) or when TransportChannel is attached after DTLS is
587 // negotiated.
588 if (state != DTLS_TRANSPORT_CONNECTED) {
589 srtp_filter_.ResetParams();
590 }
591}
592
Honghai Zhangcc411c02016-03-29 17:27:21 -0700593void BaseChannel::OnSelectedCandidatePairChanged(
zhihuangb2cdd932017-01-19 16:54:25 -0800594 IceTransportInternal* ice_transport,
Honghai Zhang52dce732016-03-31 12:37:31 -0700595 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700596 int last_sent_packet_id,
597 bool ready_to_send) {
deadbeeff5346592017-01-24 21:51:21 -0800598 RTC_DCHECK((rtp_dtls_transport_ &&
599 ice_transport == rtp_dtls_transport_->ice_transport()) ||
600 (rtcp_dtls_transport_ &&
601 ice_transport == rtcp_dtls_transport_->ice_transport()));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200602 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800603 selected_candidate_pair_ = selected_candidate_pair;
zhihuangb2cdd932017-01-19 16:54:25 -0800604 std::string transport_name = ice_transport->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700605 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700606 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700607 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700608 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700609 selected_candidate_pair->remote_candidate().network_id(),
610 last_sent_packet_id);
michaelt79e05882016-11-08 02:50:09 -0800611
612 UpdateTransportOverhead();
Honghai Zhangcc411c02016-03-29 17:27:21 -0700613 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200614 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700615 RTC_FROM_HERE, worker_thread_,
616 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
617 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700618}
619
zstein56162b92017-04-24 16:54:35 -0700620void BaseChannel::OnTransportReadyToSend(bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200621 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700622 RTC_FROM_HERE, worker_thread_,
zstein56162b92017-04-24 16:54:35 -0700623 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624}
625
stefanc1aeaf02015-10-15 07:26:07 -0700626bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700627 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700628 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200629 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
630 // If the thread is not our network thread, we will post to our network
631 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 // synchronize access to all the pieces of the send path, including
633 // SRTP and the inner workings of the transport channels.
634 // The only downside is that we can't return a proper failure code if
635 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200636 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200638 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
639 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800640 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700641 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700642 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 return true;
644 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200645 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646
647 // Now that we are on the correct thread, ensure we have a place to send this
648 // packet before doing anything. (We might get RTCP packets that we don't
649 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
650 // transport.
zstein56162b92017-04-24 16:54:35 -0700651 if (!rtp_transport_.IsWritable(rtcp)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 return false;
653 }
654
655 // Protect ourselves against crazy data.
656 if (!ValidPacket(rtcp, packet)) {
657 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
zstein3dcf0e92017-06-01 13:22:42 -0700658 << RtpRtcpStringLiteral(rtcp)
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000659 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 return false;
661 }
662
stefanc1aeaf02015-10-15 07:26:07 -0700663 rtc::PacketOptions updated_options;
664 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 // Protect if needed.
666 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200667 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200669 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000670 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000672 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
673 // inside libsrtp for a RTP packet. A external HMAC module will be writing
674 // a fake HMAC value. This is ONLY done for a RTP packet.
675 // Socket layer will update rtp sendtime extension header if present in
676 // packet with current time before updating the HMAC.
677#if !defined(ENABLE_EXTERNAL_AUTH)
678 res = srtp_filter_.ProtectRtp(
679 data, len, static_cast<int>(packet->capacity()), &len);
680#else
jbauchd48f4882017-03-01 15:34:36 -0800681 if (!srtp_filter_.IsExternalAuthActive()) {
682 res = srtp_filter_.ProtectRtp(
683 data, len, static_cast<int>(packet->capacity()), &len);
684 } else {
685 updated_options.packet_time_params.rtp_sendtime_extension_id =
686 rtp_abs_sendtime_extn_id_;
687 res = srtp_filter_.ProtectRtp(
688 data, len, static_cast<int>(packet->capacity()), &len,
689 &updated_options.packet_time_params.srtp_packet_index);
690 // If protection succeeds, let's get auth params from srtp.
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000691 if (res) {
jbauchd48f4882017-03-01 15:34:36 -0800692 uint8_t* auth_key = NULL;
693 int key_len;
694 res = srtp_filter_.GetRtpAuthParams(
695 &auth_key, &key_len,
696 &updated_options.packet_time_params.srtp_auth_tag_len);
697 if (res) {
698 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
699 updated_options.packet_time_params.srtp_auth_key.assign(
700 auth_key, auth_key + key_len);
701 }
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000702 }
703 }
704#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 if (!res) {
706 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200707 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 GetRtpSeqNum(data, len, &seq_num);
709 GetRtpSsrc(data, len, &ssrc);
710 LOG(LS_ERROR) << "Failed to protect " << content_name_
711 << " RTP packet: size=" << len
712 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
713 return false;
714 }
715 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000716 res = srtp_filter_.ProtectRtcp(data, len,
717 static_cast<int>(packet->capacity()),
718 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 if (!res) {
720 int type = -1;
721 GetRtcpType(data, len, &type);
722 LOG(LS_ERROR) << "Failed to protect " << content_name_
723 << " RTCP packet: size=" << len << ", type=" << type;
724 return false;
725 }
726 }
727
728 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000729 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800730 } else if (srtp_required_) {
deadbeef8f425f92016-12-01 12:26:27 -0800731 // The audio/video engines may attempt to send RTCP packets as soon as the
732 // streams are created, so don't treat this as an error for RTCP.
733 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809
734 if (rtcp) {
735 return false;
736 }
737 // However, there shouldn't be any RTP packets sent before SRTP is set up
738 // (and SetSend(true) is called).
739 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
740 << " and crypto is required";
nisseeb4ca4e2017-01-12 02:24:27 -0800741 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 return false;
743 }
744
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200746 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
zstein56162b92017-04-24 16:54:35 -0700747 return rtp_transport_.SendPacket(rtcp, packet, updated_options, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748}
749
zstein3dcf0e92017-06-01 13:22:42 -0700750bool BaseChannel::HandlesPayloadType(int packet_type) const {
751 return rtp_transport_.HandlesPayloadType(packet_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752}
753
zstein3dcf0e92017-06-01 13:22:42 -0700754void BaseChannel::OnPacketReceived(bool rtcp,
755 rtc::CopyOnWriteBuffer& packet,
756 const rtc::PacketTime& packet_time) {
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000757 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700759 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 }
761
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762 // Unprotect the packet, if needed.
763 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200764 TRACE_EVENT0("webrtc", "SRTP Decode");
zstein3dcf0e92017-06-01 13:22:42 -0700765 char* data = packet.data<char>();
766 int len = static_cast<int>(packet.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000767 bool res;
768 if (!rtcp) {
769 res = srtp_filter_.UnprotectRtp(data, len, &len);
770 if (!res) {
771 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200772 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 GetRtpSeqNum(data, len, &seq_num);
774 GetRtpSsrc(data, len, &ssrc);
775 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
zstein3dcf0e92017-06-01 13:22:42 -0700776 << " RTP packet: size=" << len << ", seqnum=" << seq_num
777 << ", SSRC=" << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 return;
779 }
780 } else {
781 res = srtp_filter_.UnprotectRtcp(data, len, &len);
782 if (!res) {
783 int type = -1;
784 GetRtcpType(data, len, &type);
785 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
786 << " RTCP packet: size=" << len << ", type=" << type;
787 return;
788 }
789 }
790
zstein3dcf0e92017-06-01 13:22:42 -0700791 packet.SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800792 } else if (srtp_required_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 // Our session description indicates that SRTP is required, but we got a
794 // packet before our SRTP filter is active. This means either that
795 // a) we got SRTP packets before we received the SDES keys, in which case
796 // we can't decrypt it anyway, or
797 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
zhihuangb2cdd932017-01-19 16:54:25 -0800798 // transports, so we haven't yet extracted keys, even if DTLS did
799 // complete on the transport that the packets are being sent on. It's
800 // really good practice to wait for both RTP and RTCP to be good to go
801 // before sending media, to prevent weird failure modes, so it's fine
802 // for us to just eat packets here. This is all sidestepped if RTCP mux
803 // is used anyway.
zstein3dcf0e92017-06-01 13:22:42 -0700804 LOG(LS_WARNING) << "Can't process incoming " << RtpRtcpStringLiteral(rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 << " packet when SRTP is inactive and crypto is required";
806 return;
807 }
808
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200809 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700810 RTC_FROM_HERE, worker_thread_,
zstein3dcf0e92017-06-01 13:22:42 -0700811 Bind(&BaseChannel::ProcessPacket, this, rtcp, packet, packet_time));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200812}
813
zstein3dcf0e92017-06-01 13:22:42 -0700814void BaseChannel::ProcessPacket(bool rtcp,
815 const rtc::CopyOnWriteBuffer& packet,
816 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200817 RTC_DCHECK(worker_thread_->IsCurrent());
zstein3dcf0e92017-06-01 13:22:42 -0700818
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200819 // Need to copy variable because OnRtcpReceived/OnPacketReceived
820 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
821 rtc::CopyOnWriteBuffer data(packet);
822 if (rtcp) {
823 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200825 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 }
827}
828
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000829bool BaseChannel::PushdownLocalDescription(
830 const SessionDescription* local_desc, ContentAction action,
831 std::string* error_desc) {
832 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 const MediaContentDescription* content_desc =
834 GetContentDescription(content_info);
835 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000836 !SetLocalContent(content_desc, action, error_desc)) {
837 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
838 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000840 return true;
841}
842
843bool BaseChannel::PushdownRemoteDescription(
844 const SessionDescription* remote_desc, ContentAction action,
845 std::string* error_desc) {
846 const ContentInfo* content_info = GetFirstContent(remote_desc);
847 const MediaContentDescription* content_desc =
848 GetContentDescription(content_info);
849 if (content_desc && content_info && !content_info->rejected &&
850 !SetRemoteContent(content_desc, action, error_desc)) {
851 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
852 return false;
853 }
854 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855}
856
857void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700858 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 if (enabled_)
860 return;
861
862 LOG(LS_INFO) << "Channel enabled";
863 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700864 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865}
866
867void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700868 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 if (!enabled_)
870 return;
871
872 LOG(LS_INFO) << "Channel disabled";
873 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700874 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875}
876
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200877void BaseChannel::UpdateWritableState_n() {
zsteind48dbda2017-04-04 19:45:57 -0700878 rtc::PacketTransportInternal* rtp_packet_transport =
879 rtp_transport_.rtp_packet_transport();
880 rtc::PacketTransportInternal* rtcp_packet_transport =
881 rtp_transport_.rtcp_packet_transport();
882 if (rtp_packet_transport && rtp_packet_transport->writable() &&
883 (!rtcp_packet_transport || rtcp_packet_transport->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200884 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700885 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200886 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700887 }
888}
889
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200890void BaseChannel::ChannelWritable_n() {
891 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800892 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800894 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895
deadbeefcbecd352015-09-23 11:50:27 -0700896 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 << (was_ever_writable_ ? "" : " for the first time");
898
michaelt79e05882016-11-08 02:50:09 -0800899 if (selected_candidate_pair_)
900 LOG(LS_INFO)
901 << "Using "
902 << selected_candidate_pair_->local_candidate().ToSensitiveString()
903 << "->"
904 << selected_candidate_pair_->remote_candidate().ToSensitiveString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200907 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700909 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910}
911
deadbeef953c2ce2017-01-09 14:53:41 -0800912void BaseChannel::SignalDtlsSrtpSetupFailure_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200913 RTC_DCHECK(network_thread_->IsCurrent());
914 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700915 RTC_FROM_HERE, signaling_thread(),
deadbeef953c2ce2017-01-09 14:53:41 -0800916 Bind(&BaseChannel::SignalDtlsSrtpSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000917}
918
deadbeef953c2ce2017-01-09 14:53:41 -0800919void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700920 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
deadbeef953c2ce2017-01-09 14:53:41 -0800921 SignalDtlsSrtpSetupFailure(this, rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000922}
923
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200924bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
zhihuangb2cdd932017-01-19 16:54:25 -0800925 // Since DTLS is applied to all transports, checking RTP should be enough.
926 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927}
928
929// This function returns true if either DTLS-SRTP is not in use
930// *or* DTLS-SRTP is successfully set up.
zhihuangb2cdd932017-01-19 16:54:25 -0800931bool BaseChannel::SetupDtlsSrtp_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200932 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 bool ret = false;
934
zhihuangb2cdd932017-01-19 16:54:25 -0800935 DtlsTransportInternal* transport =
936 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
deadbeeff5346592017-01-24 21:51:21 -0800937 RTC_DCHECK(transport);
zhihuangb2cdd932017-01-19 16:54:25 -0800938 RTC_DCHECK(transport->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800940 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941
zhihuangb2cdd932017-01-19 16:54:25 -0800942 if (!transport->GetSrtpCryptoSuite(&selected_crypto_suite)) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800943 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 return false;
945 }
946
zhihuangb2cdd932017-01-19 16:54:25 -0800947 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " << content_name() << " "
zstein3dcf0e92017-06-01 13:22:42 -0700948 << RtpRtcpStringLiteral(rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949
jbauchcb560652016-08-04 05:20:32 -0700950 int key_len;
951 int salt_len;
952 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
953 &salt_len)) {
954 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
955 return false;
956 }
957
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -0700959 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960
961 // RFC 5705 exporter using the RFC 5764 parameters
zhihuangb2cdd932017-01-19 16:54:25 -0800962 if (!transport->ExportKeyingMaterial(kDtlsSrtpExporterLabel, NULL, 0, false,
963 &dtls_buffer[0], dtls_buffer.size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
nisseeb4ca4e2017-01-12 02:24:27 -0800965 RTC_NOTREACHED(); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 return false;
967 }
968
969 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -0700970 std::vector<unsigned char> client_write_key(key_len + salt_len);
971 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -0700973 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
974 offset += key_len;
975 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
976 offset += key_len;
977 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
978 offset += salt_len;
979 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980
981 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000982 rtc::SSLRole role;
zhihuangb2cdd932017-01-19 16:54:25 -0800983 if (!transport->GetSslRole(&role)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000984 LOG(LS_WARNING) << "GetSslRole failed";
985 return false;
986 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000988 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 send_key = &server_write_key;
990 recv_key = &client_write_key;
991 } else {
992 send_key = &client_write_key;
993 recv_key = &server_write_key;
994 }
995
zhihuangb2cdd932017-01-19 16:54:25 -0800996 if (rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800997 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
998 static_cast<int>(send_key->size()),
999 selected_crypto_suite, &(*recv_key)[0],
1000 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001002 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1003 static_cast<int>(send_key->size()),
1004 selected_crypto_suite, &(*recv_key)[0],
1005 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 }
1007
michaelt79e05882016-11-08 02:50:09 -08001008 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
michaelt79e05882016-11-08 02:50:09 -08001010 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 dtls_keyed_ = true;
michaelt79e05882016-11-08 02:50:09 -08001012 UpdateTransportOverhead();
1013 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 return ret;
1015}
1016
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001017void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001018 if (srtp_filter_.IsActive()) {
1019 return;
1020 }
1021
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001022 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001023 return;
1024 }
1025
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001026 if (!SetupDtlsSrtp_n(false)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001027 SignalDtlsSrtpSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001028 return;
1029 }
1030
zhihuangb2cdd932017-01-19 16:54:25 -08001031 if (rtcp_dtls_transport_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001032 if (!SetupDtlsSrtp_n(true)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001033 SignalDtlsSrtpSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001034 return;
1035 }
1036 }
1037}
1038
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001039void BaseChannel::ChannelNotWritable_n() {
1040 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 if (!writable_)
1042 return;
1043
deadbeefcbecd352015-09-23 11:50:27 -07001044 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001046 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047}
1048
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001049bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001050 const MediaContentDescription* content,
1051 ContentAction action,
1052 ContentSource src,
1053 std::string* error_desc) {
1054 if (action == CA_UPDATE) {
1055 // These parameters never get changed by a CA_UDPATE.
1056 return true;
1057 }
1058
deadbeef7af91dd2016-12-13 11:29:11 -08001059 // Cache srtp_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001060 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001061 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1062 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001063}
1064
1065bool BaseChannel::SetRtpTransportParameters_n(
1066 const MediaContentDescription* content,
1067 ContentAction action,
1068 ContentSource src,
1069 std::string* error_desc) {
1070 RTC_DCHECK(network_thread_->IsCurrent());
1071
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001072 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001073 return false;
1074 }
1075
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001076 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001077 return false;
1078 }
1079
1080 return true;
1081}
1082
zhihuangb2cdd932017-01-19 16:54:25 -08001083// |dtls| will be set to true if DTLS is active for transport and crypto is
1084// empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001085bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1086 bool* dtls,
1087 std::string* error_desc) {
deadbeeff5346592017-01-24 21:51:21 -08001088 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001089 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001090 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001091 return false;
1092 }
1093 return true;
1094}
1095
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001096bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001097 ContentAction action,
1098 ContentSource src,
1099 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001100 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001101 if (action == CA_UPDATE) {
1102 // no crypto params.
1103 return true;
1104 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001106 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001107 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001108 if (!ret) {
1109 return false;
1110 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 switch (action) {
1112 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001113 // If DTLS is already active on the channel, we could be renegotiating
1114 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001115 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001116 ret = srtp_filter_.SetOffer(cryptos, src);
1117 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 break;
1119 case CA_PRANSWER:
1120 // If we're doing DTLS-SRTP, we don't want to update the filter
1121 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001122 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1124 }
1125 break;
1126 case CA_ANSWER:
1127 // If we're doing DTLS-SRTP, we don't want to update the filter
1128 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001129 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130 ret = srtp_filter_.SetAnswer(cryptos, src);
1131 }
1132 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 default:
1134 break;
1135 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001136 if (!ret) {
1137 SafeSetError("Failed to setup SRTP filter.", error_desc);
1138 return false;
1139 }
1140 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141}
1142
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001143bool BaseChannel::SetRtcpMux_n(bool enable,
1144 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001145 ContentSource src,
1146 std::string* error_desc) {
deadbeef8e814d72017-01-13 11:34:39 -08001147 // Provide a more specific error message for the RTCP mux "require" policy
1148 // case.
zstein56162b92017-04-24 16:54:35 -07001149 if (rtcp_mux_required_ && !enable) {
deadbeef8e814d72017-01-13 11:34:39 -08001150 SafeSetError(
1151 "rtcpMuxPolicy is 'require', but media description does not "
1152 "contain 'a=rtcp-mux'.",
1153 error_desc);
1154 return false;
1155 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 bool ret = false;
1157 switch (action) {
1158 case CA_OFFER:
1159 ret = rtcp_mux_filter_.SetOffer(enable, src);
1160 break;
1161 case CA_PRANSWER:
zhihuangb2cdd932017-01-19 16:54:25 -08001162 // This may activate RTCP muxing, but we don't yet destroy the transport
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001163 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1165 break;
1166 case CA_ANSWER:
1167 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1168 if (ret && rtcp_mux_filter_.IsActive()) {
deadbeefe814a0d2017-02-25 18:15:09 -08001169 // We permanently activated RTCP muxing; signal that we no longer need
1170 // the RTCP transport.
zsteind48dbda2017-04-04 19:45:57 -07001171 std::string debug_name =
1172 transport_name_.empty()
1173 ? rtp_transport_.rtp_packet_transport()->debug_name()
1174 : transport_name_;
deadbeefe814a0d2017-02-25 18:15:09 -08001175 ;
deadbeefcbecd352015-09-23 11:50:27 -07001176 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
deadbeefe814a0d2017-02-25 18:15:09 -08001177 << "; no longer need RTCP transport for " << debug_name;
zsteind48dbda2017-04-04 19:45:57 -07001178 if (rtp_transport_.rtcp_packet_transport()) {
deadbeeff5346592017-01-24 21:51:21 -08001179 SetTransport_n(true, nullptr, nullptr);
1180 SignalRtcpMuxFullyActive(transport_name_);
zhihuangf5b251b2017-01-12 19:37:48 -08001181 }
deadbeef062ce9f2016-08-26 21:42:15 -07001182 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 }
1184 break;
1185 case CA_UPDATE:
1186 // No RTCP mux info.
1187 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001188 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 default:
1190 break;
1191 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001192 if (!ret) {
1193 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1194 return false;
1195 }
zstein56162b92017-04-24 16:54:35 -07001196 rtp_transport_.SetRtcpMuxEnabled(rtcp_mux_filter_.IsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
zhihuangb2cdd932017-01-19 16:54:25 -08001198 // CA_ANSWER, but we only want to tear down the RTCP transport if we received
1199 // a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001200 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 // If the RTP transport is already writable, then so are we.
zsteind48dbda2017-04-04 19:45:57 -07001202 if (rtp_transport_.rtp_packet_transport()->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001203 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 }
1205 }
1206
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001207 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208}
1209
1210bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001211 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001212 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213}
1214
Peter Boström0c4e06b2015-10-07 12:23:21 +02001215bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001216 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 return media_channel()->RemoveRecvStream(ssrc);
1218}
1219
1220bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001221 ContentAction action,
1222 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001223 if (!(action == CA_OFFER || action == CA_ANSWER ||
1224 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 return false;
1226
1227 // If this is an update, streams only contain streams that have changed.
1228 if (action == CA_UPDATE) {
1229 for (StreamParamsVec::const_iterator it = streams.begin();
1230 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001231 const StreamParams* existing_stream =
1232 GetStreamByIds(local_streams_, it->groupid, it->id);
1233 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234 if (media_channel()->AddSendStream(*it)) {
1235 local_streams_.push_back(*it);
1236 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1237 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001238 std::ostringstream desc;
1239 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1240 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 return false;
1242 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001243 } else if (existing_stream && !it->has_ssrcs()) {
1244 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001245 std::ostringstream desc;
1246 desc << "Failed to remove send stream with ssrc "
1247 << it->first_ssrc() << ".";
1248 SafeSetError(desc.str(), error_desc);
1249 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001251 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 } else {
1253 LOG(LS_WARNING) << "Ignore unsupported stream update";
1254 }
1255 }
1256 return true;
1257 }
1258 // Else streams are all the streams we want to send.
1259
1260 // Check for streams that have been removed.
1261 bool ret = true;
1262 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1263 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001264 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001266 std::ostringstream desc;
1267 desc << "Failed to remove send stream with ssrc "
1268 << it->first_ssrc() << ".";
1269 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 ret = false;
1271 }
1272 }
1273 }
1274 // Check for new streams.
1275 for (StreamParamsVec::const_iterator it = streams.begin();
1276 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001277 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001279 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001281 std::ostringstream desc;
1282 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1283 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 ret = false;
1285 }
1286 }
1287 }
1288 local_streams_ = streams;
1289 return ret;
1290}
1291
1292bool BaseChannel::UpdateRemoteStreams_w(
1293 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001294 ContentAction action,
1295 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001296 if (!(action == CA_OFFER || action == CA_ANSWER ||
1297 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 return false;
1299
1300 // If this is an update, streams only contain streams that have changed.
1301 if (action == CA_UPDATE) {
1302 for (StreamParamsVec::const_iterator it = streams.begin();
1303 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001304 const StreamParams* existing_stream =
1305 GetStreamByIds(remote_streams_, it->groupid, it->id);
1306 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 if (AddRecvStream_w(*it)) {
1308 remote_streams_.push_back(*it);
1309 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1310 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001311 std::ostringstream desc;
1312 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1313 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 return false;
1315 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001316 } else if (existing_stream && !it->has_ssrcs()) {
1317 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001318 std::ostringstream desc;
1319 desc << "Failed to remove remote stream with ssrc "
1320 << it->first_ssrc() << ".";
1321 SafeSetError(desc.str(), error_desc);
1322 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001324 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 } else {
1326 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001327 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 << " new stream = " << it->ToString();
1329 }
1330 }
1331 return true;
1332 }
1333 // Else streams are all the streams we want to receive.
1334
1335 // Check for streams that have been removed.
1336 bool ret = true;
1337 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1338 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001339 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001341 std::ostringstream desc;
1342 desc << "Failed to remove remote stream with ssrc "
1343 << it->first_ssrc() << ".";
1344 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 ret = false;
1346 }
1347 }
1348 }
1349 // Check for new streams.
1350 for (StreamParamsVec::const_iterator it = streams.begin();
1351 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001352 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 if (AddRecvStream_w(*it)) {
1354 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1355 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001356 std::ostringstream desc;
1357 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1358 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359 ret = false;
1360 }
1361 }
1362 }
1363 remote_streams_ = streams;
1364 return ret;
1365}
1366
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001367void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001368 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001369// Absolute Send Time extension id is used only with external auth,
1370// so do not bother searching for it and making asyncronious call to set
1371// something that is not used.
1372#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001373 const webrtc::RtpExtension* send_time_extension =
1374 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001375 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001376 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001377 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001378 RTC_FROM_HERE, network_thread_,
1379 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1380 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001381#endif
1382}
1383
1384void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1385 int rtp_abs_sendtime_extn_id) {
1386 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001387}
1388
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001389void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001390 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001392 case MSG_SEND_RTP_PACKET:
1393 case MSG_SEND_RTCP_PACKET: {
1394 RTC_DCHECK(network_thread_->IsCurrent());
1395 SendPacketMessageData* data =
1396 static_cast<SendPacketMessageData*>(pmsg->pdata);
1397 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1398 SendPacket(rtcp, &data->packet, data->options);
1399 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001400 break;
1401 }
1402 case MSG_FIRSTPACKETRECEIVED: {
1403 SignalFirstPacketReceived(this);
1404 break;
1405 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001406 }
1407}
1408
zstein3dcf0e92017-06-01 13:22:42 -07001409void BaseChannel::AddHandledPayloadType(int payload_type) {
1410 rtp_transport_.AddHandledPayloadType(payload_type);
1411}
1412
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001413void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414 // Flush all remaining RTCP messages. This should only be called in
1415 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001416 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001417 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001418 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1419 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001420 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1421 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422 }
1423}
1424
johand89ab142016-10-25 10:50:32 -07001425void BaseChannel::SignalSentPacket_n(
deadbeef5bd5ca32017-02-10 11:31:50 -08001426 rtc::PacketTransportInternal* /* transport */,
johand89ab142016-10-25 10:50:32 -07001427 const rtc::SentPacket& sent_packet) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001428 RTC_DCHECK(network_thread_->IsCurrent());
1429 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001430 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001431 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1432}
1433
1434void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1435 RTC_DCHECK(worker_thread_->IsCurrent());
1436 SignalSentPacket(sent_packet);
1437}
1438
1439VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1440 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001441 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001442 MediaEngineInterface* media_engine,
1443 VoiceMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001445 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001446 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001447 : BaseChannel(worker_thread,
1448 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001449 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001450 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001451 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001452 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001453 srtp_required),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001454 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001455 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456
1457VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001458 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459 StopAudioMonitor();
1460 StopMediaMonitor();
1461 // this can't be done in the base class, since it calls a virtual
1462 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001463 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464}
1465
Peter Boström0c4e06b2015-10-07 12:23:21 +02001466bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001467 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001468 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001469 AudioSource* source) {
charujaind72098a2017-06-01 08:54:47 -07001470 return InvokeOnWorker(RTC_FROM_HERE,
1471 Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
1472 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473}
1474
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475// TODO(juberti): Handle early media the right way. We should get an explicit
1476// ringing message telling us to start playing local ringback, which we cancel
1477// if any early media actually arrives. For now, we do the opposite, which is
1478// to wait 1 second for early media, and start playing local ringback if none
1479// arrives.
1480void VoiceChannel::SetEarlyMedia(bool enable) {
1481 if (enable) {
1482 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001483 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1484 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 } else {
1486 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001487 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 }
1489}
1490
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491bool VoiceChannel::CanInsertDtmf() {
charujaind72098a2017-06-01 08:54:47 -07001492 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001493 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494}
1495
Peter Boström0c4e06b2015-10-07 12:23:21 +02001496bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1497 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001498 int duration) {
charujaind72098a2017-06-01 08:54:47 -07001499 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this,
1500 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501}
1502
solenberg4bac9c52015-10-09 02:32:53 -07001503bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
charujaind72098a2017-06-01 08:54:47 -07001504 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume,
1505 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001507
Tommif888bb52015-12-12 01:37:01 +01001508void VoiceChannel::SetRawAudioSink(
1509 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001510 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1511 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001512 // passing. So we invoke to our own little routine that gets a pointer to
1513 // our local variable. This is OK since we're synchronously invoking.
charujaind72098a2017-06-01 08:54:47 -07001514 InvokeOnWorker(RTC_FROM_HERE,
1515 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001516}
1517
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001518webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001519 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001520 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001521}
1522
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001523webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1524 uint32_t ssrc) const {
1525 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001526}
1527
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001528bool VoiceChannel::SetRtpSendParameters(
1529 uint32_t ssrc,
1530 const webrtc::RtpParameters& parameters) {
charujaind72098a2017-06-01 08:54:47 -07001531 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001532 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001533 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001534}
1535
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001536bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1537 webrtc::RtpParameters parameters) {
1538 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1539}
1540
1541webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1542 uint32_t ssrc) const {
1543 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001544 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001545 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1546}
1547
1548webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1549 uint32_t ssrc) const {
1550 return media_channel()->GetRtpReceiveParameters(ssrc);
1551}
1552
1553bool VoiceChannel::SetRtpReceiveParameters(
1554 uint32_t ssrc,
1555 const webrtc::RtpParameters& parameters) {
charujaind72098a2017-06-01 08:54:47 -07001556 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001557 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001558 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1559}
1560
1561bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1562 webrtc::RtpParameters parameters) {
1563 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001564}
1565
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
charujaind72098a2017-06-01 08:54:47 -07001567 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1568 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569}
1570
hbos8d609f62017-04-10 07:39:05 -07001571std::vector<webrtc::RtpSource> VoiceChannel::GetSources(uint32_t ssrc) const {
1572 return worker_thread()->Invoke<std::vector<webrtc::RtpSource>>(
1573 RTC_FROM_HERE,
1574 Bind(&WebRtcVoiceMediaChannel::GetSources,
1575 static_cast<WebRtcVoiceMediaChannel*>(media_channel()), ssrc));
1576}
1577
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578void VoiceChannel::StartMediaMonitor(int cms) {
1579 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001580 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 media_monitor_->SignalUpdate.connect(
1582 this, &VoiceChannel::OnMediaMonitorUpdate);
1583 media_monitor_->Start(cms);
1584}
1585
1586void VoiceChannel::StopMediaMonitor() {
1587 if (media_monitor_) {
1588 media_monitor_->Stop();
1589 media_monitor_->SignalUpdate.disconnect(this);
1590 media_monitor_.reset();
1591 }
1592}
1593
1594void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001595 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 audio_monitor_
1597 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1598 audio_monitor_->Start(cms);
1599}
1600
1601void VoiceChannel::StopAudioMonitor() {
1602 if (audio_monitor_) {
1603 audio_monitor_->Stop();
1604 audio_monitor_.reset();
1605 }
1606}
1607
1608bool VoiceChannel::IsAudioMonitorRunning() const {
1609 return (audio_monitor_.get() != NULL);
1610}
1611
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001613 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614}
1615
1616int VoiceChannel::GetOutputLevel_w() {
1617 return media_channel()->GetOutputLevel();
1618}
1619
1620void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1621 media_channel()->GetActiveStreams(actives);
1622}
1623
zstein3dcf0e92017-06-01 13:22:42 -07001624void VoiceChannel::OnPacketReceived(bool rtcp,
1625 rtc::CopyOnWriteBuffer& packet,
1626 const rtc::PacketTime& packet_time) {
1627 BaseChannel::OnPacketReceived(rtcp, packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 // Set a flag when we've received an RTP packet. If we're waiting for early
1629 // media, this will disable the timeout.
zstein3dcf0e92017-06-01 13:22:42 -07001630 if (!received_media_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 received_media_ = true;
1632 }
1633}
1634
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001635void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001636 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001637 invoker_.AsyncInvoke<void>(
1638 RTC_FROM_HERE, worker_thread_,
1639 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001640}
1641
michaelt79e05882016-11-08 02:50:09 -08001642int BaseChannel::GetTransportOverheadPerPacket() const {
1643 RTC_DCHECK(network_thread_->IsCurrent());
1644
1645 if (!selected_candidate_pair_)
1646 return 0;
1647
1648 int transport_overhead_per_packet = 0;
1649
1650 constexpr int kIpv4Overhaed = 20;
1651 constexpr int kIpv6Overhaed = 40;
1652 transport_overhead_per_packet +=
1653 selected_candidate_pair_->local_candidate().address().family() == AF_INET
1654 ? kIpv4Overhaed
1655 : kIpv6Overhaed;
1656
1657 constexpr int kUdpOverhaed = 8;
1658 constexpr int kTcpOverhaed = 20;
1659 transport_overhead_per_packet +=
1660 selected_candidate_pair_->local_candidate().protocol() ==
1661 TCP_PROTOCOL_NAME
1662 ? kTcpOverhaed
1663 : kUdpOverhaed;
1664
1665 if (secure()) {
1666 int srtp_overhead = 0;
1667 if (srtp_filter_.GetSrtpOverhead(&srtp_overhead))
1668 transport_overhead_per_packet += srtp_overhead;
1669 }
1670
1671 return transport_overhead_per_packet;
1672}
1673
1674void BaseChannel::UpdateTransportOverhead() {
1675 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1676 if (transport_overhead_per_packet)
1677 invoker_.AsyncInvoke<void>(
1678 RTC_FROM_HERE, worker_thread_,
1679 Bind(&MediaChannel::OnTransportOverheadChanged, media_channel_,
1680 transport_overhead_per_packet));
1681}
1682
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001683void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684 // Render incoming data if we're the active call, and we have the local
1685 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001686 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001687 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688
1689 // Send outgoing data if we're the active call, we have the remote content,
1690 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001691 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001692 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693
1694 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1695}
1696
1697const ContentInfo* VoiceChannel::GetFirstContent(
1698 const SessionDescription* sdesc) {
1699 return GetFirstAudioContent(sdesc);
1700}
1701
1702bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001703 ContentAction action,
1704 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001705 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001706 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 LOG(LS_INFO) << "Setting local voice description";
1708
1709 const AudioContentDescription* audio =
1710 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001711 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001712 if (!audio) {
1713 SafeSetError("Can't find audio content in local description.", error_desc);
1714 return false;
1715 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001717 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001718 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 }
1720
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001721 AudioRecvParameters recv_params = last_recv_params_;
1722 RtpParametersFromMediaDescription(audio, &recv_params);
1723 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001724 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001725 error_desc);
1726 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001728 for (const AudioCodec& codec : audio->codecs()) {
zstein3dcf0e92017-06-01 13:22:42 -07001729 AddHandledPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001730 }
1731 last_recv_params_ = recv_params;
1732
1733 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1734 // only give it to the media channel once we have a remote
1735 // description too (without a remote description, we won't be able
1736 // to send them anyway).
1737 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1738 SafeSetError("Failed to set local audio description streams.", error_desc);
1739 return false;
1740 }
1741
1742 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001743 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001744 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745}
1746
1747bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001748 ContentAction action,
1749 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001750 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001751 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 LOG(LS_INFO) << "Setting remote voice description";
1753
1754 const AudioContentDescription* audio =
1755 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001756 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001757 if (!audio) {
1758 SafeSetError("Can't find audio content in remote description.", error_desc);
1759 return false;
1760 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001762 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001763 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 }
1765
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001766 AudioSendParameters send_params = last_send_params_;
1767 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001768 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001769 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001770 }
skvladdc1c62c2016-03-16 19:07:43 -07001771
1772 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1773 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001774 SafeSetError("Failed to set remote audio description send parameters.",
1775 error_desc);
1776 return false;
1777 }
1778 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001780 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1781 // and only give it to the media channel once we have a local
1782 // description too (without a local description, we won't be able to
1783 // recv them anyway).
1784 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1785 SafeSetError("Failed to set remote audio description streams.", error_desc);
1786 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 }
1788
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001789 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001790 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001791 }
1792
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001793 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001794 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001795 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796}
1797
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798void VoiceChannel::HandleEarlyMediaTimeout() {
1799 // This occurs on the main thread, not the worker thread.
1800 if (!received_media_) {
1801 LOG(LS_INFO) << "No early media received before timeout";
1802 SignalEarlyMediaTimeout(this);
1803 }
1804}
1805
Peter Boström0c4e06b2015-10-07 12:23:21 +02001806bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1807 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001808 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 if (!enabled()) {
1810 return false;
1811 }
solenberg1d63dd02015-12-02 12:35:09 -08001812 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813}
1814
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001815void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 case MSG_EARLYMEDIATIMEOUT:
1818 HandleEarlyMediaTimeout();
1819 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 case MSG_CHANNEL_ERROR: {
1821 VoiceChannelErrorMessageData* data =
1822 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 delete data;
1824 break;
1825 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 default:
1827 BaseChannel::OnMessage(pmsg);
1828 break;
1829 }
1830}
1831
1832void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001833 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 SignalConnectionMonitor(this, infos);
1835}
1836
1837void VoiceChannel::OnMediaMonitorUpdate(
1838 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001839 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 SignalMediaMonitor(this, info);
1841}
1842
1843void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1844 const AudioInfo& info) {
1845 SignalAudioMonitor(this, info);
1846}
1847
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001848VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1849 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001850 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 VideoMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001853 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001854 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001855 : BaseChannel(worker_thread,
1856 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001857 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001858 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001859 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001860 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001861 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001864 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 StopMediaMonitor();
1866 // this can't be done in the base class, since it calls a virtual
1867 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001868
1869 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870}
1871
nisse08582ff2016-02-04 01:24:52 -08001872bool VideoChannel::SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -08001873 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
nisse08582ff2016-02-04 01:24:52 -08001874 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001875 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001876 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 return true;
1878}
1879
deadbeef5a4a75a2016-06-02 16:23:38 -07001880bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001881 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001882 bool mute,
1883 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -08001884 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
charujaind72098a2017-06-01 08:54:47 -07001885 return InvokeOnWorker(RTC_FROM_HERE,
1886 Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1887 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001888}
1889
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001890webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001891 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001892 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001893}
1894
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001895webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1896 uint32_t ssrc) const {
1897 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001898}
1899
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001900bool VideoChannel::SetRtpSendParameters(
1901 uint32_t ssrc,
1902 const webrtc::RtpParameters& parameters) {
charujaind72098a2017-06-01 08:54:47 -07001903 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001904 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001905 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001906}
1907
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001908bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1909 webrtc::RtpParameters parameters) {
1910 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1911}
1912
1913webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1914 uint32_t ssrc) const {
1915 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001916 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001917 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
1918}
1919
1920webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
1921 uint32_t ssrc) const {
1922 return media_channel()->GetRtpReceiveParameters(ssrc);
1923}
1924
1925bool VideoChannel::SetRtpReceiveParameters(
1926 uint32_t ssrc,
1927 const webrtc::RtpParameters& parameters) {
charujaind72098a2017-06-01 08:54:47 -07001928 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001929 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001930 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1931}
1932
1933bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1934 webrtc::RtpParameters parameters) {
1935 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001936}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001937
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001938void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 // Send outgoing data if we're the active call, we have the remote content,
1940 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001941 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 if (!media_channel()->SetSend(send)) {
1943 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1944 // TODO(gangji): Report error back to server.
1945 }
1946
Peter Boström34fbfff2015-09-24 19:20:30 +02001947 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948}
1949
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001950bool VideoChannel::GetStats(VideoMediaInfo* stats) {
charujaind72098a2017-06-01 08:54:47 -07001951 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
1952 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953}
1954
1955void VideoChannel::StartMediaMonitor(int cms) {
1956 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001957 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 media_monitor_->SignalUpdate.connect(
1959 this, &VideoChannel::OnMediaMonitorUpdate);
1960 media_monitor_->Start(cms);
1961}
1962
1963void VideoChannel::StopMediaMonitor() {
1964 if (media_monitor_) {
1965 media_monitor_->Stop();
1966 media_monitor_.reset();
1967 }
1968}
1969
1970const ContentInfo* VideoChannel::GetFirstContent(
1971 const SessionDescription* sdesc) {
1972 return GetFirstVideoContent(sdesc);
1973}
1974
1975bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001976 ContentAction action,
1977 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001978 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001979 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 LOG(LS_INFO) << "Setting local video description";
1981
1982 const VideoContentDescription* video =
1983 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001984 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001985 if (!video) {
1986 SafeSetError("Can't find video content in local description.", error_desc);
1987 return false;
1988 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001990 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001991 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 }
1993
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001994 VideoRecvParameters recv_params = last_recv_params_;
1995 RtpParametersFromMediaDescription(video, &recv_params);
1996 if (!media_channel()->SetRecvParameters(recv_params)) {
1997 SafeSetError("Failed to set local video description recv parameters.",
1998 error_desc);
1999 return false;
2000 }
2001 for (const VideoCodec& codec : video->codecs()) {
zstein3dcf0e92017-06-01 13:22:42 -07002002 AddHandledPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002003 }
2004 last_recv_params_ = recv_params;
2005
2006 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2007 // only give it to the media channel once we have a remote
2008 // description too (without a remote description, we won't be able
2009 // to send them anyway).
2010 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2011 SafeSetError("Failed to set local video description streams.", error_desc);
2012 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013 }
2014
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002015 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002016 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002017 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018}
2019
2020bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002021 ContentAction action,
2022 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002023 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002024 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 LOG(LS_INFO) << "Setting remote video description";
2026
2027 const VideoContentDescription* video =
2028 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002029 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002030 if (!video) {
2031 SafeSetError("Can't find video content in remote description.", error_desc);
2032 return false;
2033 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002035 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002036 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037 }
2038
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002039 VideoSendParameters send_params = last_send_params_;
2040 RtpSendParametersFromMediaDescription(video, &send_params);
2041 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002042 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002043 }
skvladdc1c62c2016-03-16 19:07:43 -07002044
2045 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2046
2047 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002048 SafeSetError("Failed to set remote video description send parameters.",
2049 error_desc);
2050 return false;
2051 }
2052 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002054 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2055 // and only give it to the media channel once we have a local
2056 // description too (without a local description, we won't be able to
2057 // recv them anyway).
2058 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2059 SafeSetError("Failed to set remote video description streams.", error_desc);
2060 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 }
2062
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002063 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002064 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002066
2067 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002068 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002069 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070}
2071
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002072void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 case MSG_CHANNEL_ERROR: {
2075 const VideoChannelErrorMessageData* data =
2076 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002077 delete data;
2078 break;
2079 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080 default:
2081 BaseChannel::OnMessage(pmsg);
2082 break;
2083 }
2084}
2085
2086void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002087 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088 SignalConnectionMonitor(this, infos);
2089}
2090
2091// TODO(pthatcher): Look into removing duplicate code between
2092// audio, video, and data, perhaps by using templates.
2093void VideoChannel::OnMediaMonitorUpdate(
2094 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002095 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 SignalMediaMonitor(this, info);
2097}
2098
deadbeef953c2ce2017-01-09 14:53:41 -08002099RtpDataChannel::RtpDataChannel(rtc::Thread* worker_thread,
2100 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002101 rtc::Thread* signaling_thread,
deadbeef953c2ce2017-01-09 14:53:41 -08002102 DataMediaChannel* media_channel,
deadbeef953c2ce2017-01-09 14:53:41 -08002103 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08002104 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002105 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002106 : BaseChannel(worker_thread,
2107 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002108 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002109 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002110 content_name,
deadbeefac22f702017-01-12 21:59:29 -08002111 rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002112 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113
deadbeef953c2ce2017-01-09 14:53:41 -08002114RtpDataChannel::~RtpDataChannel() {
2115 TRACE_EVENT0("webrtc", "RtpDataChannel::~RtpDataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116 StopMediaMonitor();
2117 // this can't be done in the base class, since it calls a virtual
2118 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002119
2120 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121}
2122
deadbeeff5346592017-01-24 21:51:21 -08002123bool RtpDataChannel::Init_w(
2124 DtlsTransportInternal* rtp_dtls_transport,
2125 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -08002126 rtc::PacketTransportInternal* rtp_packet_transport,
2127 rtc::PacketTransportInternal* rtcp_packet_transport) {
deadbeeff5346592017-01-24 21:51:21 -08002128 if (!BaseChannel::Init_w(rtp_dtls_transport, rtcp_dtls_transport,
2129 rtp_packet_transport, rtcp_packet_transport)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002130 return false;
2131 }
deadbeef953c2ce2017-01-09 14:53:41 -08002132 media_channel()->SignalDataReceived.connect(this,
2133 &RtpDataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002134 media_channel()->SignalReadyToSend.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08002135 this, &RtpDataChannel::OnDataChannelReadyToSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136 return true;
2137}
2138
deadbeef953c2ce2017-01-09 14:53:41 -08002139bool RtpDataChannel::SendData(const SendDataParams& params,
2140 const rtc::CopyOnWriteBuffer& payload,
2141 SendDataResult* result) {
charujaind72098a2017-06-01 08:54:47 -07002142 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002143 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2144 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145}
2146
deadbeef953c2ce2017-01-09 14:53:41 -08002147const ContentInfo* RtpDataChannel::GetFirstContent(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148 const SessionDescription* sdesc) {
2149 return GetFirstDataContent(sdesc);
2150}
2151
deadbeef953c2ce2017-01-09 14:53:41 -08002152bool RtpDataChannel::CheckDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002153 const DataContentDescription* content,
2154 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002155 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2156 (content->protocol() == kMediaProtocolDtlsSctp));
deadbeef953c2ce2017-01-09 14:53:41 -08002157 // It's been set before, but doesn't match. That's bad.
2158 if (is_sctp) {
2159 SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.",
2160 error_desc);
2161 return false;
2162 }
2163 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164}
2165
deadbeef953c2ce2017-01-09 14:53:41 -08002166bool RtpDataChannel::SetLocalContent_w(const MediaContentDescription* content,
2167 ContentAction action,
2168 std::string* error_desc) {
2169 TRACE_EVENT0("webrtc", "RtpDataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002170 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171 LOG(LS_INFO) << "Setting local data description";
2172
2173 const DataContentDescription* data =
2174 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002175 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002176 if (!data) {
2177 SafeSetError("Can't find data content in local description.", error_desc);
2178 return false;
2179 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180
deadbeef953c2ce2017-01-09 14:53:41 -08002181 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182 return false;
2183 }
2184
deadbeef953c2ce2017-01-09 14:53:41 -08002185 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
2186 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002187 }
2188
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002189 DataRecvParameters recv_params = last_recv_params_;
2190 RtpParametersFromMediaDescription(data, &recv_params);
2191 if (!media_channel()->SetRecvParameters(recv_params)) {
2192 SafeSetError("Failed to set remote data description recv parameters.",
2193 error_desc);
2194 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 }
deadbeef953c2ce2017-01-09 14:53:41 -08002196 for (const DataCodec& codec : data->codecs()) {
zstein3dcf0e92017-06-01 13:22:42 -07002197 AddHandledPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002198 }
2199 last_recv_params_ = recv_params;
2200
2201 // TODO(pthatcher): Move local streams into DataSendParameters, and
2202 // only give it to the media channel once we have a remote
2203 // description too (without a remote description, we won't be able
2204 // to send them anyway).
2205 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2206 SafeSetError("Failed to set local data description streams.", error_desc);
2207 return false;
2208 }
2209
2210 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002211 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002212 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213}
2214
deadbeef953c2ce2017-01-09 14:53:41 -08002215bool RtpDataChannel::SetRemoteContent_w(const MediaContentDescription* content,
2216 ContentAction action,
2217 std::string* error_desc) {
2218 TRACE_EVENT0("webrtc", "RtpDataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002219 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220
2221 const DataContentDescription* data =
2222 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002223 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002224 if (!data) {
2225 SafeSetError("Can't find data content in remote description.", error_desc);
2226 return false;
2227 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002229 // If the remote data doesn't have codecs and isn't an update, it
2230 // must be empty, so ignore it.
2231 if (!data->has_codecs() && action != CA_UPDATE) {
2232 return true;
2233 }
2234
deadbeef953c2ce2017-01-09 14:53:41 -08002235 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 return false;
2237 }
2238
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002239 LOG(LS_INFO) << "Setting remote data description";
deadbeef953c2ce2017-01-09 14:53:41 -08002240 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002241 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242 }
2243
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002244 DataSendParameters send_params = last_send_params_;
2245 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2246 if (!media_channel()->SetSendParameters(send_params)) {
2247 SafeSetError("Failed to set remote data description send parameters.",
2248 error_desc);
2249 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002251 last_send_params_ = send_params;
2252
2253 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2254 // and only give it to the media channel once we have a local
2255 // description too (without a local description, we won't be able to
2256 // recv them anyway).
2257 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2258 SafeSetError("Failed to set remote data description streams.",
2259 error_desc);
2260 return false;
2261 }
2262
2263 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002264 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002265 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002266}
2267
deadbeef953c2ce2017-01-09 14:53:41 -08002268void RtpDataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002269 // Render incoming data if we're the active call, and we have the local
2270 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002271 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272 if (!media_channel()->SetReceive(recv)) {
2273 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2274 }
2275
2276 // Send outgoing data if we're the active call, we have the remote content,
2277 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002278 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279 if (!media_channel()->SetSend(send)) {
2280 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2281 }
2282
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002283 // Trigger SignalReadyToSendData asynchronously.
2284 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285
2286 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2287}
2288
deadbeef953c2ce2017-01-09 14:53:41 -08002289void RtpDataChannel::OnMessage(rtc::Message* pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 switch (pmsg->message_id) {
2291 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002292 DataChannelReadyToSendMessageData* data =
2293 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002294 ready_to_send_data_ = data->data();
2295 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 delete data;
2297 break;
2298 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299 case MSG_DATARECEIVED: {
2300 DataReceivedMessageData* data =
2301 static_cast<DataReceivedMessageData*>(pmsg->pdata);
deadbeef953c2ce2017-01-09 14:53:41 -08002302 SignalDataReceived(data->params, data->payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002303 delete data;
2304 break;
2305 }
2306 case MSG_CHANNEL_ERROR: {
2307 const DataChannelErrorMessageData* data =
2308 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309 delete data;
2310 break;
2311 }
2312 default:
2313 BaseChannel::OnMessage(pmsg);
2314 break;
2315 }
2316}
2317
deadbeef953c2ce2017-01-09 14:53:41 -08002318void RtpDataChannel::OnConnectionMonitorUpdate(
2319 ConnectionMonitor* monitor,
2320 const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002321 SignalConnectionMonitor(this, infos);
2322}
2323
deadbeef953c2ce2017-01-09 14:53:41 -08002324void RtpDataChannel::StartMediaMonitor(int cms) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002326 rtc::Thread::Current()));
deadbeef953c2ce2017-01-09 14:53:41 -08002327 media_monitor_->SignalUpdate.connect(this,
2328 &RtpDataChannel::OnMediaMonitorUpdate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 media_monitor_->Start(cms);
2330}
2331
deadbeef953c2ce2017-01-09 14:53:41 -08002332void RtpDataChannel::StopMediaMonitor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333 if (media_monitor_) {
2334 media_monitor_->Stop();
2335 media_monitor_->SignalUpdate.disconnect(this);
2336 media_monitor_.reset();
2337 }
2338}
2339
deadbeef953c2ce2017-01-09 14:53:41 -08002340void RtpDataChannel::OnMediaMonitorUpdate(DataMediaChannel* media_channel,
2341 const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002342 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343 SignalMediaMonitor(this, info);
2344}
2345
deadbeef953c2ce2017-01-09 14:53:41 -08002346void RtpDataChannel::OnDataReceived(const ReceiveDataParams& params,
2347 const char* data,
2348 size_t len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002349 DataReceivedMessageData* msg = new DataReceivedMessageData(
2350 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002351 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352}
2353
deadbeef953c2ce2017-01-09 14:53:41 -08002354void RtpDataChannel::OnDataChannelError(uint32_t ssrc,
2355 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2357 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002358 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002359}
2360
deadbeef953c2ce2017-01-09 14:53:41 -08002361void RtpDataChannel::OnDataChannelReadyToSend(bool writable) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002362 // This is usded for congestion control to indicate that the stream is ready
2363 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2364 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002365 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002366 new DataChannelReadyToSendMessageData(writable));
2367}
2368
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369} // namespace cricket