blob: 56335e24d085c74c37050233eb6ca6b73b75adaa [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,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072};
73
74// Value specified in RFC 5764.
75static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
76
77static const int kAgcMinus10db = -10;
78
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000079static void SafeSetError(const std::string& message, std::string* error_desc) {
80 if (error_desc) {
81 *error_desc = message;
82 }
83}
84
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000085struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020086 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020088 : ssrc(in_ssrc), error(in_error) {}
89 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 VoiceMediaChannel::Error error;
91};
92
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020094 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020096 : ssrc(in_ssrc), error(in_error) {}
97 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 VideoMediaChannel::Error error;
99};
100
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000101struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200104 : ssrc(in_ssrc), error(in_error) {}
105 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 DataMediaChannel::Error error;
107};
108
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109static const char* PacketType(bool rtcp) {
110 return (!rtcp) ? "RTP" : "RTCP";
111}
112
jbaucheec21bd2016-03-20 06:15:43 -0700113static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 // Check the packet size. We could check the header too if needed.
115 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000116 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
117 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118}
119
120static bool IsReceiveContentDirection(MediaContentDirection direction) {
121 return direction == MD_SENDRECV || direction == MD_RECVONLY;
122}
123
124static bool IsSendContentDirection(MediaContentDirection direction) {
125 return direction == MD_SENDRECV || direction == MD_SENDONLY;
126}
127
128static const MediaContentDescription* GetContentDescription(
129 const ContentInfo* cinfo) {
130 if (cinfo == NULL)
131 return NULL;
132 return static_cast<const MediaContentDescription*>(cinfo->description);
133}
134
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700135template <class Codec>
136void RtpParametersFromMediaDescription(
137 const MediaContentDescriptionImpl<Codec>* desc,
138 RtpParameters<Codec>* params) {
139 // TODO(pthatcher): Remove this once we're sure no one will give us
140 // a description without codecs (currently a CA_UPDATE with just
141 // streams can).
142 if (desc->has_codecs()) {
143 params->codecs = desc->codecs();
144 }
145 // TODO(pthatcher): See if we really need
146 // rtp_header_extensions_set() and remove it if we don't.
147 if (desc->rtp_header_extensions_set()) {
148 params->extensions = desc->rtp_header_extensions();
149 }
deadbeef13871492015-12-09 12:37:51 -0800150 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700151}
152
nisse05103312016-03-16 02:22:50 -0700153template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700154void RtpSendParametersFromMediaDescription(
155 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700156 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700157 RtpParametersFromMediaDescription(desc, send_params);
158 send_params->max_bandwidth_bps = desc->bandwidth();
159}
160
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200161BaseChannel::BaseChannel(rtc::Thread* worker_thread,
162 rtc::Thread* network_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700163 MediaChannel* media_channel,
164 TransportController* transport_controller,
165 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -0800166 bool rtcp,
167 bool srtp_required)
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),
deadbeef7af91dd2016-12-13 11:29:11 -0800175 srtp_required_(srtp_required),
michaelt79e05882016-11-08 02:50:09 -0800176 media_channel_(media_channel),
177 selected_candidate_pair_(nullptr) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700178 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200179 if (transport_controller) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200180 RTC_DCHECK_EQ(network_thread, transport_controller->network_thread());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200181 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 LOG(LS_INFO) << "Created channel for " << content_name;
183}
184
185BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800186 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700187 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000188 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200190 // Eats any outstanding messages or packets.
191 worker_thread_->Clear(&invoker_);
192 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 // We must destroy the media channel before the transport channel, otherwise
194 // the media channel may try to send on the dead transport channel. NULLing
195 // is not an effective strategy since the sends will come on another thread.
196 delete media_channel_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200197 // Note that we don't just call SetTransportChannel_n(nullptr) because that
deadbeefcbecd352015-09-23 11:50:27 -0700198 // would call a pure virtual method which we can't do from a destructor.
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200199 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700200 RTC_FROM_HERE, Bind(&BaseChannel::DestroyTransportChannels_n, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200201 LOG(LS_INFO) << "Destroyed channel";
202}
203
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200204void BaseChannel::DisconnectTransportChannels_n() {
205 // Send any outstanding RTCP packets.
206 FlushRtcpMessages_n();
207
208 // Stop signals from transport channels, but keep them alive because
209 // media_channel may use them from a different thread.
deadbeefcbecd352015-09-23 11:50:27 -0700210 if (transport_channel_) {
211 DisconnectFromTransportChannel(transport_channel_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200212 }
213 if (rtcp_transport_channel_) {
214 DisconnectFromTransportChannel(rtcp_transport_channel_);
215 }
216
217 // Clear pending read packets/messages.
218 network_thread_->Clear(&invoker_);
219 network_thread_->Clear(this);
220}
221
222void BaseChannel::DestroyTransportChannels_n() {
223 if (transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200224 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700225 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
226 }
227 if (rtcp_transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200228 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700229 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
230 }
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200231 // Clear pending send packets/messages.
232 network_thread_->Clear(&invoker_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200233 network_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234}
235
skvlad6c87a672016-05-17 17:49:52 -0700236bool BaseChannel::Init_w(const std::string* bundle_transport_name) {
237 if (!network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700238 RTC_FROM_HERE,
skvlad6c87a672016-05-17 17:49:52 -0700239 Bind(&BaseChannel::InitNetwork_n, this, bundle_transport_name))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000240 return false;
241 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242
wu@webrtc.orgde305012013-10-31 15:40:38 +0000243 // Both RTP and RTCP channels are set, we can call SetInterface on
244 // media channel and it can set network options.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200245 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.orgde305012013-10-31 15:40:38 +0000246 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247 return true;
248}
249
skvlad6c87a672016-05-17 17:49:52 -0700250bool BaseChannel::InitNetwork_n(const std::string* bundle_transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200251 RTC_DCHECK(network_thread_->IsCurrent());
skvlad6c87a672016-05-17 17:49:52 -0700252 const std::string& transport_name =
253 (bundle_transport_name ? *bundle_transport_name : content_name());
254 if (!SetTransport_n(transport_name)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200255 return false;
256 }
257
258 if (!SetDtlsSrtpCryptoSuites_n(transport_channel_, false)) {
259 return false;
260 }
deadbeef23d947d2016-08-22 16:00:30 -0700261 if (rtcp_transport_channel_ &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200262 !SetDtlsSrtpCryptoSuites_n(rtcp_transport_channel_, true)) {
263 return false;
264 }
265 return true;
266}
267
wu@webrtc.org78187522013-10-07 23:32:02 +0000268void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200269 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000270 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200271 // Packets arrive on the network thread, processing packets calls virtual
272 // functions, so need to stop this process in Deinit that is called in
273 // derived classes destructor.
274 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700275 RTC_FROM_HERE, Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000276}
277
deadbeefcbecd352015-09-23 11:50:27 -0700278bool BaseChannel::SetTransport(const std::string& transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200279 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700280 RTC_FROM_HERE, Bind(&BaseChannel::SetTransport_n, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000281}
282
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200283bool BaseChannel::SetTransport_n(const std::string& transport_name) {
284 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000285
deadbeefcbecd352015-09-23 11:50:27 -0700286 if (transport_name == transport_name_) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700287 // Nothing to do if transport name isn't changing.
deadbeefcbecd352015-09-23 11:50:27 -0700288 return true;
289 }
290
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800291 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
292 // changes and wait until the DTLS handshake is complete to set the newly
293 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200294 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800295 // Set |writable_| to false such that UpdateWritableState_w can set up
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700296 // DTLS-SRTP when |writable_| becomes true again.
guoweis46383312015-12-17 16:45:59 -0800297 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800298 srtp_filter_.ResetParams();
299 }
300
deadbeef23d947d2016-08-22 16:00:30 -0700301 // If this BaseChannel uses RTCP and we haven't fully negotiated RTCP mux,
302 // we need an RTCP channel.
303 if (rtcp_enabled_ && !rtcp_mux_filter_.IsFullyActive()) {
deadbeefcbecd352015-09-23 11:50:27 -0700304 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
305 << " on " << transport_name << " transport ";
deadbeef062ce9f2016-08-26 21:42:15 -0700306 SetTransportChannel_n(
307 true, transport_controller_->CreateTransportChannel_n(
308 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200309 if (!rtcp_transport_channel_) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000310 return false;
311 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000312 }
313
deadbeef062ce9f2016-08-26 21:42:15 -0700314 LOG(LS_INFO) << "Create non-RTCP TransportChannel for " << content_name()
315 << " on " << transport_name << " transport ";
316 SetTransportChannel_n(
317 false, transport_controller_->CreateTransportChannel_n(
318 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200319 if (!transport_channel_) {
guoweis46383312015-12-17 16:45:59 -0800320 return false;
321 }
322
deadbeefcbecd352015-09-23 11:50:27 -0700323 transport_name_ = transport_name;
deadbeefcbecd352015-09-23 11:50:27 -0700324
325 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700326 // setting new transport channels.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200327 UpdateWritableState_n();
deadbeef062ce9f2016-08-26 21:42:15 -0700328 // We can only update ready-to-send after updating writability.
329 //
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700330 // On setting a new channel, assume it's ready to send if it's writable,
331 // because we have no way of knowing otherwise (the channel doesn't give us
332 // "was last send successful?").
333 //
334 // This won't always be accurate (the last SendPacket call from another
335 // BaseChannel could have resulted in an error), but even so, we'll just
336 // encounter the error again and update "ready to send" accordingly.
deadbeef062ce9f2016-08-26 21:42:15 -0700337 SetTransportChannelReadyToSend(
338 false, transport_channel_ && transport_channel_->writable());
339 SetTransportChannelReadyToSend(
340 true, rtcp_transport_channel_ && rtcp_transport_channel_->writable());
341 return true;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000342}
343
deadbeef062ce9f2016-08-26 21:42:15 -0700344void BaseChannel::SetTransportChannel_n(bool rtcp,
345 TransportChannel* new_channel) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200346 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef062ce9f2016-08-26 21:42:15 -0700347 TransportChannel*& old_channel =
348 rtcp ? rtcp_transport_channel_ : transport_channel_;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000349
deadbeef062ce9f2016-08-26 21:42:15 -0700350 if (!old_channel && !new_channel) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700351 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000352 return;
353 }
deadbeef062ce9f2016-08-26 21:42:15 -0700354 RTC_DCHECK(old_channel != new_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700355
deadbeef062ce9f2016-08-26 21:42:15 -0700356 if (old_channel) {
357 DisconnectFromTransportChannel(old_channel);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200358 transport_controller_->DestroyTransportChannel_n(
deadbeef062ce9f2016-08-26 21:42:15 -0700359 transport_name_, rtcp ? cricket::ICE_CANDIDATE_COMPONENT_RTCP
360 : cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000361 }
362
deadbeef062ce9f2016-08-26 21:42:15 -0700363 old_channel = new_channel;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000364
deadbeef062ce9f2016-08-26 21:42:15 -0700365 if (new_channel) {
366 if (rtcp) {
367 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
368 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
369 << "should never happen.";
deadbeefcbecd352015-09-23 11:50:27 -0700370 }
deadbeef062ce9f2016-08-26 21:42:15 -0700371 ConnectToTransportChannel(new_channel);
372 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
373 for (const auto& pair : socket_options) {
374 new_channel->SetOption(pair.first, pair.second);
375 }
guoweis46383312015-12-17 16:45:59 -0800376 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000377}
378
379void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200380 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000381
382 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
johand89ab142016-10-25 10:50:32 -0700383 tc->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000384 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800385 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700386 tc->SignalSelectedCandidatePairChanged.connect(
387 this, &BaseChannel::OnSelectedCandidatePairChanged);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200388 tc->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000389}
390
391void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200392 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800393 OnSelectedCandidatePairChanged(tc, nullptr, -1, false);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000394
395 tc->SignalWritableState.disconnect(this);
396 tc->SignalReadPacket.disconnect(this);
397 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800398 tc->SignalDtlsState.disconnect(this);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200399 tc->SignalSelectedCandidatePairChanged.disconnect(this);
400 tc->SignalSentPacket.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000401}
402
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700404 worker_thread_->Invoke<void>(
405 RTC_FROM_HERE,
406 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
407 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 return true;
409}
410
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411bool BaseChannel::AddRecvStream(const StreamParams& sp) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700412 return InvokeOnWorker(RTC_FROM_HERE,
413 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414}
415
Peter Boström0c4e06b2015-10-07 12:23:21 +0200416bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700417 return InvokeOnWorker(RTC_FROM_HERE,
418 Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419}
420
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000421bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000422 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700423 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000424}
425
Peter Boström0c4e06b2015-10-07 12:23:21 +0200426bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700427 return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream,
428 media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000429}
430
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000432 ContentAction action,
433 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100434 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700435 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w,
436 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437}
438
439bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000440 ContentAction action,
441 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100442 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700443 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w,
444 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445}
446
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000448 // We pass in the BaseChannel instead of the transport_channel_
449 // because if the transport_channel_ changes, the ConnectionMonitor
450 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200451 // We pass in the network thread because on that thread connection monitor
452 // will call BaseChannel::GetConnectionStats which must be called on the
453 // network thread.
454 connection_monitor_.reset(
455 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000456 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000458 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459}
460
461void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000462 if (connection_monitor_) {
463 connection_monitor_->Stop();
464 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 }
466}
467
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000468bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200469 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000470 return transport_channel_->GetStats(infos);
471}
472
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700473bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 // Receive data if we are enabled and have local content,
475 return enabled() && IsReceiveContentDirection(local_content_direction_);
476}
477
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700478bool BaseChannel::IsReadyToSendMedia_w() const {
479 // Need to access some state updated on the network thread.
480 return network_thread_->Invoke<bool>(
481 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
482}
483
484bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 // Send outgoing data if we are enabled, have local and remote content,
486 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800487 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700489 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200490 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491}
492
jbaucheec21bd2016-03-20 06:15:43 -0700493bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700494 const rtc::PacketOptions& options) {
495 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496}
497
jbaucheec21bd2016-03-20 06:15:43 -0700498bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700499 const rtc::PacketOptions& options) {
500 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501}
502
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000503int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200505 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700506 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200507}
508
509int BaseChannel::SetOption_n(SocketType type,
510 rtc::Socket::Option opt,
511 int value) {
512 RTC_DCHECK(network_thread_->IsCurrent());
513 TransportChannel* channel = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000515 case ST_RTP:
516 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700517 socket_options_.push_back(
518 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000519 break;
520 case ST_RTCP:
521 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700522 rtcp_socket_options_.push_back(
523 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000524 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000526 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527}
528
jbauchcb560652016-08-04 05:20:32 -0700529bool BaseChannel::SetCryptoOptions(const rtc::CryptoOptions& crypto_options) {
530 crypto_options_ = crypto_options;
531 return true;
532}
533
johand89ab142016-10-25 10:50:32 -0700534void BaseChannel::OnWritableState(rtc::PacketTransportInterface* transport) {
535 RTC_DCHECK(transport == transport_channel_ ||
536 transport == rtcp_transport_channel_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200537 RTC_DCHECK(network_thread_->IsCurrent());
538 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539}
540
johand89ab142016-10-25 10:50:32 -0700541void BaseChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
542 const char* data,
543 size_t len,
544 const rtc::PacketTime& packet_time,
545 int flags) {
546 TRACE_EVENT0("webrtc", "BaseChannel::OnPacketRead");
547 // OnPacketRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200548 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549
550 // When using RTCP multiplexing we might get RTCP packets on the RTP
551 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
johand89ab142016-10-25 10:50:32 -0700552 bool rtcp = PacketIsRtcp(transport, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700553 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000554 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555}
556
johand89ab142016-10-25 10:50:32 -0700557void BaseChannel::OnReadyToSend(rtc::PacketTransportInterface* transport) {
558 RTC_DCHECK(transport == transport_channel_ ||
559 transport == rtcp_transport_channel_);
560 SetTransportChannelReadyToSend(transport == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561}
562
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800563void BaseChannel::OnDtlsState(TransportChannel* channel,
564 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200565 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800566 return;
567 }
568
569 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
570 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
571 // cover other scenarios like the whole channel is writable (not just this
572 // TransportChannel) or when TransportChannel is attached after DTLS is
573 // negotiated.
574 if (state != DTLS_TRANSPORT_CONNECTED) {
575 srtp_filter_.ResetParams();
576 }
577}
578
Honghai Zhangcc411c02016-03-29 17:27:21 -0700579void BaseChannel::OnSelectedCandidatePairChanged(
580 TransportChannel* channel,
Honghai Zhang52dce732016-03-31 12:37:31 -0700581 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700582 int last_sent_packet_id,
583 bool ready_to_send) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700584 RTC_DCHECK(channel == transport_channel_ ||
585 channel == rtcp_transport_channel_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200586 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800587 selected_candidate_pair_ = selected_candidate_pair;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200588 std::string transport_name = channel->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700589 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700590 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700591 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700592 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700593 selected_candidate_pair->remote_candidate().network_id(),
594 last_sent_packet_id);
michaelt79e05882016-11-08 02:50:09 -0800595
596 UpdateTransportOverhead();
Honghai Zhangcc411c02016-03-29 17:27:21 -0700597 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200598 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700599 RTC_FROM_HERE, worker_thread_,
600 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
601 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700602}
603
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700604void BaseChannel::SetTransportChannelReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200605 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700606 if (rtcp) {
607 rtcp_ready_to_send_ = ready;
608 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 rtp_ready_to_send_ = ready;
610 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200612 bool ready_to_send =
613 (rtp_ready_to_send_ &&
614 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
615 (rtcp_ready_to_send_ || !rtcp_transport_channel_));
616
617 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700618 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200619 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620}
621
johand89ab142016-10-25 10:50:32 -0700622bool BaseChannel::PacketIsRtcp(const rtc::PacketTransportInterface* transport,
623 const char* data,
624 size_t len) {
625 return (transport == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000626 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627}
628
stefanc1aeaf02015-10-15 07:26:07 -0700629bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700630 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700631 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200632 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
633 // If the thread is not our network thread, we will post to our network
634 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 // synchronize access to all the pieces of the send path, including
636 // SRTP and the inner workings of the transport channels.
637 // The only downside is that we can't return a proper failure code if
638 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200639 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200641 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
642 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800643 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700644 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700645 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 return true;
647 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200648 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649
650 // Now that we are on the correct thread, ensure we have a place to send this
651 // packet before doing anything. (We might get RTCP packets that we don't
652 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
653 // transport.
654 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
655 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000656 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 return false;
658 }
659
660 // Protect ourselves against crazy data.
661 if (!ValidPacket(rtcp, packet)) {
662 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000663 << PacketType(rtcp)
664 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 return false;
666 }
667
stefanc1aeaf02015-10-15 07:26:07 -0700668 rtc::PacketOptions updated_options;
669 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 // Protect if needed.
671 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200672 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200674 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000675 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000677 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
678 // inside libsrtp for a RTP packet. A external HMAC module will be writing
679 // a fake HMAC value. This is ONLY done for a RTP packet.
680 // Socket layer will update rtp sendtime extension header if present in
681 // packet with current time before updating the HMAC.
682#if !defined(ENABLE_EXTERNAL_AUTH)
683 res = srtp_filter_.ProtectRtp(
684 data, len, static_cast<int>(packet->capacity()), &len);
685#else
stefanc1aeaf02015-10-15 07:26:07 -0700686 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000687 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000688 res = srtp_filter_.ProtectRtp(
689 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700690 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000691 // If protection succeeds, let's get auth params from srtp.
692 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200693 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000694 int key_len;
695 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700696 &auth_key, &key_len,
697 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000698 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700699 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
700 updated_options.packet_time_params.srtp_auth_key.assign(
701 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000702 }
703 }
704#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 if (!res) {
706 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200707 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 GetRtpSeqNum(data, len, &seq_num);
709 GetRtpSsrc(data, len, &ssrc);
710 LOG(LS_ERROR) << "Failed to protect " << content_name_
711 << " RTP packet: size=" << len
712 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
713 return false;
714 }
715 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000716 res = srtp_filter_.ProtectRtcp(data, len,
717 static_cast<int>(packet->capacity()),
718 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 if (!res) {
720 int type = -1;
721 GetRtcpType(data, len, &type);
722 LOG(LS_ERROR) << "Failed to protect " << content_name_
723 << " RTCP packet: size=" << len << ", type=" << type;
724 return false;
725 }
726 }
727
728 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000729 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800730 } else if (srtp_required_) {
deadbeef8f425f92016-12-01 12:26:27 -0800731 // The audio/video engines may attempt to send RTCP packets as soon as the
732 // streams are created, so don't treat this as an error for RTCP.
733 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809
734 if (rtcp) {
735 return false;
736 }
737 // However, there shouldn't be any RTP packets sent before SRTP is set up
738 // (and SetSend(true) is called).
739 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
740 << " and crypto is required";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700741 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 return false;
743 }
744
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200746 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
747 int ret = channel->SendPacket(packet->data<char>(), packet->size(),
748 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000749 if (ret != static_cast<int>(packet->size())) {
skvladc309e0e2016-07-28 17:15:20 -0700750 if (channel->GetError() == ENOTCONN) {
751 LOG(LS_WARNING) << "Got ENOTCONN from transport.";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700752 SetTransportChannelReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 }
754 return false;
755 }
756 return true;
757}
758
jbaucheec21bd2016-03-20 06:15:43 -0700759bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 // Protect ourselves against crazy data.
761 if (!ValidPacket(rtcp, packet)) {
762 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000763 << PacketType(rtcp)
764 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 return false;
766 }
pbos482b12e2015-11-16 10:19:58 -0800767 if (rtcp) {
768 // Permit all (seemingly valid) RTCP packets.
769 return true;
770 }
771 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700772 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000773}
774
jbaucheec21bd2016-03-20 06:15:43 -0700775void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000776 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200777 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 if (!WantsPacket(rtcp, packet)) {
779 return;
780 }
781
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000782 // We are only interested in the first rtp packet because that
783 // indicates the media has started flowing.
784 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700786 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 }
788
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 // Unprotect the packet, if needed.
790 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200791 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200792 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000793 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 bool res;
795 if (!rtcp) {
796 res = srtp_filter_.UnprotectRtp(data, len, &len);
797 if (!res) {
798 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200799 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800 GetRtpSeqNum(data, len, &seq_num);
801 GetRtpSsrc(data, len, &ssrc);
802 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
803 << " RTP packet: size=" << len
804 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
805 return;
806 }
807 } else {
808 res = srtp_filter_.UnprotectRtcp(data, len, &len);
809 if (!res) {
810 int type = -1;
811 GetRtcpType(data, len, &type);
812 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
813 << " RTCP packet: size=" << len << ", type=" << type;
814 return;
815 }
816 }
817
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000818 packet->SetSize(len);
deadbeef7af91dd2016-12-13 11:29:11 -0800819 } else if (srtp_required_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 // Our session description indicates that SRTP is required, but we got a
821 // packet before our SRTP filter is active. This means either that
822 // a) we got SRTP packets before we received the SDES keys, in which case
823 // we can't decrypt it anyway, or
824 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
825 // channels, so we haven't yet extracted keys, even if DTLS did complete
826 // on the channel that the packets are being sent on. It's really good
827 // practice to wait for both RTP and RTCP to be good to go before sending
828 // media, to prevent weird failure modes, so it's fine for us to just eat
829 // packets here. This is all sidestepped if RTCP mux is used anyway.
830 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
831 << " packet when SRTP is inactive and crypto is required";
832 return;
833 }
834
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200835 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700836 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200837 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
838}
839
840void BaseChannel::OnPacketReceived(bool rtcp,
841 const rtc::CopyOnWriteBuffer& packet,
842 const rtc::PacketTime& packet_time) {
843 RTC_DCHECK(worker_thread_->IsCurrent());
844 // Need to copy variable because OnRtcpReceived/OnPacketReceived
845 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
846 rtc::CopyOnWriteBuffer data(packet);
847 if (rtcp) {
848 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200850 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 }
852}
853
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000854bool BaseChannel::PushdownLocalDescription(
855 const SessionDescription* local_desc, ContentAction action,
856 std::string* error_desc) {
857 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 const MediaContentDescription* content_desc =
859 GetContentDescription(content_info);
860 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000861 !SetLocalContent(content_desc, action, error_desc)) {
862 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
863 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000865 return true;
866}
867
868bool BaseChannel::PushdownRemoteDescription(
869 const SessionDescription* remote_desc, ContentAction action,
870 std::string* error_desc) {
871 const ContentInfo* content_info = GetFirstContent(remote_desc);
872 const MediaContentDescription* content_desc =
873 GetContentDescription(content_info);
874 if (content_desc && content_info && !content_info->rejected &&
875 !SetRemoteContent(content_desc, action, error_desc)) {
876 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
877 return false;
878 }
879 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880}
881
882void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700883 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 if (enabled_)
885 return;
886
887 LOG(LS_INFO) << "Channel enabled";
888 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700889 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890}
891
892void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700893 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 if (!enabled_)
895 return;
896
897 LOG(LS_INFO) << "Channel disabled";
898 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700899 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900}
901
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200902void BaseChannel::UpdateWritableState_n() {
deadbeefcbecd352015-09-23 11:50:27 -0700903 if (transport_channel_ && transport_channel_->writable() &&
904 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200905 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700906 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200907 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700908 }
909}
910
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200911void BaseChannel::ChannelWritable_n() {
912 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800913 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800915 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916
deadbeefcbecd352015-09-23 11:50:27 -0700917 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 << (was_ever_writable_ ? "" : " for the first time");
919
michaelt79e05882016-11-08 02:50:09 -0800920 if (selected_candidate_pair_)
921 LOG(LS_INFO)
922 << "Using "
923 << selected_candidate_pair_->local_candidate().ToSensitiveString()
924 << "->"
925 << selected_candidate_pair_->remote_candidate().ToSensitiveString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200928 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700930 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931}
932
deadbeef953c2ce2017-01-09 14:53:41 -0800933void BaseChannel::SignalDtlsSrtpSetupFailure_n(bool rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200934 RTC_DCHECK(network_thread_->IsCurrent());
935 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700936 RTC_FROM_HERE, signaling_thread(),
deadbeef953c2ce2017-01-09 14:53:41 -0800937 Bind(&BaseChannel::SignalDtlsSrtpSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000938}
939
deadbeef953c2ce2017-01-09 14:53:41 -0800940void BaseChannel::SignalDtlsSrtpSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700941 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
deadbeef953c2ce2017-01-09 14:53:41 -0800942 SignalDtlsSrtpSetupFailure(this, rtcp);
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000943}
944
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200945bool BaseChannel::SetDtlsSrtpCryptoSuites_n(TransportChannel* tc, bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800946 std::vector<int> crypto_suites;
947 // We always use the default SRTP crypto suites for RTCP, but we may use
948 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200950 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 } else {
jbauchcb560652016-08-04 05:20:32 -0700952 GetDefaultSrtpCryptoSuites(crypto_options(), &crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800954 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955}
956
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200957bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800958 // Since DTLS is applied to all channels, checking RTP should be enough.
959 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960}
961
962// This function returns true if either DTLS-SRTP is not in use
963// *or* DTLS-SRTP is successfully set up.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200964bool BaseChannel::SetupDtlsSrtp_n(bool rtcp_channel) {
965 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 bool ret = false;
967
deadbeefcbecd352015-09-23 11:50:27 -0700968 TransportChannel* channel =
969 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800971 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800973 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800975 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
976 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 return false;
978 }
979
980 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
981 << content_name() << " "
982 << PacketType(rtcp_channel);
983
jbauchcb560652016-08-04 05:20:32 -0700984 int key_len;
985 int salt_len;
986 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
987 &salt_len)) {
988 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
989 return false;
990 }
991
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -0700993 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994
995 // RFC 5705 exporter using the RFC 5764 parameters
996 if (!channel->ExportKeyingMaterial(
997 kDtlsSrtpExporterLabel,
998 NULL, 0, false,
999 &dtls_buffer[0], dtls_buffer.size())) {
1000 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001001 RTC_DCHECK(false); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 return false;
1003 }
1004
1005 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -07001006 std::vector<unsigned char> client_write_key(key_len + salt_len);
1007 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -07001009 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
1010 offset += key_len;
1011 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
1012 offset += key_len;
1013 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
1014 offset += salt_len;
1015 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016
1017 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001018 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001019 if (!channel->GetSslRole(&role)) {
1020 LOG(LS_WARNING) << "GetSslRole failed";
1021 return false;
1022 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001024 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 send_key = &server_write_key;
1026 recv_key = &client_write_key;
1027 } else {
1028 send_key = &client_write_key;
1029 recv_key = &server_write_key;
1030 }
1031
1032 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001033 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1034 static_cast<int>(send_key->size()),
1035 selected_crypto_suite, &(*recv_key)[0],
1036 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001038 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1039 static_cast<int>(send_key->size()),
1040 selected_crypto_suite, &(*recv_key)[0],
1041 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 }
1043
michaelt79e05882016-11-08 02:50:09 -08001044 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
michaelt79e05882016-11-08 02:50:09 -08001046 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 dtls_keyed_ = true;
michaelt79e05882016-11-08 02:50:09 -08001048 UpdateTransportOverhead();
1049 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 return ret;
1051}
1052
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001053void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001054 if (srtp_filter_.IsActive()) {
1055 return;
1056 }
1057
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001058 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001059 return;
1060 }
1061
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001062 if (!SetupDtlsSrtp_n(false)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001063 SignalDtlsSrtpSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001064 return;
1065 }
1066
1067 if (rtcp_transport_channel_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001068 if (!SetupDtlsSrtp_n(true)) {
deadbeef953c2ce2017-01-09 14:53:41 -08001069 SignalDtlsSrtpSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001070 return;
1071 }
1072 }
1073}
1074
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001075void BaseChannel::ChannelNotWritable_n() {
1076 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 if (!writable_)
1078 return;
1079
deadbeefcbecd352015-09-23 11:50:27 -07001080 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001081 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001082 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083}
1084
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001085bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001086 const MediaContentDescription* content,
1087 ContentAction action,
1088 ContentSource src,
1089 std::string* error_desc) {
1090 if (action == CA_UPDATE) {
1091 // These parameters never get changed by a CA_UDPATE.
1092 return true;
1093 }
1094
deadbeef7af91dd2016-12-13 11:29:11 -08001095 // Cache srtp_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001096 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001097 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1098 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001099}
1100
1101bool BaseChannel::SetRtpTransportParameters_n(
1102 const MediaContentDescription* content,
1103 ContentAction action,
1104 ContentSource src,
1105 std::string* error_desc) {
1106 RTC_DCHECK(network_thread_->IsCurrent());
1107
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001108 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001109 return false;
1110 }
1111
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001112 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001113 return false;
1114 }
1115
1116 return true;
1117}
1118
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001119// |dtls| will be set to true if DTLS is active for transport channel and
1120// crypto is empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001121bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1122 bool* dtls,
1123 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001124 *dtls = transport_channel_->IsDtlsActive();
1125 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001126 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001127 return false;
1128 }
1129 return true;
1130}
1131
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001132bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001133 ContentAction action,
1134 ContentSource src,
1135 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001136 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001137 if (action == CA_UPDATE) {
1138 // no crypto params.
1139 return true;
1140 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001142 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001143 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001144 if (!ret) {
1145 return false;
1146 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 switch (action) {
1148 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001149 // If DTLS is already active on the channel, we could be renegotiating
1150 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001151 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001152 ret = srtp_filter_.SetOffer(cryptos, src);
1153 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154 break;
1155 case CA_PRANSWER:
1156 // If we're doing DTLS-SRTP, we don't want to update the filter
1157 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001158 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1160 }
1161 break;
1162 case CA_ANSWER:
1163 // If we're doing DTLS-SRTP, we don't want to update the filter
1164 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001165 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 ret = srtp_filter_.SetAnswer(cryptos, src);
1167 }
1168 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 default:
1170 break;
1171 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001172 if (!ret) {
1173 SafeSetError("Failed to setup SRTP filter.", error_desc);
1174 return false;
1175 }
1176 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177}
1178
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001179void BaseChannel::ActivateRtcpMux() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001180 network_thread_->Invoke<void>(RTC_FROM_HERE,
1181 Bind(&BaseChannel::ActivateRtcpMux_n, this));
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001182}
1183
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001184void BaseChannel::ActivateRtcpMux_n() {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001185 if (!rtcp_mux_filter_.IsActive()) {
1186 rtcp_mux_filter_.SetActive();
deadbeef062ce9f2016-08-26 21:42:15 -07001187 SetTransportChannel_n(true, nullptr);
1188 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
1189 // removing channel.
1190 UpdateWritableState_n();
1191 SetTransportChannelReadyToSend(true, false);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001192 }
1193}
1194
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001195bool BaseChannel::SetRtcpMux_n(bool enable,
1196 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001197 ContentSource src,
1198 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 bool ret = false;
1200 switch (action) {
1201 case CA_OFFER:
1202 ret = rtcp_mux_filter_.SetOffer(enable, src);
1203 break;
1204 case CA_PRANSWER:
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001205 // This may activate RTCP muxing, but we don't yet destroy the channel
1206 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1208 break;
1209 case CA_ANSWER:
1210 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1211 if (ret && rtcp_mux_filter_.IsActive()) {
1212 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001213 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1214 << " by destroying RTCP transport channel for "
1215 << transport_name();
deadbeef062ce9f2016-08-26 21:42:15 -07001216 SetTransportChannel_n(true, nullptr);
1217 UpdateWritableState_n();
1218 SetTransportChannelReadyToSend(true, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 }
1220 break;
1221 case CA_UPDATE:
1222 // No RTCP mux info.
1223 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001224 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 default:
1226 break;
1227 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001228 if (!ret) {
1229 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1230 return false;
1231 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1233 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1234 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001235 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236 // If the RTP transport is already writable, then so are we.
1237 if (transport_channel_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001238 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 }
1240 }
1241
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001242 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243}
1244
1245bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001246 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001247 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248}
1249
Peter Boström0c4e06b2015-10-07 12:23:21 +02001250bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001251 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 return media_channel()->RemoveRecvStream(ssrc);
1253}
1254
1255bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001256 ContentAction action,
1257 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1259 action == CA_PRANSWER || action == CA_UPDATE))
1260 return false;
1261
1262 // If this is an update, streams only contain streams that have changed.
1263 if (action == CA_UPDATE) {
1264 for (StreamParamsVec::const_iterator it = streams.begin();
1265 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001266 const StreamParams* existing_stream =
1267 GetStreamByIds(local_streams_, it->groupid, it->id);
1268 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 if (media_channel()->AddSendStream(*it)) {
1270 local_streams_.push_back(*it);
1271 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1272 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001273 std::ostringstream desc;
1274 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1275 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276 return false;
1277 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001278 } else if (existing_stream && !it->has_ssrcs()) {
1279 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001280 std::ostringstream desc;
1281 desc << "Failed to remove send stream with ssrc "
1282 << it->first_ssrc() << ".";
1283 SafeSetError(desc.str(), error_desc);
1284 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001286 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 } else {
1288 LOG(LS_WARNING) << "Ignore unsupported stream update";
1289 }
1290 }
1291 return true;
1292 }
1293 // Else streams are all the streams we want to send.
1294
1295 // Check for streams that have been removed.
1296 bool ret = true;
1297 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1298 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001299 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001301 std::ostringstream desc;
1302 desc << "Failed to remove send stream with ssrc "
1303 << it->first_ssrc() << ".";
1304 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305 ret = false;
1306 }
1307 }
1308 }
1309 // Check for new streams.
1310 for (StreamParamsVec::const_iterator it = streams.begin();
1311 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001312 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001314 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001316 std::ostringstream desc;
1317 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1318 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 ret = false;
1320 }
1321 }
1322 }
1323 local_streams_ = streams;
1324 return ret;
1325}
1326
1327bool BaseChannel::UpdateRemoteStreams_w(
1328 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001329 ContentAction action,
1330 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1332 action == CA_PRANSWER || action == CA_UPDATE))
1333 return false;
1334
1335 // If this is an update, streams only contain streams that have changed.
1336 if (action == CA_UPDATE) {
1337 for (StreamParamsVec::const_iterator it = streams.begin();
1338 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001339 const StreamParams* existing_stream =
1340 GetStreamByIds(remote_streams_, it->groupid, it->id);
1341 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342 if (AddRecvStream_w(*it)) {
1343 remote_streams_.push_back(*it);
1344 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1345 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001346 std::ostringstream desc;
1347 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1348 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349 return false;
1350 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001351 } else if (existing_stream && !it->has_ssrcs()) {
1352 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001353 std::ostringstream desc;
1354 desc << "Failed to remove remote stream with ssrc "
1355 << it->first_ssrc() << ".";
1356 SafeSetError(desc.str(), error_desc);
1357 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001359 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 } else {
1361 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001362 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363 << " new stream = " << it->ToString();
1364 }
1365 }
1366 return true;
1367 }
1368 // Else streams are all the streams we want to receive.
1369
1370 // Check for streams that have been removed.
1371 bool ret = true;
1372 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1373 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001374 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001375 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001376 std::ostringstream desc;
1377 desc << "Failed to remove remote stream with ssrc "
1378 << it->first_ssrc() << ".";
1379 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380 ret = false;
1381 }
1382 }
1383 }
1384 // Check for new streams.
1385 for (StreamParamsVec::const_iterator it = streams.begin();
1386 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001387 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388 if (AddRecvStream_w(*it)) {
1389 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1390 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001391 std::ostringstream desc;
1392 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1393 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001394 ret = false;
1395 }
1396 }
1397 }
1398 remote_streams_ = streams;
1399 return ret;
1400}
1401
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001402void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001403 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001404// Absolute Send Time extension id is used only with external auth,
1405// so do not bother searching for it and making asyncronious call to set
1406// something that is not used.
1407#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001408 const webrtc::RtpExtension* send_time_extension =
1409 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001410 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001411 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001412 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001413 RTC_FROM_HERE, network_thread_,
1414 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1415 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001416#endif
1417}
1418
1419void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1420 int rtp_abs_sendtime_extn_id) {
1421 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001422}
1423
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001424void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001425 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001427 case MSG_SEND_RTP_PACKET:
1428 case MSG_SEND_RTCP_PACKET: {
1429 RTC_DCHECK(network_thread_->IsCurrent());
1430 SendPacketMessageData* data =
1431 static_cast<SendPacketMessageData*>(pmsg->pdata);
1432 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1433 SendPacket(rtcp, &data->packet, data->options);
1434 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435 break;
1436 }
1437 case MSG_FIRSTPACKETRECEIVED: {
1438 SignalFirstPacketReceived(this);
1439 break;
1440 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 }
1442}
1443
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001444void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 // Flush all remaining RTCP messages. This should only be called in
1446 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001447 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001448 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001449 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1450 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001451 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1452 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453 }
1454}
1455
johand89ab142016-10-25 10:50:32 -07001456void BaseChannel::SignalSentPacket_n(
1457 rtc::PacketTransportInterface* /* transport */,
1458 const rtc::SentPacket& sent_packet) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001459 RTC_DCHECK(network_thread_->IsCurrent());
1460 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001461 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001462 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1463}
1464
1465void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1466 RTC_DCHECK(worker_thread_->IsCurrent());
1467 SignalSentPacket(sent_packet);
1468}
1469
1470VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1471 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 MediaEngineInterface* media_engine,
1473 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001474 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -08001476 bool rtcp,
1477 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001478 : BaseChannel(worker_thread,
1479 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001480 media_channel,
1481 transport_controller,
1482 content_name,
deadbeef7af91dd2016-12-13 11:29:11 -08001483 rtcp,
1484 srtp_required),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001485 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001486 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487
1488VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001489 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 StopAudioMonitor();
1491 StopMediaMonitor();
1492 // this can't be done in the base class, since it calls a virtual
1493 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001494 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495}
1496
skvlad6c87a672016-05-17 17:49:52 -07001497bool VoiceChannel::Init_w(const std::string* bundle_transport_name) {
1498 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 return false;
1500 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501 return true;
1502}
1503
Peter Boström0c4e06b2015-10-07 12:23:21 +02001504bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001505 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001506 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001507 AudioSource* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001508 return InvokeOnWorker(RTC_FROM_HERE,
1509 Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001510 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511}
1512
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513// TODO(juberti): Handle early media the right way. We should get an explicit
1514// ringing message telling us to start playing local ringback, which we cancel
1515// if any early media actually arrives. For now, we do the opposite, which is
1516// to wait 1 second for early media, and start playing local ringback if none
1517// arrives.
1518void VoiceChannel::SetEarlyMedia(bool enable) {
1519 if (enable) {
1520 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001521 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1522 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 } else {
1524 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001525 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 }
1527}
1528
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529bool VoiceChannel::CanInsertDtmf() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001530 return InvokeOnWorker(
1531 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532}
1533
Peter Boström0c4e06b2015-10-07 12:23:21 +02001534bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1535 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001536 int duration) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001537 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this,
1538 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539}
1540
solenberg4bac9c52015-10-09 02:32:53 -07001541bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001542 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume,
1543 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001545
Tommif888bb52015-12-12 01:37:01 +01001546void VoiceChannel::SetRawAudioSink(
1547 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001548 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1549 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001550 // passing. So we invoke to our own little routine that gets a pointer to
1551 // our local variable. This is OK since we're synchronously invoking.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001552 InvokeOnWorker(RTC_FROM_HERE,
1553 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001554}
1555
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001556webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001557 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001558 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001559}
1560
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001561webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1562 uint32_t ssrc) const {
1563 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001564}
1565
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001566bool VoiceChannel::SetRtpSendParameters(
1567 uint32_t ssrc,
1568 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001569 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001570 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001571 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001572}
1573
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001574bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1575 webrtc::RtpParameters parameters) {
1576 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1577}
1578
1579webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1580 uint32_t ssrc) const {
1581 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001582 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001583 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1584}
1585
1586webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1587 uint32_t ssrc) const {
1588 return media_channel()->GetRtpReceiveParameters(ssrc);
1589}
1590
1591bool VoiceChannel::SetRtpReceiveParameters(
1592 uint32_t ssrc,
1593 const webrtc::RtpParameters& parameters) {
1594 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001595 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001596 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1597}
1598
1599bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1600 webrtc::RtpParameters parameters) {
1601 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001602}
1603
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001605 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1606 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607}
1608
1609void VoiceChannel::StartMediaMonitor(int cms) {
1610 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001611 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612 media_monitor_->SignalUpdate.connect(
1613 this, &VoiceChannel::OnMediaMonitorUpdate);
1614 media_monitor_->Start(cms);
1615}
1616
1617void VoiceChannel::StopMediaMonitor() {
1618 if (media_monitor_) {
1619 media_monitor_->Stop();
1620 media_monitor_->SignalUpdate.disconnect(this);
1621 media_monitor_.reset();
1622 }
1623}
1624
1625void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001626 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627 audio_monitor_
1628 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1629 audio_monitor_->Start(cms);
1630}
1631
1632void VoiceChannel::StopAudioMonitor() {
1633 if (audio_monitor_) {
1634 audio_monitor_->Stop();
1635 audio_monitor_.reset();
1636 }
1637}
1638
1639bool VoiceChannel::IsAudioMonitorRunning() const {
1640 return (audio_monitor_.get() != NULL);
1641}
1642
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001644 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645}
1646
1647int VoiceChannel::GetOutputLevel_w() {
1648 return media_channel()->GetOutputLevel();
1649}
1650
1651void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1652 media_channel()->GetActiveStreams(actives);
1653}
1654
johand89ab142016-10-25 10:50:32 -07001655void VoiceChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
1656 const char* data,
1657 size_t len,
1658 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001659 int flags) {
johand89ab142016-10-25 10:50:32 -07001660 BaseChannel::OnPacketRead(transport, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 // Set a flag when we've received an RTP packet. If we're waiting for early
1662 // media, this will disable the timeout.
johand89ab142016-10-25 10:50:32 -07001663 if (!received_media_ && !PacketIsRtcp(transport, data, len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 received_media_ = true;
1665 }
1666}
1667
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001668void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001669 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001670 invoker_.AsyncInvoke<void>(
1671 RTC_FROM_HERE, worker_thread_,
1672 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001673}
1674
michaelt79e05882016-11-08 02:50:09 -08001675int BaseChannel::GetTransportOverheadPerPacket() const {
1676 RTC_DCHECK(network_thread_->IsCurrent());
1677
1678 if (!selected_candidate_pair_)
1679 return 0;
1680
1681 int transport_overhead_per_packet = 0;
1682
1683 constexpr int kIpv4Overhaed = 20;
1684 constexpr int kIpv6Overhaed = 40;
1685 transport_overhead_per_packet +=
1686 selected_candidate_pair_->local_candidate().address().family() == AF_INET
1687 ? kIpv4Overhaed
1688 : kIpv6Overhaed;
1689
1690 constexpr int kUdpOverhaed = 8;
1691 constexpr int kTcpOverhaed = 20;
1692 transport_overhead_per_packet +=
1693 selected_candidate_pair_->local_candidate().protocol() ==
1694 TCP_PROTOCOL_NAME
1695 ? kTcpOverhaed
1696 : kUdpOverhaed;
1697
1698 if (secure()) {
1699 int srtp_overhead = 0;
1700 if (srtp_filter_.GetSrtpOverhead(&srtp_overhead))
1701 transport_overhead_per_packet += srtp_overhead;
1702 }
1703
1704 return transport_overhead_per_packet;
1705}
1706
1707void BaseChannel::UpdateTransportOverhead() {
1708 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1709 if (transport_overhead_per_packet)
1710 invoker_.AsyncInvoke<void>(
1711 RTC_FROM_HERE, worker_thread_,
1712 Bind(&MediaChannel::OnTransportOverheadChanged, media_channel_,
1713 transport_overhead_per_packet));
1714}
1715
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001716void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 // Render incoming data if we're the active call, and we have the local
1718 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001719 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001720 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721
1722 // Send outgoing data if we're the active call, we have the remote content,
1723 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001724 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001725 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726
1727 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1728}
1729
1730const ContentInfo* VoiceChannel::GetFirstContent(
1731 const SessionDescription* sdesc) {
1732 return GetFirstAudioContent(sdesc);
1733}
1734
1735bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001736 ContentAction action,
1737 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001738 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001739 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 LOG(LS_INFO) << "Setting local voice description";
1741
1742 const AudioContentDescription* audio =
1743 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001744 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001745 if (!audio) {
1746 SafeSetError("Can't find audio content in local description.", error_desc);
1747 return false;
1748 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001750 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001751 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 }
1753
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001754 AudioRecvParameters recv_params = last_recv_params_;
1755 RtpParametersFromMediaDescription(audio, &recv_params);
1756 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001757 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001758 error_desc);
1759 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001761 for (const AudioCodec& codec : audio->codecs()) {
1762 bundle_filter()->AddPayloadType(codec.id);
1763 }
1764 last_recv_params_ = recv_params;
1765
1766 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1767 // only give it to the media channel once we have a remote
1768 // description too (without a remote description, we won't be able
1769 // to send them anyway).
1770 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1771 SafeSetError("Failed to set local audio description streams.", error_desc);
1772 return false;
1773 }
1774
1775 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001776 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001777 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778}
1779
1780bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001781 ContentAction action,
1782 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001783 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001784 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 LOG(LS_INFO) << "Setting remote voice description";
1786
1787 const AudioContentDescription* audio =
1788 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001789 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001790 if (!audio) {
1791 SafeSetError("Can't find audio content in remote description.", error_desc);
1792 return false;
1793 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001795 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001796 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 }
1798
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001799 AudioSendParameters send_params = last_send_params_;
1800 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001801 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001802 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001803 }
skvladdc1c62c2016-03-16 19:07:43 -07001804
1805 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1806 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001807 SafeSetError("Failed to set remote audio description send parameters.",
1808 error_desc);
1809 return false;
1810 }
1811 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001813 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1814 // and only give it to the media channel once we have a local
1815 // description too (without a local description, we won't be able to
1816 // recv them anyway).
1817 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1818 SafeSetError("Failed to set remote audio description streams.", error_desc);
1819 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 }
1821
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001822 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001823 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001824 }
1825
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001826 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001827 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001828 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829}
1830
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831void VoiceChannel::HandleEarlyMediaTimeout() {
1832 // This occurs on the main thread, not the worker thread.
1833 if (!received_media_) {
1834 LOG(LS_INFO) << "No early media received before timeout";
1835 SignalEarlyMediaTimeout(this);
1836 }
1837}
1838
Peter Boström0c4e06b2015-10-07 12:23:21 +02001839bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1840 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001841 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 if (!enabled()) {
1843 return false;
1844 }
solenberg1d63dd02015-12-02 12:35:09 -08001845 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846}
1847
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001848void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 case MSG_EARLYMEDIATIMEOUT:
1851 HandleEarlyMediaTimeout();
1852 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 case MSG_CHANNEL_ERROR: {
1854 VoiceChannelErrorMessageData* data =
1855 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 delete data;
1857 break;
1858 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 default:
1860 BaseChannel::OnMessage(pmsg);
1861 break;
1862 }
1863}
1864
1865void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001866 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 SignalConnectionMonitor(this, infos);
1868}
1869
1870void VoiceChannel::OnMediaMonitorUpdate(
1871 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001872 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 SignalMediaMonitor(this, info);
1874}
1875
1876void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1877 const AudioInfo& info) {
1878 SignalAudioMonitor(this, info);
1879}
1880
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001881void VoiceChannel::GetSrtpCryptoSuites_n(
1882 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07001883 GetSupportedAudioCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884}
1885
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001886VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1887 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001889 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 const std::string& content_name,
deadbeef7af91dd2016-12-13 11:29:11 -08001891 bool rtcp,
1892 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001893 : BaseChannel(worker_thread,
1894 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001895 media_channel,
1896 transport_controller,
1897 content_name,
deadbeef7af91dd2016-12-13 11:29:11 -08001898 rtcp,
1899 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900
skvlad6c87a672016-05-17 17:49:52 -07001901bool VideoChannel::Init_w(const std::string* bundle_transport_name) {
1902 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903 return false;
1904 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001905 return true;
1906}
1907
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001909 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 StopMediaMonitor();
1911 // this can't be done in the base class, since it calls a virtual
1912 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001913
1914 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001915}
1916
nisse08582ff2016-02-04 01:24:52 -08001917bool VideoChannel::SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -08001918 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
nisse08582ff2016-02-04 01:24:52 -08001919 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001920 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001921 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922 return true;
1923}
1924
deadbeef5a4a75a2016-06-02 16:23:38 -07001925bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001926 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001927 bool mute,
1928 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -08001929 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001930 return InvokeOnWorker(RTC_FROM_HERE,
1931 Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
deadbeef5a4a75a2016-06-02 16:23:38 -07001932 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001933}
1934
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001935webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001936 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001937 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001938}
1939
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001940webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1941 uint32_t ssrc) const {
1942 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001943}
1944
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001945bool VideoChannel::SetRtpSendParameters(
1946 uint32_t ssrc,
1947 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001948 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001949 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001950 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001951}
1952
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001953bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1954 webrtc::RtpParameters parameters) {
1955 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1956}
1957
1958webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1959 uint32_t ssrc) const {
1960 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001961 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001962 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
1963}
1964
1965webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
1966 uint32_t ssrc) const {
1967 return media_channel()->GetRtpReceiveParameters(ssrc);
1968}
1969
1970bool VideoChannel::SetRtpReceiveParameters(
1971 uint32_t ssrc,
1972 const webrtc::RtpParameters& parameters) {
1973 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001974 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001975 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1976}
1977
1978bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1979 webrtc::RtpParameters parameters) {
1980 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001981}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001982
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001983void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 // Send outgoing data if we're the active call, we have the remote content,
1985 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001986 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987 if (!media_channel()->SetSend(send)) {
1988 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1989 // TODO(gangji): Report error back to server.
1990 }
1991
Peter Boström34fbfff2015-09-24 19:20:30 +02001992 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993}
1994
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001995bool VideoChannel::GetStats(VideoMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001996 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
1997 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998}
1999
2000void VideoChannel::StartMediaMonitor(int cms) {
2001 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002002 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002003 media_monitor_->SignalUpdate.connect(
2004 this, &VideoChannel::OnMediaMonitorUpdate);
2005 media_monitor_->Start(cms);
2006}
2007
2008void VideoChannel::StopMediaMonitor() {
2009 if (media_monitor_) {
2010 media_monitor_->Stop();
2011 media_monitor_.reset();
2012 }
2013}
2014
2015const ContentInfo* VideoChannel::GetFirstContent(
2016 const SessionDescription* sdesc) {
2017 return GetFirstVideoContent(sdesc);
2018}
2019
2020bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002021 ContentAction action,
2022 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002023 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002024 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 LOG(LS_INFO) << "Setting local video description";
2026
2027 const VideoContentDescription* video =
2028 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002029 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002030 if (!video) {
2031 SafeSetError("Can't find video content in local description.", error_desc);
2032 return false;
2033 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002035 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002036 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037 }
2038
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002039 VideoRecvParameters recv_params = last_recv_params_;
2040 RtpParametersFromMediaDescription(video, &recv_params);
2041 if (!media_channel()->SetRecvParameters(recv_params)) {
2042 SafeSetError("Failed to set local video description recv parameters.",
2043 error_desc);
2044 return false;
2045 }
2046 for (const VideoCodec& codec : video->codecs()) {
2047 bundle_filter()->AddPayloadType(codec.id);
2048 }
2049 last_recv_params_ = recv_params;
2050
2051 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2052 // only give it to the media channel once we have a remote
2053 // description too (without a remote description, we won't be able
2054 // to send them anyway).
2055 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2056 SafeSetError("Failed to set local video description streams.", error_desc);
2057 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 }
2059
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002060 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002061 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002062 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063}
2064
2065bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002066 ContentAction action,
2067 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002068 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002069 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070 LOG(LS_INFO) << "Setting remote video description";
2071
2072 const VideoContentDescription* video =
2073 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002074 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002075 if (!video) {
2076 SafeSetError("Can't find video content in remote description.", error_desc);
2077 return false;
2078 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002080 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002081 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082 }
2083
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002084 VideoSendParameters send_params = last_send_params_;
2085 RtpSendParametersFromMediaDescription(video, &send_params);
2086 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002087 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002088 }
skvladdc1c62c2016-03-16 19:07:43 -07002089
2090 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2091
2092 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002093 SafeSetError("Failed to set remote video description send parameters.",
2094 error_desc);
2095 return false;
2096 }
2097 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002099 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2100 // and only give it to the media channel once we have a local
2101 // description too (without a local description, we won't be able to
2102 // recv them anyway).
2103 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2104 SafeSetError("Failed to set remote video description streams.", error_desc);
2105 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 }
2107
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002108 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002109 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002110 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002111
2112 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002113 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002114 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115}
2116
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002117void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119 case MSG_CHANNEL_ERROR: {
2120 const VideoChannelErrorMessageData* data =
2121 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122 delete data;
2123 break;
2124 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 default:
2126 BaseChannel::OnMessage(pmsg);
2127 break;
2128 }
2129}
2130
2131void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002132 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 SignalConnectionMonitor(this, infos);
2134}
2135
2136// TODO(pthatcher): Look into removing duplicate code between
2137// audio, video, and data, perhaps by using templates.
2138void VideoChannel::OnMediaMonitorUpdate(
2139 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002140 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141 SignalMediaMonitor(this, info);
2142}
2143
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002144void VideoChannel::GetSrtpCryptoSuites_n(
2145 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002146 GetSupportedVideoCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147}
2148
deadbeef953c2ce2017-01-09 14:53:41 -08002149RtpDataChannel::RtpDataChannel(rtc::Thread* worker_thread,
2150 rtc::Thread* network_thread,
2151 DataMediaChannel* media_channel,
2152 TransportController* transport_controller,
2153 const std::string& content_name,
2154 bool rtcp,
2155 bool srtp_required)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002156 : BaseChannel(worker_thread,
2157 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002158 media_channel,
2159 transport_controller,
2160 content_name,
deadbeef7af91dd2016-12-13 11:29:11 -08002161 rtcp,
deadbeef953c2ce2017-01-09 14:53:41 -08002162 srtp_required) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163
deadbeef953c2ce2017-01-09 14:53:41 -08002164RtpDataChannel::~RtpDataChannel() {
2165 TRACE_EVENT0("webrtc", "RtpDataChannel::~RtpDataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166 StopMediaMonitor();
2167 // this can't be done in the base class, since it calls a virtual
2168 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002169
2170 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171}
2172
deadbeef953c2ce2017-01-09 14:53:41 -08002173bool RtpDataChannel::Init_w(const std::string* bundle_transport_name) {
skvlad6c87a672016-05-17 17:49:52 -07002174 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175 return false;
2176 }
deadbeef953c2ce2017-01-09 14:53:41 -08002177 media_channel()->SignalDataReceived.connect(this,
2178 &RtpDataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002179 media_channel()->SignalReadyToSend.connect(
deadbeef953c2ce2017-01-09 14:53:41 -08002180 this, &RtpDataChannel::OnDataChannelReadyToSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181 return true;
2182}
2183
deadbeef953c2ce2017-01-09 14:53:41 -08002184bool RtpDataChannel::SendData(const SendDataParams& params,
2185 const rtc::CopyOnWriteBuffer& payload,
2186 SendDataResult* result) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002187 return InvokeOnWorker(
2188 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2189 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002190}
2191
deadbeef953c2ce2017-01-09 14:53:41 -08002192const ContentInfo* RtpDataChannel::GetFirstContent(
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193 const SessionDescription* sdesc) {
2194 return GetFirstDataContent(sdesc);
2195}
2196
deadbeef953c2ce2017-01-09 14:53:41 -08002197bool RtpDataChannel::CheckDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002198 const DataContentDescription* content,
2199 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2201 (content->protocol() == kMediaProtocolDtlsSctp));
deadbeef953c2ce2017-01-09 14:53:41 -08002202 // It's been set before, but doesn't match. That's bad.
2203 if (is_sctp) {
2204 SafeSetError("Data channel type mismatch. Expected RTP, got SCTP.",
2205 error_desc);
2206 return false;
2207 }
2208 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209}
2210
deadbeef953c2ce2017-01-09 14:53:41 -08002211bool RtpDataChannel::SetLocalContent_w(const MediaContentDescription* content,
2212 ContentAction action,
2213 std::string* error_desc) {
2214 TRACE_EVENT0("webrtc", "RtpDataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002215 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216 LOG(LS_INFO) << "Setting local data description";
2217
2218 const DataContentDescription* data =
2219 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002220 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002221 if (!data) {
2222 SafeSetError("Can't find data content in local description.", error_desc);
2223 return false;
2224 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225
deadbeef953c2ce2017-01-09 14:53:41 -08002226 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002227 return false;
2228 }
2229
deadbeef953c2ce2017-01-09 14:53:41 -08002230 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
2231 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002232 }
2233
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002234 DataRecvParameters recv_params = last_recv_params_;
2235 RtpParametersFromMediaDescription(data, &recv_params);
2236 if (!media_channel()->SetRecvParameters(recv_params)) {
2237 SafeSetError("Failed to set remote data description recv parameters.",
2238 error_desc);
2239 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240 }
deadbeef953c2ce2017-01-09 14:53:41 -08002241 for (const DataCodec& codec : data->codecs()) {
2242 bundle_filter()->AddPayloadType(codec.id);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002243 }
2244 last_recv_params_ = recv_params;
2245
2246 // TODO(pthatcher): Move local streams into DataSendParameters, and
2247 // only give it to the media channel once we have a remote
2248 // description too (without a remote description, we won't be able
2249 // to send them anyway).
2250 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2251 SafeSetError("Failed to set local data description streams.", error_desc);
2252 return false;
2253 }
2254
2255 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002256 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002257 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002258}
2259
deadbeef953c2ce2017-01-09 14:53:41 -08002260bool RtpDataChannel::SetRemoteContent_w(const MediaContentDescription* content,
2261 ContentAction action,
2262 std::string* error_desc) {
2263 TRACE_EVENT0("webrtc", "RtpDataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002264 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002265
2266 const DataContentDescription* data =
2267 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002268 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002269 if (!data) {
2270 SafeSetError("Can't find data content in remote description.", error_desc);
2271 return false;
2272 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002273
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002274 // If the remote data doesn't have codecs and isn't an update, it
2275 // must be empty, so ignore it.
2276 if (!data->has_codecs() && action != CA_UPDATE) {
2277 return true;
2278 }
2279
deadbeef953c2ce2017-01-09 14:53:41 -08002280 if (!CheckDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002281 return false;
2282 }
2283
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002284 LOG(LS_INFO) << "Setting remote data description";
deadbeef953c2ce2017-01-09 14:53:41 -08002285 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002286 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 }
2288
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002289 DataSendParameters send_params = last_send_params_;
2290 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2291 if (!media_channel()->SetSendParameters(send_params)) {
2292 SafeSetError("Failed to set remote data description send parameters.",
2293 error_desc);
2294 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002295 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002296 last_send_params_ = send_params;
2297
2298 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2299 // and only give it to the media channel once we have a local
2300 // description too (without a local description, we won't be able to
2301 // recv them anyway).
2302 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2303 SafeSetError("Failed to set remote data description streams.",
2304 error_desc);
2305 return false;
2306 }
2307
2308 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002309 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002310 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311}
2312
deadbeef953c2ce2017-01-09 14:53:41 -08002313void RtpDataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002314 // Render incoming data if we're the active call, and we have the local
2315 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002316 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317 if (!media_channel()->SetReceive(recv)) {
2318 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2319 }
2320
2321 // Send outgoing data if we're the active call, we have the remote content,
2322 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002323 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 if (!media_channel()->SetSend(send)) {
2325 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2326 }
2327
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002328 // Trigger SignalReadyToSendData asynchronously.
2329 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330
2331 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2332}
2333
deadbeef953c2ce2017-01-09 14:53:41 -08002334void RtpDataChannel::OnMessage(rtc::Message* pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002335 switch (pmsg->message_id) {
2336 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002337 DataChannelReadyToSendMessageData* data =
2338 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002339 ready_to_send_data_ = data->data();
2340 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002341 delete data;
2342 break;
2343 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002344 case MSG_DATARECEIVED: {
2345 DataReceivedMessageData* data =
2346 static_cast<DataReceivedMessageData*>(pmsg->pdata);
deadbeef953c2ce2017-01-09 14:53:41 -08002347 SignalDataReceived(data->params, data->payload);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 delete data;
2349 break;
2350 }
2351 case MSG_CHANNEL_ERROR: {
2352 const DataChannelErrorMessageData* data =
2353 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354 delete data;
2355 break;
2356 }
2357 default:
2358 BaseChannel::OnMessage(pmsg);
2359 break;
2360 }
2361}
2362
deadbeef953c2ce2017-01-09 14:53:41 -08002363void RtpDataChannel::OnConnectionMonitorUpdate(
2364 ConnectionMonitor* monitor,
2365 const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002366 SignalConnectionMonitor(this, infos);
2367}
2368
deadbeef953c2ce2017-01-09 14:53:41 -08002369void RtpDataChannel::StartMediaMonitor(int cms) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002370 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002371 rtc::Thread::Current()));
deadbeef953c2ce2017-01-09 14:53:41 -08002372 media_monitor_->SignalUpdate.connect(this,
2373 &RtpDataChannel::OnMediaMonitorUpdate);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002374 media_monitor_->Start(cms);
2375}
2376
deadbeef953c2ce2017-01-09 14:53:41 -08002377void RtpDataChannel::StopMediaMonitor() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002378 if (media_monitor_) {
2379 media_monitor_->Stop();
2380 media_monitor_->SignalUpdate.disconnect(this);
2381 media_monitor_.reset();
2382 }
2383}
2384
deadbeef953c2ce2017-01-09 14:53:41 -08002385void RtpDataChannel::OnMediaMonitorUpdate(DataMediaChannel* media_channel,
2386 const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002387 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002388 SignalMediaMonitor(this, info);
2389}
2390
deadbeef953c2ce2017-01-09 14:53:41 -08002391void RtpDataChannel::OnDataReceived(const ReceiveDataParams& params,
2392 const char* data,
2393 size_t len) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002394 DataReceivedMessageData* msg = new DataReceivedMessageData(
2395 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002396 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002397}
2398
deadbeef953c2ce2017-01-09 14:53:41 -08002399void RtpDataChannel::OnDataChannelError(uint32_t ssrc,
2400 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002401 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2402 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002403 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002404}
2405
deadbeef953c2ce2017-01-09 14:53:41 -08002406void RtpDataChannel::OnDataChannelReadyToSend(bool writable) {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002407 // This is usded for congestion control to indicate that the stream is ready
2408 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2409 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002410 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002411 new DataChannelReadyToSendMessageData(writable));
2412}
2413
deadbeef953c2ce2017-01-09 14:53:41 -08002414void RtpDataChannel::GetSrtpCryptoSuites_n(
2415 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002416 GetSupportedDataCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002417}
2418
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002419} // namespace cricket