blob: cc4588a1cf86b1d76f4dc18c54d65fa8f18588dc [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) {
stefanf79ade12017-06-02 06:44:03 -0700444 return InvokeOnWorker<bool>(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) {
stefanf79ade12017-06-02 06:44:03 -0700449 return InvokeOnWorker<bool>(
450 RTC_FROM_HERE, 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) {
stefanf79ade12017-06-02 06:44:03 -0700454 return InvokeOnWorker<bool>(
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) {
stefanf79ade12017-06-02 06:44:03 -0700459 return InvokeOnWorker<bool>(
460 RTC_FROM_HERE,
461 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000462}
463
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000465 ContentAction action,
466 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100467 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
stefanf79ade12017-06-02 06:44:03 -0700468 return InvokeOnWorker<bool>(
469 RTC_FROM_HERE,
470 Bind(&BaseChannel::SetLocalContent_w, this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471}
472
473bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000474 ContentAction action,
475 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100476 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
stefanf79ade12017-06-02 06:44:03 -0700477 return InvokeOnWorker<bool>(
478 RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w, this, content,
479 action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480}
481
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482void BaseChannel::StartConnectionMonitor(int cms) {
zhihuangb2cdd932017-01-19 16:54:25 -0800483 // We pass in the BaseChannel instead of the rtp_dtls_transport_
484 // because if the rtp_dtls_transport_ changes, the ConnectionMonitor
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000485 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200486 // We pass in the network thread because on that thread connection monitor
487 // will call BaseChannel::GetConnectionStats which must be called on the
488 // network thread.
489 connection_monitor_.reset(
490 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000491 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000493 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494}
495
496void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000497 if (connection_monitor_) {
498 connection_monitor_->Stop();
499 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 }
501}
502
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000503bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200504 RTC_DCHECK(network_thread_->IsCurrent());
deadbeeff5346592017-01-24 21:51:21 -0800505 if (!rtp_dtls_transport_) {
506 return false;
507 }
zhihuangb2cdd932017-01-19 16:54:25 -0800508 return rtp_dtls_transport_->ice_transport()->GetStats(infos);
zhihuangf5b251b2017-01-12 19:37:48 -0800509}
510
511bool BaseChannel::NeedsRtcpTransport() {
deadbeefac22f702017-01-12 21:59:29 -0800512 // If this BaseChannel doesn't require RTCP mux and we haven't fully
513 // negotiated RTCP mux, we need an RTCP transport.
zstein56162b92017-04-24 16:54:35 -0700514 return !rtcp_mux_required_ && !rtcp_mux_filter_.IsFullyActive();
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000515}
516
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700517bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000518 // Receive data if we are enabled and have local content,
519 return enabled() && IsReceiveContentDirection(local_content_direction_);
520}
521
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700522bool BaseChannel::IsReadyToSendMedia_w() const {
523 // Need to access some state updated on the network thread.
524 return network_thread_->Invoke<bool>(
525 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
526}
527
528bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 // Send outgoing data if we are enabled, have local and remote content,
530 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800531 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700533 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200534 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535}
536
jbaucheec21bd2016-03-20 06:15:43 -0700537bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700538 const rtc::PacketOptions& options) {
539 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540}
541
jbaucheec21bd2016-03-20 06:15:43 -0700542bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700543 const rtc::PacketOptions& options) {
544 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545}
546
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000547int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200549 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700550 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200551}
552
553int BaseChannel::SetOption_n(SocketType type,
554 rtc::Socket::Option opt,
555 int value) {
556 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef5bd5ca32017-02-10 11:31:50 -0800557 rtc::PacketTransportInternal* transport = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000559 case ST_RTP:
zsteind48dbda2017-04-04 19:45:57 -0700560 transport = rtp_transport_.rtp_packet_transport();
deadbeefcbecd352015-09-23 11:50:27 -0700561 socket_options_.push_back(
562 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000563 break;
564 case ST_RTCP:
zsteind48dbda2017-04-04 19:45:57 -0700565 transport = rtp_transport_.rtcp_packet_transport();
deadbeefcbecd352015-09-23 11:50:27 -0700566 rtcp_socket_options_.push_back(
567 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000568 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 }
deadbeeff5346592017-01-24 21:51:21 -0800570 return transport ? transport->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571}
572
deadbeef5bd5ca32017-02-10 11:31:50 -0800573void BaseChannel::OnWritableState(rtc::PacketTransportInternal* transport) {
zsteind48dbda2017-04-04 19:45:57 -0700574 RTC_DCHECK(transport == rtp_transport_.rtp_packet_transport() ||
575 transport == rtp_transport_.rtcp_packet_transport());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200576 RTC_DCHECK(network_thread_->IsCurrent());
577 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578}
579
zhihuangb2cdd932017-01-19 16:54:25 -0800580void BaseChannel::OnDtlsState(DtlsTransportInternal* transport,
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800581 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200582 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800583 return;
584 }
585
586 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
587 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
zhihuangb2cdd932017-01-19 16:54:25 -0800588 // cover other scenarios like the whole transport is writable (not just this
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800589 // TransportChannel) or when TransportChannel is attached after DTLS is
590 // negotiated.
591 if (state != DTLS_TRANSPORT_CONNECTED) {
592 srtp_filter_.ResetParams();
593 }
594}
595
Honghai Zhangcc411c02016-03-29 17:27:21 -0700596void BaseChannel::OnSelectedCandidatePairChanged(
zhihuangb2cdd932017-01-19 16:54:25 -0800597 IceTransportInternal* ice_transport,
Honghai Zhang52dce732016-03-31 12:37:31 -0700598 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700599 int last_sent_packet_id,
600 bool ready_to_send) {
deadbeeff5346592017-01-24 21:51:21 -0800601 RTC_DCHECK((rtp_dtls_transport_ &&
602 ice_transport == rtp_dtls_transport_->ice_transport()) ||
603 (rtcp_dtls_transport_ &&
604 ice_transport == rtcp_dtls_transport_->ice_transport()));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200605 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800606 selected_candidate_pair_ = selected_candidate_pair;
zhihuangb2cdd932017-01-19 16:54:25 -0800607 std::string transport_name = ice_transport->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700608 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700609 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700610 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700611 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700612 selected_candidate_pair->remote_candidate().network_id(),
613 last_sent_packet_id);
michaelt79e05882016-11-08 02:50:09 -0800614
615 UpdateTransportOverhead();
Honghai Zhangcc411c02016-03-29 17:27:21 -0700616 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200617 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700618 RTC_FROM_HERE, worker_thread_,
619 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
620 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700621}
622
zstein56162b92017-04-24 16:54:35 -0700623void BaseChannel::OnTransportReadyToSend(bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200624 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700625 RTC_FROM_HERE, worker_thread_,
zstein56162b92017-04-24 16:54:35 -0700626 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627}
628
stefanc1aeaf02015-10-15 07:26:07 -0700629bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700630 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700631 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200632 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
633 // If the thread is not our network thread, we will post to our network
634 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 // synchronize access to all the pieces of the send path, including
636 // SRTP and the inner workings of the transport channels.
637 // The only downside is that we can't return a proper failure code if
638 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200639 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200641 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
642 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800643 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700644 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700645 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 return true;
647 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200648 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649
650 // Now that we are on the correct thread, ensure we have a place to send this
651 // packet before doing anything. (We might get RTCP packets that we don't
652 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
653 // transport.
zstein56162b92017-04-24 16:54:35 -0700654 if (!rtp_transport_.IsWritable(rtcp)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 return false;
656 }
657
658 // Protect ourselves against crazy data.
659 if (!ValidPacket(rtcp, packet)) {
660 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
zstein3dcf0e92017-06-01 13:22:42 -0700661 << RtpRtcpStringLiteral(rtcp)
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000662 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 return false;
664 }
665
stefanc1aeaf02015-10-15 07:26:07 -0700666 rtc::PacketOptions updated_options;
667 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 // Protect if needed.
669 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200670 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200672 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000673 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000675 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
676 // inside libsrtp for a RTP packet. A external HMAC module will be writing
677 // a fake HMAC value. This is ONLY done for a RTP packet.
678 // Socket layer will update rtp sendtime extension header if present in
679 // packet with current time before updating the HMAC.
680#if !defined(ENABLE_EXTERNAL_AUTH)
681 res = srtp_filter_.ProtectRtp(
682 data, len, static_cast<int>(packet->capacity()), &len);
683#else
jbauchd48f4882017-03-01 15:34:36 -0800684 if (!srtp_filter_.IsExternalAuthActive()) {
685 res = srtp_filter_.ProtectRtp(
686 data, len, static_cast<int>(packet->capacity()), &len);
687 } else {
688 updated_options.packet_time_params.rtp_sendtime_extension_id =
689 rtp_abs_sendtime_extn_id_;
690 res = srtp_filter_.ProtectRtp(
691 data, len, static_cast<int>(packet->capacity()), &len,
692 &updated_options.packet_time_params.srtp_packet_index);
693 // If protection succeeds, let's get auth params from srtp.
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000694 if (res) {
jbauchd48f4882017-03-01 15:34:36 -0800695 uint8_t* auth_key = NULL;
696 int key_len;
697 res = srtp_filter_.GetRtpAuthParams(
698 &auth_key, &key_len,
699 &updated_options.packet_time_params.srtp_auth_tag_len);
700 if (res) {
701 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
702 updated_options.packet_time_params.srtp_auth_key.assign(
703 auth_key, auth_key + key_len);
704 }
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000705 }
706 }
707#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 if (!res) {
709 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200710 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 GetRtpSeqNum(data, len, &seq_num);
712 GetRtpSsrc(data, len, &ssrc);
713 LOG(LS_ERROR) << "Failed to protect " << content_name_
714 << " RTP packet: size=" << len
715 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
716 return false;
717 }
718 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000719 res = srtp_filter_.ProtectRtcp(data, len,
720 static_cast<int>(packet->capacity()),
721 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000722 if (!res) {
723 int type = -1;
724 GetRtcpType(data, len, &type);
725 LOG(LS_ERROR) << "Failed to protect " << content_name_
726 << " RTCP packet: size=" << len << ", type=" << type;
727 return false;
728 }
729 }
730
731 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000732 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800733 } else if (srtp_required_) {
deadbeef8f425f92016-12-01 12:26:27 -0800734 // The audio/video engines may attempt to send RTCP packets as soon as the
735 // streams are created, so don't treat this as an error for RTCP.
736 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809
737 if (rtcp) {
738 return false;
739 }
740 // However, there shouldn't be any RTP packets sent before SRTP is set up
741 // (and SetSend(true) is called).
742 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
743 << " and crypto is required";
nisseeb4ca4e2017-01-12 02:24:27 -0800744 RTC_NOTREACHED();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 return false;
746 }
747
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200749 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
zstein56162b92017-04-24 16:54:35 -0700750 return rtp_transport_.SendPacket(rtcp, packet, updated_options, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751}
752
zstein3dcf0e92017-06-01 13:22:42 -0700753bool BaseChannel::HandlesPayloadType(int packet_type) const {
754 return rtp_transport_.HandlesPayloadType(packet_type);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755}
756
zstein3dcf0e92017-06-01 13:22:42 -0700757void BaseChannel::OnPacketReceived(bool rtcp,
758 rtc::CopyOnWriteBuffer& packet,
759 const rtc::PacketTime& packet_time) {
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000760 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700762 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 }
764
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 // Unprotect the packet, if needed.
766 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200767 TRACE_EVENT0("webrtc", "SRTP Decode");
zstein3dcf0e92017-06-01 13:22:42 -0700768 char* data = packet.data<char>();
769 int len = static_cast<int>(packet.size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 bool res;
771 if (!rtcp) {
772 res = srtp_filter_.UnprotectRtp(data, len, &len);
773 if (!res) {
774 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200775 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776 GetRtpSeqNum(data, len, &seq_num);
777 GetRtpSsrc(data, len, &ssrc);
778 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
zstein3dcf0e92017-06-01 13:22:42 -0700779 << " RTP packet: size=" << len << ", seqnum=" << seq_num
780 << ", SSRC=" << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000781 return;
782 }
783 } else {
784 res = srtp_filter_.UnprotectRtcp(data, len, &len);
785 if (!res) {
786 int type = -1;
787 GetRtcpType(data, len, &type);
788 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
789 << " RTCP packet: size=" << len << ", type=" << type;
790 return;
791 }
792 }
793
zstein3dcf0e92017-06-01 13:22:42 -0700794 packet.SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800795 } else if (srtp_required_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796 // Our session description indicates that SRTP is required, but we got a
797 // packet before our SRTP filter is active. This means either that
798 // a) we got SRTP packets before we received the SDES keys, in which case
799 // we can't decrypt it anyway, or
800 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
zhihuangb2cdd932017-01-19 16:54:25 -0800801 // transports, so we haven't yet extracted keys, even if DTLS did
802 // complete on the transport that the packets are being sent on. It's
803 // really good practice to wait for both RTP and RTCP to be good to go
804 // before sending media, to prevent weird failure modes, so it's fine
805 // for us to just eat packets here. This is all sidestepped if RTCP mux
806 // is used anyway.
zstein3dcf0e92017-06-01 13:22:42 -0700807 LOG(LS_WARNING) << "Can't process incoming " << RtpRtcpStringLiteral(rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 << " packet when SRTP is inactive and crypto is required";
809 return;
810 }
811
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200812 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700813 RTC_FROM_HERE, worker_thread_,
zstein3dcf0e92017-06-01 13:22:42 -0700814 Bind(&BaseChannel::ProcessPacket, this, rtcp, packet, packet_time));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200815}
816
zstein3dcf0e92017-06-01 13:22:42 -0700817void BaseChannel::ProcessPacket(bool rtcp,
818 const rtc::CopyOnWriteBuffer& packet,
819 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200820 RTC_DCHECK(worker_thread_->IsCurrent());
zstein3dcf0e92017-06-01 13:22:42 -0700821
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200822 // Need to copy variable because OnRtcpReceived/OnPacketReceived
823 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
824 rtc::CopyOnWriteBuffer data(packet);
825 if (rtcp) {
826 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200828 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 }
830}
831
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000832bool BaseChannel::PushdownLocalDescription(
833 const SessionDescription* local_desc, ContentAction action,
834 std::string* error_desc) {
835 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 const MediaContentDescription* content_desc =
837 GetContentDescription(content_info);
838 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000839 !SetLocalContent(content_desc, action, error_desc)) {
840 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
841 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000843 return true;
844}
845
846bool BaseChannel::PushdownRemoteDescription(
847 const SessionDescription* remote_desc, ContentAction action,
848 std::string* error_desc) {
849 const ContentInfo* content_info = GetFirstContent(remote_desc);
850 const MediaContentDescription* content_desc =
851 GetContentDescription(content_info);
852 if (content_desc && content_info && !content_info->rejected &&
853 !SetRemoteContent(content_desc, action, error_desc)) {
854 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
855 return false;
856 }
857 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858}
859
860void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700861 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 if (enabled_)
863 return;
864
865 LOG(LS_INFO) << "Channel enabled";
866 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700867 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868}
869
870void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700871 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 if (!enabled_)
873 return;
874
875 LOG(LS_INFO) << "Channel disabled";
876 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700877 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878}
879
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200880void BaseChannel::UpdateWritableState_n() {
zsteind48dbda2017-04-04 19:45:57 -0700881 rtc::PacketTransportInternal* rtp_packet_transport =
882 rtp_transport_.rtp_packet_transport();
883 rtc::PacketTransportInternal* rtcp_packet_transport =
884 rtp_transport_.rtcp_packet_transport();
885 if (rtp_packet_transport && rtp_packet_transport->writable() &&
886 (!rtcp_packet_transport || rtcp_packet_transport->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200887 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700888 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200889 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700890 }
891}
892
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200893void BaseChannel::ChannelWritable_n() {
894 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800895 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800897 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898
deadbeefcbecd352015-09-23 11:50:27 -0700899 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 << (was_ever_writable_ ? "" : " for the first time");
901
michaelt79e05882016-11-08 02:50:09 -0800902 if (selected_candidate_pair_)
903 LOG(LS_INFO)
904 << "Using "
905 << selected_candidate_pair_->local_candidate().ToSensitiveString()
906 << "->"
907 << selected_candidate_pair_->remote_candidate().ToSensitiveString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200910 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700912 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913}
914
deadbeef953c2ce2017-01-09 14:53:41 -0800915void BaseChannel::SignalDtlsSrtpSetupFailure_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200916 RTC_DCHECK(network_thread_->IsCurrent());
917 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700918 RTC_FROM_HERE, signaling_thread(),
deadbeef953c2ce2017-01-09 14:53:41 -0800919 Bind(&BaseChannel::SignalDtlsSrtpSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000920}
921
deadbeef953c2ce2017-01-09 14:53:41 -0800922void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700923 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
deadbeef953c2ce2017-01-09 14:53:41 -0800924 SignalDtlsSrtpSetupFailure(this, rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000925}
926
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200927bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
zhihuangb2cdd932017-01-19 16:54:25 -0800928 // Since DTLS is applied to all transports, checking RTP should be enough.
929 return rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930}
931
932// This function returns true if either DTLS-SRTP is not in use
933// *or* DTLS-SRTP is successfully set up.
zhihuangb2cdd932017-01-19 16:54:25 -0800934bool BaseChannel::SetupDtlsSrtp_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200935 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 bool ret = false;
937
zhihuangb2cdd932017-01-19 16:54:25 -0800938 DtlsTransportInternal* transport =
939 rtcp ? rtcp_dtls_transport_ : rtp_dtls_transport_;
deadbeeff5346592017-01-24 21:51:21 -0800940 RTC_DCHECK(transport);
zhihuangb2cdd932017-01-19 16:54:25 -0800941 RTC_DCHECK(transport->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800943 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944
zhihuangb2cdd932017-01-19 16:54:25 -0800945 if (!transport->GetSrtpCryptoSuite(&selected_crypto_suite)) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800946 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 return false;
948 }
949
zhihuangb2cdd932017-01-19 16:54:25 -0800950 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on " << content_name() << " "
zstein3dcf0e92017-06-01 13:22:42 -0700951 << RtpRtcpStringLiteral(rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952
jbauchcb560652016-08-04 05:20:32 -0700953 int key_len;
954 int salt_len;
955 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
956 &salt_len)) {
957 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
958 return false;
959 }
960
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -0700962 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963
964 // RFC 5705 exporter using the RFC 5764 parameters
zhihuangb2cdd932017-01-19 16:54:25 -0800965 if (!transport->ExportKeyingMaterial(kDtlsSrtpExporterLabel, NULL, 0, false,
966 &dtls_buffer[0], dtls_buffer.size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
nisseeb4ca4e2017-01-12 02:24:27 -0800968 RTC_NOTREACHED(); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 return false;
970 }
971
972 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -0700973 std::vector<unsigned char> client_write_key(key_len + salt_len);
974 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -0700976 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
977 offset += key_len;
978 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
979 offset += key_len;
980 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
981 offset += salt_len;
982 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983
984 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000985 rtc::SSLRole role;
zhihuangb2cdd932017-01-19 16:54:25 -0800986 if (!transport->GetSslRole(&role)) {
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000987 LOG(LS_WARNING) << "GetSslRole failed";
988 return false;
989 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000991 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 send_key = &server_write_key;
993 recv_key = &client_write_key;
994 } else {
995 send_key = &client_write_key;
996 recv_key = &server_write_key;
997 }
998
zhihuangb2cdd932017-01-19 16:54:25 -0800999 if (rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001000 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1001 static_cast<int>(send_key->size()),
1002 selected_crypto_suite, &(*recv_key)[0],
1003 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001005 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1006 static_cast<int>(send_key->size()),
1007 selected_crypto_suite, &(*recv_key)[0],
1008 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 }
1010
michaelt79e05882016-11-08 02:50:09 -08001011 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
michaelt79e05882016-11-08 02:50:09 -08001013 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 dtls_keyed_ = true;
michaelt79e05882016-11-08 02:50:09 -08001015 UpdateTransportOverhead();
1016 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017 return ret;
1018}
1019
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001020void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001021 if (srtp_filter_.IsActive()) {
1022 return;
1023 }
1024
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001025 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001026 return;
1027 }
1028
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001029 if (!SetupDtlsSrtp_n(false)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001030 SignalDtlsSrtpSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001031 return;
1032 }
1033
zhihuangb2cdd932017-01-19 16:54:25 -08001034 if (rtcp_dtls_transport_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001035 if (!SetupDtlsSrtp_n(true)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001036 SignalDtlsSrtpSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001037 return;
1038 }
1039 }
1040}
1041
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001042void BaseChannel::ChannelNotWritable_n() {
1043 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 if (!writable_)
1045 return;
1046
deadbeefcbecd352015-09-23 11:50:27 -07001047 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001049 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050}
1051
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001052bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001053 const MediaContentDescription* content,
1054 ContentAction action,
1055 ContentSource src,
1056 std::string* error_desc) {
1057 if (action == CA_UPDATE) {
1058 // These parameters never get changed by a CA_UDPATE.
1059 return true;
1060 }
1061
deadbeef7af91dd2016-12-13 11:29:11 -08001062 // Cache srtp_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001063 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001064 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1065 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001066}
1067
1068bool BaseChannel::SetRtpTransportParameters_n(
1069 const MediaContentDescription* content,
1070 ContentAction action,
1071 ContentSource src,
1072 std::string* error_desc) {
1073 RTC_DCHECK(network_thread_->IsCurrent());
1074
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001075 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001076 return false;
1077 }
1078
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001079 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001080 return false;
1081 }
1082
1083 return true;
1084}
1085
zhihuangb2cdd932017-01-19 16:54:25 -08001086// |dtls| will be set to true if DTLS is active for transport and crypto is
1087// empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001088bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1089 bool* dtls,
1090 std::string* error_desc) {
deadbeeff5346592017-01-24 21:51:21 -08001091 *dtls = rtp_dtls_transport_ && rtp_dtls_transport_->IsDtlsActive();
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001092 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001093 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001094 return false;
1095 }
1096 return true;
1097}
1098
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001099bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001100 ContentAction action,
1101 ContentSource src,
1102 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001103 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001104 if (action == CA_UPDATE) {
1105 // no crypto params.
1106 return true;
1107 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001109 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001110 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001111 if (!ret) {
1112 return false;
1113 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114 switch (action) {
1115 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001116 // If DTLS is already active on the channel, we could be renegotiating
1117 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001118 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001119 ret = srtp_filter_.SetOffer(cryptos, src);
1120 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121 break;
1122 case CA_PRANSWER:
1123 // If we're doing DTLS-SRTP, we don't want to update the filter
1124 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001125 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001126 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1127 }
1128 break;
1129 case CA_ANSWER:
1130 // If we're doing DTLS-SRTP, we don't want to update the filter
1131 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001132 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 ret = srtp_filter_.SetAnswer(cryptos, src);
1134 }
1135 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 default:
1137 break;
1138 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001139 if (!ret) {
1140 SafeSetError("Failed to setup SRTP filter.", error_desc);
1141 return false;
1142 }
1143 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001144}
1145
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001146bool BaseChannel::SetRtcpMux_n(bool enable,
1147 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001148 ContentSource src,
1149 std::string* error_desc) {
deadbeef8e814d72017-01-13 11:34:39 -08001150 // Provide a more specific error message for the RTCP mux "require" policy
1151 // case.
zstein56162b92017-04-24 16:54:35 -07001152 if (rtcp_mux_required_ && !enable) {
deadbeef8e814d72017-01-13 11:34:39 -08001153 SafeSetError(
1154 "rtcpMuxPolicy is 'require', but media description does not "
1155 "contain 'a=rtcp-mux'.",
1156 error_desc);
1157 return false;
1158 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 bool ret = false;
1160 switch (action) {
1161 case CA_OFFER:
1162 ret = rtcp_mux_filter_.SetOffer(enable, src);
1163 break;
1164 case CA_PRANSWER:
zhihuangb2cdd932017-01-19 16:54:25 -08001165 // This may activate RTCP muxing, but we don't yet destroy the transport
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001166 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1168 break;
1169 case CA_ANSWER:
1170 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1171 if (ret && rtcp_mux_filter_.IsActive()) {
deadbeefe814a0d2017-02-25 18:15:09 -08001172 // We permanently activated RTCP muxing; signal that we no longer need
1173 // the RTCP transport.
zsteind48dbda2017-04-04 19:45:57 -07001174 std::string debug_name =
1175 transport_name_.empty()
1176 ? rtp_transport_.rtp_packet_transport()->debug_name()
1177 : transport_name_;
deadbeefe814a0d2017-02-25 18:15:09 -08001178 ;
deadbeefcbecd352015-09-23 11:50:27 -07001179 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
deadbeefe814a0d2017-02-25 18:15:09 -08001180 << "; no longer need RTCP transport for " << debug_name;
zsteind48dbda2017-04-04 19:45:57 -07001181 if (rtp_transport_.rtcp_packet_transport()) {
deadbeeff5346592017-01-24 21:51:21 -08001182 SetTransport_n(true, nullptr, nullptr);
1183 SignalRtcpMuxFullyActive(transport_name_);
zhihuangf5b251b2017-01-12 19:37:48 -08001184 }
deadbeef062ce9f2016-08-26 21:42:15 -07001185 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 }
1187 break;
1188 case CA_UPDATE:
1189 // No RTCP mux info.
1190 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001191 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 default:
1193 break;
1194 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001195 if (!ret) {
1196 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1197 return false;
1198 }
zstein56162b92017-04-24 16:54:35 -07001199 rtp_transport_.SetRtcpMuxEnabled(rtcp_mux_filter_.IsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
zhihuangb2cdd932017-01-19 16:54:25 -08001201 // CA_ANSWER, but we only want to tear down the RTCP transport if we received
1202 // a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001203 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 // If the RTP transport is already writable, then so are we.
zsteind48dbda2017-04-04 19:45:57 -07001205 if (rtp_transport_.rtp_packet_transport()->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001206 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 }
1208 }
1209
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001210 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211}
1212
1213bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001214 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001215 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216}
1217
Peter Boström0c4e06b2015-10-07 12:23:21 +02001218bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001219 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 return media_channel()->RemoveRecvStream(ssrc);
1221}
1222
1223bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001224 ContentAction action,
1225 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001226 if (!(action == CA_OFFER || action == CA_ANSWER ||
1227 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 return false;
1229
1230 // If this is an update, streams only contain streams that have changed.
1231 if (action == CA_UPDATE) {
1232 for (StreamParamsVec::const_iterator it = streams.begin();
1233 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001234 const StreamParams* existing_stream =
1235 GetStreamByIds(local_streams_, it->groupid, it->id);
1236 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 if (media_channel()->AddSendStream(*it)) {
1238 local_streams_.push_back(*it);
1239 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1240 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001241 std::ostringstream desc;
1242 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1243 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 return false;
1245 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001246 } else if (existing_stream && !it->has_ssrcs()) {
1247 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001248 std::ostringstream desc;
1249 desc << "Failed to remove send stream with ssrc "
1250 << it->first_ssrc() << ".";
1251 SafeSetError(desc.str(), error_desc);
1252 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001254 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 } else {
1256 LOG(LS_WARNING) << "Ignore unsupported stream update";
1257 }
1258 }
1259 return true;
1260 }
1261 // Else streams are all the streams we want to send.
1262
1263 // Check for streams that have been removed.
1264 bool ret = true;
1265 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1266 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001267 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001269 std::ostringstream desc;
1270 desc << "Failed to remove send stream with ssrc "
1271 << it->first_ssrc() << ".";
1272 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273 ret = false;
1274 }
1275 }
1276 }
1277 // Check for new streams.
1278 for (StreamParamsVec::const_iterator it = streams.begin();
1279 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001280 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001282 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001283 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001284 std::ostringstream desc;
1285 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1286 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 ret = false;
1288 }
1289 }
1290 }
1291 local_streams_ = streams;
1292 return ret;
1293}
1294
1295bool BaseChannel::UpdateRemoteStreams_w(
1296 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001297 ContentAction action,
1298 std::string* error_desc) {
nisse7ce109a2017-01-31 00:57:56 -08001299 if (!(action == CA_OFFER || action == CA_ANSWER ||
1300 action == CA_PRANSWER || action == CA_UPDATE))
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301 return false;
1302
1303 // If this is an update, streams only contain streams that have changed.
1304 if (action == CA_UPDATE) {
1305 for (StreamParamsVec::const_iterator it = streams.begin();
1306 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001307 const StreamParams* existing_stream =
1308 GetStreamByIds(remote_streams_, it->groupid, it->id);
1309 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 if (AddRecvStream_w(*it)) {
1311 remote_streams_.push_back(*it);
1312 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1313 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001314 std::ostringstream desc;
1315 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1316 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 return false;
1318 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001319 } else if (existing_stream && !it->has_ssrcs()) {
1320 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001321 std::ostringstream desc;
1322 desc << "Failed to remove remote stream with ssrc "
1323 << it->first_ssrc() << ".";
1324 SafeSetError(desc.str(), error_desc);
1325 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001327 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 } else {
1329 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001330 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 << " new stream = " << it->ToString();
1332 }
1333 }
1334 return true;
1335 }
1336 // Else streams are all the streams we want to receive.
1337
1338 // Check for streams that have been removed.
1339 bool ret = true;
1340 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1341 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001342 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001344 std::ostringstream desc;
1345 desc << "Failed to remove remote stream with ssrc "
1346 << it->first_ssrc() << ".";
1347 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 ret = false;
1349 }
1350 }
1351 }
1352 // Check for new streams.
1353 for (StreamParamsVec::const_iterator it = streams.begin();
1354 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001355 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356 if (AddRecvStream_w(*it)) {
1357 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1358 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001359 std::ostringstream desc;
1360 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1361 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001362 ret = false;
1363 }
1364 }
1365 }
1366 remote_streams_ = streams;
1367 return ret;
1368}
1369
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001370void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001371 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001372// Absolute Send Time extension id is used only with external auth,
1373// so do not bother searching for it and making asyncronious call to set
1374// something that is not used.
1375#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001376 const webrtc::RtpExtension* send_time_extension =
1377 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001378 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001379 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001380 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001381 RTC_FROM_HERE, network_thread_,
1382 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1383 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001384#endif
1385}
1386
1387void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1388 int rtp_abs_sendtime_extn_id) {
1389 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001390}
1391
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001392void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001393 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001394 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001395 case MSG_SEND_RTP_PACKET:
1396 case MSG_SEND_RTCP_PACKET: {
1397 RTC_DCHECK(network_thread_->IsCurrent());
1398 SendPacketMessageData* data =
1399 static_cast<SendPacketMessageData*>(pmsg->pdata);
1400 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1401 SendPacket(rtcp, &data->packet, data->options);
1402 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403 break;
1404 }
1405 case MSG_FIRSTPACKETRECEIVED: {
1406 SignalFirstPacketReceived(this);
1407 break;
1408 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409 }
1410}
1411
zstein3dcf0e92017-06-01 13:22:42 -07001412void BaseChannel::AddHandledPayloadType(int payload_type) {
1413 rtp_transport_.AddHandledPayloadType(payload_type);
1414}
1415
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001416void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417 // Flush all remaining RTCP messages. This should only be called in
1418 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001419 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001420 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001421 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1422 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001423 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1424 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 }
1426}
1427
johand89ab142016-10-25 10:50:32 -07001428void BaseChannel::SignalSentPacket_n(
deadbeef5bd5ca32017-02-10 11:31:50 -08001429 rtc::PacketTransportInternal* /* transport */,
johand89ab142016-10-25 10:50:32 -07001430 const rtc::SentPacket& sent_packet) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001431 RTC_DCHECK(network_thread_->IsCurrent());
1432 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001433 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001434 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1435}
1436
1437void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1438 RTC_DCHECK(worker_thread_->IsCurrent());
1439 SignalSentPacket(sent_packet);
1440}
1441
1442VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1443 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001444 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 MediaEngineInterface* media_engine,
1446 VoiceMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001448 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001449 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001450 : BaseChannel(worker_thread,
1451 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001452 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001453 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001454 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001455 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001456 srtp_required),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001457 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001458 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459
1460VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001461 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001462 StopAudioMonitor();
1463 StopMediaMonitor();
1464 // this can't be done in the base class, since it calls a virtual
1465 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001466 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467}
1468
Peter Boström0c4e06b2015-10-07 12:23:21 +02001469bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001470 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001471 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001472 AudioSource* source) {
stefanf79ade12017-06-02 06:44:03 -07001473 return InvokeOnWorker<bool>(
1474 RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
1475 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476}
1477
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478// TODO(juberti): Handle early media the right way. We should get an explicit
1479// ringing message telling us to start playing local ringback, which we cancel
1480// if any early media actually arrives. For now, we do the opposite, which is
1481// to wait 1 second for early media, and start playing local ringback if none
1482// arrives.
1483void VoiceChannel::SetEarlyMedia(bool enable) {
1484 if (enable) {
1485 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001486 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1487 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 } else {
1489 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001490 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491 }
1492}
1493
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494bool VoiceChannel::CanInsertDtmf() {
stefanf79ade12017-06-02 06:44:03 -07001495 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001496 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497}
1498
Peter Boström0c4e06b2015-10-07 12:23:21 +02001499bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1500 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001501 int duration) {
stefanf79ade12017-06-02 06:44:03 -07001502 return InvokeOnWorker<bool>(
1503 RTC_FROM_HERE,
1504 Bind(&VoiceChannel::InsertDtmf_w, this, ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505}
1506
solenberg4bac9c52015-10-09 02:32:53 -07001507bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
stefanf79ade12017-06-02 06:44:03 -07001508 return InvokeOnWorker<bool>(
1509 RTC_FROM_HERE,
1510 Bind(&VoiceMediaChannel::SetOutputVolume, media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001512
Tommif888bb52015-12-12 01:37:01 +01001513void VoiceChannel::SetRawAudioSink(
1514 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001515 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1516 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001517 // passing. So we invoke to our own little routine that gets a pointer to
1518 // our local variable. This is OK since we're synchronously invoking.
stefanf79ade12017-06-02 06:44:03 -07001519 InvokeOnWorker<bool>(RTC_FROM_HERE,
1520 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001521}
1522
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001523webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001524 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001525 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001526}
1527
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001528webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1529 uint32_t ssrc) const {
1530 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001531}
1532
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001533bool VoiceChannel::SetRtpSendParameters(
1534 uint32_t ssrc,
1535 const webrtc::RtpParameters& parameters) {
stefanf79ade12017-06-02 06:44:03 -07001536 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001537 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001538 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001539}
1540
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001541bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1542 webrtc::RtpParameters parameters) {
1543 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1544}
1545
1546webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1547 uint32_t ssrc) const {
1548 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001549 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001550 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1551}
1552
1553webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1554 uint32_t ssrc) const {
1555 return media_channel()->GetRtpReceiveParameters(ssrc);
1556}
1557
1558bool VoiceChannel::SetRtpReceiveParameters(
1559 uint32_t ssrc,
1560 const webrtc::RtpParameters& parameters) {
stefanf79ade12017-06-02 06:44:03 -07001561 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001562 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001563 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1564}
1565
1566bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1567 webrtc::RtpParameters parameters) {
1568 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001569}
1570
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
stefanf79ade12017-06-02 06:44:03 -07001572 return InvokeOnWorker<bool>(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1573 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574}
1575
hbos8d609f62017-04-10 07:39:05 -07001576std::vector<webrtc::RtpSource> VoiceChannel::GetSources(uint32_t ssrc) const {
1577 return worker_thread()->Invoke<std::vector<webrtc::RtpSource>>(
1578 RTC_FROM_HERE,
1579 Bind(&WebRtcVoiceMediaChannel::GetSources,
1580 static_cast<WebRtcVoiceMediaChannel*>(media_channel()), ssrc));
1581}
1582
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583void VoiceChannel::StartMediaMonitor(int cms) {
1584 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001585 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 media_monitor_->SignalUpdate.connect(
1587 this, &VoiceChannel::OnMediaMonitorUpdate);
1588 media_monitor_->Start(cms);
1589}
1590
1591void VoiceChannel::StopMediaMonitor() {
1592 if (media_monitor_) {
1593 media_monitor_->Stop();
1594 media_monitor_->SignalUpdate.disconnect(this);
1595 media_monitor_.reset();
1596 }
1597}
1598
1599void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001600 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 audio_monitor_
1602 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1603 audio_monitor_->Start(cms);
1604}
1605
1606void VoiceChannel::StopAudioMonitor() {
1607 if (audio_monitor_) {
1608 audio_monitor_->Stop();
1609 audio_monitor_.reset();
1610 }
1611}
1612
1613bool VoiceChannel::IsAudioMonitorRunning() const {
1614 return (audio_monitor_.get() != NULL);
1615}
1616
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001618 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619}
1620
1621int VoiceChannel::GetOutputLevel_w() {
1622 return media_channel()->GetOutputLevel();
1623}
1624
1625void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1626 media_channel()->GetActiveStreams(actives);
1627}
1628
zstein3dcf0e92017-06-01 13:22:42 -07001629void VoiceChannel::OnPacketReceived(bool rtcp,
1630 rtc::CopyOnWriteBuffer& packet,
1631 const rtc::PacketTime& packet_time) {
1632 BaseChannel::OnPacketReceived(rtcp, packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 // Set a flag when we've received an RTP packet. If we're waiting for early
1634 // media, this will disable the timeout.
zstein3dcf0e92017-06-01 13:22:42 -07001635 if (!received_media_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 received_media_ = true;
1637 }
1638}
1639
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001640void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001641 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001642 invoker_.AsyncInvoke<void>(
1643 RTC_FROM_HERE, worker_thread_,
1644 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001645}
1646
michaelt79e05882016-11-08 02:50:09 -08001647int BaseChannel::GetTransportOverheadPerPacket() const {
1648 RTC_DCHECK(network_thread_->IsCurrent());
1649
1650 if (!selected_candidate_pair_)
1651 return 0;
1652
1653 int transport_overhead_per_packet = 0;
1654
1655 constexpr int kIpv4Overhaed = 20;
1656 constexpr int kIpv6Overhaed = 40;
1657 transport_overhead_per_packet +=
1658 selected_candidate_pair_->local_candidate().address().family() == AF_INET
1659 ? kIpv4Overhaed
1660 : kIpv6Overhaed;
1661
1662 constexpr int kUdpOverhaed = 8;
1663 constexpr int kTcpOverhaed = 20;
1664 transport_overhead_per_packet +=
1665 selected_candidate_pair_->local_candidate().protocol() ==
1666 TCP_PROTOCOL_NAME
1667 ? kTcpOverhaed
1668 : kUdpOverhaed;
1669
1670 if (secure()) {
1671 int srtp_overhead = 0;
1672 if (srtp_filter_.GetSrtpOverhead(&srtp_overhead))
1673 transport_overhead_per_packet += srtp_overhead;
1674 }
1675
1676 return transport_overhead_per_packet;
1677}
1678
1679void BaseChannel::UpdateTransportOverhead() {
1680 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1681 if (transport_overhead_per_packet)
1682 invoker_.AsyncInvoke<void>(
1683 RTC_FROM_HERE, worker_thread_,
1684 Bind(&MediaChannel::OnTransportOverheadChanged, media_channel_,
1685 transport_overhead_per_packet));
1686}
1687
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001688void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 // Render incoming data if we're the active call, and we have the local
1690 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001691 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001692 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693
1694 // Send outgoing data if we're the active call, we have the remote content,
1695 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001696 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001697 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698
1699 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1700}
1701
1702const ContentInfo* VoiceChannel::GetFirstContent(
1703 const SessionDescription* sdesc) {
1704 return GetFirstAudioContent(sdesc);
1705}
1706
1707bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001708 ContentAction action,
1709 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001710 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001711 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712 LOG(LS_INFO) << "Setting local voice description";
1713
1714 const AudioContentDescription* audio =
1715 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001716 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001717 if (!audio) {
1718 SafeSetError("Can't find audio content in local description.", error_desc);
1719 return false;
1720 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001722 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001723 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 }
1725
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001726 AudioRecvParameters recv_params = last_recv_params_;
1727 RtpParametersFromMediaDescription(audio, &recv_params);
1728 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001729 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001730 error_desc);
1731 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001733 for (const AudioCodec& codec : audio->codecs()) {
zstein3dcf0e92017-06-01 13:22:42 -07001734 AddHandledPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001735 }
1736 last_recv_params_ = recv_params;
1737
1738 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1739 // only give it to the media channel once we have a remote
1740 // description too (without a remote description, we won't be able
1741 // to send them anyway).
1742 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1743 SafeSetError("Failed to set local audio description streams.", error_desc);
1744 return false;
1745 }
1746
1747 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001748 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001749 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750}
1751
1752bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001753 ContentAction action,
1754 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001755 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001756 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 LOG(LS_INFO) << "Setting remote voice description";
1758
1759 const AudioContentDescription* audio =
1760 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001761 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001762 if (!audio) {
1763 SafeSetError("Can't find audio content in remote description.", error_desc);
1764 return false;
1765 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001767 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001768 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 }
1770
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001771 AudioSendParameters send_params = last_send_params_;
1772 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001773 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001774 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001775 }
skvladdc1c62c2016-03-16 19:07:43 -07001776
1777 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1778 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001779 SafeSetError("Failed to set remote audio description send parameters.",
1780 error_desc);
1781 return false;
1782 }
1783 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001785 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1786 // and only give it to the media channel once we have a local
1787 // description too (without a local description, we won't be able to
1788 // recv them anyway).
1789 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1790 SafeSetError("Failed to set remote audio description streams.", error_desc);
1791 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792 }
1793
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001794 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001795 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001796 }
1797
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001798 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001799 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001800 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801}
1802
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803void VoiceChannel::HandleEarlyMediaTimeout() {
1804 // This occurs on the main thread, not the worker thread.
1805 if (!received_media_) {
1806 LOG(LS_INFO) << "No early media received before timeout";
1807 SignalEarlyMediaTimeout(this);
1808 }
1809}
1810
Peter Boström0c4e06b2015-10-07 12:23:21 +02001811bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1812 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001813 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 if (!enabled()) {
1815 return false;
1816 }
solenberg1d63dd02015-12-02 12:35:09 -08001817 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818}
1819
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001820void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 case MSG_EARLYMEDIATIMEOUT:
1823 HandleEarlyMediaTimeout();
1824 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 case MSG_CHANNEL_ERROR: {
1826 VoiceChannelErrorMessageData* data =
1827 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 delete data;
1829 break;
1830 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 default:
1832 BaseChannel::OnMessage(pmsg);
1833 break;
1834 }
1835}
1836
1837void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001838 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 SignalConnectionMonitor(this, infos);
1840}
1841
1842void VoiceChannel::OnMediaMonitorUpdate(
1843 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001844 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 SignalMediaMonitor(this, info);
1846}
1847
1848void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1849 const AudioInfo& info) {
1850 SignalAudioMonitor(this, info);
1851}
1852
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001853VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1854 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001855 rtc::Thread* signaling_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 VideoMediaChannel* media_channel,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08001858 bool rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001859 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001860 : BaseChannel(worker_thread,
1861 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08001862 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001863 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001864 content_name,
deadbeefac22f702017-01-12 21:59:29 -08001865 rtcp_mux_required,
deadbeef7af91dd2016-12-13 11:29:11 -08001866 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001869 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 StopMediaMonitor();
1871 // this can't be done in the base class, since it calls a virtual
1872 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001873
1874 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875}
1876
nisse08582ff2016-02-04 01:24:52 -08001877bool VideoChannel::SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -08001878 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
nisse08582ff2016-02-04 01:24:52 -08001879 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001880 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001881 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882 return true;
1883}
1884
deadbeef5a4a75a2016-06-02 16:23:38 -07001885bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001886 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001887 bool mute,
1888 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -08001889 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
stefanf79ade12017-06-02 06:44:03 -07001890 return InvokeOnWorker<bool>(
1891 RTC_FROM_HERE, Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1892 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001893}
1894
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001895webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001896 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001897 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001898}
1899
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001900webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1901 uint32_t ssrc) const {
1902 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001903}
1904
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001905bool VideoChannel::SetRtpSendParameters(
1906 uint32_t ssrc,
1907 const webrtc::RtpParameters& parameters) {
stefanf79ade12017-06-02 06:44:03 -07001908 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001909 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001910 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001911}
1912
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001913bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1914 webrtc::RtpParameters parameters) {
1915 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1916}
1917
1918webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1919 uint32_t ssrc) const {
1920 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001921 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001922 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
1923}
1924
1925webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
1926 uint32_t ssrc) const {
1927 return media_channel()->GetRtpReceiveParameters(ssrc);
1928}
1929
1930bool VideoChannel::SetRtpReceiveParameters(
1931 uint32_t ssrc,
1932 const webrtc::RtpParameters& parameters) {
stefanf79ade12017-06-02 06:44:03 -07001933 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001934 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001935 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1936}
1937
1938bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1939 webrtc::RtpParameters parameters) {
1940 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001941}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001942
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001943void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 // Send outgoing data if we're the active call, we have the remote content,
1945 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001946 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947 if (!media_channel()->SetSend(send)) {
1948 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1949 // TODO(gangji): Report error back to server.
1950 }
1951
Peter Boström34fbfff2015-09-24 19:20:30 +02001952 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953}
1954
stefanf79ade12017-06-02 06:44:03 -07001955void VideoChannel::FillBitrateInfo(BandwidthEstimationInfo* bwe_info) {
1956 InvokeOnWorker<void>(RTC_FROM_HERE, Bind(&VideoMediaChannel::FillBitrateInfo,
1957 media_channel(), bwe_info));
1958}
1959
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001960bool VideoChannel::GetStats(VideoMediaInfo* stats) {
stefanf79ade12017-06-02 06:44:03 -07001961 return InvokeOnWorker<bool>(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
1962 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963}
1964
1965void VideoChannel::StartMediaMonitor(int cms) {
1966 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001967 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968 media_monitor_->SignalUpdate.connect(
1969 this, &VideoChannel::OnMediaMonitorUpdate);
1970 media_monitor_->Start(cms);
1971}
1972
1973void VideoChannel::StopMediaMonitor() {
1974 if (media_monitor_) {
1975 media_monitor_->Stop();
1976 media_monitor_.reset();
1977 }
1978}
1979
1980const ContentInfo* VideoChannel::GetFirstContent(
1981 const SessionDescription* sdesc) {
1982 return GetFirstVideoContent(sdesc);
1983}
1984
1985bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001986 ContentAction action,
1987 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001988 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001989 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 LOG(LS_INFO) << "Setting local video description";
1991
1992 const VideoContentDescription* video =
1993 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001994 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001995 if (!video) {
1996 SafeSetError("Can't find video content in local description.", error_desc);
1997 return false;
1998 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002000 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002001 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 }
2003
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002004 VideoRecvParameters recv_params = last_recv_params_;
2005 RtpParametersFromMediaDescription(video, &recv_params);
2006 if (!media_channel()->SetRecvParameters(recv_params)) {
2007 SafeSetError("Failed to set local video description recv parameters.",
2008 error_desc);
2009 return false;
2010 }
2011 for (const VideoCodec& codec : video->codecs()) {
zstein3dcf0e92017-06-01 13:22:42 -07002012 AddHandledPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002013 }
2014 last_recv_params_ = recv_params;
2015
2016 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2017 // only give it to the media channel once we have a remote
2018 // description too (without a remote description, we won't be able
2019 // to send them anyway).
2020 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2021 SafeSetError("Failed to set local video description streams.", error_desc);
2022 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 }
2024
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002025 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002026 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002027 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028}
2029
2030bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002031 ContentAction action,
2032 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002033 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002034 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035 LOG(LS_INFO) << "Setting remote video description";
2036
2037 const VideoContentDescription* video =
2038 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002039 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002040 if (!video) {
2041 SafeSetError("Can't find video content in remote description.", error_desc);
2042 return false;
2043 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002045 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002046 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047 }
2048
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002049 VideoSendParameters send_params = last_send_params_;
2050 RtpSendParametersFromMediaDescription(video, &send_params);
2051 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002052 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002053 }
skvladdc1c62c2016-03-16 19:07:43 -07002054
2055 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2056
2057 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002058 SafeSetError("Failed to set remote video description send parameters.",
2059 error_desc);
2060 return false;
2061 }
2062 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002064 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2065 // and only give it to the media channel once we have a local
2066 // description too (without a local description, we won't be able to
2067 // recv them anyway).
2068 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2069 SafeSetError("Failed to set remote video description streams.", error_desc);
2070 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002071 }
2072
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002073 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002074 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002075 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002076
2077 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002078 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002079 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080}
2081
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002082void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 case MSG_CHANNEL_ERROR: {
2085 const VideoChannelErrorMessageData* data =
2086 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 delete data;
2088 break;
2089 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090 default:
2091 BaseChannel::OnMessage(pmsg);
2092 break;
2093 }
2094}
2095
2096void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002097 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 SignalConnectionMonitor(this, infos);
2099}
2100
2101// TODO(pthatcher): Look into removing duplicate code between
2102// audio, video, and data, perhaps by using templates.
2103void VideoChannel::OnMediaMonitorUpdate(
2104 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002105 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 SignalMediaMonitor(this, info);
2107}
2108
deadbeef953c2ce2017-01-09 14:53:41 -08002109RtpDataChannel::RtpDataChannel(rtc::Thread* worker_thread,
2110 rtc::Thread* network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002111 rtc::Thread* signaling_thread,
deadbeef953c2ce2017-01-09 14:53:41 -08002112 DataMediaChannel* media_channel,
deadbeef953c2ce2017-01-09 14:53:41 -08002113 const std::string& content_name,
deadbeefac22f702017-01-12 21:59:29 -08002114 bool rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002115 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002116 : BaseChannel(worker_thread,
2117 network_thread,
zhihuangf5b251b2017-01-12 19:37:48 -08002118 signaling_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002119 media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002120 content_name,
deadbeefac22f702017-01-12 21:59:29 -08002121 rtcp_mux_required,
deadbeef953c2ce2017-01-09 14:53:41 -08002122 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123
deadbeef953c2ce2017-01-09 14:53:41 -08002124RtpDataChannel::~RtpDataChannel() {
2125 TRACE_EVENT0("webrtc", "RtpDataChannel::~RtpDataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 StopMediaMonitor();
2127 // this can't be done in the base class, since it calls a virtual
2128 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002129
2130 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002131}
2132
deadbeeff5346592017-01-24 21:51:21 -08002133bool RtpDataChannel::Init_w(
2134 DtlsTransportInternal* rtp_dtls_transport,
2135 DtlsTransportInternal* rtcp_dtls_transport,
deadbeef5bd5ca32017-02-10 11:31:50 -08002136 rtc::PacketTransportInternal* rtp_packet_transport,
2137 rtc::PacketTransportInternal* rtcp_packet_transport) {
deadbeeff5346592017-01-24 21:51:21 -08002138 if (!BaseChannel::Init_w(rtp_dtls_transport, rtcp_dtls_transport,
2139 rtp_packet_transport, rtcp_packet_transport)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140 return false;
2141 }
deadbeef953c2ce2017-01-09 14:53:41 -08002142 media_channel()->SignalDataReceived.connect(this,
2143 &RtpDataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002144 media_channel()->SignalReadyToSend.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08002145 this, &RtpDataChannel::OnDataChannelReadyToSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146 return true;
2147}
2148
deadbeef953c2ce2017-01-09 14:53:41 -08002149bool RtpDataChannel::SendData(const SendDataParams& params,
2150 const rtc::CopyOnWriteBuffer& payload,
2151 SendDataResult* result) {
stefanf79ade12017-06-02 06:44:03 -07002152 return InvokeOnWorker<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002153 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2154 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002155}
2156
deadbeef953c2ce2017-01-09 14:53:41 -08002157const ContentInfo* RtpDataChannel::GetFirstContent(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002158 const SessionDescription* sdesc) {
2159 return GetFirstDataContent(sdesc);
2160}
2161
deadbeef953c2ce2017-01-09 14:53:41 -08002162bool RtpDataChannel::CheckDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002163 const DataContentDescription* content,
2164 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2166 (content->protocol() == kMediaProtocolDtlsSctp));
deadbeef953c2ce2017-01-09 14:53:41 -08002167 // It's been set before, but doesn't match. That's bad.
2168 if (is_sctp) {
2169 SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.",
2170 error_desc);
2171 return false;
2172 }
2173 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174}
2175
deadbeef953c2ce2017-01-09 14:53:41 -08002176bool RtpDataChannel::SetLocalContent_w(const MediaContentDescription* content,
2177 ContentAction action,
2178 std::string* error_desc) {
2179 TRACE_EVENT0("webrtc", "RtpDataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002180 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181 LOG(LS_INFO) << "Setting local data description";
2182
2183 const DataContentDescription* data =
2184 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002185 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002186 if (!data) {
2187 SafeSetError("Can't find data content in local description.", error_desc);
2188 return false;
2189 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002190
deadbeef953c2ce2017-01-09 14:53:41 -08002191 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192 return false;
2193 }
2194
deadbeef953c2ce2017-01-09 14:53:41 -08002195 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
2196 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 }
2198
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002199 DataRecvParameters recv_params = last_recv_params_;
2200 RtpParametersFromMediaDescription(data, &recv_params);
2201 if (!media_channel()->SetRecvParameters(recv_params)) {
2202 SafeSetError("Failed to set remote data description recv parameters.",
2203 error_desc);
2204 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205 }
deadbeef953c2ce2017-01-09 14:53:41 -08002206 for (const DataCodec& codec : data->codecs()) {
zstein3dcf0e92017-06-01 13:22:42 -07002207 AddHandledPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002208 }
2209 last_recv_params_ = recv_params;
2210
2211 // TODO(pthatcher): Move local streams into DataSendParameters, and
2212 // only give it to the media channel once we have a remote
2213 // description too (without a remote description, we won't be able
2214 // to send them anyway).
2215 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2216 SafeSetError("Failed to set local data description streams.", error_desc);
2217 return false;
2218 }
2219
2220 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002221 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002222 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002223}
2224
deadbeef953c2ce2017-01-09 14:53:41 -08002225bool RtpDataChannel::SetRemoteContent_w(const MediaContentDescription* content,
2226 ContentAction action,
2227 std::string* error_desc) {
2228 TRACE_EVENT0("webrtc", "RtpDataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002229 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230
2231 const DataContentDescription* data =
2232 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002233 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002234 if (!data) {
2235 SafeSetError("Can't find data content in remote description.", error_desc);
2236 return false;
2237 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002238
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002239 // If the remote data doesn't have codecs and isn't an update, it
2240 // must be empty, so ignore it.
2241 if (!data->has_codecs() && action != CA_UPDATE) {
2242 return true;
2243 }
2244
deadbeef953c2ce2017-01-09 14:53:41 -08002245 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002246 return false;
2247 }
2248
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002249 LOG(LS_INFO) << "Setting remote data description";
deadbeef953c2ce2017-01-09 14:53:41 -08002250 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002251 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252 }
2253
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002254 DataSendParameters send_params = last_send_params_;
2255 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2256 if (!media_channel()->SetSendParameters(send_params)) {
2257 SafeSetError("Failed to set remote data description send parameters.",
2258 error_desc);
2259 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002260 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002261 last_send_params_ = send_params;
2262
2263 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2264 // and only give it to the media channel once we have a local
2265 // description too (without a local description, we won't be able to
2266 // recv them anyway).
2267 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2268 SafeSetError("Failed to set remote data description streams.",
2269 error_desc);
2270 return false;
2271 }
2272
2273 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002274 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002275 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276}
2277
deadbeef953c2ce2017-01-09 14:53:41 -08002278void RtpDataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279 // Render incoming data if we're the active call, and we have the local
2280 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002281 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 if (!media_channel()->SetReceive(recv)) {
2283 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2284 }
2285
2286 // Send outgoing data if we're the active call, we have the remote content,
2287 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002288 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289 if (!media_channel()->SetSend(send)) {
2290 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2291 }
2292
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002293 // Trigger SignalReadyToSendData asynchronously.
2294 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002295
2296 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2297}
2298
deadbeef953c2ce2017-01-09 14:53:41 -08002299void RtpDataChannel::OnMessage(rtc::Message* pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300 switch (pmsg->message_id) {
2301 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002302 DataChannelReadyToSendMessageData* data =
2303 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002304 ready_to_send_data_ = data->data();
2305 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002306 delete data;
2307 break;
2308 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309 case MSG_DATARECEIVED: {
2310 DataReceivedMessageData* data =
2311 static_cast<DataReceivedMessageData*>(pmsg->pdata);
deadbeef953c2ce2017-01-09 14:53:41 -08002312 SignalDataReceived(data->params, data->payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313 delete data;
2314 break;
2315 }
2316 case MSG_CHANNEL_ERROR: {
2317 const DataChannelErrorMessageData* data =
2318 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319 delete data;
2320 break;
2321 }
2322 default:
2323 BaseChannel::OnMessage(pmsg);
2324 break;
2325 }
2326}
2327
deadbeef953c2ce2017-01-09 14:53:41 -08002328void RtpDataChannel::OnConnectionMonitorUpdate(
2329 ConnectionMonitor* monitor,
2330 const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002331 SignalConnectionMonitor(this, infos);
2332}
2333
deadbeef953c2ce2017-01-09 14:53:41 -08002334void RtpDataChannel::StartMediaMonitor(int cms) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002335 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002336 rtc::Thread::Current()));
deadbeef953c2ce2017-01-09 14:53:41 -08002337 media_monitor_->SignalUpdate.connect(this,
2338 &RtpDataChannel::OnMediaMonitorUpdate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339 media_monitor_->Start(cms);
2340}
2341
deadbeef953c2ce2017-01-09 14:53:41 -08002342void RtpDataChannel::StopMediaMonitor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343 if (media_monitor_) {
2344 media_monitor_->Stop();
2345 media_monitor_->SignalUpdate.disconnect(this);
2346 media_monitor_.reset();
2347 }
2348}
2349
deadbeef953c2ce2017-01-09 14:53:41 -08002350void RtpDataChannel::OnMediaMonitorUpdate(DataMediaChannel* media_channel,
2351 const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002352 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353 SignalMediaMonitor(this, info);
2354}
2355
deadbeef953c2ce2017-01-09 14:53:41 -08002356void RtpDataChannel::OnDataReceived(const ReceiveDataParams& params,
2357 const char* data,
2358 size_t len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002359 DataReceivedMessageData* msg = new DataReceivedMessageData(
2360 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002361 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002362}
2363
deadbeef953c2ce2017-01-09 14:53:41 -08002364void RtpDataChannel::OnDataChannelError(uint32_t ssrc,
2365 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002366 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2367 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002368 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369}
2370
deadbeef953c2ce2017-01-09 14:53:41 -08002371void RtpDataChannel::OnDataChannelReadyToSend(bool writable) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002372 // This is usded for congestion control to indicate that the stream is ready
2373 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2374 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002375 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002376 new DataChannelReadyToSendMessageData(writable));
2377}
2378
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002379} // namespace cricket