blob: 92945fd92b6d4a21c953b23f680aee5040ba589d [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwiberg0eb15ed2015-12-17 03:04:15 -080011#include <utility>
12
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010013#include "webrtc/pc/channel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
kjellandera69d9732016-08-31 07:33:05 -070015#include "webrtc/api/call/audio_sink.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000016#include "webrtc/base/bind.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000017#include "webrtc/base/byteorder.h"
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -070018#include "webrtc/base/checks.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000019#include "webrtc/base/common.h"
jbaucheec21bd2016-03-20 06:15:43 -070020#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000021#include "webrtc/base/dscp.h"
22#include "webrtc/base/logging.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070023#include "webrtc/base/networkroute.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010024#include "webrtc/base/trace_event.h"
kjellanderf4752772016-03-02 05:42:30 -080025#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080026#include "webrtc/media/base/rtputils.h"
johand89ab142016-10-25 10:50:32 -070027#include "webrtc/p2p/base/packettransportinterface.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010028#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010029#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
31namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000033
deadbeef2d110be2016-01-13 12:00:26 -080034namespace {
kwiberg31022942016-03-11 14:18:21 -080035// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080036bool SetRawAudioSink_w(VoiceMediaChannel* channel,
37 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080038 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
39 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080040 return true;
41}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020042
43struct SendPacketMessageData : public rtc::MessageData {
44 rtc::CopyOnWriteBuffer packet;
45 rtc::PacketOptions options;
46};
47
isheriff6f8d6862016-05-26 11:24:55 -070048#if defined(ENABLE_EXTERNAL_AUTH)
49// Returns the named header extension if found among all extensions,
50// nullptr otherwise.
51const webrtc::RtpExtension* FindHeaderExtension(
52 const std::vector<webrtc::RtpExtension>& extensions,
53 const std::string& uri) {
54 for (const auto& extension : extensions) {
55 if (extension.uri == uri)
56 return &extension;
57 }
58 return nullptr;
59}
60#endif
61
deadbeef2d110be2016-01-13 12:00:26 -080062} // namespace
63
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000065 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020066 MSG_SEND_RTP_PACKET,
67 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000072 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073};
74
75// Value specified in RFC 5764.
76static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
77
78static const int kAgcMinus10db = -10;
79
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000080static void SafeSetError(const std::string& message, std::string* error_desc) {
81 if (error_desc) {
82 *error_desc = message;
83 }
84}
85
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000086struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 : ssrc(in_ssrc), error(in_error) {}
90 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 VoiceMediaChannel::Error error;
92};
93
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000094struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020095 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020097 : ssrc(in_ssrc), error(in_error) {}
98 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 VideoMediaChannel::Error error;
100};
101
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000102struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200105 : ssrc(in_ssrc), error(in_error) {}
106 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 DataMediaChannel::Error error;
108};
109
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110static const char* PacketType(bool rtcp) {
111 return (!rtcp) ? "RTP" : "RTCP";
112}
113
jbaucheec21bd2016-03-20 06:15:43 -0700114static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 // Check the packet size. We could check the header too if needed.
116 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000117 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
118 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119}
120
121static bool IsReceiveContentDirection(MediaContentDirection direction) {
122 return direction == MD_SENDRECV || direction == MD_RECVONLY;
123}
124
125static bool IsSendContentDirection(MediaContentDirection direction) {
126 return direction == MD_SENDRECV || direction == MD_SENDONLY;
127}
128
129static const MediaContentDescription* GetContentDescription(
130 const ContentInfo* cinfo) {
131 if (cinfo == NULL)
132 return NULL;
133 return static_cast<const MediaContentDescription*>(cinfo->description);
134}
135
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700136template <class Codec>
137void RtpParametersFromMediaDescription(
138 const MediaContentDescriptionImpl<Codec>* desc,
139 RtpParameters<Codec>* params) {
140 // TODO(pthatcher): Remove this once we're sure no one will give us
141 // a description without codecs (currently a CA_UPDATE with just
142 // streams can).
143 if (desc->has_codecs()) {
144 params->codecs = desc->codecs();
145 }
146 // TODO(pthatcher): See if we really need
147 // rtp_header_extensions_set() and remove it if we don't.
148 if (desc->rtp_header_extensions_set()) {
149 params->extensions = desc->rtp_header_extensions();
150 }
deadbeef13871492015-12-09 12:37:51 -0800151 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700152}
153
nisse05103312016-03-16 02:22:50 -0700154template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700155void RtpSendParametersFromMediaDescription(
156 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700157 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700158 RtpParametersFromMediaDescription(desc, send_params);
159 send_params->max_bandwidth_bps = desc->bandwidth();
160}
161
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200162BaseChannel::BaseChannel(rtc::Thread* worker_thread,
163 rtc::Thread* network_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700164 MediaChannel* media_channel,
165 TransportController* transport_controller,
166 const std::string& content_name,
167 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200168 : worker_thread_(worker_thread),
169 network_thread_(network_thread),
170
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 content_name_(content_name),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200172
173 transport_controller_(transport_controller),
deadbeef23d947d2016-08-22 16:00:30 -0700174 rtcp_enabled_(rtcp),
175 media_channel_(media_channel) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700176 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200177 if (transport_controller) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200178 RTC_DCHECK_EQ(network_thread, transport_controller->network_thread());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200179 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 LOG(LS_INFO) << "Created channel for " << content_name;
181}
182
183BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800184 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700185 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000186 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200188 // Eats any outstanding messages or packets.
189 worker_thread_->Clear(&invoker_);
190 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 // We must destroy the media channel before the transport channel, otherwise
192 // the media channel may try to send on the dead transport channel. NULLing
193 // is not an effective strategy since the sends will come on another thread.
194 delete media_channel_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200195 // Note that we don't just call SetTransportChannel_n(nullptr) because that
deadbeefcbecd352015-09-23 11:50:27 -0700196 // would call a pure virtual method which we can't do from a destructor.
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200197 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700198 RTC_FROM_HERE, Bind(&BaseChannel::DestroyTransportChannels_n, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200199 LOG(LS_INFO) << "Destroyed channel";
200}
201
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200202void BaseChannel::DisconnectTransportChannels_n() {
203 // Send any outstanding RTCP packets.
204 FlushRtcpMessages_n();
205
206 // Stop signals from transport channels, but keep them alive because
207 // media_channel may use them from a different thread.
deadbeefcbecd352015-09-23 11:50:27 -0700208 if (transport_channel_) {
209 DisconnectFromTransportChannel(transport_channel_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200210 }
211 if (rtcp_transport_channel_) {
212 DisconnectFromTransportChannel(rtcp_transport_channel_);
213 }
214
215 // Clear pending read packets/messages.
216 network_thread_->Clear(&invoker_);
217 network_thread_->Clear(this);
218}
219
220void BaseChannel::DestroyTransportChannels_n() {
221 if (transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200222 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700223 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
224 }
225 if (rtcp_transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200226 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700227 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
228 }
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200229 // Clear pending send packets/messages.
230 network_thread_->Clear(&invoker_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200231 network_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232}
233
skvlad6c87a672016-05-17 17:49:52 -0700234bool BaseChannel::Init_w(const std::string* bundle_transport_name) {
235 if (!network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700236 RTC_FROM_HERE,
skvlad6c87a672016-05-17 17:49:52 -0700237 Bind(&BaseChannel::InitNetwork_n, this, bundle_transport_name))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000238 return false;
239 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240
wu@webrtc.orgde305012013-10-31 15:40:38 +0000241 // Both RTP and RTCP channels are set, we can call SetInterface on
242 // media channel and it can set network options.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200243 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.orgde305012013-10-31 15:40:38 +0000244 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000245 return true;
246}
247
skvlad6c87a672016-05-17 17:49:52 -0700248bool BaseChannel::InitNetwork_n(const std::string* bundle_transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200249 RTC_DCHECK(network_thread_->IsCurrent());
skvlad6c87a672016-05-17 17:49:52 -0700250 const std::string& transport_name =
251 (bundle_transport_name ? *bundle_transport_name : content_name());
252 if (!SetTransport_n(transport_name)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200253 return false;
254 }
255
256 if (!SetDtlsSrtpCryptoSuites_n(transport_channel_, false)) {
257 return false;
258 }
deadbeef23d947d2016-08-22 16:00:30 -0700259 if (rtcp_transport_channel_ &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200260 !SetDtlsSrtpCryptoSuites_n(rtcp_transport_channel_, true)) {
261 return false;
262 }
263 return true;
264}
265
wu@webrtc.org78187522013-10-07 23:32:02 +0000266void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200267 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000268 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200269 // Packets arrive on the network thread, processing packets calls virtual
270 // functions, so need to stop this process in Deinit that is called in
271 // derived classes destructor.
272 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700273 RTC_FROM_HERE, Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000274}
275
deadbeefcbecd352015-09-23 11:50:27 -0700276bool BaseChannel::SetTransport(const std::string& transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200277 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700278 RTC_FROM_HERE, Bind(&BaseChannel::SetTransport_n, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000279}
280
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200281bool BaseChannel::SetTransport_n(const std::string& transport_name) {
282 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000283
deadbeefcbecd352015-09-23 11:50:27 -0700284 if (transport_name == transport_name_) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700285 // Nothing to do if transport name isn't changing.
deadbeefcbecd352015-09-23 11:50:27 -0700286 return true;
287 }
288
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800289 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
290 // changes and wait until the DTLS handshake is complete to set the newly
291 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200292 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800293 // Set |writable_| to false such that UpdateWritableState_w can set up
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700294 // DTLS-SRTP when |writable_| becomes true again.
guoweis46383312015-12-17 16:45:59 -0800295 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800296 srtp_filter_.ResetParams();
297 }
298
deadbeef23d947d2016-08-22 16:00:30 -0700299 // If this BaseChannel uses RTCP and we haven't fully negotiated RTCP mux,
300 // we need an RTCP channel.
301 if (rtcp_enabled_ && !rtcp_mux_filter_.IsFullyActive()) {
deadbeefcbecd352015-09-23 11:50:27 -0700302 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
303 << " on " << transport_name << " transport ";
deadbeef062ce9f2016-08-26 21:42:15 -0700304 SetTransportChannel_n(
305 true, transport_controller_->CreateTransportChannel_n(
306 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200307 if (!rtcp_transport_channel_) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000308 return false;
309 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000310 }
311
deadbeef062ce9f2016-08-26 21:42:15 -0700312 LOG(LS_INFO) << "Create non-RTCP TransportChannel for " << content_name()
313 << " on " << transport_name << " transport ";
314 SetTransportChannel_n(
315 false, transport_controller_->CreateTransportChannel_n(
316 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200317 if (!transport_channel_) {
guoweis46383312015-12-17 16:45:59 -0800318 return false;
319 }
320
deadbeefcbecd352015-09-23 11:50:27 -0700321 transport_name_ = transport_name;
deadbeefcbecd352015-09-23 11:50:27 -0700322
323 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700324 // setting new transport channels.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200325 UpdateWritableState_n();
deadbeef062ce9f2016-08-26 21:42:15 -0700326 // We can only update ready-to-send after updating writability.
327 //
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700328 // On setting a new channel, assume it's ready to send if it's writable,
329 // because we have no way of knowing otherwise (the channel doesn't give us
330 // "was last send successful?").
331 //
332 // This won't always be accurate (the last SendPacket call from another
333 // BaseChannel could have resulted in an error), but even so, we'll just
334 // encounter the error again and update "ready to send" accordingly.
deadbeef062ce9f2016-08-26 21:42:15 -0700335 SetTransportChannelReadyToSend(
336 false, transport_channel_ && transport_channel_->writable());
337 SetTransportChannelReadyToSend(
338 true, rtcp_transport_channel_ && rtcp_transport_channel_->writable());
339 return true;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000340}
341
deadbeef062ce9f2016-08-26 21:42:15 -0700342void BaseChannel::SetTransportChannel_n(bool rtcp,
343 TransportChannel* new_channel) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200344 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef062ce9f2016-08-26 21:42:15 -0700345 TransportChannel*& old_channel =
346 rtcp ? rtcp_transport_channel_ : transport_channel_;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000347
deadbeef062ce9f2016-08-26 21:42:15 -0700348 if (!old_channel && !new_channel) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700349 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000350 return;
351 }
deadbeef062ce9f2016-08-26 21:42:15 -0700352 RTC_DCHECK(old_channel != new_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700353
deadbeef062ce9f2016-08-26 21:42:15 -0700354 if (old_channel) {
355 DisconnectFromTransportChannel(old_channel);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200356 transport_controller_->DestroyTransportChannel_n(
deadbeef062ce9f2016-08-26 21:42:15 -0700357 transport_name_, rtcp ? cricket::ICE_CANDIDATE_COMPONENT_RTCP
358 : cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000359 }
360
deadbeef062ce9f2016-08-26 21:42:15 -0700361 old_channel = new_channel;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000362
deadbeef062ce9f2016-08-26 21:42:15 -0700363 if (new_channel) {
364 if (rtcp) {
365 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
366 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
367 << "should never happen.";
deadbeefcbecd352015-09-23 11:50:27 -0700368 }
deadbeef062ce9f2016-08-26 21:42:15 -0700369 ConnectToTransportChannel(new_channel);
370 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
371 for (const auto& pair : socket_options) {
372 new_channel->SetOption(pair.first, pair.second);
373 }
guoweis46383312015-12-17 16:45:59 -0800374 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000375}
376
377void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200378 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000379
380 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
johand89ab142016-10-25 10:50:32 -0700381 tc->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000382 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800383 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700384 tc->SignalSelectedCandidatePairChanged.connect(
385 this, &BaseChannel::OnSelectedCandidatePairChanged);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200386 tc->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000387}
388
389void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200390 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000391
392 tc->SignalWritableState.disconnect(this);
393 tc->SignalReadPacket.disconnect(this);
394 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800395 tc->SignalDtlsState.disconnect(this);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200396 tc->SignalSelectedCandidatePairChanged.disconnect(this);
397 tc->SignalSentPacket.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000398}
399
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700401 worker_thread_->Invoke<void>(
402 RTC_FROM_HERE,
403 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
404 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 return true;
406}
407
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408bool BaseChannel::AddRecvStream(const StreamParams& sp) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700409 return InvokeOnWorker(RTC_FROM_HERE,
410 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411}
412
Peter Boström0c4e06b2015-10-07 12:23:21 +0200413bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700414 return InvokeOnWorker(RTC_FROM_HERE,
415 Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416}
417
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000418bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000419 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700420 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000421}
422
Peter Boström0c4e06b2015-10-07 12:23:21 +0200423bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700424 return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream,
425 media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000426}
427
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000429 ContentAction action,
430 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100431 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700432 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w,
433 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434}
435
436bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000437 ContentAction action,
438 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100439 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700440 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w,
441 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442}
443
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000445 // We pass in the BaseChannel instead of the transport_channel_
446 // because if the transport_channel_ changes, the ConnectionMonitor
447 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200448 // We pass in the network thread because on that thread connection monitor
449 // will call BaseChannel::GetConnectionStats which must be called on the
450 // network thread.
451 connection_monitor_.reset(
452 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000453 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000455 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456}
457
458void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000459 if (connection_monitor_) {
460 connection_monitor_->Stop();
461 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 }
463}
464
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000465bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200466 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000467 return transport_channel_->GetStats(infos);
468}
469
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700470bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 // Receive data if we are enabled and have local content,
472 return enabled() && IsReceiveContentDirection(local_content_direction_);
473}
474
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700475bool BaseChannel::IsReadyToSendMedia_w() const {
476 // Need to access some state updated on the network thread.
477 return network_thread_->Invoke<bool>(
478 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
479}
480
481bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 // Send outgoing data if we are enabled, have local and remote content,
483 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800484 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700486 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200487 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488}
489
jbaucheec21bd2016-03-20 06:15:43 -0700490bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700491 const rtc::PacketOptions& options) {
492 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493}
494
jbaucheec21bd2016-03-20 06:15:43 -0700495bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700496 const rtc::PacketOptions& options) {
497 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498}
499
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000500int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200502 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700503 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200504}
505
506int BaseChannel::SetOption_n(SocketType type,
507 rtc::Socket::Option opt,
508 int value) {
509 RTC_DCHECK(network_thread_->IsCurrent());
510 TransportChannel* channel = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000512 case ST_RTP:
513 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700514 socket_options_.push_back(
515 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000516 break;
517 case ST_RTCP:
518 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700519 rtcp_socket_options_.push_back(
520 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000521 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000523 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524}
525
jbauchcb560652016-08-04 05:20:32 -0700526bool BaseChannel::SetCryptoOptions(const rtc::CryptoOptions& crypto_options) {
527 crypto_options_ = crypto_options;
528 return true;
529}
530
johand89ab142016-10-25 10:50:32 -0700531void BaseChannel::OnWritableState(rtc::PacketTransportInterface* transport) {
532 RTC_DCHECK(transport == transport_channel_ ||
533 transport == rtcp_transport_channel_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200534 RTC_DCHECK(network_thread_->IsCurrent());
535 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536}
537
johand89ab142016-10-25 10:50:32 -0700538void BaseChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
539 const char* data,
540 size_t len,
541 const rtc::PacketTime& packet_time,
542 int flags) {
543 TRACE_EVENT0("webrtc", "BaseChannel::OnPacketRead");
544 // OnPacketRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200545 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546
547 // When using RTCP multiplexing we might get RTCP packets on the RTP
548 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
johand89ab142016-10-25 10:50:32 -0700549 bool rtcp = PacketIsRtcp(transport, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700550 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000551 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552}
553
johand89ab142016-10-25 10:50:32 -0700554void BaseChannel::OnReadyToSend(rtc::PacketTransportInterface* transport) {
555 RTC_DCHECK(transport == transport_channel_ ||
556 transport == rtcp_transport_channel_);
557 SetTransportChannelReadyToSend(transport == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558}
559
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800560void BaseChannel::OnDtlsState(TransportChannel* channel,
561 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200562 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800563 return;
564 }
565
566 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
567 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
568 // cover other scenarios like the whole channel is writable (not just this
569 // TransportChannel) or when TransportChannel is attached after DTLS is
570 // negotiated.
571 if (state != DTLS_TRANSPORT_CONNECTED) {
572 srtp_filter_.ResetParams();
573 }
574}
575
Honghai Zhangcc411c02016-03-29 17:27:21 -0700576void BaseChannel::OnSelectedCandidatePairChanged(
577 TransportChannel* channel,
Honghai Zhang52dce732016-03-31 12:37:31 -0700578 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700579 int last_sent_packet_id,
580 bool ready_to_send) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700581 RTC_DCHECK(channel == transport_channel_ ||
582 channel == rtcp_transport_channel_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200583 RTC_DCHECK(network_thread_->IsCurrent());
584 std::string transport_name = channel->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700585 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700586 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700587 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700588 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700589 selected_candidate_pair->remote_candidate().network_id(),
590 last_sent_packet_id);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700591 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200592 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700593 RTC_FROM_HERE, worker_thread_,
594 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
595 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700596}
597
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700598void BaseChannel::SetTransportChannelReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200599 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700600 if (rtcp) {
601 rtcp_ready_to_send_ = ready;
602 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000603 rtp_ready_to_send_ = ready;
604 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200606 bool ready_to_send =
607 (rtp_ready_to_send_ &&
608 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
609 (rtcp_ready_to_send_ || !rtcp_transport_channel_));
610
611 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700612 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200613 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614}
615
johand89ab142016-10-25 10:50:32 -0700616bool BaseChannel::PacketIsRtcp(const rtc::PacketTransportInterface* transport,
617 const char* data,
618 size_t len) {
619 return (transport == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000620 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621}
622
stefanc1aeaf02015-10-15 07:26:07 -0700623bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700624 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700625 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200626 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
627 // If the thread is not our network thread, we will post to our network
628 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 // synchronize access to all the pieces of the send path, including
630 // SRTP and the inner workings of the transport channels.
631 // The only downside is that we can't return a proper failure code if
632 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200633 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200635 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
636 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800637 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700638 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700639 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 return true;
641 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200642 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643
644 // Now that we are on the correct thread, ensure we have a place to send this
645 // packet before doing anything. (We might get RTCP packets that we don't
646 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
647 // transport.
648 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
649 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000650 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 return false;
652 }
653
654 // Protect ourselves against crazy data.
655 if (!ValidPacket(rtcp, packet)) {
656 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000657 << PacketType(rtcp)
658 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 return false;
660 }
661
stefanc1aeaf02015-10-15 07:26:07 -0700662 rtc::PacketOptions updated_options;
663 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 // Protect if needed.
665 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200666 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200668 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000669 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000671 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
672 // inside libsrtp for a RTP packet. A external HMAC module will be writing
673 // a fake HMAC value. This is ONLY done for a RTP packet.
674 // Socket layer will update rtp sendtime extension header if present in
675 // packet with current time before updating the HMAC.
676#if !defined(ENABLE_EXTERNAL_AUTH)
677 res = srtp_filter_.ProtectRtp(
678 data, len, static_cast<int>(packet->capacity()), &len);
679#else
stefanc1aeaf02015-10-15 07:26:07 -0700680 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000681 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000682 res = srtp_filter_.ProtectRtp(
683 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700684 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000685 // If protection succeeds, let's get auth params from srtp.
686 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200687 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000688 int key_len;
689 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700690 &auth_key, &key_len,
691 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000692 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700693 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
694 updated_options.packet_time_params.srtp_auth_key.assign(
695 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000696 }
697 }
698#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 if (!res) {
700 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200701 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 GetRtpSeqNum(data, len, &seq_num);
703 GetRtpSsrc(data, len, &ssrc);
704 LOG(LS_ERROR) << "Failed to protect " << content_name_
705 << " RTP packet: size=" << len
706 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
707 return false;
708 }
709 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000710 res = srtp_filter_.ProtectRtcp(data, len,
711 static_cast<int>(packet->capacity()),
712 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 if (!res) {
714 int type = -1;
715 GetRtcpType(data, len, &type);
716 LOG(LS_ERROR) << "Failed to protect " << content_name_
717 << " RTCP packet: size=" << len << ", type=" << type;
718 return false;
719 }
720 }
721
722 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000723 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 } else if (secure_required_) {
725 // This is a double check for something that supposedly can't happen.
726 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
727 << " packet when SRTP is inactive and crypto is required";
728
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700729 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 return false;
731 }
732
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200734 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
735 int ret = channel->SendPacket(packet->data<char>(), packet->size(),
736 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000737 if (ret != static_cast<int>(packet->size())) {
skvladc309e0e2016-07-28 17:15:20 -0700738 if (channel->GetError() == ENOTCONN) {
739 LOG(LS_WARNING) << "Got ENOTCONN from transport.";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700740 SetTransportChannelReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 }
742 return false;
743 }
744 return true;
745}
746
jbaucheec21bd2016-03-20 06:15:43 -0700747bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 // Protect ourselves against crazy data.
749 if (!ValidPacket(rtcp, packet)) {
750 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000751 << PacketType(rtcp)
752 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 return false;
754 }
pbos482b12e2015-11-16 10:19:58 -0800755 if (rtcp) {
756 // Permit all (seemingly valid) RTCP packets.
757 return true;
758 }
759 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700760 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761}
762
jbaucheec21bd2016-03-20 06:15:43 -0700763void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000764 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200765 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 if (!WantsPacket(rtcp, packet)) {
767 return;
768 }
769
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000770 // We are only interested in the first rtp packet because that
771 // indicates the media has started flowing.
772 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700774 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 }
776
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 // Unprotect the packet, if needed.
778 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200779 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200780 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000781 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 bool res;
783 if (!rtcp) {
784 res = srtp_filter_.UnprotectRtp(data, len, &len);
785 if (!res) {
786 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200787 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 GetRtpSeqNum(data, len, &seq_num);
789 GetRtpSsrc(data, len, &ssrc);
790 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
791 << " RTP packet: size=" << len
792 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
793 return;
794 }
795 } else {
796 res = srtp_filter_.UnprotectRtcp(data, len, &len);
797 if (!res) {
798 int type = -1;
799 GetRtcpType(data, len, &type);
800 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
801 << " RTCP packet: size=" << len << ", type=" << type;
802 return;
803 }
804 }
805
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000806 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807 } else if (secure_required_) {
808 // Our session description indicates that SRTP is required, but we got a
809 // packet before our SRTP filter is active. This means either that
810 // a) we got SRTP packets before we received the SDES keys, in which case
811 // we can't decrypt it anyway, or
812 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
813 // channels, so we haven't yet extracted keys, even if DTLS did complete
814 // on the channel that the packets are being sent on. It's really good
815 // practice to wait for both RTP and RTCP to be good to go before sending
816 // media, to prevent weird failure modes, so it's fine for us to just eat
817 // packets here. This is all sidestepped if RTCP mux is used anyway.
818 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
819 << " packet when SRTP is inactive and crypto is required";
820 return;
821 }
822
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200823 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700824 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200825 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
826}
827
828void BaseChannel::OnPacketReceived(bool rtcp,
829 const rtc::CopyOnWriteBuffer& packet,
830 const rtc::PacketTime& packet_time) {
831 RTC_DCHECK(worker_thread_->IsCurrent());
832 // Need to copy variable because OnRtcpReceived/OnPacketReceived
833 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
834 rtc::CopyOnWriteBuffer data(packet);
835 if (rtcp) {
836 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200838 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 }
840}
841
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000842bool BaseChannel::PushdownLocalDescription(
843 const SessionDescription* local_desc, ContentAction action,
844 std::string* error_desc) {
845 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 const MediaContentDescription* content_desc =
847 GetContentDescription(content_info);
848 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000849 !SetLocalContent(content_desc, action, error_desc)) {
850 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
851 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000853 return true;
854}
855
856bool BaseChannel::PushdownRemoteDescription(
857 const SessionDescription* remote_desc, ContentAction action,
858 std::string* error_desc) {
859 const ContentInfo* content_info = GetFirstContent(remote_desc);
860 const MediaContentDescription* content_desc =
861 GetContentDescription(content_info);
862 if (content_desc && content_info && !content_info->rejected &&
863 !SetRemoteContent(content_desc, action, error_desc)) {
864 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
865 return false;
866 }
867 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868}
869
870void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700871 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 if (enabled_)
873 return;
874
875 LOG(LS_INFO) << "Channel enabled";
876 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700877 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878}
879
880void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700881 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 if (!enabled_)
883 return;
884
885 LOG(LS_INFO) << "Channel disabled";
886 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700887 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888}
889
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200890void BaseChannel::UpdateWritableState_n() {
deadbeefcbecd352015-09-23 11:50:27 -0700891 if (transport_channel_ && transport_channel_->writable() &&
892 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200893 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700894 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200895 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700896 }
897}
898
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200899void BaseChannel::ChannelWritable_n() {
900 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800901 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800903 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904
deadbeefcbecd352015-09-23 11:50:27 -0700905 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 << (was_ever_writable_ ? "" : " for the first time");
907
908 std::vector<ConnectionInfo> infos;
909 transport_channel_->GetStats(&infos);
910 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
911 it != infos.end(); ++it) {
912 if (it->best_connection) {
913 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
914 << "->" << it->remote_candidate.ToSensitiveString();
915 break;
916 }
917 }
918
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200920 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700922 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923}
924
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200925void BaseChannel::SignalDtlsSetupFailure_n(bool rtcp) {
926 RTC_DCHECK(network_thread_->IsCurrent());
927 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700928 RTC_FROM_HERE, signaling_thread(),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200929 Bind(&BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000930}
931
932void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700933 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000934 SignalDtlsSetupFailure(this, rtcp);
935}
936
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200937bool BaseChannel::SetDtlsSrtpCryptoSuites_n(TransportChannel* tc, bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800938 std::vector<int> crypto_suites;
939 // We always use the default SRTP crypto suites for RTCP, but we may use
940 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200942 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 } else {
jbauchcb560652016-08-04 05:20:32 -0700944 GetDefaultSrtpCryptoSuites(crypto_options(), &crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800946 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947}
948
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200949bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800950 // Since DTLS is applied to all channels, checking RTP should be enough.
951 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952}
953
954// This function returns true if either DTLS-SRTP is not in use
955// *or* DTLS-SRTP is successfully set up.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200956bool BaseChannel::SetupDtlsSrtp_n(bool rtcp_channel) {
957 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 bool ret = false;
959
deadbeefcbecd352015-09-23 11:50:27 -0700960 TransportChannel* channel =
961 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800963 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800965 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800967 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
968 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 return false;
970 }
971
972 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
973 << content_name() << " "
974 << PacketType(rtcp_channel);
975
jbauchcb560652016-08-04 05:20:32 -0700976 int key_len;
977 int salt_len;
978 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
979 &salt_len)) {
980 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
981 return false;
982 }
983
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -0700985 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986
987 // RFC 5705 exporter using the RFC 5764 parameters
988 if (!channel->ExportKeyingMaterial(
989 kDtlsSrtpExporterLabel,
990 NULL, 0, false,
991 &dtls_buffer[0], dtls_buffer.size())) {
992 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700993 RTC_DCHECK(false); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 return false;
995 }
996
997 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -0700998 std::vector<unsigned char> client_write_key(key_len + salt_len);
999 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -07001001 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
1002 offset += key_len;
1003 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
1004 offset += key_len;
1005 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
1006 offset += salt_len;
1007 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008
1009 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001010 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001011 if (!channel->GetSslRole(&role)) {
1012 LOG(LS_WARNING) << "GetSslRole failed";
1013 return false;
1014 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001016 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017 send_key = &server_write_key;
1018 recv_key = &client_write_key;
1019 } else {
1020 send_key = &client_write_key;
1021 recv_key = &server_write_key;
1022 }
1023
1024 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001025 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1026 static_cast<int>(send_key->size()),
1027 selected_crypto_suite, &(*recv_key)[0],
1028 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001030 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1031 static_cast<int>(send_key->size()),
1032 selected_crypto_suite, &(*recv_key)[0],
1033 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 }
1035
1036 if (!ret)
1037 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
1038 else
1039 dtls_keyed_ = true;
1040
1041 return ret;
1042}
1043
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001044void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001045 if (srtp_filter_.IsActive()) {
1046 return;
1047 }
1048
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001049 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001050 return;
1051 }
1052
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001053 if (!SetupDtlsSrtp_n(false)) {
1054 SignalDtlsSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001055 return;
1056 }
1057
1058 if (rtcp_transport_channel_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001059 if (!SetupDtlsSrtp_n(true)) {
1060 SignalDtlsSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001061 return;
1062 }
1063 }
1064}
1065
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001066void BaseChannel::ChannelNotWritable_n() {
1067 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 if (!writable_)
1069 return;
1070
deadbeefcbecd352015-09-23 11:50:27 -07001071 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001073 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074}
1075
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001076bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001077 const MediaContentDescription* content,
1078 ContentAction action,
1079 ContentSource src,
1080 std::string* error_desc) {
1081 if (action == CA_UPDATE) {
1082 // These parameters never get changed by a CA_UDPATE.
1083 return true;
1084 }
1085
1086 // Cache secure_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001087 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001088 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1089 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001090}
1091
1092bool BaseChannel::SetRtpTransportParameters_n(
1093 const MediaContentDescription* content,
1094 ContentAction action,
1095 ContentSource src,
1096 std::string* error_desc) {
1097 RTC_DCHECK(network_thread_->IsCurrent());
1098
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001099 if (src == CS_LOCAL) {
1100 set_secure_required(content->crypto_required() != CT_NONE);
1101 }
1102
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001103 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001104 return false;
1105 }
1106
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001107 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001108 return false;
1109 }
1110
1111 return true;
1112}
1113
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001114// |dtls| will be set to true if DTLS is active for transport channel and
1115// crypto is empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001116bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1117 bool* dtls,
1118 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001119 *dtls = transport_channel_->IsDtlsActive();
1120 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001121 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001122 return false;
1123 }
1124 return true;
1125}
1126
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001127bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001128 ContentAction action,
1129 ContentSource src,
1130 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001131 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001132 if (action == CA_UPDATE) {
1133 // no crypto params.
1134 return true;
1135 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001137 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001138 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001139 if (!ret) {
1140 return false;
1141 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 switch (action) {
1143 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001144 // If DTLS is already active on the channel, we could be renegotiating
1145 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001146 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001147 ret = srtp_filter_.SetOffer(cryptos, src);
1148 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149 break;
1150 case CA_PRANSWER:
1151 // If we're doing DTLS-SRTP, we don't want to update the filter
1152 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001153 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1155 }
1156 break;
1157 case CA_ANSWER:
1158 // If we're doing DTLS-SRTP, we don't want to update the filter
1159 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001160 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 ret = srtp_filter_.SetAnswer(cryptos, src);
1162 }
1163 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 default:
1165 break;
1166 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001167 if (!ret) {
1168 SafeSetError("Failed to setup SRTP filter.", error_desc);
1169 return false;
1170 }
1171 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172}
1173
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001174void BaseChannel::ActivateRtcpMux() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001175 network_thread_->Invoke<void>(RTC_FROM_HERE,
1176 Bind(&BaseChannel::ActivateRtcpMux_n, this));
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001177}
1178
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001179void BaseChannel::ActivateRtcpMux_n() {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001180 if (!rtcp_mux_filter_.IsActive()) {
1181 rtcp_mux_filter_.SetActive();
deadbeef062ce9f2016-08-26 21:42:15 -07001182 SetTransportChannel_n(true, nullptr);
1183 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
1184 // removing channel.
1185 UpdateWritableState_n();
1186 SetTransportChannelReadyToSend(true, false);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001187 }
1188}
1189
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001190bool BaseChannel::SetRtcpMux_n(bool enable,
1191 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001192 ContentSource src,
1193 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 bool ret = false;
1195 switch (action) {
1196 case CA_OFFER:
1197 ret = rtcp_mux_filter_.SetOffer(enable, src);
1198 break;
1199 case CA_PRANSWER:
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001200 // This may activate RTCP muxing, but we don't yet destroy the channel
1201 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1203 break;
1204 case CA_ANSWER:
1205 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1206 if (ret && rtcp_mux_filter_.IsActive()) {
1207 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001208 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1209 << " by destroying RTCP transport channel for "
1210 << transport_name();
deadbeef062ce9f2016-08-26 21:42:15 -07001211 SetTransportChannel_n(true, nullptr);
1212 UpdateWritableState_n();
1213 SetTransportChannelReadyToSend(true, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 }
1215 break;
1216 case CA_UPDATE:
1217 // No RTCP mux info.
1218 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001219 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 default:
1221 break;
1222 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001223 if (!ret) {
1224 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1225 return false;
1226 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1228 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1229 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001230 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 // If the RTP transport is already writable, then so are we.
1232 if (transport_channel_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001233 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234 }
1235 }
1236
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001237 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238}
1239
1240bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001241 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001242 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243}
1244
Peter Boström0c4e06b2015-10-07 12:23:21 +02001245bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001246 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 return media_channel()->RemoveRecvStream(ssrc);
1248}
1249
1250bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001251 ContentAction action,
1252 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1254 action == CA_PRANSWER || action == CA_UPDATE))
1255 return false;
1256
1257 // If this is an update, streams only contain streams that have changed.
1258 if (action == CA_UPDATE) {
1259 for (StreamParamsVec::const_iterator it = streams.begin();
1260 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001261 const StreamParams* existing_stream =
1262 GetStreamByIds(local_streams_, it->groupid, it->id);
1263 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 if (media_channel()->AddSendStream(*it)) {
1265 local_streams_.push_back(*it);
1266 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1267 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001268 std::ostringstream desc;
1269 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1270 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 return false;
1272 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001273 } else if (existing_stream && !it->has_ssrcs()) {
1274 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001275 std::ostringstream desc;
1276 desc << "Failed to remove send stream with ssrc "
1277 << it->first_ssrc() << ".";
1278 SafeSetError(desc.str(), error_desc);
1279 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001281 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 } else {
1283 LOG(LS_WARNING) << "Ignore unsupported stream update";
1284 }
1285 }
1286 return true;
1287 }
1288 // Else streams are all the streams we want to send.
1289
1290 // Check for streams that have been removed.
1291 bool ret = true;
1292 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1293 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001294 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001296 std::ostringstream desc;
1297 desc << "Failed to remove send stream with ssrc "
1298 << it->first_ssrc() << ".";
1299 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 ret = false;
1301 }
1302 }
1303 }
1304 // Check for new streams.
1305 for (StreamParamsVec::const_iterator it = streams.begin();
1306 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001307 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001309 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001311 std::ostringstream desc;
1312 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1313 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 ret = false;
1315 }
1316 }
1317 }
1318 local_streams_ = streams;
1319 return ret;
1320}
1321
1322bool BaseChannel::UpdateRemoteStreams_w(
1323 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001324 ContentAction action,
1325 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1327 action == CA_PRANSWER || action == CA_UPDATE))
1328 return false;
1329
1330 // If this is an update, streams only contain streams that have changed.
1331 if (action == CA_UPDATE) {
1332 for (StreamParamsVec::const_iterator it = streams.begin();
1333 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001334 const StreamParams* existing_stream =
1335 GetStreamByIds(remote_streams_, it->groupid, it->id);
1336 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 if (AddRecvStream_w(*it)) {
1338 remote_streams_.push_back(*it);
1339 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1340 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001341 std::ostringstream desc;
1342 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1343 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344 return false;
1345 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001346 } else if (existing_stream && !it->has_ssrcs()) {
1347 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001348 std::ostringstream desc;
1349 desc << "Failed to remove remote stream with ssrc "
1350 << it->first_ssrc() << ".";
1351 SafeSetError(desc.str(), error_desc);
1352 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001354 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355 } else {
1356 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001357 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 << " new stream = " << it->ToString();
1359 }
1360 }
1361 return true;
1362 }
1363 // Else streams are all the streams we want to receive.
1364
1365 // Check for streams that have been removed.
1366 bool ret = true;
1367 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1368 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001369 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001371 std::ostringstream desc;
1372 desc << "Failed to remove remote stream with ssrc "
1373 << it->first_ssrc() << ".";
1374 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001375 ret = false;
1376 }
1377 }
1378 }
1379 // Check for new streams.
1380 for (StreamParamsVec::const_iterator it = streams.begin();
1381 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001382 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383 if (AddRecvStream_w(*it)) {
1384 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1385 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001386 std::ostringstream desc;
1387 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1388 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001389 ret = false;
1390 }
1391 }
1392 }
1393 remote_streams_ = streams;
1394 return ret;
1395}
1396
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001397void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001398 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001399// Absolute Send Time extension id is used only with external auth,
1400// so do not bother searching for it and making asyncronious call to set
1401// something that is not used.
1402#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001403 const webrtc::RtpExtension* send_time_extension =
1404 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001405 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001406 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001407 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001408 RTC_FROM_HERE, network_thread_,
1409 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1410 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001411#endif
1412}
1413
1414void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1415 int rtp_abs_sendtime_extn_id) {
1416 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001417}
1418
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001419void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001420 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001422 case MSG_SEND_RTP_PACKET:
1423 case MSG_SEND_RTCP_PACKET: {
1424 RTC_DCHECK(network_thread_->IsCurrent());
1425 SendPacketMessageData* data =
1426 static_cast<SendPacketMessageData*>(pmsg->pdata);
1427 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1428 SendPacket(rtcp, &data->packet, data->options);
1429 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430 break;
1431 }
1432 case MSG_FIRSTPACKETRECEIVED: {
1433 SignalFirstPacketReceived(this);
1434 break;
1435 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436 }
1437}
1438
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001439void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440 // Flush all remaining RTCP messages. This should only be called in
1441 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001442 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001443 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001444 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1445 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001446 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1447 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 }
1449}
1450
johand89ab142016-10-25 10:50:32 -07001451void BaseChannel::SignalSentPacket_n(
1452 rtc::PacketTransportInterface* /* transport */,
1453 const rtc::SentPacket& sent_packet) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001454 RTC_DCHECK(network_thread_->IsCurrent());
1455 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001456 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001457 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1458}
1459
1460void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1461 RTC_DCHECK(worker_thread_->IsCurrent());
1462 SignalSentPacket(sent_packet);
1463}
1464
1465VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1466 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467 MediaEngineInterface* media_engine,
1468 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001469 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470 const std::string& content_name,
1471 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001472 : BaseChannel(worker_thread,
1473 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001474 media_channel,
1475 transport_controller,
1476 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001478 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001479 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480
1481VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001482 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 StopAudioMonitor();
1484 StopMediaMonitor();
1485 // this can't be done in the base class, since it calls a virtual
1486 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001487 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488}
1489
skvlad6c87a672016-05-17 17:49:52 -07001490bool VoiceChannel::Init_w(const std::string* bundle_transport_name) {
1491 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 return false;
1493 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 return true;
1495}
1496
Peter Boström0c4e06b2015-10-07 12:23:21 +02001497bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001498 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001499 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001500 AudioSource* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001501 return InvokeOnWorker(RTC_FROM_HERE,
1502 Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001503 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504}
1505
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506// TODO(juberti): Handle early media the right way. We should get an explicit
1507// ringing message telling us to start playing local ringback, which we cancel
1508// if any early media actually arrives. For now, we do the opposite, which is
1509// to wait 1 second for early media, and start playing local ringback if none
1510// arrives.
1511void VoiceChannel::SetEarlyMedia(bool enable) {
1512 if (enable) {
1513 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001514 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1515 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516 } else {
1517 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001518 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 }
1520}
1521
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522bool VoiceChannel::CanInsertDtmf() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001523 return InvokeOnWorker(
1524 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001525}
1526
Peter Boström0c4e06b2015-10-07 12:23:21 +02001527bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1528 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001529 int duration) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001530 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this,
1531 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532}
1533
solenberg4bac9c52015-10-09 02:32:53 -07001534bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001535 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume,
1536 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001538
Tommif888bb52015-12-12 01:37:01 +01001539void VoiceChannel::SetRawAudioSink(
1540 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001541 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1542 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001543 // passing. So we invoke to our own little routine that gets a pointer to
1544 // our local variable. This is OK since we're synchronously invoking.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001545 InvokeOnWorker(RTC_FROM_HERE,
1546 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001547}
1548
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001549webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001550 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001551 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001552}
1553
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001554webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1555 uint32_t ssrc) const {
1556 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001557}
1558
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001559bool VoiceChannel::SetRtpSendParameters(
1560 uint32_t ssrc,
1561 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001562 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001563 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001564 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001565}
1566
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001567bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1568 webrtc::RtpParameters parameters) {
1569 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1570}
1571
1572webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1573 uint32_t ssrc) const {
1574 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001575 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001576 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1577}
1578
1579webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1580 uint32_t ssrc) const {
1581 return media_channel()->GetRtpReceiveParameters(ssrc);
1582}
1583
1584bool VoiceChannel::SetRtpReceiveParameters(
1585 uint32_t ssrc,
1586 const webrtc::RtpParameters& parameters) {
1587 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001588 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001589 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1590}
1591
1592bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1593 webrtc::RtpParameters parameters) {
1594 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001595}
1596
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001598 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1599 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600}
1601
1602void VoiceChannel::StartMediaMonitor(int cms) {
1603 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001604 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 media_monitor_->SignalUpdate.connect(
1606 this, &VoiceChannel::OnMediaMonitorUpdate);
1607 media_monitor_->Start(cms);
1608}
1609
1610void VoiceChannel::StopMediaMonitor() {
1611 if (media_monitor_) {
1612 media_monitor_->Stop();
1613 media_monitor_->SignalUpdate.disconnect(this);
1614 media_monitor_.reset();
1615 }
1616}
1617
1618void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001619 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620 audio_monitor_
1621 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1622 audio_monitor_->Start(cms);
1623}
1624
1625void VoiceChannel::StopAudioMonitor() {
1626 if (audio_monitor_) {
1627 audio_monitor_->Stop();
1628 audio_monitor_.reset();
1629 }
1630}
1631
1632bool VoiceChannel::IsAudioMonitorRunning() const {
1633 return (audio_monitor_.get() != NULL);
1634}
1635
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001637 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638}
1639
1640int VoiceChannel::GetOutputLevel_w() {
1641 return media_channel()->GetOutputLevel();
1642}
1643
1644void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1645 media_channel()->GetActiveStreams(actives);
1646}
1647
johand89ab142016-10-25 10:50:32 -07001648void VoiceChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
1649 const char* data,
1650 size_t len,
1651 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001652 int flags) {
johand89ab142016-10-25 10:50:32 -07001653 BaseChannel::OnPacketRead(transport, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 // Set a flag when we've received an RTP packet. If we're waiting for early
1655 // media, this will disable the timeout.
johand89ab142016-10-25 10:50:32 -07001656 if (!received_media_ && !PacketIsRtcp(transport, data, len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 received_media_ = true;
1658 }
1659}
1660
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001661void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001662 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001663 invoker_.AsyncInvoke<void>(
1664 RTC_FROM_HERE, worker_thread_,
1665 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001666}
1667
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001668void VoiceChannel::UpdateMediaSendRecvState_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.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001671 bool recv = IsReadyToReceiveMedia_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.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001676 bool send = IsReadyToSendMedia_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");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001691 RTC_DCHECK(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);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001696 RTC_DCHECK(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());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001728 UpdateMediaSendRecvState_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");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001736 RTC_DCHECK(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);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001741 RTC_DCHECK(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());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001779 UpdateMediaSendRecvState_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) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001824 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 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 {
jbauchcb560652016-08-04 05:20:32 -07001835 GetSupportedAudioCryptoSuites(crypto_options(), 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,
nisse45c8b892016-11-02 03:20:19 -07001868 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
nisse08582ff2016-02-04 01:24:52 -08001869 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001870 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001871 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 return true;
1873}
1874
deadbeef5a4a75a2016-06-02 16:23:38 -07001875bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001876 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001877 bool mute,
1878 const VideoOptions* options,
nisse45c8b892016-11-02 03:20:19 -07001879 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001880 return InvokeOnWorker(RTC_FROM_HERE,
1881 Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
deadbeef5a4a75a2016-06-02 16:23:38 -07001882 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001883}
1884
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001885webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001886 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001887 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001888}
1889
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001890webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1891 uint32_t ssrc) const {
1892 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001893}
1894
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001895bool VideoChannel::SetRtpSendParameters(
1896 uint32_t ssrc,
1897 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001898 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001899 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001900 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001901}
1902
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001903bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1904 webrtc::RtpParameters parameters) {
1905 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1906}
1907
1908webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1909 uint32_t ssrc) const {
1910 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001911 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001912 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
1913}
1914
1915webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
1916 uint32_t ssrc) const {
1917 return media_channel()->GetRtpReceiveParameters(ssrc);
1918}
1919
1920bool VideoChannel::SetRtpReceiveParameters(
1921 uint32_t ssrc,
1922 const webrtc::RtpParameters& parameters) {
1923 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001924 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001925 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
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001933void VideoChannel::UpdateMediaSendRecvState_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.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001936 bool send = IsReadyToSendMedia_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) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001946 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
1947 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");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001974 RTC_DCHECK(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);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001979 RTC_DCHECK(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());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002011 UpdateMediaSendRecvState_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");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002019 RTC_DCHECK(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);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002024 RTC_DCHECK(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());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002063 UpdateMediaSendRecvState_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) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002090 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091 SignalMediaMonitor(this, info);
2092}
2093
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002094void VideoChannel::GetSrtpCryptoSuites_n(
2095 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002096 GetSupportedVideoCryptoSuites(crypto_options(), 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) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002139 return InvokeOnWorker(
2140 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2141 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142}
2143
2144const ContentInfo* DataChannel::GetFirstContent(
2145 const SessionDescription* sdesc) {
2146 return GetFirstDataContent(sdesc);
2147}
2148
jbaucheec21bd2016-03-20 06:15:43 -07002149bool DataChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002150 if (data_channel_type_ == DCT_SCTP) {
2151 // TODO(pthatcher): Do this in a more robust way by checking for
2152 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002153 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154 } else if (data_channel_type_ == DCT_RTP) {
2155 return BaseChannel::WantsPacket(rtcp, packet);
2156 }
2157 return false;
2158}
2159
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002160bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2161 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162 // It hasn't been set before, so set it now.
2163 if (data_channel_type_ == DCT_NONE) {
2164 data_channel_type_ = new_data_channel_type;
2165 return true;
2166 }
2167
2168 // It's been set before, but doesn't match. That's bad.
2169 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002170 std::ostringstream desc;
2171 desc << "Data channel type mismatch."
2172 << " Expected " << data_channel_type_
2173 << " Got " << new_data_channel_type;
2174 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175 return false;
2176 }
2177
2178 // It's hasn't changed. Nothing to do.
2179 return true;
2180}
2181
2182bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002183 const DataContentDescription* content,
2184 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2186 (content->protocol() == kMediaProtocolDtlsSctp));
2187 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002188 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189}
2190
2191bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002192 ContentAction action,
2193 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002194 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002195 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196 LOG(LS_INFO) << "Setting local data description";
2197
2198 const DataContentDescription* data =
2199 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002200 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002201 if (!data) {
2202 SafeSetError("Can't find data content in local description.", error_desc);
2203 return false;
2204 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002206 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207 return false;
2208 }
2209
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002210 if (data_channel_type_ == DCT_RTP) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002211 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002212 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 }
2214 }
2215
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002216 // FYI: We send the SCTP port number (not to be confused with the
2217 // underlying UDP port number) as a codec parameter. So even SCTP
2218 // data channels need codecs.
2219 DataRecvParameters recv_params = last_recv_params_;
2220 RtpParametersFromMediaDescription(data, &recv_params);
2221 if (!media_channel()->SetRecvParameters(recv_params)) {
2222 SafeSetError("Failed to set remote data description recv parameters.",
2223 error_desc);
2224 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002226 if (data_channel_type_ == DCT_RTP) {
2227 for (const DataCodec& codec : data->codecs()) {
2228 bundle_filter()->AddPayloadType(codec.id);
2229 }
2230 }
2231 last_recv_params_ = recv_params;
2232
2233 // TODO(pthatcher): Move local streams into DataSendParameters, and
2234 // only give it to the media channel once we have a remote
2235 // description too (without a remote description, we won't be able
2236 // to send them anyway).
2237 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2238 SafeSetError("Failed to set local data description streams.", error_desc);
2239 return false;
2240 }
2241
2242 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002243 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002244 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245}
2246
2247bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002248 ContentAction action,
2249 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002250 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002251 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252
2253 const DataContentDescription* data =
2254 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002255 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002256 if (!data) {
2257 SafeSetError("Can't find data content in remote description.", error_desc);
2258 return false;
2259 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002260
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002261 // If the remote data doesn't have codecs and isn't an update, it
2262 // must be empty, so ignore it.
2263 if (!data->has_codecs() && action != CA_UPDATE) {
2264 return true;
2265 }
2266
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002267 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002268 return false;
2269 }
2270
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002271 LOG(LS_INFO) << "Setting remote data description";
2272 if (data_channel_type_ == DCT_RTP &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002273 !SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002274 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275 }
2276
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002277
2278 DataSendParameters send_params = last_send_params_;
2279 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2280 if (!media_channel()->SetSendParameters(send_params)) {
2281 SafeSetError("Failed to set remote data description send parameters.",
2282 error_desc);
2283 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002285 last_send_params_ = send_params;
2286
2287 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2288 // and only give it to the media channel once we have a local
2289 // description too (without a local description, we won't be able to
2290 // recv them anyway).
2291 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2292 SafeSetError("Failed to set remote data description streams.",
2293 error_desc);
2294 return false;
2295 }
2296
2297 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002298 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002299 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300}
2301
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002302void DataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002303 // Render incoming data if we're the active call, and we have the local
2304 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002305 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002306 if (!media_channel()->SetReceive(recv)) {
2307 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2308 }
2309
2310 // Send outgoing data if we're the active call, we have the remote content,
2311 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002312 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313 if (!media_channel()->SetSend(send)) {
2314 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2315 }
2316
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002317 // Trigger SignalReadyToSendData asynchronously.
2318 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319
2320 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2321}
2322
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002323void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 switch (pmsg->message_id) {
2325 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002326 DataChannelReadyToSendMessageData* data =
2327 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002328 ready_to_send_data_ = data->data();
2329 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330 delete data;
2331 break;
2332 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333 case MSG_DATARECEIVED: {
2334 DataReceivedMessageData* data =
2335 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2336 SignalDataReceived(this, data->params, data->payload);
2337 delete data;
2338 break;
2339 }
2340 case MSG_CHANNEL_ERROR: {
2341 const DataChannelErrorMessageData* data =
2342 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343 delete data;
2344 break;
2345 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002346 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002347 rtc::TypedMessageData<uint32_t>* data =
2348 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002349 SignalStreamClosedRemotely(data->data());
2350 delete data;
2351 break;
2352 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353 default:
2354 BaseChannel::OnMessage(pmsg);
2355 break;
2356 }
2357}
2358
2359void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002360 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002361 SignalConnectionMonitor(this, infos);
2362}
2363
2364void DataChannel::StartMediaMonitor(int cms) {
2365 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002366 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367 media_monitor_->SignalUpdate.connect(
2368 this, &DataChannel::OnMediaMonitorUpdate);
2369 media_monitor_->Start(cms);
2370}
2371
2372void DataChannel::StopMediaMonitor() {
2373 if (media_monitor_) {
2374 media_monitor_->Stop();
2375 media_monitor_->SignalUpdate.disconnect(this);
2376 media_monitor_.reset();
2377 }
2378}
2379
2380void DataChannel::OnMediaMonitorUpdate(
2381 DataMediaChannel* media_channel, const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002382 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002383 SignalMediaMonitor(this, info);
2384}
2385
2386void DataChannel::OnDataReceived(
2387 const ReceiveDataParams& params, const char* data, size_t len) {
2388 DataReceivedMessageData* msg = new DataReceivedMessageData(
2389 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002390 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002391}
2392
Peter Boström0c4e06b2015-10-07 12:23:21 +02002393void DataChannel::OnDataChannelError(uint32_t ssrc,
2394 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002395 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2396 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002397 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002398}
2399
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002400void DataChannel::OnDataChannelReadyToSend(bool writable) {
2401 // This is usded for congestion control to indicate that the stream is ready
2402 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2403 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002404 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002405 new DataChannelReadyToSendMessageData(writable));
2406}
2407
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002408void DataChannel::GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002409 GetSupportedDataCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410}
2411
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002412bool DataChannel::ShouldSetupDtlsSrtp_n() const {
2413 return data_channel_type_ == DCT_RTP && BaseChannel::ShouldSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002414}
2415
Peter Boström0c4e06b2015-10-07 12:23:21 +02002416void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2417 rtc::TypedMessageData<uint32_t>* message =
2418 new rtc::TypedMessageData<uint32_t>(sid);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002419 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_STREAMCLOSEDREMOTELY,
2420 message);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002421}
2422
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002423} // namespace cricket