blob: cac0ea2ea893f504470ba075bc3d4445e2c36f31 [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
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010015#include "webrtc/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"
18#include "webrtc/base/common.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"
Peter Boström6f28cf02015-12-07 23:17:15 +010026#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010027#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
29namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000030using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000031
deadbeef2d110be2016-01-13 12:00:26 -080032namespace {
kwiberg31022942016-03-11 14:18:21 -080033// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080034bool SetRawAudioSink_w(VoiceMediaChannel* channel,
35 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080036 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
37 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080038 return true;
39}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020040
41struct SendPacketMessageData : public rtc::MessageData {
42 rtc::CopyOnWriteBuffer packet;
43 rtc::PacketOptions options;
44};
45
isheriff6f8d6862016-05-26 11:24:55 -070046#if defined(ENABLE_EXTERNAL_AUTH)
47// Returns the named header extension if found among all extensions,
48// nullptr otherwise.
49const webrtc::RtpExtension* FindHeaderExtension(
50 const std::vector<webrtc::RtpExtension>& extensions,
51 const std::string& uri) {
52 for (const auto& extension : extensions) {
53 if (extension.uri == uri)
54 return &extension;
55 }
56 return nullptr;
57}
58#endif
59
deadbeef2d110be2016-01-13 12:00:26 -080060} // namespace
61
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000063 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020064 MSG_SEND_RTP_PACKET,
65 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000070 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071};
72
73// Value specified in RFC 5764.
74static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
75
76static const int kAgcMinus10db = -10;
77
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000078static void SafeSetError(const std::string& message, std::string* error_desc) {
79 if (error_desc) {
80 *error_desc = message;
81 }
82}
83
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 : ssrc(in_ssrc), error(in_error) {}
88 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 VoiceMediaChannel::Error error;
90};
91
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000092struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020093 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020095 : ssrc(in_ssrc), error(in_error) {}
96 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 VideoMediaChannel::Error error;
98};
99
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000100struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 : ssrc(in_ssrc), error(in_error) {}
104 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 DataMediaChannel::Error error;
106};
107
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108static const char* PacketType(bool rtcp) {
109 return (!rtcp) ? "RTP" : "RTCP";
110}
111
jbaucheec21bd2016-03-20 06:15:43 -0700112static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 // Check the packet size. We could check the header too if needed.
114 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000115 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
116 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117}
118
119static bool IsReceiveContentDirection(MediaContentDirection direction) {
120 return direction == MD_SENDRECV || direction == MD_RECVONLY;
121}
122
123static bool IsSendContentDirection(MediaContentDirection direction) {
124 return direction == MD_SENDRECV || direction == MD_SENDONLY;
125}
126
127static const MediaContentDescription* GetContentDescription(
128 const ContentInfo* cinfo) {
129 if (cinfo == NULL)
130 return NULL;
131 return static_cast<const MediaContentDescription*>(cinfo->description);
132}
133
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700134template <class Codec>
135void RtpParametersFromMediaDescription(
136 const MediaContentDescriptionImpl<Codec>* desc,
137 RtpParameters<Codec>* params) {
138 // TODO(pthatcher): Remove this once we're sure no one will give us
139 // a description without codecs (currently a CA_UPDATE with just
140 // streams can).
141 if (desc->has_codecs()) {
142 params->codecs = desc->codecs();
143 }
144 // TODO(pthatcher): See if we really need
145 // rtp_header_extensions_set() and remove it if we don't.
146 if (desc->rtp_header_extensions_set()) {
147 params->extensions = desc->rtp_header_extensions();
148 }
deadbeef13871492015-12-09 12:37:51 -0800149 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700150}
151
nisse05103312016-03-16 02:22:50 -0700152template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700153void RtpSendParametersFromMediaDescription(
154 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700155 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700156 RtpParametersFromMediaDescription(desc, send_params);
157 send_params->max_bandwidth_bps = desc->bandwidth();
158}
159
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200160BaseChannel::BaseChannel(rtc::Thread* worker_thread,
161 rtc::Thread* network_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700162 MediaChannel* media_channel,
163 TransportController* transport_controller,
164 const std::string& content_name,
165 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200166 : worker_thread_(worker_thread),
167 network_thread_(network_thread),
168
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 content_name_(content_name),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200170
171 transport_controller_(transport_controller),
deadbeefcbecd352015-09-23 11:50:27 -0700172 rtcp_transport_enabled_(rtcp),
173 transport_channel_(nullptr),
174 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 rtp_ready_to_send_(false),
176 rtcp_ready_to_send_(false),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200177 writable_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 was_ever_writable_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 has_received_packet_(false),
180 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000181 secure_required_(false),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200182 rtp_abs_sendtime_extn_id_(-1),
183
184 media_channel_(media_channel),
185 enabled_(false),
186 local_content_direction_(MD_INACTIVE),
187 remote_content_direction_(MD_INACTIVE) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000188 ASSERT(worker_thread_ == rtc::Thread::Current());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200189 if (transport_controller) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200190 RTC_DCHECK_EQ(network_thread, transport_controller->network_thread());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200191 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 LOG(LS_INFO) << "Created channel for " << content_name;
193}
194
195BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800196 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000197 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000198 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200200 // Eats any outstanding messages or packets.
201 worker_thread_->Clear(&invoker_);
202 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 // We must destroy the media channel before the transport channel, otherwise
204 // the media channel may try to send on the dead transport channel. NULLing
205 // is not an effective strategy since the sends will come on another thread.
206 delete media_channel_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200207 // Note that we don't just call SetTransportChannel_n(nullptr) because that
deadbeefcbecd352015-09-23 11:50:27 -0700208 // would call a pure virtual method which we can't do from a destructor.
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200209 network_thread_->Invoke<void>(
210 Bind(&BaseChannel::DestroyTransportChannels_n, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200211 LOG(LS_INFO) << "Destroyed channel";
212}
213
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200214void BaseChannel::DisconnectTransportChannels_n() {
215 // Send any outstanding RTCP packets.
216 FlushRtcpMessages_n();
217
218 // Stop signals from transport channels, but keep them alive because
219 // media_channel may use them from a different thread.
deadbeefcbecd352015-09-23 11:50:27 -0700220 if (transport_channel_) {
221 DisconnectFromTransportChannel(transport_channel_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200222 }
223 if (rtcp_transport_channel_) {
224 DisconnectFromTransportChannel(rtcp_transport_channel_);
225 }
226
227 // Clear pending read packets/messages.
228 network_thread_->Clear(&invoker_);
229 network_thread_->Clear(this);
230}
231
232void BaseChannel::DestroyTransportChannels_n() {
233 if (transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200234 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700235 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
236 }
237 if (rtcp_transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200238 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700239 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
240 }
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200241 // Clear pending send packets/messages.
242 network_thread_->Clear(&invoker_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200243 network_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244}
245
skvlad6c87a672016-05-17 17:49:52 -0700246bool BaseChannel::Init_w(const std::string* bundle_transport_name) {
247 if (!network_thread_->Invoke<bool>(
248 Bind(&BaseChannel::InitNetwork_n, this, bundle_transport_name))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000249 return false;
250 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000251
wu@webrtc.orgde305012013-10-31 15:40:38 +0000252 // Both RTP and RTCP channels are set, we can call SetInterface on
253 // media channel and it can set network options.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200254 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.orgde305012013-10-31 15:40:38 +0000255 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256 return true;
257}
258
skvlad6c87a672016-05-17 17:49:52 -0700259bool BaseChannel::InitNetwork_n(const std::string* bundle_transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200260 RTC_DCHECK(network_thread_->IsCurrent());
skvlad6c87a672016-05-17 17:49:52 -0700261 const std::string& transport_name =
262 (bundle_transport_name ? *bundle_transport_name : content_name());
263 if (!SetTransport_n(transport_name)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200264 return false;
265 }
266
267 if (!SetDtlsSrtpCryptoSuites_n(transport_channel_, false)) {
268 return false;
269 }
270 if (rtcp_transport_enabled() &&
271 !SetDtlsSrtpCryptoSuites_n(rtcp_transport_channel_, true)) {
272 return false;
273 }
274 return true;
275}
276
wu@webrtc.org78187522013-10-07 23:32:02 +0000277void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200278 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000279 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200280 // Packets arrive on the network thread, processing packets calls virtual
281 // functions, so need to stop this process in Deinit that is called in
282 // derived classes destructor.
283 network_thread_->Invoke<void>(
284 Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000285}
286
deadbeefcbecd352015-09-23 11:50:27 -0700287bool BaseChannel::SetTransport(const std::string& transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200288 return network_thread_->Invoke<bool>(
289 Bind(&BaseChannel::SetTransport_n, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000290}
291
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200292bool BaseChannel::SetTransport_n(const std::string& transport_name) {
293 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000294
deadbeefcbecd352015-09-23 11:50:27 -0700295 if (transport_name == transport_name_) {
296 // Nothing to do if transport name isn't changing
297 return true;
298 }
299
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800300 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
301 // changes and wait until the DTLS handshake is complete to set the newly
302 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200303 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800304 // Set |writable_| to false such that UpdateWritableState_w can set up
305 // DTLS-SRTP when the writable_ becomes true again.
306 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800307 srtp_filter_.ResetParams();
308 }
309
guoweis46383312015-12-17 16:45:59 -0800310 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
deadbeefcbecd352015-09-23 11:50:27 -0700311 if (rtcp_transport_enabled()) {
312 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
313 << " on " << transport_name << " transport ";
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200314 SetRtcpTransportChannel_n(
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200315 transport_controller_->CreateTransportChannel_n(
guoweis46383312015-12-17 16:45:59 -0800316 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP),
317 false /* update_writablity */);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200318 if (!rtcp_transport_channel_) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000319 return false;
320 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000321 }
322
guoweis46383312015-12-17 16:45:59 -0800323 // We're not updating the writablity during the transition state.
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200324 SetTransportChannel_n(transport_controller_->CreateTransportChannel_n(
guoweis46383312015-12-17 16:45:59 -0800325 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200326 if (!transport_channel_) {
guoweis46383312015-12-17 16:45:59 -0800327 return false;
328 }
329
330 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
331 if (rtcp_transport_enabled()) {
332 // We can only update the RTCP ready to send after set_transport_channel has
333 // handled channel writability.
334 SetReadyToSend(
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200335 true, rtcp_transport_channel_ && rtcp_transport_channel_->writable());
guoweis46383312015-12-17 16:45:59 -0800336 }
deadbeefcbecd352015-09-23 11:50:27 -0700337 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000338 return true;
339}
340
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200341void BaseChannel::SetTransportChannel_n(TransportChannel* new_tc) {
342 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000343
344 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700345 if (!old_tc && !new_tc) {
346 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000347 return;
348 }
deadbeefcbecd352015-09-23 11:50:27 -0700349 ASSERT(old_tc != new_tc);
350
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000351 if (old_tc) {
352 DisconnectFromTransportChannel(old_tc);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200353 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700354 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000355 }
356
357 transport_channel_ = new_tc;
358
359 if (new_tc) {
360 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700361 for (const auto& pair : socket_options_) {
362 new_tc->SetOption(pair.first, pair.second);
363 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000364 }
deadbeefcbecd352015-09-23 11:50:27 -0700365
366 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
367 // setting new channel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200368 UpdateWritableState_n();
deadbeefcbecd352015-09-23 11:50:27 -0700369 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000370}
371
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200372void BaseChannel::SetRtcpTransportChannel_n(TransportChannel* new_tc,
373 bool update_writablity) {
374 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000375
376 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700377 if (!old_tc && !new_tc) {
378 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000379 return;
380 }
deadbeefcbecd352015-09-23 11:50:27 -0700381 ASSERT(old_tc != new_tc);
382
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000383 if (old_tc) {
384 DisconnectFromTransportChannel(old_tc);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200385 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700386 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000387 }
388
389 rtcp_transport_channel_ = new_tc;
390
391 if (new_tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200392 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800393 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
394 << "should never happen.";
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000395 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700396 for (const auto& pair : rtcp_socket_options_) {
397 new_tc->SetOption(pair.first, pair.second);
398 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000399 }
deadbeefcbecd352015-09-23 11:50:27 -0700400
guoweis46383312015-12-17 16:45:59 -0800401 if (update_writablity) {
402 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
403 // setting new channel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200404 UpdateWritableState_n();
guoweis46383312015-12-17 16:45:59 -0800405 SetReadyToSend(true, new_tc && new_tc->writable());
406 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000407}
408
409void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200410 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000411
412 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
413 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
414 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800415 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700416 tc->SignalSelectedCandidatePairChanged.connect(
417 this, &BaseChannel::OnSelectedCandidatePairChanged);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200418 tc->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000419}
420
421void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200422 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000423
424 tc->SignalWritableState.disconnect(this);
425 tc->SignalReadPacket.disconnect(this);
426 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800427 tc->SignalDtlsState.disconnect(this);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200428 tc->SignalSelectedCandidatePairChanged.disconnect(this);
429 tc->SignalSentPacket.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000430}
431
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000433 worker_thread_->Invoke<void>(Bind(
434 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
435 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 return true;
437}
438
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000440 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441}
442
Peter Boström0c4e06b2015-10-07 12:23:21 +0200443bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000444 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445}
446
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000447bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000448 return InvokeOnWorker(
449 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000450}
451
Peter Boström0c4e06b2015-10-07 12:23:21 +0200452bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000453 return InvokeOnWorker(
454 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000455}
456
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000458 ContentAction action,
459 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100460 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000461 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
462 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463}
464
465bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000466 ContentAction action,
467 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100468 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000469 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
470 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471}
472
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000474 // We pass in the BaseChannel instead of the transport_channel_
475 // because if the transport_channel_ changes, the ConnectionMonitor
476 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200477 // We pass in the network thread because on that thread connection monitor
478 // will call BaseChannel::GetConnectionStats which must be called on the
479 // network thread.
480 connection_monitor_.reset(
481 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000482 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000484 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485}
486
487void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000488 if (connection_monitor_) {
489 connection_monitor_->Stop();
490 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 }
492}
493
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000494bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200495 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000496 return transport_channel_->GetStats(infos);
497}
498
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200499bool BaseChannel::IsReadyToReceive_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 // Receive data if we are enabled and have local content,
501 return enabled() && IsReceiveContentDirection(local_content_direction_);
502}
503
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200504bool BaseChannel::IsReadyToSend_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 // Send outgoing data if we are enabled, have local and remote content,
506 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800507 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 IsSendContentDirection(local_content_direction_) &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200509 network_thread_->Invoke<bool>(
510 Bind(&BaseChannel::IsTransportReadyToSend_n, this));
511}
512
513bool BaseChannel::IsTransportReadyToSend_n() const {
514 return was_ever_writable() &&
515 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516}
517
jbaucheec21bd2016-03-20 06:15:43 -0700518bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700519 const rtc::PacketOptions& options) {
520 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521}
522
jbaucheec21bd2016-03-20 06:15:43 -0700523bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700524 const rtc::PacketOptions& options) {
525 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526}
527
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000528int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200530 return network_thread_->Invoke<int>(
531 Bind(&BaseChannel::SetOption_n, this, type, opt, value));
532}
533
534int BaseChannel::SetOption_n(SocketType type,
535 rtc::Socket::Option opt,
536 int value) {
537 RTC_DCHECK(network_thread_->IsCurrent());
538 TransportChannel* channel = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000540 case ST_RTP:
541 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700542 socket_options_.push_back(
543 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000544 break;
545 case ST_RTCP:
546 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700547 rtcp_socket_options_.push_back(
548 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000549 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000551 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552}
553
554void BaseChannel::OnWritableState(TransportChannel* channel) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200555 RTC_DCHECK(channel == transport_channel_ ||
556 channel == rtcp_transport_channel_);
557 RTC_DCHECK(network_thread_->IsCurrent());
558 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559}
560
561void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000562 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000563 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000564 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100565 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200567 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568
569 // When using RTCP multiplexing we might get RTCP packets on the RTP
570 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
571 bool rtcp = PacketIsRtcp(channel, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700572 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000573 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574}
575
576void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700577 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
578 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579}
580
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800581void BaseChannel::OnDtlsState(TransportChannel* channel,
582 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200583 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800584 return;
585 }
586
587 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
588 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
589 // cover other scenarios like the whole channel is writable (not just this
590 // TransportChannel) or when TransportChannel is attached after DTLS is
591 // negotiated.
592 if (state != DTLS_TRANSPORT_CONNECTED) {
593 srtp_filter_.ResetParams();
594 }
595}
596
Honghai Zhangcc411c02016-03-29 17:27:21 -0700597void BaseChannel::OnSelectedCandidatePairChanged(
598 TransportChannel* channel,
Honghai Zhang52dce732016-03-31 12:37:31 -0700599 CandidatePairInterface* selected_candidate_pair,
600 int last_sent_packet_id) {
Honghai Zhangcc411c02016-03-29 17:27:21 -0700601 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200602 RTC_DCHECK(network_thread_->IsCurrent());
603 std::string transport_name = channel->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700604 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700605 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700606 network_route = rtc::NetworkRoute(
607 selected_candidate_pair->local_candidate().network_id(),
608 selected_candidate_pair->remote_candidate().network_id(),
609 last_sent_packet_id);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700610 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200611 invoker_.AsyncInvoke<void>(
612 worker_thread_, Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_,
613 transport_name, network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700614}
615
deadbeefcbecd352015-09-23 11:50:27 -0700616void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200617 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700618 if (rtcp) {
619 rtcp_ready_to_send_ = ready;
620 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 rtp_ready_to_send_ = ready;
622 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200624 bool ready_to_send =
625 (rtp_ready_to_send_ &&
626 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
627 (rtcp_ready_to_send_ || !rtcp_transport_channel_));
628
629 invoker_.AsyncInvoke<void>(
630 worker_thread_,
631 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632}
633
634bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
635 const char* data, size_t len) {
636 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000637 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638}
639
stefanc1aeaf02015-10-15 07:26:07 -0700640bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700641 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700642 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200643 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
644 // If the thread is not our network thread, we will post to our network
645 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 // synchronize access to all the pieces of the send path, including
647 // SRTP and the inner workings of the transport channels.
648 // The only downside is that we can't return a proper failure code if
649 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200650 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200652 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
653 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800654 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700655 data->options = options;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200656 network_thread_->Post(this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 return true;
658 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200659 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660
661 // Now that we are on the correct thread, ensure we have a place to send this
662 // packet before doing anything. (We might get RTCP packets that we don't
663 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
664 // transport.
665 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
666 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000667 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 return false;
669 }
670
671 // Protect ourselves against crazy data.
672 if (!ValidPacket(rtcp, packet)) {
673 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000674 << PacketType(rtcp)
675 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 return false;
677 }
678
stefanc1aeaf02015-10-15 07:26:07 -0700679 rtc::PacketOptions updated_options;
680 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 // Protect if needed.
682 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200683 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200685 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000686 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000688 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
689 // inside libsrtp for a RTP packet. A external HMAC module will be writing
690 // a fake HMAC value. This is ONLY done for a RTP packet.
691 // Socket layer will update rtp sendtime extension header if present in
692 // packet with current time before updating the HMAC.
693#if !defined(ENABLE_EXTERNAL_AUTH)
694 res = srtp_filter_.ProtectRtp(
695 data, len, static_cast<int>(packet->capacity()), &len);
696#else
stefanc1aeaf02015-10-15 07:26:07 -0700697 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000698 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000699 res = srtp_filter_.ProtectRtp(
700 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700701 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000702 // If protection succeeds, let's get auth params from srtp.
703 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200704 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000705 int key_len;
706 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700707 &auth_key, &key_len,
708 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000709 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700710 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
711 updated_options.packet_time_params.srtp_auth_key.assign(
712 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000713 }
714 }
715#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 if (!res) {
717 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200718 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 GetRtpSeqNum(data, len, &seq_num);
720 GetRtpSsrc(data, len, &ssrc);
721 LOG(LS_ERROR) << "Failed to protect " << content_name_
722 << " RTP packet: size=" << len
723 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
724 return false;
725 }
726 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000727 res = srtp_filter_.ProtectRtcp(data, len,
728 static_cast<int>(packet->capacity()),
729 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 if (!res) {
731 int type = -1;
732 GetRtcpType(data, len, &type);
733 LOG(LS_ERROR) << "Failed to protect " << content_name_
734 << " RTCP packet: size=" << len << ", type=" << type;
735 return false;
736 }
737 }
738
739 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000740 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 } else if (secure_required_) {
742 // This is a double check for something that supposedly can't happen.
743 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
744 << " packet when SRTP is inactive and crypto is required";
745
746 ASSERT(false);
747 return false;
748 }
749
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000750 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200751 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
752 int ret = channel->SendPacket(packet->data<char>(), packet->size(),
753 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000754 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 if (channel->GetError() == EWOULDBLOCK) {
756 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700757 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 }
759 return false;
760 }
761 return true;
762}
763
jbaucheec21bd2016-03-20 06:15:43 -0700764bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 // Protect ourselves against crazy data.
766 if (!ValidPacket(rtcp, packet)) {
767 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000768 << PacketType(rtcp)
769 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 return false;
771 }
pbos482b12e2015-11-16 10:19:58 -0800772 if (rtcp) {
773 // Permit all (seemingly valid) RTCP packets.
774 return true;
775 }
776 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700777 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778}
779
jbaucheec21bd2016-03-20 06:15:43 -0700780void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000781 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200782 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000783 if (!WantsPacket(rtcp, packet)) {
784 return;
785 }
786
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000787 // We are only interested in the first rtp packet because that
788 // indicates the media has started flowing.
789 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 has_received_packet_ = true;
791 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
792 }
793
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 // Unprotect the packet, if needed.
795 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200796 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200797 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000798 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 bool res;
800 if (!rtcp) {
801 res = srtp_filter_.UnprotectRtp(data, len, &len);
802 if (!res) {
803 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200804 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805 GetRtpSeqNum(data, len, &seq_num);
806 GetRtpSsrc(data, len, &ssrc);
807 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
808 << " RTP packet: size=" << len
809 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
810 return;
811 }
812 } else {
813 res = srtp_filter_.UnprotectRtcp(data, len, &len);
814 if (!res) {
815 int type = -1;
816 GetRtcpType(data, len, &type);
817 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
818 << " RTCP packet: size=" << len << ", type=" << type;
819 return;
820 }
821 }
822
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000823 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 } else if (secure_required_) {
825 // Our session description indicates that SRTP is required, but we got a
826 // packet before our SRTP filter is active. This means either that
827 // a) we got SRTP packets before we received the SDES keys, in which case
828 // we can't decrypt it anyway, or
829 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
830 // channels, so we haven't yet extracted keys, even if DTLS did complete
831 // on the channel that the packets are being sent on. It's really good
832 // practice to wait for both RTP and RTCP to be good to go before sending
833 // media, to prevent weird failure modes, so it's fine for us to just eat
834 // packets here. This is all sidestepped if RTCP mux is used anyway.
835 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
836 << " packet when SRTP is inactive and crypto is required";
837 return;
838 }
839
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200840 invoker_.AsyncInvoke<void>(
841 worker_thread_,
842 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
843}
844
845void BaseChannel::OnPacketReceived(bool rtcp,
846 const rtc::CopyOnWriteBuffer& packet,
847 const rtc::PacketTime& packet_time) {
848 RTC_DCHECK(worker_thread_->IsCurrent());
849 // Need to copy variable because OnRtcpReceived/OnPacketReceived
850 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
851 rtc::CopyOnWriteBuffer data(packet);
852 if (rtcp) {
853 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200855 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856 }
857}
858
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000859bool BaseChannel::PushdownLocalDescription(
860 const SessionDescription* local_desc, ContentAction action,
861 std::string* error_desc) {
862 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 const MediaContentDescription* content_desc =
864 GetContentDescription(content_info);
865 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000866 !SetLocalContent(content_desc, action, error_desc)) {
867 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
868 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000870 return true;
871}
872
873bool BaseChannel::PushdownRemoteDescription(
874 const SessionDescription* remote_desc, ContentAction action,
875 std::string* error_desc) {
876 const ContentInfo* content_info = GetFirstContent(remote_desc);
877 const MediaContentDescription* content_desc =
878 GetContentDescription(content_info);
879 if (content_desc && content_info && !content_info->rejected &&
880 !SetRemoteContent(content_desc, action, error_desc)) {
881 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
882 return false;
883 }
884 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885}
886
887void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000888 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889 if (enabled_)
890 return;
891
892 LOG(LS_INFO) << "Channel enabled";
893 enabled_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200894 ChangeState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895}
896
897void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000898 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 if (!enabled_)
900 return;
901
902 LOG(LS_INFO) << "Channel disabled";
903 enabled_ = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200904 ChangeState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905}
906
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200907void BaseChannel::UpdateWritableState_n() {
deadbeefcbecd352015-09-23 11:50:27 -0700908 if (transport_channel_ && transport_channel_->writable() &&
909 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200910 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700911 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200912 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700913 }
914}
915
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200916void BaseChannel::ChannelWritable_n() {
917 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800918 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800920 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921
deadbeefcbecd352015-09-23 11:50:27 -0700922 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 << (was_ever_writable_ ? "" : " for the first time");
924
925 std::vector<ConnectionInfo> infos;
926 transport_channel_->GetStats(&infos);
927 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
928 it != infos.end(); ++it) {
929 if (it->best_connection) {
930 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
931 << "->" << it->remote_candidate.ToSensitiveString();
932 break;
933 }
934 }
935
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200937 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 writable_ = true;
939 ChangeState();
940}
941
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200942void BaseChannel::SignalDtlsSetupFailure_n(bool rtcp) {
943 RTC_DCHECK(network_thread_->IsCurrent());
944 invoker_.AsyncInvoke<void>(
945 signaling_thread(),
946 Bind(&BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000947}
948
949void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
950 ASSERT(signaling_thread() == rtc::Thread::Current());
951 SignalDtlsSetupFailure(this, rtcp);
952}
953
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200954bool BaseChannel::SetDtlsSrtpCryptoSuites_n(TransportChannel* tc, bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800955 std::vector<int> crypto_suites;
956 // We always use the default SRTP crypto suites for RTCP, but we may use
957 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200959 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800961 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800963 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964}
965
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200966bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800967 // Since DTLS is applied to all channels, checking RTP should be enough.
968 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969}
970
971// This function returns true if either DTLS-SRTP is not in use
972// *or* DTLS-SRTP is successfully set up.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200973bool BaseChannel::SetupDtlsSrtp_n(bool rtcp_channel) {
974 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 bool ret = false;
976
deadbeefcbecd352015-09-23 11:50:27 -0700977 TransportChannel* channel =
978 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800980 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800982 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800984 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
985 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 return false;
987 }
988
989 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
990 << content_name() << " "
991 << PacketType(rtcp_channel);
992
993 // OK, we're now doing DTLS (RFC 5764)
994 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
995 SRTP_MASTER_KEY_SALT_LEN * 2);
996
997 // RFC 5705 exporter using the RFC 5764 parameters
998 if (!channel->ExportKeyingMaterial(
999 kDtlsSrtpExporterLabel,
1000 NULL, 0, false,
1001 &dtls_buffer[0], dtls_buffer.size())) {
1002 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
1003 ASSERT(false); // This should never happen
1004 return false;
1005 }
1006
1007 // Sync up the keys with the DTLS-SRTP interface
1008 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
1009 SRTP_MASTER_KEY_SALT_LEN);
1010 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
1011 SRTP_MASTER_KEY_SALT_LEN);
1012 size_t offset = 0;
1013 memcpy(&client_write_key[0], &dtls_buffer[offset],
1014 SRTP_MASTER_KEY_KEY_LEN);
1015 offset += SRTP_MASTER_KEY_KEY_LEN;
1016 memcpy(&server_write_key[0], &dtls_buffer[offset],
1017 SRTP_MASTER_KEY_KEY_LEN);
1018 offset += SRTP_MASTER_KEY_KEY_LEN;
1019 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
1020 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
1021 offset += SRTP_MASTER_KEY_SALT_LEN;
1022 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
1023 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
1024
1025 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001026 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001027 if (!channel->GetSslRole(&role)) {
1028 LOG(LS_WARNING) << "GetSslRole failed";
1029 return false;
1030 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001032 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 send_key = &server_write_key;
1034 recv_key = &client_write_key;
1035 } else {
1036 send_key = &client_write_key;
1037 recv_key = &server_write_key;
1038 }
1039
1040 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001041 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1042 static_cast<int>(send_key->size()),
1043 selected_crypto_suite, &(*recv_key)[0],
1044 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001046 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1047 static_cast<int>(send_key->size()),
1048 selected_crypto_suite, &(*recv_key)[0],
1049 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 }
1051
1052 if (!ret)
1053 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
1054 else
1055 dtls_keyed_ = true;
1056
1057 return ret;
1058}
1059
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001060void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001061 if (srtp_filter_.IsActive()) {
1062 return;
1063 }
1064
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001065 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001066 return;
1067 }
1068
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001069 if (!SetupDtlsSrtp_n(false)) {
1070 SignalDtlsSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001071 return;
1072 }
1073
1074 if (rtcp_transport_channel_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001075 if (!SetupDtlsSrtp_n(true)) {
1076 SignalDtlsSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001077 return;
1078 }
1079 }
1080}
1081
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001082void BaseChannel::ChannelNotWritable_n() {
1083 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 if (!writable_)
1085 return;
1086
deadbeefcbecd352015-09-23 11:50:27 -07001087 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088 writable_ = false;
1089 ChangeState();
1090}
1091
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001092bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001093 const MediaContentDescription* content,
1094 ContentAction action,
1095 ContentSource src,
1096 std::string* error_desc) {
1097 if (action == CA_UPDATE) {
1098 // These parameters never get changed by a CA_UDPATE.
1099 return true;
1100 }
1101
1102 // Cache secure_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001103 return network_thread_->Invoke<bool>(
1104 Bind(&BaseChannel::SetRtpTransportParameters_n, this, content, action,
1105 src, error_desc));
1106}
1107
1108bool BaseChannel::SetRtpTransportParameters_n(
1109 const MediaContentDescription* content,
1110 ContentAction action,
1111 ContentSource src,
1112 std::string* error_desc) {
1113 RTC_DCHECK(network_thread_->IsCurrent());
1114
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001115 if (src == CS_LOCAL) {
1116 set_secure_required(content->crypto_required() != CT_NONE);
1117 }
1118
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001119 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001120 return false;
1121 }
1122
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001123 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001124 return false;
1125 }
1126
1127 return true;
1128}
1129
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001130// |dtls| will be set to true if DTLS is active for transport channel and
1131// crypto is empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001132bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1133 bool* dtls,
1134 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001135 *dtls = transport_channel_->IsDtlsActive();
1136 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001137 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001138 return false;
1139 }
1140 return true;
1141}
1142
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001143bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001144 ContentAction action,
1145 ContentSource src,
1146 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001147 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001148 if (action == CA_UPDATE) {
1149 // no crypto params.
1150 return true;
1151 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001153 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001154 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001155 if (!ret) {
1156 return false;
1157 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 switch (action) {
1159 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001160 // If DTLS is already active on the channel, we could be renegotiating
1161 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001162 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001163 ret = srtp_filter_.SetOffer(cryptos, src);
1164 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165 break;
1166 case CA_PRANSWER:
1167 // If we're doing DTLS-SRTP, we don't want to update the filter
1168 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001169 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1171 }
1172 break;
1173 case CA_ANSWER:
1174 // If we're doing DTLS-SRTP, we don't want to update the filter
1175 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001176 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 ret = srtp_filter_.SetAnswer(cryptos, src);
1178 }
1179 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 default:
1181 break;
1182 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001183 if (!ret) {
1184 SafeSetError("Failed to setup SRTP filter.", error_desc);
1185 return false;
1186 }
1187 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188}
1189
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001190void BaseChannel::ActivateRtcpMux() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001191 network_thread_->Invoke<void>(Bind(&BaseChannel::ActivateRtcpMux_n, this));
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001192}
1193
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001194void BaseChannel::ActivateRtcpMux_n() {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001195 if (!rtcp_mux_filter_.IsActive()) {
1196 rtcp_mux_filter_.SetActive();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001197 SetRtcpTransportChannel_n(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001198 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001199 }
1200}
1201
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001202bool BaseChannel::SetRtcpMux_n(bool enable,
1203 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001204 ContentSource src,
1205 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 bool ret = false;
1207 switch (action) {
1208 case CA_OFFER:
1209 ret = rtcp_mux_filter_.SetOffer(enable, src);
1210 break;
1211 case CA_PRANSWER:
1212 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1213 break;
1214 case CA_ANSWER:
1215 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1216 if (ret && rtcp_mux_filter_.IsActive()) {
1217 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001218 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1219 << " by destroying RTCP transport channel for "
1220 << transport_name();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001221 SetRtcpTransportChannel_n(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001222 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 }
1224 break;
1225 case CA_UPDATE:
1226 // No RTCP mux info.
1227 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001228 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229 default:
1230 break;
1231 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001232 if (!ret) {
1233 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1234 return false;
1235 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1237 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1238 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001239 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 // If the RTP transport is already writable, then so are we.
1241 if (transport_channel_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001242 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 }
1244 }
1245
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001246 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247}
1248
1249bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001250 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001251 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252}
1253
Peter Boström0c4e06b2015-10-07 12:23:21 +02001254bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001255 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 return media_channel()->RemoveRecvStream(ssrc);
1257}
1258
1259bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001260 ContentAction action,
1261 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1263 action == CA_PRANSWER || action == CA_UPDATE))
1264 return false;
1265
1266 // If this is an update, streams only contain streams that have changed.
1267 if (action == CA_UPDATE) {
1268 for (StreamParamsVec::const_iterator it = streams.begin();
1269 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001270 const StreamParams* existing_stream =
1271 GetStreamByIds(local_streams_, it->groupid, it->id);
1272 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273 if (media_channel()->AddSendStream(*it)) {
1274 local_streams_.push_back(*it);
1275 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1276 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001277 std::ostringstream desc;
1278 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1279 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 return false;
1281 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001282 } else if (existing_stream && !it->has_ssrcs()) {
1283 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001284 std::ostringstream desc;
1285 desc << "Failed to remove send stream with ssrc "
1286 << it->first_ssrc() << ".";
1287 SafeSetError(desc.str(), error_desc);
1288 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001289 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001290 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 } else {
1292 LOG(LS_WARNING) << "Ignore unsupported stream update";
1293 }
1294 }
1295 return true;
1296 }
1297 // Else streams are all the streams we want to send.
1298
1299 // Check for streams that have been removed.
1300 bool ret = true;
1301 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1302 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001303 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001305 std::ostringstream desc;
1306 desc << "Failed to remove send stream with ssrc "
1307 << it->first_ssrc() << ".";
1308 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 ret = false;
1310 }
1311 }
1312 }
1313 // Check for new streams.
1314 for (StreamParamsVec::const_iterator it = streams.begin();
1315 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001316 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001318 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001320 std::ostringstream desc;
1321 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1322 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 ret = false;
1324 }
1325 }
1326 }
1327 local_streams_ = streams;
1328 return ret;
1329}
1330
1331bool BaseChannel::UpdateRemoteStreams_w(
1332 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001333 ContentAction action,
1334 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1336 action == CA_PRANSWER || action == CA_UPDATE))
1337 return false;
1338
1339 // If this is an update, streams only contain streams that have changed.
1340 if (action == CA_UPDATE) {
1341 for (StreamParamsVec::const_iterator it = streams.begin();
1342 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001343 const StreamParams* existing_stream =
1344 GetStreamByIds(remote_streams_, it->groupid, it->id);
1345 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 if (AddRecvStream_w(*it)) {
1347 remote_streams_.push_back(*it);
1348 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1349 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001350 std::ostringstream desc;
1351 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1352 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 return false;
1354 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001355 } else if (existing_stream && !it->has_ssrcs()) {
1356 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001357 std::ostringstream desc;
1358 desc << "Failed to remove remote stream with ssrc "
1359 << it->first_ssrc() << ".";
1360 SafeSetError(desc.str(), error_desc);
1361 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001362 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001363 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364 } else {
1365 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001366 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 << " new stream = " << it->ToString();
1368 }
1369 }
1370 return true;
1371 }
1372 // Else streams are all the streams we want to receive.
1373
1374 // Check for streams that have been removed.
1375 bool ret = true;
1376 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1377 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001378 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001380 std::ostringstream desc;
1381 desc << "Failed to remove remote stream with ssrc "
1382 << it->first_ssrc() << ".";
1383 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 ret = false;
1385 }
1386 }
1387 }
1388 // Check for new streams.
1389 for (StreamParamsVec::const_iterator it = streams.begin();
1390 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001391 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392 if (AddRecvStream_w(*it)) {
1393 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1394 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001395 std::ostringstream desc;
1396 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1397 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001398 ret = false;
1399 }
1400 }
1401 }
1402 remote_streams_ = streams;
1403 return ret;
1404}
1405
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001406void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001407 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001408// Absolute Send Time extension id is used only with external auth,
1409// so do not bother searching for it and making asyncronious call to set
1410// something that is not used.
1411#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001412 const webrtc::RtpExtension* send_time_extension =
1413 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001414 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001415 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001416 invoker_.AsyncInvoke<void>(
1417 network_thread_, Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n,
1418 this, rtp_abs_sendtime_extn_id));
1419#endif
1420}
1421
1422void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1423 int rtp_abs_sendtime_extn_id) {
1424 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001425}
1426
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001427void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001428 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001430 case MSG_SEND_RTP_PACKET:
1431 case MSG_SEND_RTCP_PACKET: {
1432 RTC_DCHECK(network_thread_->IsCurrent());
1433 SendPacketMessageData* data =
1434 static_cast<SendPacketMessageData*>(pmsg->pdata);
1435 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1436 SendPacket(rtcp, &data->packet, data->options);
1437 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 break;
1439 }
1440 case MSG_FIRSTPACKETRECEIVED: {
1441 SignalFirstPacketReceived(this);
1442 break;
1443 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444 }
1445}
1446
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001447void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 // Flush all remaining RTCP messages. This should only be called in
1449 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001450 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001451 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001452 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1453 for (const auto& message : rtcp_messages) {
1454 network_thread_->Send(this, MSG_SEND_RTCP_PACKET, message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 }
1456}
1457
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001458void BaseChannel::SignalSentPacket_n(TransportChannel* /* channel */,
1459 const rtc::SentPacket& sent_packet) {
1460 RTC_DCHECK(network_thread_->IsCurrent());
1461 invoker_.AsyncInvoke<void>(
1462 worker_thread_,
1463 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1464}
1465
1466void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1467 RTC_DCHECK(worker_thread_->IsCurrent());
1468 SignalSentPacket(sent_packet);
1469}
1470
1471VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1472 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 MediaEngineInterface* media_engine,
1474 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001475 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 const std::string& content_name,
1477 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001478 : BaseChannel(worker_thread,
1479 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001480 media_channel,
1481 transport_controller,
1482 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001484 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001485 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486
1487VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001488 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489 StopAudioMonitor();
1490 StopMediaMonitor();
1491 // this can't be done in the base class, since it calls a virtual
1492 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001493 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494}
1495
skvlad6c87a672016-05-17 17:49:52 -07001496bool VoiceChannel::Init_w(const std::string* bundle_transport_name) {
1497 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498 return false;
1499 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 return true;
1501}
1502
Peter Boström0c4e06b2015-10-07 12:23:21 +02001503bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001504 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001505 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001506 AudioSource* source) {
deadbeefcbecd352015-09-23 11:50:27 -07001507 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001508 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509}
1510
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511// TODO(juberti): Handle early media the right way. We should get an explicit
1512// ringing message telling us to start playing local ringback, which we cancel
1513// if any early media actually arrives. For now, we do the opposite, which is
1514// to wait 1 second for early media, and start playing local ringback if none
1515// arrives.
1516void VoiceChannel::SetEarlyMedia(bool enable) {
1517 if (enable) {
1518 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001519 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1520 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 } else {
1522 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001523 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524 }
1525}
1526
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001528 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1529 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530}
1531
Peter Boström0c4e06b2015-10-07 12:23:21 +02001532bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1533 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001534 int duration) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001535 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
solenberg1d63dd02015-12-02 12:35:09 -08001536 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537}
1538
solenberg4bac9c52015-10-09 02:32:53 -07001539bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1540 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1541 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001543
Tommif888bb52015-12-12 01:37:01 +01001544void VoiceChannel::SetRawAudioSink(
1545 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001546 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1547 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001548 // passing. So we invoke to our own little routine that gets a pointer to
1549 // our local variable. This is OK since we're synchronously invoking.
1550 InvokeOnWorker(Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001551}
1552
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001553webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001554 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001555 Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001556}
1557
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001558webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1559 uint32_t ssrc) const {
1560 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001561}
1562
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001563bool VoiceChannel::SetRtpSendParameters(
1564 uint32_t ssrc,
1565 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001566 return InvokeOnWorker(
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001567 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001568}
1569
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001570bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1571 webrtc::RtpParameters parameters) {
1572 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1573}
1574
1575webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1576 uint32_t ssrc) const {
1577 return worker_thread()->Invoke<webrtc::RtpParameters>(
1578 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1579}
1580
1581webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1582 uint32_t ssrc) const {
1583 return media_channel()->GetRtpReceiveParameters(ssrc);
1584}
1585
1586bool VoiceChannel::SetRtpReceiveParameters(
1587 uint32_t ssrc,
1588 const webrtc::RtpParameters& parameters) {
1589 return InvokeOnWorker(
1590 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1591}
1592
1593bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1594 webrtc::RtpParameters parameters) {
1595 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001596}
1597
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001599 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1600 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601}
1602
1603void VoiceChannel::StartMediaMonitor(int cms) {
1604 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001605 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 media_monitor_->SignalUpdate.connect(
1607 this, &VoiceChannel::OnMediaMonitorUpdate);
1608 media_monitor_->Start(cms);
1609}
1610
1611void VoiceChannel::StopMediaMonitor() {
1612 if (media_monitor_) {
1613 media_monitor_->Stop();
1614 media_monitor_->SignalUpdate.disconnect(this);
1615 media_monitor_.reset();
1616 }
1617}
1618
1619void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001620 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 audio_monitor_
1622 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1623 audio_monitor_->Start(cms);
1624}
1625
1626void VoiceChannel::StopAudioMonitor() {
1627 if (audio_monitor_) {
1628 audio_monitor_->Stop();
1629 audio_monitor_.reset();
1630 }
1631}
1632
1633bool VoiceChannel::IsAudioMonitorRunning() const {
1634 return (audio_monitor_.get() != NULL);
1635}
1636
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001638 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639}
1640
1641int VoiceChannel::GetOutputLevel_w() {
1642 return media_channel()->GetOutputLevel();
1643}
1644
1645void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1646 media_channel()->GetActiveStreams(actives);
1647}
1648
1649void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001650 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001651 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001652 int flags) {
1653 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654
1655 // Set a flag when we've received an RTP packet. If we're waiting for early
1656 // media, this will disable the timeout.
1657 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1658 received_media_ = true;
1659 }
1660}
1661
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001662void BaseChannel::ChangeState() {
1663 RTC_DCHECK(network_thread_->IsCurrent());
1664 invoker_.AsyncInvoke<void>(worker_thread_,
1665 Bind(&BaseChannel::ChangeState_w, this));
1666}
1667
1668void VoiceChannel::ChangeState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 // Render incoming data if we're the active call, and we have the local
1670 // content. We receive data on the default channel and multiplexed streams.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001671 bool recv = IsReadyToReceive_w();
solenberg5b14b422015-10-01 04:10:31 -07001672 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673
1674 // Send outgoing data if we're the active call, we have the remote content,
1675 // and we have had some form of connectivity.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001676 bool send = IsReadyToSend_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001677 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678
1679 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1680}
1681
1682const ContentInfo* VoiceChannel::GetFirstContent(
1683 const SessionDescription* sdesc) {
1684 return GetFirstAudioContent(sdesc);
1685}
1686
1687bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001688 ContentAction action,
1689 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001690 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001691 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 LOG(LS_INFO) << "Setting local voice description";
1693
1694 const AudioContentDescription* audio =
1695 static_cast<const AudioContentDescription*>(content);
1696 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001697 if (!audio) {
1698 SafeSetError("Can't find audio content in local description.", error_desc);
1699 return false;
1700 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001702 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001703 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 }
1705
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001706 AudioRecvParameters recv_params = last_recv_params_;
1707 RtpParametersFromMediaDescription(audio, &recv_params);
1708 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001709 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001710 error_desc);
1711 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001713 for (const AudioCodec& codec : audio->codecs()) {
1714 bundle_filter()->AddPayloadType(codec.id);
1715 }
1716 last_recv_params_ = recv_params;
1717
1718 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1719 // only give it to the media channel once we have a remote
1720 // description too (without a remote description, we won't be able
1721 // to send them anyway).
1722 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1723 SafeSetError("Failed to set local audio description streams.", error_desc);
1724 return false;
1725 }
1726
1727 set_local_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001728 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001729 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730}
1731
1732bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001733 ContentAction action,
1734 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001735 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001736 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 LOG(LS_INFO) << "Setting remote voice description";
1738
1739 const AudioContentDescription* audio =
1740 static_cast<const AudioContentDescription*>(content);
1741 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001742 if (!audio) {
1743 SafeSetError("Can't find audio content in remote description.", error_desc);
1744 return false;
1745 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001747 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001748 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 }
1750
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001751 AudioSendParameters send_params = last_send_params_;
1752 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001753 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001754 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001755 }
skvladdc1c62c2016-03-16 19:07:43 -07001756
1757 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1758 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001759 SafeSetError("Failed to set remote audio description send parameters.",
1760 error_desc);
1761 return false;
1762 }
1763 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001765 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1766 // and only give it to the media channel once we have a local
1767 // description too (without a local description, we won't be able to
1768 // recv them anyway).
1769 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1770 SafeSetError("Failed to set remote audio description streams.", error_desc);
1771 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 }
1773
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001774 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001775 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001776 }
1777
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001778 set_remote_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001779 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001780 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781}
1782
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783void VoiceChannel::HandleEarlyMediaTimeout() {
1784 // This occurs on the main thread, not the worker thread.
1785 if (!received_media_) {
1786 LOG(LS_INFO) << "No early media received before timeout";
1787 SignalEarlyMediaTimeout(this);
1788 }
1789}
1790
Peter Boström0c4e06b2015-10-07 12:23:21 +02001791bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1792 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001793 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 if (!enabled()) {
1795 return false;
1796 }
solenberg1d63dd02015-12-02 12:35:09 -08001797 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798}
1799
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001800void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802 case MSG_EARLYMEDIATIMEOUT:
1803 HandleEarlyMediaTimeout();
1804 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 case MSG_CHANNEL_ERROR: {
1806 VoiceChannelErrorMessageData* data =
1807 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 delete data;
1809 break;
1810 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 default:
1812 BaseChannel::OnMessage(pmsg);
1813 break;
1814 }
1815}
1816
1817void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001818 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 SignalConnectionMonitor(this, infos);
1820}
1821
1822void VoiceChannel::OnMediaMonitorUpdate(
1823 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1824 ASSERT(media_channel == this->media_channel());
1825 SignalMediaMonitor(this, info);
1826}
1827
1828void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1829 const AudioInfo& info) {
1830 SignalAudioMonitor(this, info);
1831}
1832
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001833void VoiceChannel::GetSrtpCryptoSuites_n(
1834 std::vector<int>* crypto_suites) const {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001835 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836}
1837
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001838VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1839 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001841 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001843 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001844 : BaseChannel(worker_thread,
1845 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001846 media_channel,
1847 transport_controller,
1848 content_name,
perkjc11b1842016-03-07 17:34:13 -08001849 rtcp) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850
skvlad6c87a672016-05-17 17:49:52 -07001851bool VideoChannel::Init_w(const std::string* bundle_transport_name) {
1852 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 return false;
1854 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 return true;
1856}
1857
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001859 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860 StopMediaMonitor();
1861 // this can't be done in the base class, since it calls a virtual
1862 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001863
1864 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865}
1866
nisse08582ff2016-02-04 01:24:52 -08001867bool VideoChannel::SetSink(uint32_t ssrc,
1868 rtc::VideoSinkInterface<VideoFrame>* sink) {
1869 worker_thread()->Invoke<void>(
1870 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 return true;
1872}
1873
nisse2ded9b12016-04-08 02:23:55 -07001874void VideoChannel::SetSource(
1875 uint32_t ssrc,
1876 rtc::VideoSourceInterface<cricket::VideoFrame>* source) {
1877 worker_thread()->Invoke<void>(
1878 Bind(&VideoMediaChannel::SetSource, media_channel(), ssrc, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879}
1880
Peter Boström0c4e06b2015-10-07 12:23:21 +02001881bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001882 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001883 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001884 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1885 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001886}
1887
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001888webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001889 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001890 Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001891}
1892
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001893webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1894 uint32_t ssrc) const {
1895 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001896}
1897
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001898bool VideoChannel::SetRtpSendParameters(
1899 uint32_t ssrc,
1900 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001901 return InvokeOnWorker(
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001902 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001903}
1904
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001905bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1906 webrtc::RtpParameters parameters) {
1907 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1908}
1909
1910webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1911 uint32_t ssrc) const {
1912 return worker_thread()->Invoke<webrtc::RtpParameters>(
1913 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
1914}
1915
1916webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
1917 uint32_t ssrc) const {
1918 return media_channel()->GetRtpReceiveParameters(ssrc);
1919}
1920
1921bool VideoChannel::SetRtpReceiveParameters(
1922 uint32_t ssrc,
1923 const webrtc::RtpParameters& parameters) {
1924 return InvokeOnWorker(
1925 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1926}
1927
1928bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1929 webrtc::RtpParameters parameters) {
1930 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001931}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001932
1933void VideoChannel::ChangeState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 // Send outgoing data if we're the active call, we have the remote content,
1935 // and we have had some form of connectivity.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001936 bool send = IsReadyToSend_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 if (!media_channel()->SetSend(send)) {
1938 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1939 // TODO(gangji): Report error back to server.
1940 }
1941
Peter Boström34fbfff2015-09-24 19:20:30 +02001942 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943}
1944
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001945bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1946 return InvokeOnWorker(
1947 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948}
1949
1950void VideoChannel::StartMediaMonitor(int cms) {
1951 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001952 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 media_monitor_->SignalUpdate.connect(
1954 this, &VideoChannel::OnMediaMonitorUpdate);
1955 media_monitor_->Start(cms);
1956}
1957
1958void VideoChannel::StopMediaMonitor() {
1959 if (media_monitor_) {
1960 media_monitor_->Stop();
1961 media_monitor_.reset();
1962 }
1963}
1964
1965const ContentInfo* VideoChannel::GetFirstContent(
1966 const SessionDescription* sdesc) {
1967 return GetFirstVideoContent(sdesc);
1968}
1969
1970bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001971 ContentAction action,
1972 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001973 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001974 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975 LOG(LS_INFO) << "Setting local video description";
1976
1977 const VideoContentDescription* video =
1978 static_cast<const VideoContentDescription*>(content);
1979 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001980 if (!video) {
1981 SafeSetError("Can't find video content in local description.", error_desc);
1982 return false;
1983 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001985 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001986 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987 }
1988
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001989 VideoRecvParameters recv_params = last_recv_params_;
1990 RtpParametersFromMediaDescription(video, &recv_params);
1991 if (!media_channel()->SetRecvParameters(recv_params)) {
1992 SafeSetError("Failed to set local video description recv parameters.",
1993 error_desc);
1994 return false;
1995 }
1996 for (const VideoCodec& codec : video->codecs()) {
1997 bundle_filter()->AddPayloadType(codec.id);
1998 }
1999 last_recv_params_ = recv_params;
2000
2001 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2002 // only give it to the media channel once we have a remote
2003 // description too (without a remote description, we won't be able
2004 // to send them anyway).
2005 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2006 SafeSetError("Failed to set local video description streams.", error_desc);
2007 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 }
2009
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002010 set_local_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002011 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002012 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013}
2014
2015bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002016 ContentAction action,
2017 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002018 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002019 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002020 LOG(LS_INFO) << "Setting remote video description";
2021
2022 const VideoContentDescription* video =
2023 static_cast<const VideoContentDescription*>(content);
2024 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002025 if (!video) {
2026 SafeSetError("Can't find video content in remote description.", error_desc);
2027 return false;
2028 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002030 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002031 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 }
2033
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002034 VideoSendParameters send_params = last_send_params_;
2035 RtpSendParametersFromMediaDescription(video, &send_params);
2036 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002037 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002038 }
skvladdc1c62c2016-03-16 19:07:43 -07002039
2040 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2041
2042 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002043 SafeSetError("Failed to set remote video description send parameters.",
2044 error_desc);
2045 return false;
2046 }
2047 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002049 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2050 // and only give it to the media channel once we have a local
2051 // description too (without a local description, we won't be able to
2052 // recv them anyway).
2053 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2054 SafeSetError("Failed to set remote video description streams.", error_desc);
2055 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056 }
2057
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002058 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002059 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002061
2062 set_remote_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002063 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002064 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065}
2066
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002067void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069 case MSG_CHANNEL_ERROR: {
2070 const VideoChannelErrorMessageData* data =
2071 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072 delete data;
2073 break;
2074 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002075 default:
2076 BaseChannel::OnMessage(pmsg);
2077 break;
2078 }
2079}
2080
2081void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002082 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083 SignalConnectionMonitor(this, infos);
2084}
2085
2086// TODO(pthatcher): Look into removing duplicate code between
2087// audio, video, and data, perhaps by using templates.
2088void VideoChannel::OnMediaMonitorUpdate(
2089 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2090 ASSERT(media_channel == this->media_channel());
2091 SignalMediaMonitor(this, info);
2092}
2093
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002094void VideoChannel::GetSrtpCryptoSuites_n(
2095 std::vector<int>* crypto_suites) const {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002096 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097}
2098
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002099DataChannel::DataChannel(rtc::Thread* worker_thread,
2100 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002101 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002102 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 const std::string& content_name,
2104 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002105 : BaseChannel(worker_thread,
2106 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002107 media_channel,
2108 transport_controller,
2109 content_name,
2110 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002111 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002112 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113
2114DataChannel::~DataChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08002115 TRACE_EVENT0("webrtc", "DataChannel::~DataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116 StopMediaMonitor();
2117 // this can't be done in the base class, since it calls a virtual
2118 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002119
2120 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121}
2122
skvlad6c87a672016-05-17 17:49:52 -07002123bool DataChannel::Init_w(const std::string* bundle_transport_name) {
2124 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 return false;
2126 }
2127 media_channel()->SignalDataReceived.connect(
2128 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002129 media_channel()->SignalReadyToSend.connect(
2130 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002131 media_channel()->SignalStreamClosedRemotely.connect(
2132 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 return true;
2134}
2135
2136bool DataChannel::SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07002137 const rtc::CopyOnWriteBuffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002139 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2140 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141}
2142
2143const ContentInfo* DataChannel::GetFirstContent(
2144 const SessionDescription* sdesc) {
2145 return GetFirstDataContent(sdesc);
2146}
2147
jbaucheec21bd2016-03-20 06:15:43 -07002148bool DataChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 if (data_channel_type_ == DCT_SCTP) {
2150 // TODO(pthatcher): Do this in a more robust way by checking for
2151 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002152 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153 } else if (data_channel_type_ == DCT_RTP) {
2154 return BaseChannel::WantsPacket(rtcp, packet);
2155 }
2156 return false;
2157}
2158
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002159bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2160 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161 // It hasn't been set before, so set it now.
2162 if (data_channel_type_ == DCT_NONE) {
2163 data_channel_type_ = new_data_channel_type;
2164 return true;
2165 }
2166
2167 // It's been set before, but doesn't match. That's bad.
2168 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002169 std::ostringstream desc;
2170 desc << "Data channel type mismatch."
2171 << " Expected " << data_channel_type_
2172 << " Got " << new_data_channel_type;
2173 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 return false;
2175 }
2176
2177 // It's hasn't changed. Nothing to do.
2178 return true;
2179}
2180
2181bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002182 const DataContentDescription* content,
2183 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002184 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2185 (content->protocol() == kMediaProtocolDtlsSctp));
2186 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002187 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188}
2189
2190bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002191 ContentAction action,
2192 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002193 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002194 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 LOG(LS_INFO) << "Setting local data description";
2196
2197 const DataContentDescription* data =
2198 static_cast<const DataContentDescription*>(content);
2199 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002200 if (!data) {
2201 SafeSetError("Can't find data content in local description.", error_desc);
2202 return false;
2203 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002205 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 return false;
2207 }
2208
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002209 if (data_channel_type_ == DCT_RTP) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002210 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002211 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212 }
2213 }
2214
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002215 // FYI: We send the SCTP port number (not to be confused with the
2216 // underlying UDP port number) as a codec parameter. So even SCTP
2217 // data channels need codecs.
2218 DataRecvParameters recv_params = last_recv_params_;
2219 RtpParametersFromMediaDescription(data, &recv_params);
2220 if (!media_channel()->SetRecvParameters(recv_params)) {
2221 SafeSetError("Failed to set remote data description recv parameters.",
2222 error_desc);
2223 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002225 if (data_channel_type_ == DCT_RTP) {
2226 for (const DataCodec& codec : data->codecs()) {
2227 bundle_filter()->AddPayloadType(codec.id);
2228 }
2229 }
2230 last_recv_params_ = recv_params;
2231
2232 // TODO(pthatcher): Move local streams into DataSendParameters, and
2233 // only give it to the media channel once we have a remote
2234 // description too (without a remote description, we won't be able
2235 // to send them anyway).
2236 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2237 SafeSetError("Failed to set local data description streams.", error_desc);
2238 return false;
2239 }
2240
2241 set_local_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002242 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002243 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002244}
2245
2246bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002247 ContentAction action,
2248 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002249 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002250 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251
2252 const DataContentDescription* data =
2253 static_cast<const DataContentDescription*>(content);
2254 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002255 if (!data) {
2256 SafeSetError("Can't find data content in remote description.", error_desc);
2257 return false;
2258 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002260 // If the remote data doesn't have codecs and isn't an update, it
2261 // must be empty, so ignore it.
2262 if (!data->has_codecs() && action != CA_UPDATE) {
2263 return true;
2264 }
2265
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002266 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267 return false;
2268 }
2269
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002270 LOG(LS_INFO) << "Setting remote data description";
2271 if (data_channel_type_ == DCT_RTP &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002272 !SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002273 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274 }
2275
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002276
2277 DataSendParameters send_params = last_send_params_;
2278 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2279 if (!media_channel()->SetSendParameters(send_params)) {
2280 SafeSetError("Failed to set remote data description send parameters.",
2281 error_desc);
2282 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002283 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002284 last_send_params_ = send_params;
2285
2286 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2287 // and only give it to the media channel once we have a local
2288 // description too (without a local description, we won't be able to
2289 // recv them anyway).
2290 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2291 SafeSetError("Failed to set remote data description streams.",
2292 error_desc);
2293 return false;
2294 }
2295
2296 set_remote_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002297 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002298 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299}
2300
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002301void DataChannel::ChangeState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002302 // Render incoming data if we're the active call, and we have the local
2303 // content. We receive data on the default channel and multiplexed streams.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002304 bool recv = IsReadyToReceive_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002305 if (!media_channel()->SetReceive(recv)) {
2306 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2307 }
2308
2309 // Send outgoing data if we're the active call, we have the remote content,
2310 // and we have had some form of connectivity.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002311 bool send = IsReadyToSend_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312 if (!media_channel()->SetSend(send)) {
2313 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2314 }
2315
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002316 // Trigger SignalReadyToSendData asynchronously.
2317 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002318
2319 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2320}
2321
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002322void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 switch (pmsg->message_id) {
2324 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002325 DataChannelReadyToSendMessageData* data =
2326 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002327 ready_to_send_data_ = data->data();
2328 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 delete data;
2330 break;
2331 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332 case MSG_DATARECEIVED: {
2333 DataReceivedMessageData* data =
2334 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2335 SignalDataReceived(this, data->params, data->payload);
2336 delete data;
2337 break;
2338 }
2339 case MSG_CHANNEL_ERROR: {
2340 const DataChannelErrorMessageData* data =
2341 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342 delete data;
2343 break;
2344 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002345 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002346 rtc::TypedMessageData<uint32_t>* data =
2347 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002348 SignalStreamClosedRemotely(data->data());
2349 delete data;
2350 break;
2351 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352 default:
2353 BaseChannel::OnMessage(pmsg);
2354 break;
2355 }
2356}
2357
2358void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002359 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 SignalConnectionMonitor(this, infos);
2361}
2362
2363void DataChannel::StartMediaMonitor(int cms) {
2364 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002365 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002366 media_monitor_->SignalUpdate.connect(
2367 this, &DataChannel::OnMediaMonitorUpdate);
2368 media_monitor_->Start(cms);
2369}
2370
2371void DataChannel::StopMediaMonitor() {
2372 if (media_monitor_) {
2373 media_monitor_->Stop();
2374 media_monitor_->SignalUpdate.disconnect(this);
2375 media_monitor_.reset();
2376 }
2377}
2378
2379void DataChannel::OnMediaMonitorUpdate(
2380 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2381 ASSERT(media_channel == this->media_channel());
2382 SignalMediaMonitor(this, info);
2383}
2384
2385void DataChannel::OnDataReceived(
2386 const ReceiveDataParams& params, const char* data, size_t len) {
2387 DataReceivedMessageData* msg = new DataReceivedMessageData(
2388 params, data, len);
2389 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2390}
2391
Peter Boström0c4e06b2015-10-07 12:23:21 +02002392void DataChannel::OnDataChannelError(uint32_t ssrc,
2393 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002394 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2395 ssrc, err);
2396 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2397}
2398
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002399void DataChannel::OnDataChannelReadyToSend(bool writable) {
2400 // This is usded for congestion control to indicate that the stream is ready
2401 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2402 // that the transport channel is ready.
2403 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2404 new DataChannelReadyToSendMessageData(writable));
2405}
2406
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002407void DataChannel::GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002408 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002409}
2410
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002411bool DataChannel::ShouldSetupDtlsSrtp_n() const {
2412 return data_channel_type_ == DCT_RTP && BaseChannel::ShouldSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413}
2414
Peter Boström0c4e06b2015-10-07 12:23:21 +02002415void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2416 rtc::TypedMessageData<uint32_t>* message =
2417 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002418 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2419}
2420
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002421} // namespace cricket