blob: 4e58c8fecc4d621bd99b4d9b742b0de024c4390e [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
kwiberg0eb15ed2015-12-17 03:04:15 -080011#include <utility>
12
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010013#include "webrtc/pc/channel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010015#include "webrtc/audio_sink.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000016#include "webrtc/base/bind.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000017#include "webrtc/base/byteorder.h"
18#include "webrtc/base/common.h"
jbaucheec21bd2016-03-20 06:15:43 -070019#include "webrtc/base/copyonwritebuffer.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000020#include "webrtc/base/dscp.h"
21#include "webrtc/base/logging.h"
Honghai Zhangcc411c02016-03-29 17:27:21 -070022#include "webrtc/base/networkroute.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010023#include "webrtc/base/trace_event.h"
kjellanderf4752772016-03-02 05:42:30 -080024#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080025#include "webrtc/media/base/rtputils.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010026#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010027#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000028
29namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000030using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000031
deadbeef2d110be2016-01-13 12:00:26 -080032namespace {
kwiberg31022942016-03-11 14:18:21 -080033// See comment below for why we need to use a pointer to a unique_ptr.
deadbeef2d110be2016-01-13 12:00:26 -080034bool SetRawAudioSink_w(VoiceMediaChannel* channel,
35 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -080036 std::unique_ptr<webrtc::AudioSinkInterface>* sink) {
37 channel->SetRawAudioSink(ssrc, std::move(*sink));
deadbeef2d110be2016-01-13 12:00:26 -080038 return true;
39}
Danil Chapovalov33b01f22016-05-11 19:55:27 +020040
41struct SendPacketMessageData : public rtc::MessageData {
42 rtc::CopyOnWriteBuffer packet;
43 rtc::PacketOptions options;
44};
45
deadbeef2d110be2016-01-13 12:00:26 -080046} // namespace
47
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000049 MSG_EARLYMEDIATIMEOUT = 1,
Danil Chapovalov33b01f22016-05-11 19:55:27 +020050 MSG_SEND_RTP_PACKET,
51 MSG_SEND_RTCP_PACKET,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000056 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057};
58
59// Value specified in RFC 5764.
60static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
61
62static const int kAgcMinus10db = -10;
63
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000064static void SafeSetError(const std::string& message, std::string* error_desc) {
65 if (error_desc) {
66 *error_desc = message;
67 }
68}
69
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000070struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020071 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020073 : ssrc(in_ssrc), error(in_error) {}
74 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 VoiceMediaChannel::Error error;
76};
77
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020079 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020081 : ssrc(in_ssrc), error(in_error) {}
82 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 VideoMediaChannel::Error error;
84};
85
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000086struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 DataMediaChannel::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 DataMediaChannel::Error error;
92};
93
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094static const char* PacketType(bool rtcp) {
95 return (!rtcp) ? "RTP" : "RTCP";
96}
97
jbaucheec21bd2016-03-20 06:15:43 -070098static bool ValidPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 // Check the packet size. We could check the header too if needed.
100 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000101 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
102 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103}
104
105static bool IsReceiveContentDirection(MediaContentDirection direction) {
106 return direction == MD_SENDRECV || direction == MD_RECVONLY;
107}
108
109static bool IsSendContentDirection(MediaContentDirection direction) {
110 return direction == MD_SENDRECV || direction == MD_SENDONLY;
111}
112
113static const MediaContentDescription* GetContentDescription(
114 const ContentInfo* cinfo) {
115 if (cinfo == NULL)
116 return NULL;
117 return static_cast<const MediaContentDescription*>(cinfo->description);
118}
119
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700120template <class Codec>
121void RtpParametersFromMediaDescription(
122 const MediaContentDescriptionImpl<Codec>* desc,
123 RtpParameters<Codec>* params) {
124 // TODO(pthatcher): Remove this once we're sure no one will give us
125 // a description without codecs (currently a CA_UPDATE with just
126 // streams can).
127 if (desc->has_codecs()) {
128 params->codecs = desc->codecs();
129 }
130 // TODO(pthatcher): See if we really need
131 // rtp_header_extensions_set() and remove it if we don't.
132 if (desc->rtp_header_extensions_set()) {
133 params->extensions = desc->rtp_header_extensions();
134 }
deadbeef13871492015-12-09 12:37:51 -0800135 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700136}
137
nisse05103312016-03-16 02:22:50 -0700138template <class Codec>
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700139void RtpSendParametersFromMediaDescription(
140 const MediaContentDescriptionImpl<Codec>* desc,
nisse05103312016-03-16 02:22:50 -0700141 RtpSendParameters<Codec>* send_params) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700142 RtpParametersFromMediaDescription(desc, send_params);
143 send_params->max_bandwidth_bps = desc->bandwidth();
144}
145
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200146BaseChannel::BaseChannel(rtc::Thread* worker_thread,
147 rtc::Thread* network_thread,
deadbeefcbecd352015-09-23 11:50:27 -0700148 MediaChannel* media_channel,
149 TransportController* transport_controller,
150 const std::string& content_name,
151 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200152 : worker_thread_(worker_thread),
153 network_thread_(network_thread),
154
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 content_name_(content_name),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200156
157 transport_controller_(transport_controller),
deadbeefcbecd352015-09-23 11:50:27 -0700158 rtcp_transport_enabled_(rtcp),
159 transport_channel_(nullptr),
160 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 rtp_ready_to_send_(false),
162 rtcp_ready_to_send_(false),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200163 writable_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 was_ever_writable_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 has_received_packet_(false),
166 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000167 secure_required_(false),
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200168 rtp_abs_sendtime_extn_id_(-1),
169
170 media_channel_(media_channel),
171 enabled_(false),
172 local_content_direction_(MD_INACTIVE),
173 remote_content_direction_(MD_INACTIVE) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000174 ASSERT(worker_thread_ == rtc::Thread::Current());
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200175 if (transport_controller) {
176 RTC_DCHECK_EQ(network_thread, transport_controller->worker_thread());
177 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 LOG(LS_INFO) << "Created channel for " << content_name;
179}
180
181BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800182 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000183 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000184 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 StopConnectionMonitor();
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200186 // Send any outstanding RTCP packets.
187 network_thread_->Invoke<void>(Bind(&BaseChannel::FlushRtcpMessages_n, this));
188 // Eats any outstanding messages or packets.
189 worker_thread_->Clear(&invoker_);
190 worker_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191 // We must destroy the media channel before the transport channel, otherwise
192 // the media channel may try to send on the dead transport channel. NULLing
193 // is not an effective strategy since the sends will come on another thread.
194 delete media_channel_;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200195 // Note that we don't just call SetTransportChannel_n(nullptr) because that
deadbeefcbecd352015-09-23 11:50:27 -0700196 // would call a pure virtual method which we can't do from a destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200197 network_thread_->Invoke<void>(Bind(&BaseChannel::DeinitNetwork_n, this));
198 LOG(LS_INFO) << "Destroyed channel";
199}
200
201void BaseChannel::DeinitNetwork_n() {
deadbeefcbecd352015-09-23 11:50:27 -0700202 if (transport_channel_) {
203 DisconnectFromTransportChannel(transport_channel_);
204 transport_controller_->DestroyTransportChannel_w(
205 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
206 }
207 if (rtcp_transport_channel_) {
208 DisconnectFromTransportChannel(rtcp_transport_channel_);
209 transport_controller_->DestroyTransportChannel_w(
210 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
211 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200212 network_thread_->Clear(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213}
214
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200215bool BaseChannel::Init_w() {
216 if (!network_thread_->Invoke<bool>(Bind(&BaseChannel::InitNetwork_n, this))) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000217 return false;
218 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219
wu@webrtc.orgde305012013-10-31 15:40:38 +0000220 // Both RTP and RTCP channels are set, we can call SetInterface on
221 // media channel and it can set network options.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200222 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.orgde305012013-10-31 15:40:38 +0000223 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224 return true;
225}
226
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200227bool BaseChannel::InitNetwork_n() {
228 RTC_DCHECK(network_thread_->IsCurrent());
229 if (!SetTransport_n(content_name())) {
230 return false;
231 }
232
233 if (!SetDtlsSrtpCryptoSuites_n(transport_channel_, false)) {
234 return false;
235 }
236 if (rtcp_transport_enabled() &&
237 !SetDtlsSrtpCryptoSuites_n(rtcp_transport_channel_, true)) {
238 return false;
239 }
240 return true;
241}
242
wu@webrtc.org78187522013-10-07 23:32:02 +0000243void BaseChannel::Deinit() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200244 RTC_DCHECK(worker_thread_->IsCurrent());
wu@webrtc.org78187522013-10-07 23:32:02 +0000245 media_channel_->SetInterface(NULL);
246}
247
deadbeefcbecd352015-09-23 11:50:27 -0700248bool BaseChannel::SetTransport(const std::string& transport_name) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200249 return network_thread_->Invoke<bool>(
250 Bind(&BaseChannel::SetTransport_n, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000251}
252
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200253bool BaseChannel::SetTransport_n(const std::string& transport_name) {
254 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000255
deadbeefcbecd352015-09-23 11:50:27 -0700256 if (transport_name == transport_name_) {
257 // Nothing to do if transport name isn't changing
258 return true;
259 }
260
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800261 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
262 // changes and wait until the DTLS handshake is complete to set the newly
263 // negotiated parameters.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200264 if (ShouldSetupDtlsSrtp_n()) {
guoweis46383312015-12-17 16:45:59 -0800265 // Set |writable_| to false such that UpdateWritableState_w can set up
266 // DTLS-SRTP when the writable_ becomes true again.
267 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800268 srtp_filter_.ResetParams();
269 }
270
guoweis46383312015-12-17 16:45:59 -0800271 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
deadbeefcbecd352015-09-23 11:50:27 -0700272 if (rtcp_transport_enabled()) {
273 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
274 << " on " << transport_name << " transport ";
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200275 SetRtcpTransportChannel_n(
guoweis46383312015-12-17 16:45:59 -0800276 transport_controller_->CreateTransportChannel_w(
277 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP),
278 false /* update_writablity */);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200279 if (!rtcp_transport_channel_) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000280 return false;
281 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000282 }
283
guoweis46383312015-12-17 16:45:59 -0800284 // We're not updating the writablity during the transition state.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200285 SetTransportChannel_n(transport_controller_->CreateTransportChannel_w(
guoweis46383312015-12-17 16:45:59 -0800286 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200287 if (!transport_channel_) {
guoweis46383312015-12-17 16:45:59 -0800288 return false;
289 }
290
291 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
292 if (rtcp_transport_enabled()) {
293 // We can only update the RTCP ready to send after set_transport_channel has
294 // handled channel writability.
295 SetReadyToSend(
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200296 true, rtcp_transport_channel_ && rtcp_transport_channel_->writable());
guoweis46383312015-12-17 16:45:59 -0800297 }
deadbeefcbecd352015-09-23 11:50:27 -0700298 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000299 return true;
300}
301
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200302void BaseChannel::SetTransportChannel_n(TransportChannel* new_tc) {
303 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000304
305 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700306 if (!old_tc && !new_tc) {
307 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000308 return;
309 }
deadbeefcbecd352015-09-23 11:50:27 -0700310 ASSERT(old_tc != new_tc);
311
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000312 if (old_tc) {
313 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700314 transport_controller_->DestroyTransportChannel_w(
315 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000316 }
317
318 transport_channel_ = new_tc;
319
320 if (new_tc) {
321 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700322 for (const auto& pair : socket_options_) {
323 new_tc->SetOption(pair.first, pair.second);
324 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000325 }
deadbeefcbecd352015-09-23 11:50:27 -0700326
327 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
328 // setting new channel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200329 UpdateWritableState_n();
deadbeefcbecd352015-09-23 11:50:27 -0700330 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000331}
332
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200333void BaseChannel::SetRtcpTransportChannel_n(TransportChannel* new_tc,
334 bool update_writablity) {
335 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000336
337 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700338 if (!old_tc && !new_tc) {
339 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000340 return;
341 }
deadbeefcbecd352015-09-23 11:50:27 -0700342 ASSERT(old_tc != new_tc);
343
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000344 if (old_tc) {
345 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700346 transport_controller_->DestroyTransportChannel_w(
347 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000348 }
349
350 rtcp_transport_channel_ = new_tc;
351
352 if (new_tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200353 RTC_CHECK(!(ShouldSetupDtlsSrtp_n() && srtp_filter_.IsActive()))
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800354 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
355 << "should never happen.";
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000356 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700357 for (const auto& pair : rtcp_socket_options_) {
358 new_tc->SetOption(pair.first, pair.second);
359 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000360 }
deadbeefcbecd352015-09-23 11:50:27 -0700361
guoweis46383312015-12-17 16:45:59 -0800362 if (update_writablity) {
363 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
364 // setting new channel
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200365 UpdateWritableState_n();
guoweis46383312015-12-17 16:45:59 -0800366 SetReadyToSend(true, new_tc && new_tc->writable());
367 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000368}
369
370void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200371 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000372
373 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
374 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
375 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800376 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700377 tc->SignalSelectedCandidatePairChanged.connect(
378 this, &BaseChannel::OnSelectedCandidatePairChanged);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200379 tc->SignalSentPacket.connect(this, &BaseChannel::SignalSentPacket_n);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000380}
381
382void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200383 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000384
385 tc->SignalWritableState.disconnect(this);
386 tc->SignalReadPacket.disconnect(this);
387 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800388 tc->SignalDtlsState.disconnect(this);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200389 tc->SignalSelectedCandidatePairChanged.disconnect(this);
390 tc->SignalSentPacket.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000391}
392
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000394 worker_thread_->Invoke<void>(Bind(
395 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
396 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 return true;
398}
399
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000401 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402}
403
Peter Boström0c4e06b2015-10-07 12:23:21 +0200404bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000405 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406}
407
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000408bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000409 return InvokeOnWorker(
410 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000411}
412
Peter Boström0c4e06b2015-10-07 12:23:21 +0200413bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000414 return InvokeOnWorker(
415 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000416}
417
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000419 ContentAction action,
420 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100421 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000422 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
423 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424}
425
426bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000427 ContentAction action,
428 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100429 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000430 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
431 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432}
433
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000435 // We pass in the BaseChannel instead of the transport_channel_
436 // because if the transport_channel_ changes, the ConnectionMonitor
437 // would be pointing to the wrong TransportChannel.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200438 // We pass in the network thread because on that thread connection monitor
439 // will call BaseChannel::GetConnectionStats which must be called on the
440 // network thread.
441 connection_monitor_.reset(
442 new ConnectionMonitor(this, network_thread(), rtc::Thread::Current()));
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000443 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000445 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446}
447
448void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000449 if (connection_monitor_) {
450 connection_monitor_->Stop();
451 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 }
453}
454
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000455bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200456 RTC_DCHECK(network_thread_->IsCurrent());
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000457 return transport_channel_->GetStats(infos);
458}
459
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200460bool BaseChannel::IsReadyToReceive_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461 // Receive data if we are enabled and have local content,
462 return enabled() && IsReceiveContentDirection(local_content_direction_);
463}
464
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200465bool BaseChannel::IsReadyToSend_w() const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 // Send outgoing data if we are enabled, have local and remote content,
467 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800468 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469 IsSendContentDirection(local_content_direction_) &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200470 network_thread_->Invoke<bool>(
471 Bind(&BaseChannel::IsTransportReadyToSend_n, this));
472}
473
474bool BaseChannel::IsTransportReadyToSend_n() const {
475 return was_ever_writable() &&
476 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp_n());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477}
478
jbaucheec21bd2016-03-20 06:15:43 -0700479bool BaseChannel::SendPacket(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700480 const rtc::PacketOptions& options) {
481 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482}
483
jbaucheec21bd2016-03-20 06:15:43 -0700484bool BaseChannel::SendRtcp(rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700485 const rtc::PacketOptions& options) {
486 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487}
488
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000489int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 int value) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200491 return network_thread_->Invoke<int>(
492 Bind(&BaseChannel::SetOption_n, this, type, opt, value));
493}
494
495int BaseChannel::SetOption_n(SocketType type,
496 rtc::Socket::Option opt,
497 int value) {
498 RTC_DCHECK(network_thread_->IsCurrent());
499 TransportChannel* channel = nullptr;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000501 case ST_RTP:
502 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700503 socket_options_.push_back(
504 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000505 break;
506 case ST_RTCP:
507 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700508 rtcp_socket_options_.push_back(
509 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000510 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000512 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513}
514
515void BaseChannel::OnWritableState(TransportChannel* channel) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200516 RTC_DCHECK(channel == transport_channel_ ||
517 channel == rtcp_transport_channel_);
518 RTC_DCHECK(network_thread_->IsCurrent());
519 UpdateWritableState_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520}
521
522void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000523 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000524 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000525 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100526 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200528 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529
530 // When using RTCP multiplexing we might get RTCP packets on the RTP
531 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
532 bool rtcp = PacketIsRtcp(channel, data, len);
jbaucheec21bd2016-03-20 06:15:43 -0700533 rtc::CopyOnWriteBuffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000534 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535}
536
537void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700538 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
539 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000540}
541
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800542void BaseChannel::OnDtlsState(TransportChannel* channel,
543 DtlsTransportState state) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200544 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800545 return;
546 }
547
548 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
549 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
550 // cover other scenarios like the whole channel is writable (not just this
551 // TransportChannel) or when TransportChannel is attached after DTLS is
552 // negotiated.
553 if (state != DTLS_TRANSPORT_CONNECTED) {
554 srtp_filter_.ResetParams();
555 }
556}
557
Honghai Zhangcc411c02016-03-29 17:27:21 -0700558void BaseChannel::OnSelectedCandidatePairChanged(
559 TransportChannel* channel,
Honghai Zhang52dce732016-03-31 12:37:31 -0700560 CandidatePairInterface* selected_candidate_pair,
561 int last_sent_packet_id) {
Honghai Zhangcc411c02016-03-29 17:27:21 -0700562 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200563 RTC_DCHECK(network_thread_->IsCurrent());
564 std::string transport_name = channel->transport_name();
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700565 rtc::NetworkRoute network_route;
Honghai Zhangcc411c02016-03-29 17:27:21 -0700566 if (selected_candidate_pair) {
Honghai Zhang0e533ef2016-04-19 15:41:36 -0700567 network_route = rtc::NetworkRoute(
568 selected_candidate_pair->local_candidate().network_id(),
569 selected_candidate_pair->remote_candidate().network_id(),
570 last_sent_packet_id);
Honghai Zhangcc411c02016-03-29 17:27:21 -0700571 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200572 invoker_.AsyncInvoke<void>(
573 worker_thread_, Bind(&MediaChannel::OnNetworkRouteChanged, media_channel_,
574 transport_name, network_route));
Honghai Zhangcc411c02016-03-29 17:27:21 -0700575}
576
deadbeefcbecd352015-09-23 11:50:27 -0700577void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200578 RTC_DCHECK(network_thread_->IsCurrent());
deadbeefcbecd352015-09-23 11:50:27 -0700579 if (rtcp) {
580 rtcp_ready_to_send_ = ready;
581 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 rtp_ready_to_send_ = ready;
583 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200585 bool ready_to_send =
586 (rtp_ready_to_send_ &&
587 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
588 (rtcp_ready_to_send_ || !rtcp_transport_channel_));
589
590 invoker_.AsyncInvoke<void>(
591 worker_thread_,
592 Bind(&MediaChannel::OnReadyToSend, media_channel_, ready_to_send));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000593}
594
595bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
596 const char* data, size_t len) {
597 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000598 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599}
600
stefanc1aeaf02015-10-15 07:26:07 -0700601bool BaseChannel::SendPacket(bool rtcp,
jbaucheec21bd2016-03-20 06:15:43 -0700602 rtc::CopyOnWriteBuffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700603 const rtc::PacketOptions& options) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200604 // SendPacket gets called from MediaEngine, on a pacer or an encoder thread.
605 // If the thread is not our network thread, we will post to our network
606 // so that the real work happens on our network. This avoids us having to
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 // synchronize access to all the pieces of the send path, including
608 // SRTP and the inner workings of the transport channels.
609 // The only downside is that we can't return a proper failure code if
610 // needed. Since UDP is unreliable anyway, this should be a non-issue.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200611 if (!network_thread_->IsCurrent()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 // Avoid a copy by transferring the ownership of the packet data.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200613 int message_id = rtcp ? MSG_SEND_RTCP_PACKET : MSG_SEND_RTP_PACKET;
614 SendPacketMessageData* data = new SendPacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800615 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700616 data->options = options;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200617 network_thread_->Post(this, message_id, data);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 return true;
619 }
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200620 TRACE_EVENT0("webrtc", "BaseChannel::SendPacket");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621
622 // Now that we are on the correct thread, ensure we have a place to send this
623 // packet before doing anything. (We might get RTCP packets that we don't
624 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
625 // transport.
626 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
627 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000628 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 return false;
630 }
631
632 // Protect ourselves against crazy data.
633 if (!ValidPacket(rtcp, packet)) {
634 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000635 << PacketType(rtcp)
636 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 return false;
638 }
639
stefanc1aeaf02015-10-15 07:26:07 -0700640 rtc::PacketOptions updated_options;
641 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 // Protect if needed.
643 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200644 TRACE_EVENT0("webrtc", "SRTP Encode");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200646 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000647 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000649 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
650 // inside libsrtp for a RTP packet. A external HMAC module will be writing
651 // a fake HMAC value. This is ONLY done for a RTP packet.
652 // Socket layer will update rtp sendtime extension header if present in
653 // packet with current time before updating the HMAC.
654#if !defined(ENABLE_EXTERNAL_AUTH)
655 res = srtp_filter_.ProtectRtp(
656 data, len, static_cast<int>(packet->capacity()), &len);
657#else
stefanc1aeaf02015-10-15 07:26:07 -0700658 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000659 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000660 res = srtp_filter_.ProtectRtp(
661 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700662 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000663 // If protection succeeds, let's get auth params from srtp.
664 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200665 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000666 int key_len;
667 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700668 &auth_key, &key_len,
669 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000670 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700671 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
672 updated_options.packet_time_params.srtp_auth_key.assign(
673 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000674 }
675 }
676#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 if (!res) {
678 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200679 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 GetRtpSeqNum(data, len, &seq_num);
681 GetRtpSsrc(data, len, &ssrc);
682 LOG(LS_ERROR) << "Failed to protect " << content_name_
683 << " RTP packet: size=" << len
684 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
685 return false;
686 }
687 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000688 res = srtp_filter_.ProtectRtcp(data, len,
689 static_cast<int>(packet->capacity()),
690 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 if (!res) {
692 int type = -1;
693 GetRtcpType(data, len, &type);
694 LOG(LS_ERROR) << "Failed to protect " << content_name_
695 << " RTCP packet: size=" << len << ", type=" << type;
696 return false;
697 }
698 }
699
700 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000701 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 } else if (secure_required_) {
703 // This is a double check for something that supposedly can't happen.
704 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
705 << " packet when SRTP is inactive and crypto is required";
706
707 ASSERT(false);
708 return false;
709 }
710
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 // Bon voyage.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200712 int flags = (secure() && secure_dtls()) ? PF_SRTP_BYPASS : PF_NORMAL;
713 int ret = channel->SendPacket(packet->data<char>(), packet->size(),
714 updated_options, flags);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000715 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000716 if (channel->GetError() == EWOULDBLOCK) {
717 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700718 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 }
720 return false;
721 }
722 return true;
723}
724
jbaucheec21bd2016-03-20 06:15:43 -0700725bool BaseChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 // Protect ourselves against crazy data.
727 if (!ValidPacket(rtcp, packet)) {
728 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000729 << PacketType(rtcp)
730 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 return false;
732 }
pbos482b12e2015-11-16 10:19:58 -0800733 if (rtcp) {
734 // Permit all (seemingly valid) RTCP packets.
735 return true;
736 }
737 // Check whether we handle this payload.
jbaucheec21bd2016-03-20 06:15:43 -0700738 return bundle_filter_.DemuxPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739}
740
jbaucheec21bd2016-03-20 06:15:43 -0700741void BaseChannel::HandlePacket(bool rtcp, rtc::CopyOnWriteBuffer* packet,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000742 const rtc::PacketTime& packet_time) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200743 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 if (!WantsPacket(rtcp, packet)) {
745 return;
746 }
747
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000748 // We are only interested in the first rtp packet because that
749 // indicates the media has started flowing.
750 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 has_received_packet_ = true;
752 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
753 }
754
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 // Unprotect the packet, if needed.
756 if (srtp_filter_.IsActive()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200757 TRACE_EVENT0("webrtc", "SRTP Decode");
Karl Wiberg94784372015-04-20 14:03:07 +0200758 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000759 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 bool res;
761 if (!rtcp) {
762 res = srtp_filter_.UnprotectRtp(data, len, &len);
763 if (!res) {
764 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200765 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 GetRtpSeqNum(data, len, &seq_num);
767 GetRtpSsrc(data, len, &ssrc);
768 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
769 << " RTP packet: size=" << len
770 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
771 return;
772 }
773 } else {
774 res = srtp_filter_.UnprotectRtcp(data, len, &len);
775 if (!res) {
776 int type = -1;
777 GetRtcpType(data, len, &type);
778 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
779 << " RTCP packet: size=" << len << ", type=" << type;
780 return;
781 }
782 }
783
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000784 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 } else if (secure_required_) {
786 // Our session description indicates that SRTP is required, but we got a
787 // packet before our SRTP filter is active. This means either that
788 // a) we got SRTP packets before we received the SDES keys, in which case
789 // we can't decrypt it anyway, or
790 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
791 // channels, so we haven't yet extracted keys, even if DTLS did complete
792 // on the channel that the packets are being sent on. It's really good
793 // practice to wait for both RTP and RTCP to be good to go before sending
794 // media, to prevent weird failure modes, so it's fine for us to just eat
795 // packets here. This is all sidestepped if RTCP mux is used anyway.
796 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
797 << " packet when SRTP is inactive and crypto is required";
798 return;
799 }
800
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200801 invoker_.AsyncInvoke<void>(
802 worker_thread_,
803 Bind(&BaseChannel::OnPacketReceived, this, rtcp, *packet, packet_time));
804}
805
806void BaseChannel::OnPacketReceived(bool rtcp,
807 const rtc::CopyOnWriteBuffer& packet,
808 const rtc::PacketTime& packet_time) {
809 RTC_DCHECK(worker_thread_->IsCurrent());
810 // Need to copy variable because OnRtcpReceived/OnPacketReceived
811 // requires non-const pointer to buffer. This doesn't memcpy the actual data.
812 rtc::CopyOnWriteBuffer data(packet);
813 if (rtcp) {
814 media_channel_->OnRtcpReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200816 media_channel_->OnPacketReceived(&data, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 }
818}
819
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000820bool BaseChannel::PushdownLocalDescription(
821 const SessionDescription* local_desc, ContentAction action,
822 std::string* error_desc) {
823 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 const MediaContentDescription* content_desc =
825 GetContentDescription(content_info);
826 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000827 !SetLocalContent(content_desc, action, error_desc)) {
828 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
829 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000831 return true;
832}
833
834bool BaseChannel::PushdownRemoteDescription(
835 const SessionDescription* remote_desc, ContentAction action,
836 std::string* error_desc) {
837 const ContentInfo* content_info = GetFirstContent(remote_desc);
838 const MediaContentDescription* content_desc =
839 GetContentDescription(content_info);
840 if (content_desc && content_info && !content_info->rejected &&
841 !SetRemoteContent(content_desc, action, error_desc)) {
842 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
843 return false;
844 }
845 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846}
847
848void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000849 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 if (enabled_)
851 return;
852
853 LOG(LS_INFO) << "Channel enabled";
854 enabled_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200855 ChangeState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856}
857
858void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000859 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 if (!enabled_)
861 return;
862
863 LOG(LS_INFO) << "Channel disabled";
864 enabled_ = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200865 ChangeState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866}
867
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200868void BaseChannel::UpdateWritableState_n() {
deadbeefcbecd352015-09-23 11:50:27 -0700869 if (transport_channel_ && transport_channel_->writable() &&
870 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200871 ChannelWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700872 } else {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200873 ChannelNotWritable_n();
deadbeefcbecd352015-09-23 11:50:27 -0700874 }
875}
876
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200877void BaseChannel::ChannelWritable_n() {
878 RTC_DCHECK(network_thread_->IsCurrent());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800879 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800881 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882
deadbeefcbecd352015-09-23 11:50:27 -0700883 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 << (was_ever_writable_ ? "" : " for the first time");
885
886 std::vector<ConnectionInfo> infos;
887 transport_channel_->GetStats(&infos);
888 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
889 it != infos.end(); ++it) {
890 if (it->best_connection) {
891 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
892 << "->" << it->remote_candidate.ToSensitiveString();
893 break;
894 }
895 }
896
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897 was_ever_writable_ = true;
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200898 MaybeSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000899 writable_ = true;
900 ChangeState();
901}
902
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200903void BaseChannel::SignalDtlsSetupFailure_n(bool rtcp) {
904 RTC_DCHECK(network_thread_->IsCurrent());
905 invoker_.AsyncInvoke<void>(
906 signaling_thread(),
907 Bind(&BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000908}
909
910void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
911 ASSERT(signaling_thread() == rtc::Thread::Current());
912 SignalDtlsSetupFailure(this, rtcp);
913}
914
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200915bool BaseChannel::SetDtlsSrtpCryptoSuites_n(TransportChannel* tc, bool rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800916 std::vector<int> crypto_suites;
917 // We always use the default SRTP crypto suites for RTCP, but we may use
918 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 if (!rtcp) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200920 GetSrtpCryptoSuites_n(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800922 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800924 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925}
926
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200927bool BaseChannel::ShouldSetupDtlsSrtp_n() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800928 // Since DTLS is applied to all channels, checking RTP should be enough.
929 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930}
931
932// This function returns true if either DTLS-SRTP is not in use
933// *or* DTLS-SRTP is successfully set up.
Danil Chapovalov33b01f22016-05-11 19:55:27 +0200934bool BaseChannel::SetupDtlsSrtp_n(bool rtcp_channel) {
935 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 bool ret = false;
937
deadbeefcbecd352015-09-23 11:50:27 -0700938 TransportChannel* channel =
939 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800941 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800943 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800945 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
946 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 return false;
948 }
949
950 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
951 << content_name() << " "
952 << PacketType(rtcp_channel);
953
954 // OK, we're now doing DTLS (RFC 5764)
955 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
956 SRTP_MASTER_KEY_SALT_LEN * 2);
957
958 // RFC 5705 exporter using the RFC 5764 parameters
959 if (!channel->ExportKeyingMaterial(
960 kDtlsSrtpExporterLabel,
961 NULL, 0, false,
962 &dtls_buffer[0], dtls_buffer.size())) {
963 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
964 ASSERT(false); // This should never happen
965 return false;
966 }
967
968 // Sync up the keys with the DTLS-SRTP interface
969 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
970 SRTP_MASTER_KEY_SALT_LEN);
971 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
972 SRTP_MASTER_KEY_SALT_LEN);
973 size_t offset = 0;
974 memcpy(&client_write_key[0], &dtls_buffer[offset],
975 SRTP_MASTER_KEY_KEY_LEN);
976 offset += SRTP_MASTER_KEY_KEY_LEN;
977 memcpy(&server_write_key[0], &dtls_buffer[offset],
978 SRTP_MASTER_KEY_KEY_LEN);
979 offset += SRTP_MASTER_KEY_KEY_LEN;
980 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
981 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
982 offset += SRTP_MASTER_KEY_SALT_LEN;
983 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
984 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
985
986 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000987 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000988 if (!channel->GetSslRole(&role)) {
989 LOG(LS_WARNING) << "GetSslRole failed";
990 return false;
991 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000993 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 send_key = &server_write_key;
995 recv_key = &client_write_key;
996 } else {
997 send_key = &client_write_key;
998 recv_key = &server_write_key;
999 }
1000
1001 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001002 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
1003 static_cast<int>(send_key->size()),
1004 selected_crypto_suite, &(*recv_key)[0],
1005 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001007 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
1008 static_cast<int>(send_key->size()),
1009 selected_crypto_suite, &(*recv_key)[0],
1010 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 }
1012
1013 if (!ret)
1014 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
1015 else
1016 dtls_keyed_ = true;
1017
1018 return ret;
1019}
1020
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001021void BaseChannel::MaybeSetupDtlsSrtp_n() {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001022 if (srtp_filter_.IsActive()) {
1023 return;
1024 }
1025
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001026 if (!ShouldSetupDtlsSrtp_n()) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001027 return;
1028 }
1029
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001030 if (!SetupDtlsSrtp_n(false)) {
1031 SignalDtlsSetupFailure_n(false);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001032 return;
1033 }
1034
1035 if (rtcp_transport_channel_) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001036 if (!SetupDtlsSrtp_n(true)) {
1037 SignalDtlsSetupFailure_n(true);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08001038 return;
1039 }
1040 }
1041}
1042
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001043void BaseChannel::ChannelNotWritable_n() {
1044 RTC_DCHECK(network_thread_->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 if (!writable_)
1046 return;
1047
deadbeefcbecd352015-09-23 11:50:27 -07001048 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 writable_ = false;
1050 ChangeState();
1051}
1052
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001053bool BaseChannel::SetRtpTransportParameters(
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001054 const MediaContentDescription* content,
1055 ContentAction action,
1056 ContentSource src,
1057 std::string* error_desc) {
1058 if (action == CA_UPDATE) {
1059 // These parameters never get changed by a CA_UDPATE.
1060 return true;
1061 }
1062
1063 // Cache secure_required_ for belt and suspenders check on SendPacket
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001064 return network_thread_->Invoke<bool>(
1065 Bind(&BaseChannel::SetRtpTransportParameters_n, this, content, action,
1066 src, error_desc));
1067}
1068
1069bool BaseChannel::SetRtpTransportParameters_n(
1070 const MediaContentDescription* content,
1071 ContentAction action,
1072 ContentSource src,
1073 std::string* error_desc) {
1074 RTC_DCHECK(network_thread_->IsCurrent());
1075
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001076 if (src == CS_LOCAL) {
1077 set_secure_required(content->crypto_required() != CT_NONE);
1078 }
1079
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001080 if (!SetSrtp_n(content->cryptos(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001081 return false;
1082 }
1083
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001084 if (!SetRtcpMux_n(content->rtcp_mux(), action, src, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001085 return false;
1086 }
1087
1088 return true;
1089}
1090
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001091// |dtls| will be set to true if DTLS is active for transport channel and
1092// crypto is empty.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001093bool BaseChannel::CheckSrtpConfig_n(const std::vector<CryptoParams>& cryptos,
1094 bool* dtls,
1095 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001096 *dtls = transport_channel_->IsDtlsActive();
1097 if (*dtls && !cryptos.empty()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001098 SafeSetError("Cryptos must be empty when DTLS is active.", error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001099 return false;
1100 }
1101 return true;
1102}
1103
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001104bool BaseChannel::SetSrtp_n(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001105 ContentAction action,
1106 ContentSource src,
1107 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001108 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001109 if (action == CA_UPDATE) {
1110 // no crypto params.
1111 return true;
1112 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001114 bool dtls = false;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001115 ret = CheckSrtpConfig_n(cryptos, &dtls, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001116 if (!ret) {
1117 return false;
1118 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 switch (action) {
1120 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001121 // If DTLS is already active on the channel, we could be renegotiating
1122 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001123 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001124 ret = srtp_filter_.SetOffer(cryptos, src);
1125 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001126 break;
1127 case CA_PRANSWER:
1128 // If we're doing DTLS-SRTP, we don't want to update the filter
1129 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001130 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1132 }
1133 break;
1134 case CA_ANSWER:
1135 // If we're doing DTLS-SRTP, we don't want to update the filter
1136 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001137 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 ret = srtp_filter_.SetAnswer(cryptos, src);
1139 }
1140 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 default:
1142 break;
1143 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001144 if (!ret) {
1145 SafeSetError("Failed to setup SRTP filter.", error_desc);
1146 return false;
1147 }
1148 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149}
1150
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001151void BaseChannel::ActivateRtcpMux() {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001152 network_thread_->Invoke<void>(Bind(&BaseChannel::ActivateRtcpMux_n, this));
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001153}
1154
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001155void BaseChannel::ActivateRtcpMux_n() {
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001156 if (!rtcp_mux_filter_.IsActive()) {
1157 rtcp_mux_filter_.SetActive();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001158 SetRtcpTransportChannel_n(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001159 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001160 }
1161}
1162
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001163bool BaseChannel::SetRtcpMux_n(bool enable,
1164 ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001165 ContentSource src,
1166 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 bool ret = false;
1168 switch (action) {
1169 case CA_OFFER:
1170 ret = rtcp_mux_filter_.SetOffer(enable, src);
1171 break;
1172 case CA_PRANSWER:
1173 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1174 break;
1175 case CA_ANSWER:
1176 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1177 if (ret && rtcp_mux_filter_.IsActive()) {
1178 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001179 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1180 << " by destroying RTCP transport channel for "
1181 << transport_name();
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001182 SetRtcpTransportChannel_n(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001183 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 }
1185 break;
1186 case CA_UPDATE:
1187 // No RTCP mux info.
1188 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001189 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 default:
1191 break;
1192 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001193 if (!ret) {
1194 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1195 return false;
1196 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1198 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1199 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001200 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 // If the RTP transport is already writable, then so are we.
1202 if (transport_channel_->writable()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001203 ChannelWritable_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 }
1205 }
1206
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001207 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208}
1209
1210bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001211 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001212 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213}
1214
Peter Boström0c4e06b2015-10-07 12:23:21 +02001215bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001216 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 return media_channel()->RemoveRecvStream(ssrc);
1218}
1219
1220bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001221 ContentAction action,
1222 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1224 action == CA_PRANSWER || action == CA_UPDATE))
1225 return false;
1226
1227 // If this is an update, streams only contain streams that have changed.
1228 if (action == CA_UPDATE) {
1229 for (StreamParamsVec::const_iterator it = streams.begin();
1230 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001231 const StreamParams* existing_stream =
1232 GetStreamByIds(local_streams_, it->groupid, it->id);
1233 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234 if (media_channel()->AddSendStream(*it)) {
1235 local_streams_.push_back(*it);
1236 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1237 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001238 std::ostringstream desc;
1239 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1240 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 return false;
1242 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001243 } else if (existing_stream && !it->has_ssrcs()) {
1244 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001245 std::ostringstream desc;
1246 desc << "Failed to remove send stream with ssrc "
1247 << it->first_ssrc() << ".";
1248 SafeSetError(desc.str(), error_desc);
1249 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001251 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 } else {
1253 LOG(LS_WARNING) << "Ignore unsupported stream update";
1254 }
1255 }
1256 return true;
1257 }
1258 // Else streams are all the streams we want to send.
1259
1260 // Check for streams that have been removed.
1261 bool ret = true;
1262 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1263 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001264 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001266 std::ostringstream desc;
1267 desc << "Failed to remove send stream with ssrc "
1268 << it->first_ssrc() << ".";
1269 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 ret = false;
1271 }
1272 }
1273 }
1274 // Check for new streams.
1275 for (StreamParamsVec::const_iterator it = streams.begin();
1276 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001277 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001279 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001281 std::ostringstream desc;
1282 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1283 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 ret = false;
1285 }
1286 }
1287 }
1288 local_streams_ = streams;
1289 return ret;
1290}
1291
1292bool BaseChannel::UpdateRemoteStreams_w(
1293 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001294 ContentAction action,
1295 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1297 action == CA_PRANSWER || action == CA_UPDATE))
1298 return false;
1299
1300 // If this is an update, streams only contain streams that have changed.
1301 if (action == CA_UPDATE) {
1302 for (StreamParamsVec::const_iterator it = streams.begin();
1303 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001304 const StreamParams* existing_stream =
1305 GetStreamByIds(remote_streams_, it->groupid, it->id);
1306 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 if (AddRecvStream_w(*it)) {
1308 remote_streams_.push_back(*it);
1309 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1310 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001311 std::ostringstream desc;
1312 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1313 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 return false;
1315 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001316 } else if (existing_stream && !it->has_ssrcs()) {
1317 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001318 std::ostringstream desc;
1319 desc << "Failed to remove remote stream with ssrc "
1320 << it->first_ssrc() << ".";
1321 SafeSetError(desc.str(), error_desc);
1322 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001324 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 } else {
1326 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001327 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 << " new stream = " << it->ToString();
1329 }
1330 }
1331 return true;
1332 }
1333 // Else streams are all the streams we want to receive.
1334
1335 // Check for streams that have been removed.
1336 bool ret = true;
1337 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1338 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001339 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001341 std::ostringstream desc;
1342 desc << "Failed to remove remote stream with ssrc "
1343 << it->first_ssrc() << ".";
1344 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 ret = false;
1346 }
1347 }
1348 }
1349 // Check for new streams.
1350 for (StreamParamsVec::const_iterator it = streams.begin();
1351 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001352 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 if (AddRecvStream_w(*it)) {
1354 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1355 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001356 std::ostringstream desc;
1357 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1358 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359 ret = false;
1360 }
1361 }
1362 }
1363 remote_streams_ = streams;
1364 return ret;
1365}
1366
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001367void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension_w(
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001368 const std::vector<RtpHeaderExtension>& extensions) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001369// Absolute Send Time extension id is used only with external auth,
1370// so do not bother searching for it and making asyncronious call to set
1371// something that is not used.
1372#if defined(ENABLE_EXTERNAL_AUTH)
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001373 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001374 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001375 int rtp_abs_sendtime_extn_id =
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001376 send_time_extension ? send_time_extension->id : -1;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001377 invoker_.AsyncInvoke<void>(
1378 network_thread_, Bind(&BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n,
1379 this, rtp_abs_sendtime_extn_id));
1380#endif
1381}
1382
1383void BaseChannel::CacheRtpAbsSendTimeHeaderExtension_n(
1384 int rtp_abs_sendtime_extn_id) {
1385 rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001386}
1387
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001388void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001389 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390 switch (pmsg->message_id) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001391 case MSG_SEND_RTP_PACKET:
1392 case MSG_SEND_RTCP_PACKET: {
1393 RTC_DCHECK(network_thread_->IsCurrent());
1394 SendPacketMessageData* data =
1395 static_cast<SendPacketMessageData*>(pmsg->pdata);
1396 bool rtcp = pmsg->message_id == MSG_SEND_RTCP_PACKET;
1397 SendPacket(rtcp, &data->packet, data->options);
1398 delete data;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001399 break;
1400 }
1401 case MSG_FIRSTPACKETRECEIVED: {
1402 SignalFirstPacketReceived(this);
1403 break;
1404 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001405 }
1406}
1407
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001408void BaseChannel::FlushRtcpMessages_n() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409 // Flush all remaining RTCP messages. This should only be called in
1410 // destructor.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001411 RTC_DCHECK(network_thread_->IsCurrent());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001412 rtc::MessageList rtcp_messages;
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001413 network_thread_->Clear(this, MSG_SEND_RTCP_PACKET, &rtcp_messages);
1414 for (const auto& message : rtcp_messages) {
1415 network_thread_->Send(this, MSG_SEND_RTCP_PACKET, message.pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416 }
1417}
1418
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001419void BaseChannel::SignalSentPacket_n(TransportChannel* /* channel */,
1420 const rtc::SentPacket& sent_packet) {
1421 RTC_DCHECK(network_thread_->IsCurrent());
1422 invoker_.AsyncInvoke<void>(
1423 worker_thread_,
1424 rtc::Bind(&BaseChannel::SignalSentPacket_w, this, sent_packet));
1425}
1426
1427void BaseChannel::SignalSentPacket_w(const rtc::SentPacket& sent_packet) {
1428 RTC_DCHECK(worker_thread_->IsCurrent());
1429 SignalSentPacket(sent_packet);
1430}
1431
1432VoiceChannel::VoiceChannel(rtc::Thread* worker_thread,
1433 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001434 MediaEngineInterface* media_engine,
1435 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001436 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 const std::string& content_name,
1438 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001439 : BaseChannel(worker_thread,
1440 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001441 media_channel,
1442 transport_controller,
1443 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001445 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001446 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447
1448VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001449 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001450 StopAudioMonitor();
1451 StopMediaMonitor();
1452 // this can't be done in the base class, since it calls a virtual
1453 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001454 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455}
1456
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001457bool VoiceChannel::Init_w() {
1458 if (!BaseChannel::Init_w()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459 return false;
1460 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 return true;
1462}
1463
Peter Boström0c4e06b2015-10-07 12:23:21 +02001464bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001465 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001466 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001467 AudioSource* source) {
deadbeefcbecd352015-09-23 11:50:27 -07001468 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001469 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470}
1471
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472// TODO(juberti): Handle early media the right way. We should get an explicit
1473// ringing message telling us to start playing local ringback, which we cancel
1474// if any early media actually arrives. For now, we do the opposite, which is
1475// to wait 1 second for early media, and start playing local ringback if none
1476// arrives.
1477void VoiceChannel::SetEarlyMedia(bool enable) {
1478 if (enable) {
1479 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001480 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1481 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482 } else {
1483 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001484 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 }
1486}
1487
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001489 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1490 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491}
1492
Peter Boström0c4e06b2015-10-07 12:23:21 +02001493bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1494 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001495 int duration) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001496 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
solenberg1d63dd02015-12-02 12:35:09 -08001497 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498}
1499
solenberg4bac9c52015-10-09 02:32:53 -07001500bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1501 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1502 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001504
Tommif888bb52015-12-12 01:37:01 +01001505void VoiceChannel::SetRawAudioSink(
1506 uint32_t ssrc,
kwiberg31022942016-03-11 14:18:21 -08001507 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
1508 // We need to work around Bind's lack of support for unique_ptr and ownership
deadbeef2d110be2016-01-13 12:00:26 -08001509 // passing. So we invoke to our own little routine that gets a pointer to
1510 // our local variable. This is OK since we're synchronously invoking.
1511 InvokeOnWorker(Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001512}
1513
skvladdc1c62c2016-03-16 19:07:43 -07001514webrtc::RtpParameters VoiceChannel::GetRtpParameters(uint32_t ssrc) const {
1515 return worker_thread()->Invoke<webrtc::RtpParameters>(
1516 Bind(&VoiceChannel::GetRtpParameters_w, this, ssrc));
1517}
1518
1519webrtc::RtpParameters VoiceChannel::GetRtpParameters_w(uint32_t ssrc) const {
skvlade0d46372016-04-07 22:59:22 -07001520 return media_channel()->GetRtpParameters(ssrc);
skvladdc1c62c2016-03-16 19:07:43 -07001521}
1522
1523bool VoiceChannel::SetRtpParameters(uint32_t ssrc,
1524 const webrtc::RtpParameters& parameters) {
1525 return InvokeOnWorker(
1526 Bind(&VoiceChannel::SetRtpParameters_w, this, ssrc, parameters));
1527}
1528
1529bool VoiceChannel::SetRtpParameters_w(uint32_t ssrc,
1530 webrtc::RtpParameters parameters) {
skvlade0d46372016-04-07 22:59:22 -07001531 return media_channel()->SetRtpParameters(ssrc, parameters);
skvladdc1c62c2016-03-16 19:07:43 -07001532}
1533
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001535 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1536 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537}
1538
1539void VoiceChannel::StartMediaMonitor(int cms) {
1540 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001541 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 media_monitor_->SignalUpdate.connect(
1543 this, &VoiceChannel::OnMediaMonitorUpdate);
1544 media_monitor_->Start(cms);
1545}
1546
1547void VoiceChannel::StopMediaMonitor() {
1548 if (media_monitor_) {
1549 media_monitor_->Stop();
1550 media_monitor_->SignalUpdate.disconnect(this);
1551 media_monitor_.reset();
1552 }
1553}
1554
1555void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001556 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557 audio_monitor_
1558 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1559 audio_monitor_->Start(cms);
1560}
1561
1562void VoiceChannel::StopAudioMonitor() {
1563 if (audio_monitor_) {
1564 audio_monitor_->Stop();
1565 audio_monitor_.reset();
1566 }
1567}
1568
1569bool VoiceChannel::IsAudioMonitorRunning() const {
1570 return (audio_monitor_.get() != NULL);
1571}
1572
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001574 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575}
1576
1577int VoiceChannel::GetOutputLevel_w() {
1578 return media_channel()->GetOutputLevel();
1579}
1580
1581void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1582 media_channel()->GetActiveStreams(actives);
1583}
1584
1585void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001586 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001587 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001588 int flags) {
1589 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590
1591 // Set a flag when we've received an RTP packet. If we're waiting for early
1592 // media, this will disable the timeout.
1593 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1594 received_media_ = true;
1595 }
1596}
1597
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001598void BaseChannel::ChangeState() {
1599 RTC_DCHECK(network_thread_->IsCurrent());
1600 invoker_.AsyncInvoke<void>(worker_thread_,
1601 Bind(&BaseChannel::ChangeState_w, this));
1602}
1603
1604void VoiceChannel::ChangeState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 // Render incoming data if we're the active call, and we have the local
1606 // content. We receive data on the default channel and multiplexed streams.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001607 bool recv = IsReadyToReceive_w();
solenberg5b14b422015-10-01 04:10:31 -07001608 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609
1610 // Send outgoing data if we're the active call, we have the remote content,
1611 // and we have had some form of connectivity.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001612 bool send = IsReadyToSend_w();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001613 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614
1615 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1616}
1617
1618const ContentInfo* VoiceChannel::GetFirstContent(
1619 const SessionDescription* sdesc) {
1620 return GetFirstAudioContent(sdesc);
1621}
1622
1623bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001624 ContentAction action,
1625 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001626 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001627 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 LOG(LS_INFO) << "Setting local voice description";
1629
1630 const AudioContentDescription* audio =
1631 static_cast<const AudioContentDescription*>(content);
1632 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001633 if (!audio) {
1634 SafeSetError("Can't find audio content in local description.", error_desc);
1635 return false;
1636 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001638 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001639 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 }
1641
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001642 AudioRecvParameters recv_params = last_recv_params_;
1643 RtpParametersFromMediaDescription(audio, &recv_params);
1644 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001645 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001646 error_desc);
1647 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001649 for (const AudioCodec& codec : audio->codecs()) {
1650 bundle_filter()->AddPayloadType(codec.id);
1651 }
1652 last_recv_params_ = recv_params;
1653
1654 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1655 // only give it to the media channel once we have a remote
1656 // description too (without a remote description, we won't be able
1657 // to send them anyway).
1658 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1659 SafeSetError("Failed to set local audio description streams.", error_desc);
1660 return false;
1661 }
1662
1663 set_local_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001664 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001665 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666}
1667
1668bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001669 ContentAction action,
1670 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001671 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001672 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 LOG(LS_INFO) << "Setting remote voice description";
1674
1675 const AudioContentDescription* audio =
1676 static_cast<const AudioContentDescription*>(content);
1677 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001678 if (!audio) {
1679 SafeSetError("Can't find audio content in remote description.", error_desc);
1680 return false;
1681 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001683 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001684 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001685 }
1686
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001687 AudioSendParameters send_params = last_send_params_;
1688 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001689 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001690 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001691 }
skvladdc1c62c2016-03-16 19:07:43 -07001692
1693 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1694 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001695 SafeSetError("Failed to set remote audio description send parameters.",
1696 error_desc);
1697 return false;
1698 }
1699 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001701 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1702 // and only give it to the media channel once we have a local
1703 // description too (without a local description, we won't be able to
1704 // recv them anyway).
1705 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1706 SafeSetError("Failed to set remote audio description streams.", error_desc);
1707 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 }
1709
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001710 if (audio->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001711 MaybeCacheRtpAbsSendTimeHeaderExtension_w(audio->rtp_header_extensions());
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001712 }
1713
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001714 set_remote_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001715 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001716 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717}
1718
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719void VoiceChannel::HandleEarlyMediaTimeout() {
1720 // This occurs on the main thread, not the worker thread.
1721 if (!received_media_) {
1722 LOG(LS_INFO) << "No early media received before timeout";
1723 SignalEarlyMediaTimeout(this);
1724 }
1725}
1726
Peter Boström0c4e06b2015-10-07 12:23:21 +02001727bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1728 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001729 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 if (!enabled()) {
1731 return false;
1732 }
solenberg1d63dd02015-12-02 12:35:09 -08001733 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734}
1735
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001736void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 case MSG_EARLYMEDIATIMEOUT:
1739 HandleEarlyMediaTimeout();
1740 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 case MSG_CHANNEL_ERROR: {
1742 VoiceChannelErrorMessageData* data =
1743 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 delete data;
1745 break;
1746 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 default:
1748 BaseChannel::OnMessage(pmsg);
1749 break;
1750 }
1751}
1752
1753void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001754 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 SignalConnectionMonitor(this, infos);
1756}
1757
1758void VoiceChannel::OnMediaMonitorUpdate(
1759 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1760 ASSERT(media_channel == this->media_channel());
1761 SignalMediaMonitor(this, info);
1762}
1763
1764void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1765 const AudioInfo& info) {
1766 SignalAudioMonitor(this, info);
1767}
1768
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001769void VoiceChannel::GetSrtpCryptoSuites_n(
1770 std::vector<int>* crypto_suites) const {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001771 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772}
1773
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001774VideoChannel::VideoChannel(rtc::Thread* worker_thread,
1775 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001777 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001779 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001780 : BaseChannel(worker_thread,
1781 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07001782 media_channel,
1783 transport_controller,
1784 content_name,
perkjc11b1842016-03-07 17:34:13 -08001785 rtcp) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001787bool VideoChannel::Init_w() {
1788 if (!BaseChannel::Init_w()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 return false;
1790 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 return true;
1792}
1793
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001795 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 StopMediaMonitor();
1797 // this can't be done in the base class, since it calls a virtual
1798 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001799
1800 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801}
1802
nisse08582ff2016-02-04 01:24:52 -08001803bool VideoChannel::SetSink(uint32_t ssrc,
1804 rtc::VideoSinkInterface<VideoFrame>* sink) {
1805 worker_thread()->Invoke<void>(
1806 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 return true;
1808}
1809
nisse2ded9b12016-04-08 02:23:55 -07001810void VideoChannel::SetSource(
1811 uint32_t ssrc,
1812 rtc::VideoSourceInterface<cricket::VideoFrame>* source) {
1813 worker_thread()->Invoke<void>(
1814 Bind(&VideoMediaChannel::SetSource, media_channel(), ssrc, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815}
1816
Peter Boström0c4e06b2015-10-07 12:23:21 +02001817bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001818 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001819 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001820 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1821 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001822}
1823
skvladdc1c62c2016-03-16 19:07:43 -07001824webrtc::RtpParameters VideoChannel::GetRtpParameters(uint32_t ssrc) const {
1825 return worker_thread()->Invoke<webrtc::RtpParameters>(
1826 Bind(&VideoChannel::GetRtpParameters_w, this, ssrc));
1827}
1828
1829webrtc::RtpParameters VideoChannel::GetRtpParameters_w(uint32_t ssrc) const {
1830 return media_channel()->GetRtpParameters(ssrc);
1831}
1832
1833bool VideoChannel::SetRtpParameters(uint32_t ssrc,
1834 const webrtc::RtpParameters& parameters) {
1835 return InvokeOnWorker(
1836 Bind(&VideoChannel::SetRtpParameters_w, this, ssrc, parameters));
1837}
1838
1839bool VideoChannel::SetRtpParameters_w(uint32_t ssrc,
1840 webrtc::RtpParameters parameters) {
1841 return media_channel()->SetRtpParameters(ssrc, parameters);
1842}
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001843
1844void VideoChannel::ChangeState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 // Send outgoing data if we're the active call, we have the remote content,
1846 // and we have had some form of connectivity.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001847 bool send = IsReadyToSend_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848 if (!media_channel()->SetSend(send)) {
1849 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1850 // TODO(gangji): Report error back to server.
1851 }
1852
Peter Boström34fbfff2015-09-24 19:20:30 +02001853 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854}
1855
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001856bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1857 return InvokeOnWorker(
1858 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859}
1860
1861void VideoChannel::StartMediaMonitor(int cms) {
1862 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001863 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 media_monitor_->SignalUpdate.connect(
1865 this, &VideoChannel::OnMediaMonitorUpdate);
1866 media_monitor_->Start(cms);
1867}
1868
1869void VideoChannel::StopMediaMonitor() {
1870 if (media_monitor_) {
1871 media_monitor_->Stop();
1872 media_monitor_.reset();
1873 }
1874}
1875
1876const ContentInfo* VideoChannel::GetFirstContent(
1877 const SessionDescription* sdesc) {
1878 return GetFirstVideoContent(sdesc);
1879}
1880
1881bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001882 ContentAction action,
1883 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001884 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001885 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 LOG(LS_INFO) << "Setting local video description";
1887
1888 const VideoContentDescription* video =
1889 static_cast<const VideoContentDescription*>(content);
1890 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001891 if (!video) {
1892 SafeSetError("Can't find video content in local description.", error_desc);
1893 return false;
1894 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001896 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001897 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898 }
1899
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001900 VideoRecvParameters recv_params = last_recv_params_;
1901 RtpParametersFromMediaDescription(video, &recv_params);
1902 if (!media_channel()->SetRecvParameters(recv_params)) {
1903 SafeSetError("Failed to set local video description recv parameters.",
1904 error_desc);
1905 return false;
1906 }
1907 for (const VideoCodec& codec : video->codecs()) {
1908 bundle_filter()->AddPayloadType(codec.id);
1909 }
1910 last_recv_params_ = recv_params;
1911
1912 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1913 // only give it to the media channel once we have a remote
1914 // description too (without a remote description, we won't be able
1915 // to send them anyway).
1916 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1917 SafeSetError("Failed to set local video description streams.", error_desc);
1918 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001919 }
1920
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001921 set_local_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001922 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001923 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924}
1925
1926bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001927 ContentAction action,
1928 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001929 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001930 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 LOG(LS_INFO) << "Setting remote video description";
1932
1933 const VideoContentDescription* video =
1934 static_cast<const VideoContentDescription*>(content);
1935 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001936 if (!video) {
1937 SafeSetError("Can't find video content in remote description.", error_desc);
1938 return false;
1939 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001941 if (!SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001942 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 }
1944
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001945 VideoSendParameters send_params = last_send_params_;
1946 RtpSendParametersFromMediaDescription(video, &send_params);
1947 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08001948 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001949 }
skvladdc1c62c2016-03-16 19:07:43 -07001950
1951 bool parameters_applied = media_channel()->SetSendParameters(send_params);
1952
1953 if (!parameters_applied) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001954 SafeSetError("Failed to set remote video description send parameters.",
1955 error_desc);
1956 return false;
1957 }
1958 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001960 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1961 // and only give it to the media channel once we have a local
1962 // description too (without a local description, we won't be able to
1963 // recv them anyway).
1964 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1965 SafeSetError("Failed to set remote video description streams.", error_desc);
1966 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 }
1968
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001969 if (video->rtp_header_extensions_set()) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001970 MaybeCacheRtpAbsSendTimeHeaderExtension_w(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001972
1973 set_remote_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02001974 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001975 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976}
1977
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001978void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 case MSG_CHANNEL_ERROR: {
1981 const VideoChannelErrorMessageData* data =
1982 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 delete data;
1984 break;
1985 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 default:
1987 BaseChannel::OnMessage(pmsg);
1988 break;
1989 }
1990}
1991
1992void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001993 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994 SignalConnectionMonitor(this, infos);
1995}
1996
1997// TODO(pthatcher): Look into removing duplicate code between
1998// audio, video, and data, perhaps by using templates.
1999void VideoChannel::OnMediaMonitorUpdate(
2000 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2001 ASSERT(media_channel == this->media_channel());
2002 SignalMediaMonitor(this, info);
2003}
2004
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002005void VideoChannel::GetSrtpCryptoSuites_n(
2006 std::vector<int>* crypto_suites) const {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002007 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008}
2009
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002010DataChannel::DataChannel(rtc::Thread* worker_thread,
2011 rtc::Thread* network_thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002013 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 const std::string& content_name,
2015 bool rtcp)
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002016 : BaseChannel(worker_thread,
2017 network_thread,
deadbeefcbecd352015-09-23 11:50:27 -07002018 media_channel,
2019 transport_controller,
2020 content_name,
2021 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002022 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002023 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024
2025DataChannel::~DataChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08002026 TRACE_EVENT0("webrtc", "DataChannel::~DataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027 StopMediaMonitor();
2028 // this can't be done in the base class, since it calls a virtual
2029 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002030
2031 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032}
2033
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002034bool DataChannel::Init_w() {
2035 if (!BaseChannel::Init_w()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036 return false;
2037 }
2038 media_channel()->SignalDataReceived.connect(
2039 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002040 media_channel()->SignalReadyToSend.connect(
2041 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002042 media_channel()->SignalStreamClosedRemotely.connect(
2043 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044 return true;
2045}
2046
2047bool DataChannel::SendData(const SendDataParams& params,
jbaucheec21bd2016-03-20 06:15:43 -07002048 const rtc::CopyOnWriteBuffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002050 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2051 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052}
2053
2054const ContentInfo* DataChannel::GetFirstContent(
2055 const SessionDescription* sdesc) {
2056 return GetFirstDataContent(sdesc);
2057}
2058
jbaucheec21bd2016-03-20 06:15:43 -07002059bool DataChannel::WantsPacket(bool rtcp, const rtc::CopyOnWriteBuffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060 if (data_channel_type_ == DCT_SCTP) {
2061 // TODO(pthatcher): Do this in a more robust way by checking for
2062 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002063 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 } else if (data_channel_type_ == DCT_RTP) {
2065 return BaseChannel::WantsPacket(rtcp, packet);
2066 }
2067 return false;
2068}
2069
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002070bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2071 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072 // It hasn't been set before, so set it now.
2073 if (data_channel_type_ == DCT_NONE) {
2074 data_channel_type_ = new_data_channel_type;
2075 return true;
2076 }
2077
2078 // It's been set before, but doesn't match. That's bad.
2079 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002080 std::ostringstream desc;
2081 desc << "Data channel type mismatch."
2082 << " Expected " << data_channel_type_
2083 << " Got " << new_data_channel_type;
2084 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002085 return false;
2086 }
2087
2088 // It's hasn't changed. Nothing to do.
2089 return true;
2090}
2091
2092bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002093 const DataContentDescription* content,
2094 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2096 (content->protocol() == kMediaProtocolDtlsSctp));
2097 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002098 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099}
2100
2101bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002102 ContentAction action,
2103 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002104 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002105 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 LOG(LS_INFO) << "Setting local data description";
2107
2108 const DataContentDescription* data =
2109 static_cast<const DataContentDescription*>(content);
2110 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002111 if (!data) {
2112 SafeSetError("Can't find data content in local description.", error_desc);
2113 return false;
2114 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002116 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 return false;
2118 }
2119
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002120 if (data_channel_type_ == DCT_RTP) {
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002121 if (!SetRtpTransportParameters(content, action, CS_LOCAL, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002122 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123 }
2124 }
2125
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002126 // FYI: We send the SCTP port number (not to be confused with the
2127 // underlying UDP port number) as a codec parameter. So even SCTP
2128 // data channels need codecs.
2129 DataRecvParameters recv_params = last_recv_params_;
2130 RtpParametersFromMediaDescription(data, &recv_params);
2131 if (!media_channel()->SetRecvParameters(recv_params)) {
2132 SafeSetError("Failed to set remote data description recv parameters.",
2133 error_desc);
2134 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002136 if (data_channel_type_ == DCT_RTP) {
2137 for (const DataCodec& codec : data->codecs()) {
2138 bundle_filter()->AddPayloadType(codec.id);
2139 }
2140 }
2141 last_recv_params_ = recv_params;
2142
2143 // TODO(pthatcher): Move local streams into DataSendParameters, and
2144 // only give it to the media channel once we have a remote
2145 // description too (without a remote description, we won't be able
2146 // to send them anyway).
2147 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2148 SafeSetError("Failed to set local data description streams.", error_desc);
2149 return false;
2150 }
2151
2152 set_local_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002153 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002154 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002155}
2156
2157bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002158 ContentAction action,
2159 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002160 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002161 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162
2163 const DataContentDescription* data =
2164 static_cast<const DataContentDescription*>(content);
2165 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002166 if (!data) {
2167 SafeSetError("Can't find data content in remote description.", error_desc);
2168 return false;
2169 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002171 // If the remote data doesn't have codecs and isn't an update, it
2172 // must be empty, so ignore it.
2173 if (!data->has_codecs() && action != CA_UPDATE) {
2174 return true;
2175 }
2176
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002177 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178 return false;
2179 }
2180
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002181 LOG(LS_INFO) << "Setting remote data description";
2182 if (data_channel_type_ == DCT_RTP &&
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002183 !SetRtpTransportParameters(content, action, CS_REMOTE, error_desc)) {
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002184 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185 }
2186
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002187
2188 DataSendParameters send_params = last_send_params_;
2189 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2190 if (!media_channel()->SetSendParameters(send_params)) {
2191 SafeSetError("Failed to set remote data description send parameters.",
2192 error_desc);
2193 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002195 last_send_params_ = send_params;
2196
2197 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2198 // and only give it to the media channel once we have a local
2199 // description too (without a local description, we won't be able to
2200 // recv them anyway).
2201 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2202 SafeSetError("Failed to set remote data description streams.",
2203 error_desc);
2204 return false;
2205 }
2206
2207 set_remote_content_direction(content->direction());
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002208 ChangeState_w();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002209 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210}
2211
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002212void DataChannel::ChangeState_w() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 // Render incoming data if we're the active call, and we have the local
2214 // content. We receive data on the default channel and multiplexed streams.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002215 bool recv = IsReadyToReceive_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216 if (!media_channel()->SetReceive(recv)) {
2217 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2218 }
2219
2220 // Send outgoing data if we're the active call, we have the remote content,
2221 // and we have had some form of connectivity.
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002222 bool send = IsReadyToSend_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002223 if (!media_channel()->SetSend(send)) {
2224 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2225 }
2226
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002227 // Trigger SignalReadyToSendData asynchronously.
2228 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002229
2230 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2231}
2232
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002233void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 switch (pmsg->message_id) {
2235 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002236 DataChannelReadyToSendMessageData* data =
2237 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002238 ready_to_send_data_ = data->data();
2239 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240 delete data;
2241 break;
2242 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002243 case MSG_DATARECEIVED: {
2244 DataReceivedMessageData* data =
2245 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2246 SignalDataReceived(this, data->params, data->payload);
2247 delete data;
2248 break;
2249 }
2250 case MSG_CHANNEL_ERROR: {
2251 const DataChannelErrorMessageData* data =
2252 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253 delete data;
2254 break;
2255 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002256 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002257 rtc::TypedMessageData<uint32_t>* data =
2258 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002259 SignalStreamClosedRemotely(data->data());
2260 delete data;
2261 break;
2262 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263 default:
2264 BaseChannel::OnMessage(pmsg);
2265 break;
2266 }
2267}
2268
2269void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002270 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271 SignalConnectionMonitor(this, infos);
2272}
2273
2274void DataChannel::StartMediaMonitor(int cms) {
2275 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002276 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002277 media_monitor_->SignalUpdate.connect(
2278 this, &DataChannel::OnMediaMonitorUpdate);
2279 media_monitor_->Start(cms);
2280}
2281
2282void DataChannel::StopMediaMonitor() {
2283 if (media_monitor_) {
2284 media_monitor_->Stop();
2285 media_monitor_->SignalUpdate.disconnect(this);
2286 media_monitor_.reset();
2287 }
2288}
2289
2290void DataChannel::OnMediaMonitorUpdate(
2291 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2292 ASSERT(media_channel == this->media_channel());
2293 SignalMediaMonitor(this, info);
2294}
2295
2296void DataChannel::OnDataReceived(
2297 const ReceiveDataParams& params, const char* data, size_t len) {
2298 DataReceivedMessageData* msg = new DataReceivedMessageData(
2299 params, data, len);
2300 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2301}
2302
Peter Boström0c4e06b2015-10-07 12:23:21 +02002303void DataChannel::OnDataChannelError(uint32_t ssrc,
2304 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002305 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2306 ssrc, err);
2307 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2308}
2309
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002310void DataChannel::OnDataChannelReadyToSend(bool writable) {
2311 // This is usded for congestion control to indicate that the stream is ready
2312 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2313 // that the transport channel is ready.
2314 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2315 new DataChannelReadyToSendMessageData(writable));
2316}
2317
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002318void DataChannel::GetSrtpCryptoSuites_n(std::vector<int>* crypto_suites) const {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002319 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320}
2321
Danil Chapovalov33b01f22016-05-11 19:55:27 +02002322bool DataChannel::ShouldSetupDtlsSrtp_n() const {
2323 return data_channel_type_ == DCT_RTP && BaseChannel::ShouldSetupDtlsSrtp_n();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324}
2325
Peter Boström0c4e06b2015-10-07 12:23:21 +02002326void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2327 rtc::TypedMessageData<uint32_t>* message =
2328 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002329 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2330}
2331
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332} // namespace cricket