blob: eb2f6340ea2482a6a6d455412864a7a91723be5f [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwiberg0eb15ed2015-12-17 03:04:15 -080011#include <utility>
12
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010013#include "webrtc/pc/channel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
kjellandera69d9732016-08-31 07:33:05 -070015#include "webrtc/api/call/audio_sink.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000016#include "webrtc/base/bind.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000017#include "webrtc/base/byteorder.h"
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -070018#include "webrtc/base/checks.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000019#include "webrtc/base/common.h"
jbaucheec21bd2016-03-20 06:15:43 -070020#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000021#include "webrtc/base/dscp.h"
22#include "webrtc/base/logging.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070023#include "webrtc/base/networkroute.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010024#include "webrtc/base/trace_event.h"
kjellanderf4752772016-03-02 05:42:30 -080025#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080026#include "webrtc/media/base/rtputils.h"
johand89ab142016-10-25 10:50:32 -070027#include "webrtc/p2p/base/packettransportinterface.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010028#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010029#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
31namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000032using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000033
deadbeef2d110be2016-01-13 12:00:26 -080034namespace {
kwiberg31022942016-03-11 14:18:21 -080035// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080036bool SetRawAudioSink_w(VoiceMediaChannel* channel,
37 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080038 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
39 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080040 return true;
41}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020042
43struct SendPacketMessageData : public rtc::MessageData {
44 rtc::CopyOnWriteBuffer packet;
45 rtc::PacketOptions options;
46};
47
isheriff6f8d6862016-05-26 11:24:55 -070048#if defined(ENABLE_EXTERNAL_AUTH)
49// Returns the named header extension if found among all extensions,
50// nullptr otherwise.
51const webrtc::RtpExtension* FindHeaderExtension(
52 const std::vector<webrtc::RtpExtension>& extensions,
53 const std::string& uri) {
54 for (const auto& extension : extensions) {
55 if (extension.uri == uri)
56 return &extension;
57 }
58 return nullptr;
59}
60#endif
61
deadbeef2d110be2016-01-13 12:00:26 -080062} // namespace
63
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000065 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020066 MSG_SEND_RTP_PACKET,
67 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000072 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073};
74
75// Value specified in RFC 5764.
76static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
77
78static const int kAgcMinus10db = -10;
79
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000080static void SafeSetError(const std::string& message, std::string* error_desc) {
81 if (error_desc) {
82 *error_desc = message;
83 }
84}
85
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000086struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 : ssrc(in_ssrc), error(in_error) {}
90 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 VoiceMediaChannel::Error error;
92};
93
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000094struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020095 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020097 : ssrc(in_ssrc), error(in_error) {}
98 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 VideoMediaChannel::Error error;
100};
101
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000102struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200103 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200105 : ssrc(in_ssrc), error(in_error) {}
106 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 DataMediaChannel::Error error;
108};
109
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000110static const char* PacketType(bool rtcp) {
111 return (!rtcp) ? "RTP" : "RTCP";
112}
113
jbaucheec21bd2016-03-20 06:15:43 -0700114static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 // Check the packet size. We could check the header too if needed.
116 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000117 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
118 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119}
120
121static bool IsReceiveContentDirection(MediaContentDirection direction) {
122 return direction == MD_SENDRECV || direction == MD_RECVONLY;
123}
124
125static bool IsSendContentDirection(MediaContentDirection direction) {
126 return direction == MD_SENDRECV || direction == MD_SENDONLY;
127}
128
129static const MediaContentDescription* GetContentDescription(
130 const ContentInfo* cinfo) {
131 if (cinfo == NULL)
132 return NULL;
133 return static_cast<const MediaContentDescription*>(cinfo->description);
134}
135
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700136template <class Codec>
137void RtpParametersFromMediaDescription(
138 const MediaContentDescriptionImpl<Codec>* desc,
139 RtpParameters<Codec>* params) {
140 // TODO(pthatcher): Remove this once we're sure no one will give us
141 // a description without codecs (currently a CA_UPDATE with just
142 // streams can).
143 if (desc->has_codecs()) {
144 params->codecs = desc->codecs();
145 }
146 // TODO(pthatcher): See if we really need
147 // rtp_header_extensions_set() and remove it if we don't.
148 if (desc->rtp_header_extensions_set()) {
149 params->extensions = desc->rtp_header_extensions();
150 }
deadbeef13871492015-12-09 12:37:51 -0800151 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700152}
153
nisse05103312016-03-16 02:22:50 -0700154template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700155void RtpSendParametersFromMediaDescription(
156 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700157 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700158 RtpParametersFromMediaDescription(desc, send_params);
159 send_params->max_bandwidth_bps = desc->bandwidth();
160}
161
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200162BaseChannel::BaseChannel(rtc::Thread* worker_thread,
163 rtc::Thread* network_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700164 MediaChannel* media_channel,
165 TransportController* transport_controller,
166 const std::string& content_name,
167 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200168 : worker_thread_(worker_thread),
169 network_thread_(network_thread),
170
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 content_name_(content_name),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200172
173 transport_controller_(transport_controller),
deadbeef23d947d2016-08-22 16:00:30 -0700174 rtcp_enabled_(rtcp),
michaelt79e05882016-11-08 02:50:09 -0800175 media_channel_(media_channel),
176 selected_candidate_pair_(nullptr) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700177 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200178 if (transport_controller) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200179 RTC_DCHECK_EQ(network_thread, transport_controller->network_thread());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200180 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 LOG(LS_INFO) << "Created channel for " << content_name;
182}
183
184BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800185 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700186 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000187 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200189 // Eats any outstanding messages or packets.
190 worker_thread_->Clear(&invoker_);
191 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 // We must destroy the media channel before the transport channel, otherwise
193 // the media channel may try to send on the dead transport channel. NULLing
194 // is not an effective strategy since the sends will come on another thread.
195 delete media_channel_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200196 // Note that we don't just call SetTransportChannel_n(nullptr) because that
deadbeefcbecd352015-09-23 11:50:27 -0700197 // would call a pure virtual method which we can't do from a destructor.
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200198 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700199 RTC_FROM_HERE, Bind(&BaseChannel::DestroyTransportChannels_n, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200200 LOG(LS_INFO) << "Destroyed channel";
201}
202
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200203void BaseChannel::DisconnectTransportChannels_n() {
204 // Send any outstanding RTCP packets.
205 FlushRtcpMessages_n();
206
207 // Stop signals from transport channels, but keep them alive because
208 // media_channel may use them from a different thread.
deadbeefcbecd352015-09-23 11:50:27 -0700209 if (transport_channel_) {
210 DisconnectFromTransportChannel(transport_channel_);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200211 }
212 if (rtcp_transport_channel_) {
213 DisconnectFromTransportChannel(rtcp_transport_channel_);
214 }
215
216 // Clear pending read packets/messages.
217 network_thread_->Clear(&invoker_);
218 network_thread_->Clear(this);
219}
220
221void BaseChannel::DestroyTransportChannels_n() {
222 if (transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200223 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700224 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
225 }
226 if (rtcp_transport_channel_) {
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200227 transport_controller_->DestroyTransportChannel_n(
deadbeefcbecd352015-09-23 11:50:27 -0700228 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
229 }
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200230 // Clear pending send packets/messages.
231 network_thread_->Clear(&invoker_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200232 network_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233}
234
skvlad6c87a672016-05-17 17:49:52 -0700235bool BaseChannel::Init_w(const std::string* bundle_transport_name) {
236 if (!network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700237 RTC_FROM_HERE,
skvlad6c87a672016-05-17 17:49:52 -0700238 Bind(&BaseChannel::InitNetwork_n, this, bundle_transport_name))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000239 return false;
240 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241
wu@webrtc.orgde305012013-10-31 15:40:38 +0000242 // Both RTP and RTCP channels are set, we can call SetInterface on
243 // media channel and it can set network options.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200244 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.orgde305012013-10-31 15:40:38 +0000245 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 return true;
247}
248
skvlad6c87a672016-05-17 17:49:52 -0700249bool BaseChannel::InitNetwork_n(const std::string* bundle_transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200250 RTC_DCHECK(network_thread_->IsCurrent());
skvlad6c87a672016-05-17 17:49:52 -0700251 const std::string& transport_name =
252 (bundle_transport_name ? *bundle_transport_name : content_name());
253 if (!SetTransport_n(transport_name)) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200254 return false;
255 }
256
257 if (!SetDtlsSrtpCryptoSuites_n(transport_channel_, false)) {
258 return false;
259 }
deadbeef23d947d2016-08-22 16:00:30 -0700260 if (rtcp_transport_channel_ &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200261 !SetDtlsSrtpCryptoSuites_n(rtcp_transport_channel_, true)) {
262 return false;
263 }
264 return true;
265}
266
wu@webrtc.org78187522013-10-07 23:32:02 +0000267void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200268 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000269 media_channel_->SetInterface(NULL);
Danil Chapovalovdae07ba2016-05-14 01:43:50 +0200270 // Packets arrive on the network thread, processing packets calls virtual
271 // functions, so need to stop this process in Deinit that is called in
272 // derived classes destructor.
273 network_thread_->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700274 RTC_FROM_HERE, Bind(&BaseChannel::DisconnectTransportChannels_n, this));
wu@webrtc.org78187522013-10-07 23:32:02 +0000275}
276
deadbeefcbecd352015-09-23 11:50:27 -0700277bool BaseChannel::SetTransport(const std::string& transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200278 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700279 RTC_FROM_HERE, Bind(&BaseChannel::SetTransport_n, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000280}
281
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200282bool BaseChannel::SetTransport_n(const std::string& transport_name) {
283 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000284
deadbeefcbecd352015-09-23 11:50:27 -0700285 if (transport_name == transport_name_) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700286 // Nothing to do if transport name isn't changing.
deadbeefcbecd352015-09-23 11:50:27 -0700287 return true;
288 }
289
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800290 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
291 // changes and wait until the DTLS handshake is complete to set the newly
292 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200293 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800294 // Set |writable_| to false such that UpdateWritableState_w can set up
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700295 // DTLS-SRTP when |writable_| becomes true again.
guoweis46383312015-12-17 16:45:59 -0800296 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800297 srtp_filter_.ResetParams();
298 }
299
deadbeef23d947d2016-08-22 16:00:30 -0700300 // If this BaseChannel uses RTCP and we haven't fully negotiated RTCP mux,
301 // we need an RTCP channel.
302 if (rtcp_enabled_ && !rtcp_mux_filter_.IsFullyActive()) {
deadbeefcbecd352015-09-23 11:50:27 -0700303 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
304 << " on " << transport_name << " transport ";
deadbeef062ce9f2016-08-26 21:42:15 -0700305 SetTransportChannel_n(
306 true, transport_controller_->CreateTransportChannel_n(
307 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200308 if (!rtcp_transport_channel_) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000309 return false;
310 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000311 }
312
deadbeef062ce9f2016-08-26 21:42:15 -0700313 LOG(LS_INFO) << "Create non-RTCP TransportChannel for " << content_name()
314 << " on " << transport_name << " transport ";
315 SetTransportChannel_n(
316 false, transport_controller_->CreateTransportChannel_n(
317 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200318 if (!transport_channel_) {
guoweis46383312015-12-17 16:45:59 -0800319 return false;
320 }
321
deadbeefcbecd352015-09-23 11:50:27 -0700322 transport_name_ = transport_name;
deadbeefcbecd352015-09-23 11:50:27 -0700323
324 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700325 // setting new transport channels.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200326 UpdateWritableState_n();
deadbeef062ce9f2016-08-26 21:42:15 -0700327 // We can only update ready-to-send after updating writability.
328 //
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700329 // On setting a new channel, assume it's ready to send if it's writable,
330 // because we have no way of knowing otherwise (the channel doesn't give us
331 // "was last send successful?").
332 //
333 // This won't always be accurate (the last SendPacket call from another
334 // BaseChannel could have resulted in an error), but even so, we'll just
335 // encounter the error again and update "ready to send" accordingly.
deadbeef062ce9f2016-08-26 21:42:15 -0700336 SetTransportChannelReadyToSend(
337 false, transport_channel_ && transport_channel_->writable());
338 SetTransportChannelReadyToSend(
339 true, rtcp_transport_channel_ && rtcp_transport_channel_->writable());
340 return true;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000341}
342
deadbeef062ce9f2016-08-26 21:42:15 -0700343void BaseChannel::SetTransportChannel_n(bool rtcp,
344 TransportChannel* new_channel) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200345 RTC_DCHECK(network_thread_->IsCurrent());
deadbeef062ce9f2016-08-26 21:42:15 -0700346 TransportChannel*& old_channel =
347 rtcp ? rtcp_transport_channel_ : transport_channel_;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000348
deadbeef062ce9f2016-08-26 21:42:15 -0700349 if (!old_channel && !new_channel) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700350 // Nothing to do.
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000351 return;
352 }
deadbeef062ce9f2016-08-26 21:42:15 -0700353 RTC_DCHECK(old_channel != new_channel);
deadbeefcbecd352015-09-23 11:50:27 -0700354
deadbeef062ce9f2016-08-26 21:42:15 -0700355 if (old_channel) {
356 DisconnectFromTransportChannel(old_channel);
Danil Chapovalov7f216b72016-05-12 09:20:31 +0200357 transport_controller_->DestroyTransportChannel_n(
deadbeef062ce9f2016-08-26 21:42:15 -0700358 transport_name_, rtcp ? cricket::ICE_CANDIDATE_COMPONENT_RTCP
359 : cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000360 }
361
deadbeef062ce9f2016-08-26 21:42:15 -0700362 old_channel = new_channel;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000363
deadbeef062ce9f2016-08-26 21:42:15 -0700364 if (new_channel) {
365 if (rtcp) {
366 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
367 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
368 << "should never happen.";
deadbeefcbecd352015-09-23 11:50:27 -0700369 }
deadbeef062ce9f2016-08-26 21:42:15 -0700370 ConnectToTransportChannel(new_channel);
371 auto& socket_options = rtcp ? rtcp_socket_options_ : socket_options_;
372 for (const auto& pair : socket_options) {
373 new_channel->SetOption(pair.first, pair.second);
374 }
guoweis46383312015-12-17 16:45:59 -0800375 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000376}
377
378void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200379 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000380
381 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
johand89ab142016-10-25 10:50:32 -0700382 tc->SignalReadPacket.connect(this, &BaseChannel::OnPacketRead);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000383 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800384 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700385 tc->SignalSelectedCandidatePairChanged.connect(
386 this, &BaseChannel::OnSelectedCandidatePairChanged);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200387 tc->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000388}
389
390void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200391 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800392 OnSelectedCandidatePairChanged(tc, nullptr, -1, false);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000393
394 tc->SignalWritableState.disconnect(this);
395 tc->SignalReadPacket.disconnect(this);
396 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800397 tc->SignalDtlsState.disconnect(this);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200398 tc->SignalSelectedCandidatePairChanged.disconnect(this);
399 tc->SignalSentPacket.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000400}
401
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402bool BaseChannel::Enable(bool enable) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700403 worker_thread_->Invoke<void>(
404 RTC_FROM_HERE,
405 Bind(enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
406 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 return true;
408}
409
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410bool BaseChannel::AddRecvStream(const StreamParams& sp) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700411 return InvokeOnWorker(RTC_FROM_HERE,
412 Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413}
414
Peter Boström0c4e06b2015-10-07 12:23:21 +0200415bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700416 return InvokeOnWorker(RTC_FROM_HERE,
417 Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418}
419
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000420bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000421 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700422 RTC_FROM_HERE, Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000423}
424
Peter Boström0c4e06b2015-10-07 12:23:21 +0200425bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700426 return InvokeOnWorker(RTC_FROM_HERE, Bind(&MediaChannel::RemoveSendStream,
427 media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000428}
429
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000431 ContentAction action,
432 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100433 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700434 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetLocalContent_w,
435 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436}
437
438bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000439 ContentAction action,
440 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100441 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700442 return InvokeOnWorker(RTC_FROM_HERE, Bind(&BaseChannel::SetRemoteContent_w,
443 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444}
445
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000447 // We pass in the BaseChannel instead of the transport_channel_
448 // because if the transport_channel_ changes, the ConnectionMonitor
449 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200450 // We pass in the network thread because on that thread connection monitor
451 // will call BaseChannel::GetConnectionStats which must be called on the
452 // network thread.
453 connection_monitor_.reset(
454 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000455 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000457 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458}
459
460void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000461 if (connection_monitor_) {
462 connection_monitor_->Stop();
463 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 }
465}
466
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000467bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200468 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000469 return transport_channel_->GetStats(infos);
470}
471
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700472bool BaseChannel::IsReadyToReceiveMedia_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 // Receive data if we are enabled and have local content,
474 return enabled() && IsReceiveContentDirection(local_content_direction_);
475}
476
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700477bool BaseChannel::IsReadyToSendMedia_w() const {
478 // Need to access some state updated on the network thread.
479 return network_thread_->Invoke<bool>(
480 RTC_FROM_HERE, Bind(&BaseChannel::IsReadyToSendMedia_n, this));
481}
482
483bool BaseChannel::IsReadyToSendMedia_n() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 // Send outgoing data if we are enabled, have local and remote content,
485 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800486 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 IsSendContentDirection(local_content_direction_) &&
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700488 was_ever_writable() &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200489 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490}
491
jbaucheec21bd2016-03-20 06:15:43 -0700492bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700493 const rtc::PacketOptions& options) {
494 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495}
496
jbaucheec21bd2016-03-20 06:15:43 -0700497bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700498 const rtc::PacketOptions& options) {
499 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500}
501
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000502int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200504 return network_thread_->Invoke<int>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700505 RTC_FROM_HERE, Bind(&BaseChannel::SetOption_n, this, type, opt, value));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200506}
507
508int BaseChannel::SetOption_n(SocketType type,
509 rtc::Socket::Option opt,
510 int value) {
511 RTC_DCHECK(network_thread_->IsCurrent());
512 TransportChannel* channel = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000514 case ST_RTP:
515 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700516 socket_options_.push_back(
517 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000518 break;
519 case ST_RTCP:
520 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700521 rtcp_socket_options_.push_back(
522 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000523 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000525 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526}
527
jbauchcb560652016-08-04 05:20:32 -0700528bool BaseChannel::SetCryptoOptions(const rtc::CryptoOptions& crypto_options) {
529 crypto_options_ = crypto_options;
530 return true;
531}
532
johand89ab142016-10-25 10:50:32 -0700533void BaseChannel::OnWritableState(rtc::PacketTransportInterface* transport) {
534 RTC_DCHECK(transport == transport_channel_ ||
535 transport == rtcp_transport_channel_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200536 RTC_DCHECK(network_thread_->IsCurrent());
537 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538}
539
johand89ab142016-10-25 10:50:32 -0700540void BaseChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
541 const char* data,
542 size_t len,
543 const rtc::PacketTime& packet_time,
544 int flags) {
545 TRACE_EVENT0("webrtc", "BaseChannel::OnPacketRead");
546 // OnPacketRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200547 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548
549 // When using RTCP multiplexing we might get RTCP packets on the RTP
550 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
johand89ab142016-10-25 10:50:32 -0700551 bool rtcp = PacketIsRtcp(transport, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700552 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000553 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000554}
555
johand89ab142016-10-25 10:50:32 -0700556void BaseChannel::OnReadyToSend(rtc::PacketTransportInterface* transport) {
557 RTC_DCHECK(transport == transport_channel_ ||
558 transport == rtcp_transport_channel_);
559 SetTransportChannelReadyToSend(transport == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560}
561
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800562void BaseChannel::OnDtlsState(TransportChannel* channel,
563 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200564 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800565 return;
566 }
567
568 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
569 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
570 // cover other scenarios like the whole channel is writable (not just this
571 // TransportChannel) or when TransportChannel is attached after DTLS is
572 // negotiated.
573 if (state != DTLS_TRANSPORT_CONNECTED) {
574 srtp_filter_.ResetParams();
575 }
576}
577
Honghai Zhangcc411c02016-03-29 17:27:21 -0700578void BaseChannel::OnSelectedCandidatePairChanged(
579 TransportChannel* channel,
Honghai Zhang52dce732016-03-31 12:37:31 -0700580 CandidatePairInterface* selected_candidate_pair,
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700581 int last_sent_packet_id,
582 bool ready_to_send) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700583 RTC_DCHECK(channel == transport_channel_ ||
584 channel == rtcp_transport_channel_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200585 RTC_DCHECK(network_thread_->IsCurrent());
michaelt79e05882016-11-08 02:50:09 -0800586 selected_candidate_pair_ = selected_candidate_pair;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200587 std::string transport_name = channel->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700588 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700589 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700590 network_route = rtc::NetworkRoute(
Taylor Brandstetter6bb1ef22016-06-27 18:09:03 -0700591 ready_to_send, selected_candidate_pair->local_candidate().network_id(),
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700592 selected_candidate_pair->remote_candidate().network_id(),
593 last_sent_packet_id);
michaelt79e05882016-11-08 02:50:09 -0800594
595 UpdateTransportOverhead();
Honghai Zhangcc411c02016-03-29 17:27:21 -0700596 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200597 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700598 RTC_FROM_HERE, worker_thread_,
599 Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_, transport_name,
600 network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700601}
602
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700603void BaseChannel::SetTransportChannelReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200604 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700605 if (rtcp) {
606 rtcp_ready_to_send_ = ready;
607 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 rtp_ready_to_send_ = ready;
609 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200611 bool ready_to_send =
612 (rtp_ready_to_send_ &&
613 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
614 (rtcp_ready_to_send_ || !rtcp_transport_channel_));
615
616 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700617 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200618 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619}
620
johand89ab142016-10-25 10:50:32 -0700621bool BaseChannel::PacketIsRtcp(const rtc::PacketTransportInterface* transport,
622 const char* data,
623 size_t len) {
624 return (transport == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000625 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626}
627
stefanc1aeaf02015-10-15 07:26:07 -0700628bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700629 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700630 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200631 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
632 // If the thread is not our network thread, we will post to our network
633 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 // synchronize access to all the pieces of the send path, including
635 // SRTP and the inner workings of the transport channels.
636 // The only downside is that we can't return a proper failure code if
637 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200638 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200640 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
641 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800642 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700643 data->options = options;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700644 network_thread_->Post(RTC_FROM_HERE, this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 return true;
646 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200647 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648
649 // Now that we are on the correct thread, ensure we have a place to send this
650 // packet before doing anything. (We might get RTCP packets that we don't
651 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
652 // transport.
653 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
654 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000655 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 return false;
657 }
658
659 // Protect ourselves against crazy data.
660 if (!ValidPacket(rtcp, packet)) {
661 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000662 << PacketType(rtcp)
663 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 return false;
665 }
666
stefanc1aeaf02015-10-15 07:26:07 -0700667 rtc::PacketOptions updated_options;
668 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 // Protect if needed.
670 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200671 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200673 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000674 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000675 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000676 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
677 // inside libsrtp for a RTP packet. A external HMAC module will be writing
678 // a fake HMAC value. This is ONLY done for a RTP packet.
679 // Socket layer will update rtp sendtime extension header if present in
680 // packet with current time before updating the HMAC.
681#if !defined(ENABLE_EXTERNAL_AUTH)
682 res = srtp_filter_.ProtectRtp(
683 data, len, static_cast<int>(packet->capacity()), &len);
684#else
stefanc1aeaf02015-10-15 07:26:07 -0700685 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000686 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000687 res = srtp_filter_.ProtectRtp(
688 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700689 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000690 // If protection succeeds, let's get auth params from srtp.
691 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200692 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000693 int key_len;
694 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700695 &auth_key, &key_len,
696 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000697 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700698 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
699 updated_options.packet_time_params.srtp_auth_key.assign(
700 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000701 }
702 }
703#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 if (!res) {
705 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200706 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 GetRtpSeqNum(data, len, &seq_num);
708 GetRtpSsrc(data, len, &ssrc);
709 LOG(LS_ERROR) << "Failed to protect " << content_name_
710 << " RTP packet: size=" << len
711 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
712 return false;
713 }
714 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000715 res = srtp_filter_.ProtectRtcp(data, len,
716 static_cast<int>(packet->capacity()),
717 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 if (!res) {
719 int type = -1;
720 GetRtcpType(data, len, &type);
721 LOG(LS_ERROR) << "Failed to protect " << content_name_
722 << " RTCP packet: size=" << len << ", type=" << type;
723 return false;
724 }
725 }
726
727 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000728 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 } else if (secure_required_) {
deadbeef8f425f92016-12-01 12:26:27 -0800730 // The audio/video engines may attempt to send RTCP packets as soon as the
731 // streams are created, so don't treat this as an error for RTCP.
732 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=6809
733 if (rtcp) {
734 return false;
735 }
736 // However, there shouldn't be any RTP packets sent before SRTP is set up
737 // (and SetSend(true) is called).
738 LOG(LS_ERROR) << "Can't send outgoing RTP packet when SRTP is inactive"
739 << " and crypto is required";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700740 RTC_DCHECK(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 return false;
742 }
743
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200745 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
746 int ret = channel->SendPacket(packet->data<char>(), packet->size(),
747 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000748 if (ret != static_cast<int>(packet->size())) {
skvladc309e0e2016-07-28 17:15:20 -0700749 if (channel->GetError() == ENOTCONN) {
750 LOG(LS_WARNING) << "Got ENOTCONN from transport.";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700751 SetTransportChannelReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 }
753 return false;
754 }
755 return true;
756}
757
jbaucheec21bd2016-03-20 06:15:43 -0700758bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 // Protect ourselves against crazy data.
760 if (!ValidPacket(rtcp, packet)) {
761 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000762 << PacketType(rtcp)
763 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 return false;
765 }
pbos482b12e2015-11-16 10:19:58 -0800766 if (rtcp) {
767 // Permit all (seemingly valid) RTCP packets.
768 return true;
769 }
770 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700771 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772}
773
jbaucheec21bd2016-03-20 06:15:43 -0700774void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000775 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200776 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 if (!WantsPacket(rtcp, packet)) {
778 return;
779 }
780
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000781 // We are only interested in the first rtp packet because that
782 // indicates the media has started flowing.
783 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 has_received_packet_ = true;
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700785 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_FIRSTPACKETRECEIVED);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000786 }
787
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 // Unprotect the packet, if needed.
789 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200790 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200791 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000792 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 bool res;
794 if (!rtcp) {
795 res = srtp_filter_.UnprotectRtp(data, len, &len);
796 if (!res) {
797 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200798 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 GetRtpSeqNum(data, len, &seq_num);
800 GetRtpSsrc(data, len, &ssrc);
801 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
802 << " RTP packet: size=" << len
803 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
804 return;
805 }
806 } else {
807 res = srtp_filter_.UnprotectRtcp(data, len, &len);
808 if (!res) {
809 int type = -1;
810 GetRtcpType(data, len, &type);
811 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
812 << " RTCP packet: size=" << len << ", type=" << type;
813 return;
814 }
815 }
816
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000817 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 } else if (secure_required_) {
819 // Our session description indicates that SRTP is required, but we got a
820 // packet before our SRTP filter is active. This means either that
821 // a) we got SRTP packets before we received the SDES keys, in which case
822 // we can't decrypt it anyway, or
823 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
824 // channels, so we haven't yet extracted keys, even if DTLS did complete
825 // on the channel that the packets are being sent on. It's really good
826 // practice to wait for both RTP and RTCP to be good to go before sending
827 // media, to prevent weird failure modes, so it's fine for us to just eat
828 // packets here. This is all sidestepped if RTCP mux is used anyway.
829 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
830 << " packet when SRTP is inactive and crypto is required";
831 return;
832 }
833
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200834 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700835 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200836 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
837}
838
839void BaseChannel::OnPacketReceived(bool rtcp,
840 const rtc::CopyOnWriteBuffer& packet,
841 const rtc::PacketTime& packet_time) {
842 RTC_DCHECK(worker_thread_->IsCurrent());
843 // Need to copy variable because OnRtcpReceived/OnPacketReceived
844 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
845 rtc::CopyOnWriteBuffer data(packet);
846 if (rtcp) {
847 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200849 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 }
851}
852
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000853bool BaseChannel::PushdownLocalDescription(
854 const SessionDescription* local_desc, ContentAction action,
855 std::string* error_desc) {
856 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 const MediaContentDescription* content_desc =
858 GetContentDescription(content_info);
859 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000860 !SetLocalContent(content_desc, action, error_desc)) {
861 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
862 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000864 return true;
865}
866
867bool BaseChannel::PushdownRemoteDescription(
868 const SessionDescription* remote_desc, ContentAction action,
869 std::string* error_desc) {
870 const ContentInfo* content_info = GetFirstContent(remote_desc);
871 const MediaContentDescription* content_desc =
872 GetContentDescription(content_info);
873 if (content_desc && content_info && !content_info->rejected &&
874 !SetRemoteContent(content_desc, action, error_desc)) {
875 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
876 return false;
877 }
878 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000879}
880
881void BaseChannel::EnableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700882 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 if (enabled_)
884 return;
885
886 LOG(LS_INFO) << "Channel enabled";
887 enabled_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700888 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889}
890
891void BaseChannel::DisableMedia_w() {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700892 RTC_DCHECK(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 if (!enabled_)
894 return;
895
896 LOG(LS_INFO) << "Channel disabled";
897 enabled_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700898 UpdateMediaSendRecvState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899}
900
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200901void BaseChannel::UpdateWritableState_n() {
deadbeefcbecd352015-09-23 11:50:27 -0700902 if (transport_channel_ && transport_channel_->writable() &&
903 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200904 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700905 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200906 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700907 }
908}
909
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200910void BaseChannel::ChannelWritable_n() {
911 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800912 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800914 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915
deadbeefcbecd352015-09-23 11:50:27 -0700916 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 << (was_ever_writable_ ? "" : " for the first time");
918
michaelt79e05882016-11-08 02:50:09 -0800919 if (selected_candidate_pair_)
920 LOG(LS_INFO)
921 << "Using "
922 << selected_candidate_pair_->local_candidate().ToSensitiveString()
923 << "->"
924 << selected_candidate_pair_->remote_candidate().ToSensitiveString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200927 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 writable_ = true;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700929 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930}
931
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200932void BaseChannel::SignalDtlsSetupFailure_n(bool rtcp) {
933 RTC_DCHECK(network_thread_->IsCurrent());
934 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -0700935 RTC_FROM_HERE, signaling_thread(),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200936 Bind(&BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000937}
938
939void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -0700940 RTC_DCHECK(signaling_thread() == rtc::Thread::Current());
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000941 SignalDtlsSetupFailure(this, rtcp);
942}
943
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200944bool BaseChannel::SetDtlsSrtpCryptoSuites_n(TransportChannel* tc, bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800945 std::vector<int> crypto_suites;
946 // We always use the default SRTP crypto suites for RTCP, but we may use
947 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200949 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 } else {
jbauchcb560652016-08-04 05:20:32 -0700951 GetDefaultSrtpCryptoSuites(crypto_options(), &crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800953 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954}
955
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200956bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800957 // Since DTLS is applied to all channels, checking RTP should be enough.
958 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959}
960
961// This function returns true if either DTLS-SRTP is not in use
962// *or* DTLS-SRTP is successfully set up.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200963bool BaseChannel::SetupDtlsSrtp_n(bool rtcp_channel) {
964 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 bool ret = false;
966
deadbeefcbecd352015-09-23 11:50:27 -0700967 TransportChannel* channel =
968 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800970 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800972 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800974 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
975 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 return false;
977 }
978
979 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
980 << content_name() << " "
981 << PacketType(rtcp_channel);
982
jbauchcb560652016-08-04 05:20:32 -0700983 int key_len;
984 int salt_len;
985 if (!rtc::GetSrtpKeyAndSaltLengths(selected_crypto_suite, &key_len,
986 &salt_len)) {
987 LOG(LS_ERROR) << "Unknown DTLS-SRTP crypto suite" << selected_crypto_suite;
988 return false;
989 }
990
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 // OK, we're now doing DTLS (RFC 5764)
jbauchcb560652016-08-04 05:20:32 -0700992 std::vector<unsigned char> dtls_buffer(key_len * 2 + salt_len * 2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993
994 // RFC 5705 exporter using the RFC 5764 parameters
995 if (!channel->ExportKeyingMaterial(
996 kDtlsSrtpExporterLabel,
997 NULL, 0, false,
998 &dtls_buffer[0], dtls_buffer.size())) {
999 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001000 RTC_DCHECK(false); // This should never happen
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 return false;
1002 }
1003
1004 // Sync up the keys with the DTLS-SRTP interface
jbauchcb560652016-08-04 05:20:32 -07001005 std::vector<unsigned char> client_write_key(key_len + salt_len);
1006 std::vector<unsigned char> server_write_key(key_len + salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 size_t offset = 0;
jbauchcb560652016-08-04 05:20:32 -07001008 memcpy(&client_write_key[0], &dtls_buffer[offset], key_len);
1009 offset += key_len;
1010 memcpy(&server_write_key[0], &dtls_buffer[offset], key_len);
1011 offset += key_len;
1012 memcpy(&client_write_key[key_len], &dtls_buffer[offset], salt_len);
1013 offset += salt_len;
1014 memcpy(&server_write_key[key_len], &dtls_buffer[offset], salt_len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015
1016 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001017 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +00001018 if (!channel->GetSslRole(&role)) {
1019 LOG(LS_WARNING) << "GetSslRole failed";
1020 return false;
1021 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001023 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024 send_key = &server_write_key;
1025 recv_key = &client_write_key;
1026 } else {
1027 send_key = &client_write_key;
1028 recv_key = &server_write_key;
1029 }
1030
1031 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001032 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1033 static_cast<int>(send_key->size()),
1034 selected_crypto_suite, &(*recv_key)[0],
1035 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001037 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1038 static_cast<int>(send_key->size()),
1039 selected_crypto_suite, &(*recv_key)[0],
1040 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 }
1042
michaelt79e05882016-11-08 02:50:09 -08001043 if (!ret) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
michaelt79e05882016-11-08 02:50:09 -08001045 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 dtls_keyed_ = true;
michaelt79e05882016-11-08 02:50:09 -08001047 UpdateTransportOverhead();
1048 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 return ret;
1050}
1051
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001052void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001053 if (srtp_filter_.IsActive()) {
1054 return;
1055 }
1056
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001057 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001058 return;
1059 }
1060
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001061 if (!SetupDtlsSrtp_n(false)) {
1062 SignalDtlsSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001063 return;
1064 }
1065
1066 if (rtcp_transport_channel_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001067 if (!SetupDtlsSrtp_n(true)) {
1068 SignalDtlsSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001069 return;
1070 }
1071 }
1072}
1073
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001074void BaseChannel::ChannelNotWritable_n() {
1075 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 if (!writable_)
1077 return;
1078
deadbeefcbecd352015-09-23 11:50:27 -07001079 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 writable_ = false;
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001081 UpdateMediaSendRecvState();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082}
1083
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001084bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001085 const MediaContentDescription* content,
1086 ContentAction action,
1087 ContentSource src,
1088 std::string* error_desc) {
1089 if (action == CA_UPDATE) {
1090 // These parameters never get changed by a CA_UDPATE.
1091 return true;
1092 }
1093
1094 // Cache secure_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001095 return network_thread_->Invoke<bool>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001096 RTC_FROM_HERE, Bind(&BaseChannel::SetRtpTransportParameters_n, this,
1097 content, action, src, error_desc));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001098}
1099
1100bool BaseChannel::SetRtpTransportParameters_n(
1101 const MediaContentDescription* content,
1102 ContentAction action,
1103 ContentSource src,
1104 std::string* error_desc) {
1105 RTC_DCHECK(network_thread_->IsCurrent());
1106
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001107 if (src == CS_LOCAL) {
1108 set_secure_required(content->crypto_required() != CT_NONE);
1109 }
1110
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001111 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001112 return false;
1113 }
1114
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001115 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001116 return false;
1117 }
1118
1119 return true;
1120}
1121
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001122// |dtls| will be set to true if DTLS is active for transport channel and
1123// crypto is empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001124bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1125 bool* dtls,
1126 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001127 *dtls = transport_channel_->IsDtlsActive();
1128 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001129 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001130 return false;
1131 }
1132 return true;
1133}
1134
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001135bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001136 ContentAction action,
1137 ContentSource src,
1138 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001139 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001140 if (action == CA_UPDATE) {
1141 // no crypto params.
1142 return true;
1143 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001144 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001145 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001146 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001147 if (!ret) {
1148 return false;
1149 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 switch (action) {
1151 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001152 // If DTLS is already active on the channel, we could be renegotiating
1153 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001154 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001155 ret = srtp_filter_.SetOffer(cryptos, src);
1156 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 break;
1158 case CA_PRANSWER:
1159 // If we're doing DTLS-SRTP, we don't want to update the filter
1160 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001161 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001162 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1163 }
1164 break;
1165 case CA_ANSWER:
1166 // If we're doing DTLS-SRTP, we don't want to update the filter
1167 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001168 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 ret = srtp_filter_.SetAnswer(cryptos, src);
1170 }
1171 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172 default:
1173 break;
1174 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001175 if (!ret) {
1176 SafeSetError("Failed to setup SRTP filter.", error_desc);
1177 return false;
1178 }
1179 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180}
1181
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001182void BaseChannel::ActivateRtcpMux() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001183 network_thread_->Invoke<void>(RTC_FROM_HERE,
1184 Bind(&BaseChannel::ActivateRtcpMux_n, this));
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001185}
1186
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001187void BaseChannel::ActivateRtcpMux_n() {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001188 if (!rtcp_mux_filter_.IsActive()) {
1189 rtcp_mux_filter_.SetActive();
deadbeef062ce9f2016-08-26 21:42:15 -07001190 SetTransportChannel_n(true, nullptr);
1191 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
1192 // removing channel.
1193 UpdateWritableState_n();
1194 SetTransportChannelReadyToSend(true, false);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001195 }
1196}
1197
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001198bool BaseChannel::SetRtcpMux_n(bool enable,
1199 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001200 ContentSource src,
1201 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 bool ret = false;
1203 switch (action) {
1204 case CA_OFFER:
1205 ret = rtcp_mux_filter_.SetOffer(enable, src);
1206 break;
1207 case CA_PRANSWER:
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001208 // This may activate RTCP muxing, but we don't yet destroy the channel
1209 // because the final answer may deactivate it.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1211 break;
1212 case CA_ANSWER:
1213 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1214 if (ret && rtcp_mux_filter_.IsActive()) {
1215 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001216 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1217 << " by destroying RTCP transport channel for "
1218 << transport_name();
deadbeef062ce9f2016-08-26 21:42:15 -07001219 SetTransportChannel_n(true, nullptr);
1220 UpdateWritableState_n();
1221 SetTransportChannelReadyToSend(true, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 }
1223 break;
1224 case CA_UPDATE:
1225 // No RTCP mux info.
1226 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001227 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 default:
1229 break;
1230 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001231 if (!ret) {
1232 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1233 return false;
1234 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1236 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1237 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001238 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 // If the RTP transport is already writable, then so are we.
1240 if (transport_channel_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001241 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 }
1243 }
1244
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001245 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246}
1247
1248bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001249 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001250 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251}
1252
Peter Boström0c4e06b2015-10-07 12:23:21 +02001253bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001254 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 return media_channel()->RemoveRecvStream(ssrc);
1256}
1257
1258bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001259 ContentAction action,
1260 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1262 action == CA_PRANSWER || action == CA_UPDATE))
1263 return false;
1264
1265 // If this is an update, streams only contain streams that have changed.
1266 if (action == CA_UPDATE) {
1267 for (StreamParamsVec::const_iterator it = streams.begin();
1268 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001269 const StreamParams* existing_stream =
1270 GetStreamByIds(local_streams_, it->groupid, it->id);
1271 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 if (media_channel()->AddSendStream(*it)) {
1273 local_streams_.push_back(*it);
1274 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1275 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001276 std::ostringstream desc;
1277 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1278 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 return false;
1280 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001281 } else if (existing_stream && !it->has_ssrcs()) {
1282 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001283 std::ostringstream desc;
1284 desc << "Failed to remove send stream with ssrc "
1285 << it->first_ssrc() << ".";
1286 SafeSetError(desc.str(), error_desc);
1287 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001289 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290 } else {
1291 LOG(LS_WARNING) << "Ignore unsupported stream update";
1292 }
1293 }
1294 return true;
1295 }
1296 // Else streams are all the streams we want to send.
1297
1298 // Check for streams that have been removed.
1299 bool ret = true;
1300 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1301 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001302 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001304 std::ostringstream desc;
1305 desc << "Failed to remove send stream with ssrc "
1306 << it->first_ssrc() << ".";
1307 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308 ret = false;
1309 }
1310 }
1311 }
1312 // Check for new streams.
1313 for (StreamParamsVec::const_iterator it = streams.begin();
1314 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001315 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001317 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001319 std::ostringstream desc;
1320 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1321 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 ret = false;
1323 }
1324 }
1325 }
1326 local_streams_ = streams;
1327 return ret;
1328}
1329
1330bool BaseChannel::UpdateRemoteStreams_w(
1331 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001332 ContentAction action,
1333 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1335 action == CA_PRANSWER || action == CA_UPDATE))
1336 return false;
1337
1338 // If this is an update, streams only contain streams that have changed.
1339 if (action == CA_UPDATE) {
1340 for (StreamParamsVec::const_iterator it = streams.begin();
1341 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001342 const StreamParams* existing_stream =
1343 GetStreamByIds(remote_streams_, it->groupid, it->id);
1344 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 if (AddRecvStream_w(*it)) {
1346 remote_streams_.push_back(*it);
1347 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1348 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001349 std::ostringstream desc;
1350 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1351 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352 return false;
1353 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001354 } else if (existing_stream && !it->has_ssrcs()) {
1355 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001356 std::ostringstream desc;
1357 desc << "Failed to remove remote stream with ssrc "
1358 << it->first_ssrc() << ".";
1359 SafeSetError(desc.str(), error_desc);
1360 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001362 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363 } else {
1364 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001365 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 << " new stream = " << it->ToString();
1367 }
1368 }
1369 return true;
1370 }
1371 // Else streams are all the streams we want to receive.
1372
1373 // Check for streams that have been removed.
1374 bool ret = true;
1375 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1376 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001377 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001379 std::ostringstream desc;
1380 desc << "Failed to remove remote stream with ssrc "
1381 << it->first_ssrc() << ".";
1382 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383 ret = false;
1384 }
1385 }
1386 }
1387 // Check for new streams.
1388 for (StreamParamsVec::const_iterator it = streams.begin();
1389 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001390 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001391 if (AddRecvStream_w(*it)) {
1392 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1393 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001394 std::ostringstream desc;
1395 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1396 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 ret = false;
1398 }
1399 }
1400 }
1401 remote_streams_ = streams;
1402 return ret;
1403}
1404
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001405void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
isheriff6f8d6862016-05-26 11:24:55 -07001406 const std::vector<webrtc::RtpExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001407// Absolute Send Time extension id is used only with external auth,
1408// so do not bother searching for it and making asyncronious call to set
1409// something that is not used.
1410#if defined(ENABLE_EXTERNAL_AUTH)
isheriff6f8d6862016-05-26 11:24:55 -07001411 const webrtc::RtpExtension* send_time_extension =
1412 FindHeaderExtension(extensions, webrtc::RtpExtension::kAbsSendTimeUri);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001413 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001414 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001415 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001416 RTC_FROM_HERE, network_thread_,
1417 Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n, this,
1418 rtp_abs_sendtime_extn_id));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001419#endif
1420}
1421
1422void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1423 int rtp_abs_sendtime_extn_id) {
1424 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001425}
1426
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001427void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001428 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001430 case MSG_SEND_RTP_PACKET:
1431 case MSG_SEND_RTCP_PACKET: {
1432 RTC_DCHECK(network_thread_->IsCurrent());
1433 SendPacketMessageData* data =
1434 static_cast<SendPacketMessageData*>(pmsg->pdata);
1435 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1436 SendPacket(rtcp, &data->packet, data->options);
1437 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 break;
1439 }
1440 case MSG_FIRSTPACKETRECEIVED: {
1441 SignalFirstPacketReceived(this);
1442 break;
1443 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444 }
1445}
1446
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001447void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448 // Flush all remaining RTCP messages. This should only be called in
1449 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001450 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001451 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001452 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1453 for (const auto& message : rtcp_messages) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001454 network_thread_->Send(RTC_FROM_HERE, this, MSG_SEND_RTCP_PACKET,
1455 message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456 }
1457}
1458
johand89ab142016-10-25 10:50:32 -07001459void BaseChannel::SignalSentPacket_n(
1460 rtc::PacketTransportInterface* /* transport */,
1461 const rtc::SentPacket& sent_packet) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001462 RTC_DCHECK(network_thread_->IsCurrent());
1463 invoker_.AsyncInvoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001464 RTC_FROM_HERE, worker_thread_,
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001465 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1466}
1467
1468void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1469 RTC_DCHECK(worker_thread_->IsCurrent());
1470 SignalSentPacket(sent_packet);
1471}
1472
1473VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1474 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 MediaEngineInterface* media_engine,
1476 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001477 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 const std::string& content_name,
1479 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001480 : BaseChannel(worker_thread,
1481 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001482 media_channel,
1483 transport_controller,
1484 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001486 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001487 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488
1489VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001490 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491 StopAudioMonitor();
1492 StopMediaMonitor();
1493 // this can't be done in the base class, since it calls a virtual
1494 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001495 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496}
1497
skvlad6c87a672016-05-17 17:49:52 -07001498bool VoiceChannel::Init_w(const std::string* bundle_transport_name) {
1499 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 return false;
1501 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 return true;
1503}
1504
Peter Boström0c4e06b2015-10-07 12:23:21 +02001505bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001506 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001507 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001508 AudioSource* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001509 return InvokeOnWorker(RTC_FROM_HERE,
1510 Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001511 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512}
1513
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514// TODO(juberti): Handle early media the right way. We should get an explicit
1515// ringing message telling us to start playing local ringback, which we cancel
1516// if any early media actually arrives. For now, we do the opposite, which is
1517// to wait 1 second for early media, and start playing local ringback if none
1518// arrives.
1519void VoiceChannel::SetEarlyMedia(bool enable) {
1520 if (enable) {
1521 // Start the early media timeout
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001522 worker_thread()->PostDelayed(RTC_FROM_HERE, kEarlyMediaTimeout, this,
1523 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524 } else {
1525 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001526 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 }
1528}
1529
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530bool VoiceChannel::CanInsertDtmf() {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001531 return InvokeOnWorker(
1532 RTC_FROM_HERE, Bind(&VoiceMediaChannel::CanInsertDtmf, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533}
1534
Peter Boström0c4e06b2015-10-07 12:23:21 +02001535bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1536 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001537 int duration) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001538 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceChannel::InsertDtmf_w, this,
1539 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540}
1541
solenberg4bac9c52015-10-09 02:32:53 -07001542bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001543 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::SetOutputVolume,
1544 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001546
Tommif888bb52015-12-12 01:37:01 +01001547void VoiceChannel::SetRawAudioSink(
1548 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001549 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1550 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001551 // passing. So we invoke to our own little routine that gets a pointer to
1552 // our local variable. This is OK since we're synchronously invoking.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001553 InvokeOnWorker(RTC_FROM_HERE,
1554 Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001555}
1556
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001557webrtc::RtpParameters VoiceChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001558 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001559 RTC_FROM_HERE, Bind(&VoiceChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001560}
1561
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001562webrtc::RtpParameters VoiceChannel::GetRtpSendParameters_w(
1563 uint32_t ssrc) const {
1564 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001565}
1566
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001567bool VoiceChannel::SetRtpSendParameters(
1568 uint32_t ssrc,
1569 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001570 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001571 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001572 Bind(&VoiceChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001573}
1574
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001575bool VoiceChannel::SetRtpSendParameters_w(uint32_t ssrc,
1576 webrtc::RtpParameters parameters) {
1577 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1578}
1579
1580webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters(
1581 uint32_t ssrc) const {
1582 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001583 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001584 Bind(&VoiceChannel::GetRtpReceiveParameters_w, this, ssrc));
1585}
1586
1587webrtc::RtpParameters VoiceChannel::GetRtpReceiveParameters_w(
1588 uint32_t ssrc) const {
1589 return media_channel()->GetRtpReceiveParameters(ssrc);
1590}
1591
1592bool VoiceChannel::SetRtpReceiveParameters(
1593 uint32_t ssrc,
1594 const webrtc::RtpParameters& parameters) {
1595 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001596 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001597 Bind(&VoiceChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1598}
1599
1600bool VoiceChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1601 webrtc::RtpParameters parameters) {
1602 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001603}
1604
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001606 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VoiceMediaChannel::GetStats,
1607 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608}
1609
1610void VoiceChannel::StartMediaMonitor(int cms) {
1611 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001612 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613 media_monitor_->SignalUpdate.connect(
1614 this, &VoiceChannel::OnMediaMonitorUpdate);
1615 media_monitor_->Start(cms);
1616}
1617
1618void VoiceChannel::StopMediaMonitor() {
1619 if (media_monitor_) {
1620 media_monitor_->Stop();
1621 media_monitor_->SignalUpdate.disconnect(this);
1622 media_monitor_.reset();
1623 }
1624}
1625
1626void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001627 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 audio_monitor_
1629 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1630 audio_monitor_->Start(cms);
1631}
1632
1633void VoiceChannel::StopAudioMonitor() {
1634 if (audio_monitor_) {
1635 audio_monitor_->Stop();
1636 audio_monitor_.reset();
1637 }
1638}
1639
1640bool VoiceChannel::IsAudioMonitorRunning() const {
1641 return (audio_monitor_.get() != NULL);
1642}
1643
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001645 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646}
1647
1648int VoiceChannel::GetOutputLevel_w() {
1649 return media_channel()->GetOutputLevel();
1650}
1651
1652void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1653 media_channel()->GetActiveStreams(actives);
1654}
1655
johand89ab142016-10-25 10:50:32 -07001656void VoiceChannel::OnPacketRead(rtc::PacketTransportInterface* transport,
1657 const char* data,
1658 size_t len,
1659 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001660 int flags) {
johand89ab142016-10-25 10:50:32 -07001661 BaseChannel::OnPacketRead(transport, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 // Set a flag when we've received an RTP packet. If we're waiting for early
1663 // media, this will disable the timeout.
johand89ab142016-10-25 10:50:32 -07001664 if (!received_media_ && !PacketIsRtcp(transport, data, len)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 received_media_ = true;
1666 }
1667}
1668
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001669void BaseChannel::UpdateMediaSendRecvState() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001670 RTC_DCHECK(network_thread_->IsCurrent());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001671 invoker_.AsyncInvoke<void>(
1672 RTC_FROM_HERE, worker_thread_,
1673 Bind(&BaseChannel::UpdateMediaSendRecvState_w, this));
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001674}
1675
michaelt79e05882016-11-08 02:50:09 -08001676int BaseChannel::GetTransportOverheadPerPacket() const {
1677 RTC_DCHECK(network_thread_->IsCurrent());
1678
1679 if (!selected_candidate_pair_)
1680 return 0;
1681
1682 int transport_overhead_per_packet = 0;
1683
1684 constexpr int kIpv4Overhaed = 20;
1685 constexpr int kIpv6Overhaed = 40;
1686 transport_overhead_per_packet +=
1687 selected_candidate_pair_->local_candidate().address().family() == AF_INET
1688 ? kIpv4Overhaed
1689 : kIpv6Overhaed;
1690
1691 constexpr int kUdpOverhaed = 8;
1692 constexpr int kTcpOverhaed = 20;
1693 transport_overhead_per_packet +=
1694 selected_candidate_pair_->local_candidate().protocol() ==
1695 TCP_PROTOCOL_NAME
1696 ? kTcpOverhaed
1697 : kUdpOverhaed;
1698
1699 if (secure()) {
1700 int srtp_overhead = 0;
1701 if (srtp_filter_.GetSrtpOverhead(&srtp_overhead))
1702 transport_overhead_per_packet += srtp_overhead;
1703 }
1704
1705 return transport_overhead_per_packet;
1706}
1707
1708void BaseChannel::UpdateTransportOverhead() {
1709 int transport_overhead_per_packet = GetTransportOverheadPerPacket();
1710 if (transport_overhead_per_packet)
1711 invoker_.AsyncInvoke<void>(
1712 RTC_FROM_HERE, worker_thread_,
1713 Bind(&MediaChannel::OnTransportOverheadChanged, media_channel_,
1714 transport_overhead_per_packet));
1715}
1716
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001717void VoiceChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 // Render incoming data if we're the active call, and we have the local
1719 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001720 bool recv = IsReadyToReceiveMedia_w();
solenberg5b14b422015-10-01 04:10:31 -07001721 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722
1723 // Send outgoing data if we're the active call, we have the remote content,
1724 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001725 bool send = IsReadyToSendMedia_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001726 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727
1728 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1729}
1730
1731const ContentInfo* VoiceChannel::GetFirstContent(
1732 const SessionDescription* sdesc) {
1733 return GetFirstAudioContent(sdesc);
1734}
1735
1736bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001737 ContentAction action,
1738 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001739 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001740 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 LOG(LS_INFO) << "Setting local voice description";
1742
1743 const AudioContentDescription* audio =
1744 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001745 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001746 if (!audio) {
1747 SafeSetError("Can't find audio content in local description.", error_desc);
1748 return false;
1749 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001751 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001752 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 }
1754
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001755 AudioRecvParameters recv_params = last_recv_params_;
1756 RtpParametersFromMediaDescription(audio, &recv_params);
1757 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001758 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001759 error_desc);
1760 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001762 for (const AudioCodec& codec : audio->codecs()) {
1763 bundle_filter()->AddPayloadType(codec.id);
1764 }
1765 last_recv_params_ = recv_params;
1766
1767 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1768 // only give it to the media channel once we have a remote
1769 // description too (without a remote description, we won't be able
1770 // to send them anyway).
1771 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1772 SafeSetError("Failed to set local audio description streams.", error_desc);
1773 return false;
1774 }
1775
1776 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001777 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001778 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779}
1780
1781bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001782 ContentAction action,
1783 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001784 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001785 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786 LOG(LS_INFO) << "Setting remote voice description";
1787
1788 const AudioContentDescription* audio =
1789 static_cast<const AudioContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001790 RTC_DCHECK(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001791 if (!audio) {
1792 SafeSetError("Can't find audio content in remote description.", error_desc);
1793 return false;
1794 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001796 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001797 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 }
1799
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001800 AudioSendParameters send_params = last_send_params_;
1801 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001802 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001803 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001804 }
skvladdc1c62c2016-03-16 19:07:43 -07001805
1806 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1807 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001808 SafeSetError("Failed to set remote audio description send parameters.",
1809 error_desc);
1810 return false;
1811 }
1812 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001814 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1815 // and only give it to the media channel once we have a local
1816 // description too (without a local description, we won't be able to
1817 // recv them anyway).
1818 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1819 SafeSetError("Failed to set remote audio description streams.", error_desc);
1820 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821 }
1822
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001823 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001824 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001825 }
1826
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001827 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001828 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001829 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830}
1831
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832void VoiceChannel::HandleEarlyMediaTimeout() {
1833 // This occurs on the main thread, not the worker thread.
1834 if (!received_media_) {
1835 LOG(LS_INFO) << "No early media received before timeout";
1836 SignalEarlyMediaTimeout(this);
1837 }
1838}
1839
Peter Boström0c4e06b2015-10-07 12:23:21 +02001840bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1841 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001842 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 if (!enabled()) {
1844 return false;
1845 }
solenberg1d63dd02015-12-02 12:35:09 -08001846 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847}
1848
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001849void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 case MSG_EARLYMEDIATIMEOUT:
1852 HandleEarlyMediaTimeout();
1853 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 case MSG_CHANNEL_ERROR: {
1855 VoiceChannelErrorMessageData* data =
1856 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 delete data;
1858 break;
1859 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860 default:
1861 BaseChannel::OnMessage(pmsg);
1862 break;
1863 }
1864}
1865
1866void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001867 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 SignalConnectionMonitor(this, infos);
1869}
1870
1871void VoiceChannel::OnMediaMonitorUpdate(
1872 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001873 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 SignalMediaMonitor(this, info);
1875}
1876
1877void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1878 const AudioInfo& info) {
1879 SignalAudioMonitor(this, info);
1880}
1881
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001882void VoiceChannel::GetSrtpCryptoSuites_n(
1883 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07001884 GetSupportedAudioCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885}
1886
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001887VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1888 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001890 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001892 bool rtcp)
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,
perkjc11b1842016-03-07 17:34:13 -08001898 rtcp) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899
skvlad6c87a672016-05-17 17:49:52 -07001900bool VideoChannel::Init_w(const std::string* bundle_transport_name) {
1901 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 return false;
1903 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 return true;
1905}
1906
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001908 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909 StopMediaMonitor();
1910 // this can't be done in the base class, since it calls a virtual
1911 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001912
1913 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914}
1915
nisse08582ff2016-02-04 01:24:52 -08001916bool VideoChannel::SetSink(uint32_t ssrc,
nisseacd935b2016-11-11 03:55:13 -08001917 rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) {
nisse08582ff2016-02-04 01:24:52 -08001918 worker_thread()->Invoke<void>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001919 RTC_FROM_HERE,
nisse08582ff2016-02-04 01:24:52 -08001920 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 return true;
1922}
1923
deadbeef5a4a75a2016-06-02 16:23:38 -07001924bool VideoChannel::SetVideoSend(
nisse2ded9b12016-04-08 02:23:55 -07001925 uint32_t ssrc,
deadbeef5a4a75a2016-06-02 16:23:38 -07001926 bool mute,
1927 const VideoOptions* options,
nisseacd935b2016-11-11 03:55:13 -08001928 rtc::VideoSourceInterface<webrtc::VideoFrame>* source) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001929 return InvokeOnWorker(RTC_FROM_HERE,
1930 Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
deadbeef5a4a75a2016-06-02 16:23:38 -07001931 ssrc, mute, options, source));
solenberg1dd98f32015-09-10 01:57:14 -07001932}
1933
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001934webrtc::RtpParameters VideoChannel::GetRtpSendParameters(uint32_t ssrc) const {
skvladdc1c62c2016-03-16 19:07:43 -07001935 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001936 RTC_FROM_HERE, Bind(&VideoChannel::GetRtpSendParameters_w, this, ssrc));
skvladdc1c62c2016-03-16 19:07:43 -07001937}
1938
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001939webrtc::RtpParameters VideoChannel::GetRtpSendParameters_w(
1940 uint32_t ssrc) const {
1941 return media_channel()->GetRtpSendParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001942}
1943
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001944bool VideoChannel::SetRtpSendParameters(
1945 uint32_t ssrc,
1946 const webrtc::RtpParameters& parameters) {
skvladdc1c62c2016-03-16 19:07:43 -07001947 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001948 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001949 Bind(&VideoChannel::SetRtpSendParameters_w, this, ssrc, parameters));
skvladdc1c62c2016-03-16 19:07:43 -07001950}
1951
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001952bool VideoChannel::SetRtpSendParameters_w(uint32_t ssrc,
1953 webrtc::RtpParameters parameters) {
1954 return media_channel()->SetRtpSendParameters(ssrc, parameters);
1955}
1956
1957webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters(
1958 uint32_t ssrc) const {
1959 return worker_thread()->Invoke<webrtc::RtpParameters>(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001960 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001961 Bind(&VideoChannel::GetRtpReceiveParameters_w, this, ssrc));
1962}
1963
1964webrtc::RtpParameters VideoChannel::GetRtpReceiveParameters_w(
1965 uint32_t ssrc) const {
1966 return media_channel()->GetRtpReceiveParameters(ssrc);
1967}
1968
1969bool VideoChannel::SetRtpReceiveParameters(
1970 uint32_t ssrc,
1971 const webrtc::RtpParameters& parameters) {
1972 return InvokeOnWorker(
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001973 RTC_FROM_HERE,
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001974 Bind(&VideoChannel::SetRtpReceiveParameters_w, this, ssrc, parameters));
1975}
1976
1977bool VideoChannel::SetRtpReceiveParameters_w(uint32_t ssrc,
1978 webrtc::RtpParameters parameters) {
1979 return media_channel()->SetRtpReceiveParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001980}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001981
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001982void VideoChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 // Send outgoing data if we're the active call, we have the remote content,
1984 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07001985 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 if (!media_channel()->SetSend(send)) {
1987 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1988 // TODO(gangji): Report error back to server.
1989 }
1990
Peter Boström34fbfff2015-09-24 19:20:30 +02001991 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992}
1993
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001994bool VideoChannel::GetStats(VideoMediaInfo* stats) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07001995 return InvokeOnWorker(RTC_FROM_HERE, Bind(&VideoMediaChannel::GetStats,
1996 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997}
1998
1999void VideoChannel::StartMediaMonitor(int cms) {
2000 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002001 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 media_monitor_->SignalUpdate.connect(
2003 this, &VideoChannel::OnMediaMonitorUpdate);
2004 media_monitor_->Start(cms);
2005}
2006
2007void VideoChannel::StopMediaMonitor() {
2008 if (media_monitor_) {
2009 media_monitor_->Stop();
2010 media_monitor_.reset();
2011 }
2012}
2013
2014const ContentInfo* VideoChannel::GetFirstContent(
2015 const SessionDescription* sdesc) {
2016 return GetFirstVideoContent(sdesc);
2017}
2018
2019bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002020 ContentAction action,
2021 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002022 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002023 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024 LOG(LS_INFO) << "Setting local video description";
2025
2026 const VideoContentDescription* video =
2027 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002028 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002029 if (!video) {
2030 SafeSetError("Can't find video content in local description.", error_desc);
2031 return false;
2032 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002034 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002035 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036 }
2037
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002038 VideoRecvParameters recv_params = last_recv_params_;
2039 RtpParametersFromMediaDescription(video, &recv_params);
2040 if (!media_channel()->SetRecvParameters(recv_params)) {
2041 SafeSetError("Failed to set local video description recv parameters.",
2042 error_desc);
2043 return false;
2044 }
2045 for (const VideoCodec& codec : video->codecs()) {
2046 bundle_filter()->AddPayloadType(codec.id);
2047 }
2048 last_recv_params_ = recv_params;
2049
2050 // TODO(pthatcher): Move local streams into VideoSendParameters, and
2051 // only give it to the media channel once we have a remote
2052 // description too (without a remote description, we won't be able
2053 // to send them anyway).
2054 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
2055 SafeSetError("Failed to set local video description streams.", error_desc);
2056 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057 }
2058
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002059 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002060 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002061 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062}
2063
2064bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002065 ContentAction action,
2066 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002067 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002068 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069 LOG(LS_INFO) << "Setting remote video description";
2070
2071 const VideoContentDescription* video =
2072 static_cast<const VideoContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002073 RTC_DCHECK(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002074 if (!video) {
2075 SafeSetError("Can't find video content in remote description.", error_desc);
2076 return false;
2077 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002079 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002080 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002081 }
2082
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002083 VideoSendParameters send_params = last_send_params_;
2084 RtpSendParametersFromMediaDescription(video, &send_params);
2085 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08002086 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002087 }
skvladdc1c62c2016-03-16 19:07:43 -07002088
2089 bool parameters_applied = media_channel()->SetSendParameters(send_params);
2090
2091 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002092 SafeSetError("Failed to set remote video description send parameters.",
2093 error_desc);
2094 return false;
2095 }
2096 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002098 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
2099 // and only give it to the media channel once we have a local
2100 // description too (without a local description, we won't be able to
2101 // recv them anyway).
2102 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
2103 SafeSetError("Failed to set remote video description streams.", error_desc);
2104 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105 }
2106
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002107 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002108 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002110
2111 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002112 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002113 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114}
2115
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002116void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118 case MSG_CHANNEL_ERROR: {
2119 const VideoChannelErrorMessageData* data =
2120 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121 delete data;
2122 break;
2123 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124 default:
2125 BaseChannel::OnMessage(pmsg);
2126 break;
2127 }
2128}
2129
2130void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002131 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 SignalConnectionMonitor(this, infos);
2133}
2134
2135// TODO(pthatcher): Look into removing duplicate code between
2136// audio, video, and data, perhaps by using templates.
2137void VideoChannel::OnMediaMonitorUpdate(
2138 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002139 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140 SignalMediaMonitor(this, info);
2141}
2142
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002143void VideoChannel::GetSrtpCryptoSuites_n(
2144 std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002145 GetSupportedVideoCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146}
2147
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002148DataChannel::DataChannel(rtc::Thread* worker_thread,
2149 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002150 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002151 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152 const std::string& content_name,
2153 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002154 : BaseChannel(worker_thread,
2155 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002156 media_channel,
2157 transport_controller,
2158 content_name,
2159 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002160 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002161 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162
2163DataChannel::~DataChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08002164 TRACE_EVENT0("webrtc", "DataChannel::~DataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165 StopMediaMonitor();
2166 // this can't be done in the base class, since it calls a virtual
2167 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002168
2169 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170}
2171
skvlad6c87a672016-05-17 17:49:52 -07002172bool DataChannel::Init_w(const std::string* bundle_transport_name) {
2173 if (!BaseChannel::Init_w(bundle_transport_name)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 return false;
2175 }
2176 media_channel()->SignalDataReceived.connect(
2177 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002178 media_channel()->SignalReadyToSend.connect(
2179 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002180 media_channel()->SignalStreamClosedRemotely.connect(
2181 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182 return true;
2183}
2184
2185bool DataChannel::SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07002186 const rtc::CopyOnWriteBuffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002187 SendDataResult* result) {
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002188 return InvokeOnWorker(
2189 RTC_FROM_HERE, Bind(&DataMediaChannel::SendData, media_channel(), params,
2190 payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002191}
2192
2193const ContentInfo* DataChannel::GetFirstContent(
2194 const SessionDescription* sdesc) {
2195 return GetFirstDataContent(sdesc);
2196}
2197
jbaucheec21bd2016-03-20 06:15:43 -07002198bool DataChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199 if (data_channel_type_ == DCT_SCTP) {
2200 // TODO(pthatcher): Do this in a more robust way by checking for
2201 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002202 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203 } else if (data_channel_type_ == DCT_RTP) {
2204 return BaseChannel::WantsPacket(rtcp, packet);
2205 }
2206 return false;
2207}
2208
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002209bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2210 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 // It hasn't been set before, so set it now.
2212 if (data_channel_type_ == DCT_NONE) {
2213 data_channel_type_ = new_data_channel_type;
2214 return true;
2215 }
2216
2217 // It's been set before, but doesn't match. That's bad.
2218 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002219 std::ostringstream desc;
2220 desc << "Data channel type mismatch."
2221 << " Expected " << data_channel_type_
2222 << " Got " << new_data_channel_type;
2223 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 return false;
2225 }
2226
2227 // It's hasn't changed. Nothing to do.
2228 return true;
2229}
2230
2231bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002232 const DataContentDescription* content,
2233 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2235 (content->protocol() == kMediaProtocolDtlsSctp));
2236 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002237 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002238}
2239
2240bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002241 ContentAction action,
2242 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002243 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002244 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245 LOG(LS_INFO) << "Setting local data description";
2246
2247 const DataContentDescription* data =
2248 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002249 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002250 if (!data) {
2251 SafeSetError("Can't find data content in local description.", error_desc);
2252 return false;
2253 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002254
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002255 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256 return false;
2257 }
2258
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002259 if (data_channel_type_ == DCT_RTP) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002260 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002261 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002262 }
2263 }
2264
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002265 // FYI: We send the SCTP port number (not to be confused with the
2266 // underlying UDP port number) as a codec parameter. So even SCTP
2267 // data channels need codecs.
2268 DataRecvParameters recv_params = last_recv_params_;
2269 RtpParametersFromMediaDescription(data, &recv_params);
2270 if (!media_channel()->SetRecvParameters(recv_params)) {
2271 SafeSetError("Failed to set remote data description recv parameters.",
2272 error_desc);
2273 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002275 if (data_channel_type_ == DCT_RTP) {
2276 for (const DataCodec& codec : data->codecs()) {
2277 bundle_filter()->AddPayloadType(codec.id);
2278 }
2279 }
2280 last_recv_params_ = recv_params;
2281
2282 // TODO(pthatcher): Move local streams into DataSendParameters, and
2283 // only give it to the media channel once we have a remote
2284 // description too (without a remote description, we won't be able
2285 // to send them anyway).
2286 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2287 SafeSetError("Failed to set local data description streams.", error_desc);
2288 return false;
2289 }
2290
2291 set_local_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002292 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002293 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002294}
2295
2296bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002297 ContentAction action,
2298 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002299 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002300 RTC_DCHECK(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301
2302 const DataContentDescription* data =
2303 static_cast<const DataContentDescription*>(content);
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002304 RTC_DCHECK(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002305 if (!data) {
2306 SafeSetError("Can't find data content in remote description.", error_desc);
2307 return false;
2308 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002309
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002310 // If the remote data doesn't have codecs and isn't an update, it
2311 // must be empty, so ignore it.
2312 if (!data->has_codecs() && action != CA_UPDATE) {
2313 return true;
2314 }
2315
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002316 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317 return false;
2318 }
2319
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002320 LOG(LS_INFO) << "Setting remote data description";
2321 if (data_channel_type_ == DCT_RTP &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002322 !SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002323 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 }
2325
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002326
2327 DataSendParameters send_params = last_send_params_;
2328 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2329 if (!media_channel()->SetSendParameters(send_params)) {
2330 SafeSetError("Failed to set remote data description send parameters.",
2331 error_desc);
2332 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002334 last_send_params_ = send_params;
2335
2336 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2337 // and only give it to the media channel once we have a local
2338 // description too (without a local description, we won't be able to
2339 // recv them anyway).
2340 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2341 SafeSetError("Failed to set remote data description streams.",
2342 error_desc);
2343 return false;
2344 }
2345
2346 set_remote_content_direction(content->direction());
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002347 UpdateMediaSendRecvState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002348 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002349}
2350
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002351void DataChannel::UpdateMediaSendRecvState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352 // Render incoming data if we're the active call, and we have the local
2353 // content. We receive data on the default channel and multiplexed streams.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002354 bool recv = IsReadyToReceiveMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355 if (!media_channel()->SetReceive(recv)) {
2356 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2357 }
2358
2359 // Send outgoing data if we're the active call, we have the remote content,
2360 // and we have had some form of connectivity.
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002361 bool send = IsReadyToSendMedia_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002362 if (!media_channel()->SetSend(send)) {
2363 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2364 }
2365
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002366 // Trigger SignalReadyToSendData asynchronously.
2367 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002368
2369 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2370}
2371
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002372void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002373 switch (pmsg->message_id) {
2374 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002375 DataChannelReadyToSendMessageData* data =
2376 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002377 ready_to_send_data_ = data->data();
2378 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002379 delete data;
2380 break;
2381 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002382 case MSG_DATARECEIVED: {
2383 DataReceivedMessageData* data =
2384 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2385 SignalDataReceived(this, data->params, data->payload);
2386 delete data;
2387 break;
2388 }
2389 case MSG_CHANNEL_ERROR: {
2390 const DataChannelErrorMessageData* data =
2391 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002392 delete data;
2393 break;
2394 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002395 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002396 rtc::TypedMessageData<uint32_t>* data =
2397 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002398 SignalStreamClosedRemotely(data->data());
2399 delete data;
2400 break;
2401 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002402 default:
2403 BaseChannel::OnMessage(pmsg);
2404 break;
2405 }
2406}
2407
2408void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002409 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 SignalConnectionMonitor(this, infos);
2411}
2412
2413void DataChannel::StartMediaMonitor(int cms) {
2414 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002415 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002416 media_monitor_->SignalUpdate.connect(
2417 this, &DataChannel::OnMediaMonitorUpdate);
2418 media_monitor_->Start(cms);
2419}
2420
2421void DataChannel::StopMediaMonitor() {
2422 if (media_monitor_) {
2423 media_monitor_->Stop();
2424 media_monitor_->SignalUpdate.disconnect(this);
2425 media_monitor_.reset();
2426 }
2427}
2428
2429void DataChannel::OnMediaMonitorUpdate(
2430 DataMediaChannel* media_channel, const DataMediaInfo& info) {
Taylor Brandstetterbad33bf2016-08-25 13:31:14 -07002431 RTC_DCHECK(media_channel == this->media_channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002432 SignalMediaMonitor(this, info);
2433}
2434
2435void DataChannel::OnDataReceived(
2436 const ReceiveDataParams& params, const char* data, size_t len) {
2437 DataReceivedMessageData* msg = new DataReceivedMessageData(
2438 params, data, len);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002439 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_DATARECEIVED, msg);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002440}
2441
Peter Boström0c4e06b2015-10-07 12:23:21 +02002442void DataChannel::OnDataChannelError(uint32_t ssrc,
2443 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002444 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2445 ssrc, err);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002446 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_CHANNEL_ERROR, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447}
2448
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002449void DataChannel::OnDataChannelReadyToSend(bool writable) {
2450 // This is usded for congestion control to indicate that the stream is ready
2451 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2452 // that the transport channel is ready.
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002453 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_READYTOSENDDATA,
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002454 new DataChannelReadyToSendMessageData(writable));
2455}
2456
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002457void DataChannel::GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const {
jbauchcb560652016-08-04 05:20:32 -07002458 GetSupportedDataCryptoSuites(crypto_options(), crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459}
2460
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002461bool DataChannel::ShouldSetupDtlsSrtp_n() const {
2462 return data_channel_type_ == DCT_RTP && BaseChannel::ShouldSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002463}
2464
Peter Boström0c4e06b2015-10-07 12:23:21 +02002465void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2466 rtc::TypedMessageData<uint32_t>* message =
2467 new rtc::TypedMessageData<uint32_t>(sid);
Taylor Brandstetter5d97a9a2016-06-10 14:17:27 -07002468 signaling_thread()->Post(RTC_FROM_HERE, this, MSG_STREAMCLOSEDREMOTELY,
2469 message);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002470}
2471
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002472} // namespace cricket