blob: e978d7d4b0e2bdcf8f7c084fc16572dd5fd5b1fb [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"
17#include "webrtc/base/buffer.h"
18#include "webrtc/base/byteorder.h"
19#include "webrtc/base/common.h"
20#include "webrtc/base/dscp.h"
21#include "webrtc/base/logging.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010022#include "webrtc/base/trace_event.h"
kjellanderf4752772016-03-02 05:42:30 -080023#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080024#include "webrtc/media/base/rtputils.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010025#include "webrtc/p2p/base/transportchannel.h"
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010026#include "webrtc/pc/channelmanager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027
28namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000029using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000030
deadbeef2d110be2016-01-13 12:00:26 -080031namespace {
32// See comment below for why we need to use a pointer to a scoped_ptr.
33bool SetRawAudioSink_w(VoiceMediaChannel* channel,
34 uint32_t ssrc,
35 rtc::scoped_ptr<webrtc::AudioSinkInterface>* sink) {
kwiberg686a8ef2016-02-26 03:00:35 -080036 channel->SetRawAudioSink(ssrc, rtc::ScopedToUnique(std::move(*sink)));
deadbeef2d110be2016-01-13 12:00:26 -080037 return true;
38}
39} // namespace
40
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000042 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043 MSG_RTPPACKET,
44 MSG_RTCPPACKET,
45 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000049 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050};
51
52// Value specified in RFC 5764.
53static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
54
55static const int kAgcMinus10db = -10;
56
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000057static void SafeSetError(const std::string& message, std::string* error_desc) {
58 if (error_desc) {
59 *error_desc = message;
60 }
61}
62
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000063struct PacketMessageData : public rtc::MessageData {
64 rtc::Buffer packet;
stefanc1aeaf02015-10-15 07:26:07 -070065 rtc::PacketOptions options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066};
67
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000068struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020069 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020071 : ssrc(in_ssrc), error(in_error) {}
72 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 VoiceMediaChannel::Error error;
74};
75
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020077 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020079 : ssrc(in_ssrc), error(in_error) {}
80 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 VideoMediaChannel::Error error;
82};
83
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 : ssrc(in_ssrc), error(in_error) {}
88 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 DataMediaChannel::Error error;
90};
91
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092static const char* PacketType(bool rtcp) {
93 return (!rtcp) ? "RTP" : "RTCP";
94}
95
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000096static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 // Check the packet size. We could check the header too if needed.
98 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +000099 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
100 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101}
102
103static bool IsReceiveContentDirection(MediaContentDirection direction) {
104 return direction == MD_SENDRECV || direction == MD_RECVONLY;
105}
106
107static bool IsSendContentDirection(MediaContentDirection direction) {
108 return direction == MD_SENDRECV || direction == MD_SENDONLY;
109}
110
111static const MediaContentDescription* GetContentDescription(
112 const ContentInfo* cinfo) {
113 if (cinfo == NULL)
114 return NULL;
115 return static_cast<const MediaContentDescription*>(cinfo->description);
116}
117
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700118template <class Codec>
119void RtpParametersFromMediaDescription(
120 const MediaContentDescriptionImpl<Codec>* desc,
121 RtpParameters<Codec>* params) {
122 // TODO(pthatcher): Remove this once we're sure no one will give us
123 // a description without codecs (currently a CA_UPDATE with just
124 // streams can).
125 if (desc->has_codecs()) {
126 params->codecs = desc->codecs();
127 }
128 // TODO(pthatcher): See if we really need
129 // rtp_header_extensions_set() and remove it if we don't.
130 if (desc->rtp_header_extensions_set()) {
131 params->extensions = desc->rtp_header_extensions();
132 }
deadbeef13871492015-12-09 12:37:51 -0800133 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700134}
135
136template <class Codec, class Options>
137void RtpSendParametersFromMediaDescription(
138 const MediaContentDescriptionImpl<Codec>* desc,
139 RtpSendParameters<Codec, Options>* send_params) {
140 RtpParametersFromMediaDescription(desc, send_params);
141 send_params->max_bandwidth_bps = desc->bandwidth();
142}
143
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000144BaseChannel::BaseChannel(rtc::Thread* thread,
deadbeefcbecd352015-09-23 11:50:27 -0700145 MediaChannel* media_channel,
146 TransportController* transport_controller,
147 const std::string& content_name,
148 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 : worker_thread_(thread),
deadbeefcbecd352015-09-23 11:50:27 -0700150 transport_controller_(transport_controller),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 media_channel_(media_channel),
152 content_name_(content_name),
deadbeefcbecd352015-09-23 11:50:27 -0700153 rtcp_transport_enabled_(rtcp),
154 transport_channel_(nullptr),
155 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 enabled_(false),
157 writable_(false),
158 rtp_ready_to_send_(false),
159 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 was_ever_writable_(false),
161 local_content_direction_(MD_INACTIVE),
162 remote_content_direction_(MD_INACTIVE),
163 has_received_packet_(false),
164 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000165 secure_required_(false),
166 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000167 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 LOG(LS_INFO) << "Created channel for " << content_name;
169}
170
171BaseChannel::~BaseChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -0800172 TRACE_EVENT0("webrtc", "BaseChannel::~BaseChannel");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000173 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000174 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 StopConnectionMonitor();
176 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000177 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 // We must destroy the media channel before the transport channel, otherwise
179 // the media channel may try to send on the dead transport channel. NULLing
180 // is not an effective strategy since the sends will come on another thread.
181 delete media_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700182 // Note that we don't just call set_transport_channel(nullptr) because that
183 // would call a pure virtual method which we can't do from a destructor.
184 if (transport_channel_) {
185 DisconnectFromTransportChannel(transport_channel_);
186 transport_controller_->DestroyTransportChannel_w(
187 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
188 }
189 if (rtcp_transport_channel_) {
190 DisconnectFromTransportChannel(rtcp_transport_channel_);
191 transport_controller_->DestroyTransportChannel_w(
192 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
193 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 LOG(LS_INFO) << "Destroyed channel";
195}
196
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000197bool BaseChannel::Init() {
deadbeefcbecd352015-09-23 11:50:27 -0700198 if (!SetTransport(content_name())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 return false;
200 }
201
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800202 if (!SetDtlsSrtpCryptoSuites(transport_channel(), false)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000203 return false;
204 }
deadbeefcbecd352015-09-23 11:50:27 -0700205 if (rtcp_transport_enabled() &&
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800206 !SetDtlsSrtpCryptoSuites(rtcp_transport_channel(), true)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000207 return false;
208 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209
wu@webrtc.orgde305012013-10-31 15:40:38 +0000210 // Both RTP and RTCP channels are set, we can call SetInterface on
211 // media channel and it can set network options.
212 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 return true;
214}
215
wu@webrtc.org78187522013-10-07 23:32:02 +0000216void BaseChannel::Deinit() {
217 media_channel_->SetInterface(NULL);
218}
219
deadbeefcbecd352015-09-23 11:50:27 -0700220bool BaseChannel::SetTransport(const std::string& transport_name) {
221 return worker_thread_->Invoke<bool>(
222 Bind(&BaseChannel::SetTransport_w, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000223}
224
deadbeefcbecd352015-09-23 11:50:27 -0700225bool BaseChannel::SetTransport_w(const std::string& transport_name) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000226 ASSERT(worker_thread_ == rtc::Thread::Current());
227
deadbeefcbecd352015-09-23 11:50:27 -0700228 if (transport_name == transport_name_) {
229 // Nothing to do if transport name isn't changing
230 return true;
231 }
232
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800233 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
234 // changes and wait until the DTLS handshake is complete to set the newly
235 // negotiated parameters.
236 if (ShouldSetupDtlsSrtp()) {
guoweis46383312015-12-17 16:45:59 -0800237 // Set |writable_| to false such that UpdateWritableState_w can set up
238 // DTLS-SRTP when the writable_ becomes true again.
239 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800240 srtp_filter_.ResetParams();
241 }
242
guoweis46383312015-12-17 16:45:59 -0800243 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
deadbeefcbecd352015-09-23 11:50:27 -0700244 if (rtcp_transport_enabled()) {
245 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
246 << " on " << transport_name << " transport ";
guoweis46383312015-12-17 16:45:59 -0800247 set_rtcp_transport_channel(
248 transport_controller_->CreateTransportChannel_w(
249 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP),
250 false /* update_writablity */);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000251 if (!rtcp_transport_channel()) {
252 return false;
253 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000254 }
255
guoweis46383312015-12-17 16:45:59 -0800256 // We're not updating the writablity during the transition state.
257 set_transport_channel(transport_controller_->CreateTransportChannel_w(
258 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
259 if (!transport_channel()) {
260 return false;
261 }
262
263 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
264 if (rtcp_transport_enabled()) {
265 // We can only update the RTCP ready to send after set_transport_channel has
266 // handled channel writability.
267 SetReadyToSend(
268 true, rtcp_transport_channel() && rtcp_transport_channel()->writable());
269 }
deadbeefcbecd352015-09-23 11:50:27 -0700270 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000271 return true;
272}
273
274void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
275 ASSERT(worker_thread_ == rtc::Thread::Current());
276
277 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700278 if (!old_tc && !new_tc) {
279 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000280 return;
281 }
deadbeefcbecd352015-09-23 11:50:27 -0700282 ASSERT(old_tc != new_tc);
283
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000284 if (old_tc) {
285 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700286 transport_controller_->DestroyTransportChannel_w(
287 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000288 }
289
290 transport_channel_ = new_tc;
291
292 if (new_tc) {
293 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700294 for (const auto& pair : socket_options_) {
295 new_tc->SetOption(pair.first, pair.second);
296 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000297 }
deadbeefcbecd352015-09-23 11:50:27 -0700298
299 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
300 // setting new channel
301 UpdateWritableState_w();
302 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000303}
304
guoweis46383312015-12-17 16:45:59 -0800305void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc,
306 bool update_writablity) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000307 ASSERT(worker_thread_ == rtc::Thread::Current());
308
309 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700310 if (!old_tc && !new_tc) {
311 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000312 return;
313 }
deadbeefcbecd352015-09-23 11:50:27 -0700314 ASSERT(old_tc != new_tc);
315
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000316 if (old_tc) {
317 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700318 transport_controller_->DestroyTransportChannel_w(
319 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000320 }
321
322 rtcp_transport_channel_ = new_tc;
323
324 if (new_tc) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800325 RTC_CHECK(!(ShouldSetupDtlsSrtp() && srtp_filter_.IsActive()))
326 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
327 << "should never happen.";
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000328 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700329 for (const auto& pair : rtcp_socket_options_) {
330 new_tc->SetOption(pair.first, pair.second);
331 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000332 }
deadbeefcbecd352015-09-23 11:50:27 -0700333
guoweis46383312015-12-17 16:45:59 -0800334 if (update_writablity) {
335 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
336 // setting new channel
337 UpdateWritableState_w();
338 SetReadyToSend(true, new_tc && new_tc->writable());
339 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000340}
341
342void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
343 ASSERT(worker_thread_ == rtc::Thread::Current());
344
345 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
346 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
347 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800348 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000349}
350
351void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
352 ASSERT(worker_thread_ == rtc::Thread::Current());
353
354 tc->SignalWritableState.disconnect(this);
355 tc->SignalReadPacket.disconnect(this);
356 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800357 tc->SignalDtlsState.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000358}
359
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000361 worker_thread_->Invoke<void>(Bind(
362 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
363 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 return true;
365}
366
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000368 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369}
370
Peter Boström0c4e06b2015-10-07 12:23:21 +0200371bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000372 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373}
374
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000375bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000376 return InvokeOnWorker(
377 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000378}
379
Peter Boström0c4e06b2015-10-07 12:23:21 +0200380bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000381 return InvokeOnWorker(
382 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000383}
384
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000386 ContentAction action,
387 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100388 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000389 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
390 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391}
392
393bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000394 ContentAction action,
395 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100396 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000397 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
398 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399}
400
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000402 // We pass in the BaseChannel instead of the transport_channel_
403 // because if the transport_channel_ changes, the ConnectionMonitor
404 // would be pointing to the wrong TransportChannel.
405 connection_monitor_.reset(new ConnectionMonitor(
406 this, worker_thread(), rtc::Thread::Current()));
407 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000409 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410}
411
412void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000413 if (connection_monitor_) {
414 connection_monitor_->Stop();
415 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 }
417}
418
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000419bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
420 ASSERT(worker_thread_ == rtc::Thread::Current());
421 return transport_channel_->GetStats(infos);
422}
423
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424bool BaseChannel::IsReadyToReceive() const {
425 // Receive data if we are enabled and have local content,
426 return enabled() && IsReceiveContentDirection(local_content_direction_);
427}
428
429bool BaseChannel::IsReadyToSend() const {
430 // Send outgoing data if we are enabled, have local and remote content,
431 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800432 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 IsSendContentDirection(local_content_direction_) &&
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800434 was_ever_writable() &&
435 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436}
437
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000438bool BaseChannel::SendPacket(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700439 const rtc::PacketOptions& options) {
440 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441}
442
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000443bool BaseChannel::SendRtcp(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700444 const rtc::PacketOptions& options) {
445 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446}
447
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000448int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000450 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000452 case ST_RTP:
453 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700454 socket_options_.push_back(
455 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000456 break;
457 case ST_RTCP:
458 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700459 rtcp_socket_options_.push_back(
460 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000461 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000463 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464}
465
466void BaseChannel::OnWritableState(TransportChannel* channel) {
467 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
deadbeefcbecd352015-09-23 11:50:27 -0700468 UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469}
470
471void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000472 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000473 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000474 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100475 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000477 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478
479 // When using RTCP multiplexing we might get RTCP packets on the RTP
480 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
481 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000482 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000483 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484}
485
486void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700487 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
488 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489}
490
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800491void BaseChannel::OnDtlsState(TransportChannel* channel,
492 DtlsTransportState state) {
493 if (!ShouldSetupDtlsSrtp()) {
494 return;
495 }
496
497 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
498 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
499 // cover other scenarios like the whole channel is writable (not just this
500 // TransportChannel) or when TransportChannel is attached after DTLS is
501 // negotiated.
502 if (state != DTLS_TRANSPORT_CONNECTED) {
503 srtp_filter_.ResetParams();
504 }
505}
506
deadbeefcbecd352015-09-23 11:50:27 -0700507void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
508 if (rtcp) {
509 rtcp_ready_to_send_ = ready;
510 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 rtp_ready_to_send_ = ready;
512 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513
deadbeefcbecd352015-09-23 11:50:27 -0700514 if (rtp_ready_to_send_ &&
515 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
516 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
torbjornga81a42f2015-09-23 02:16:58 -0700517 // Notify the MediaChannel when both rtp and rtcp channel can send.
518 media_channel_->OnReadyToSend(true);
deadbeefcbecd352015-09-23 11:50:27 -0700519 } else {
520 // Notify the MediaChannel when either rtp or rtcp channel can't send.
521 media_channel_->OnReadyToSend(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 }
523}
524
525bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
526 const char* data, size_t len) {
527 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000528 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529}
530
stefanc1aeaf02015-10-15 07:26:07 -0700531bool BaseChannel::SendPacket(bool rtcp,
532 rtc::Buffer* packet,
533 const rtc::PacketOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 // SendPacket gets called from MediaEngine, typically on an encoder thread.
535 // If the thread is not our worker thread, we will post to our worker
536 // so that the real work happens on our worker. This avoids us having to
537 // synchronize access to all the pieces of the send path, including
538 // SRTP and the inner workings of the transport channels.
539 // The only downside is that we can't return a proper failure code if
540 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000541 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 // Avoid a copy by transferring the ownership of the packet data.
543 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
544 PacketMessageData* data = new PacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800545 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700546 data->options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 worker_thread_->Post(this, message_id, data);
548 return true;
549 }
550
551 // Now that we are on the correct thread, ensure we have a place to send this
552 // packet before doing anything. (We might get RTCP packets that we don't
553 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
554 // transport.
555 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
556 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000557 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 return false;
559 }
560
561 // Protect ourselves against crazy data.
562 if (!ValidPacket(rtcp, packet)) {
563 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000564 << PacketType(rtcp)
565 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 return false;
567 }
568
stefanc1aeaf02015-10-15 07:26:07 -0700569 rtc::PacketOptions updated_options;
570 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 // Protect if needed.
572 if (srtp_filter_.IsActive()) {
573 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200574 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000575 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000577 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
578 // inside libsrtp for a RTP packet. A external HMAC module will be writing
579 // a fake HMAC value. This is ONLY done for a RTP packet.
580 // Socket layer will update rtp sendtime extension header if present in
581 // packet with current time before updating the HMAC.
582#if !defined(ENABLE_EXTERNAL_AUTH)
583 res = srtp_filter_.ProtectRtp(
584 data, len, static_cast<int>(packet->capacity()), &len);
585#else
stefanc1aeaf02015-10-15 07:26:07 -0700586 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000587 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000588 res = srtp_filter_.ProtectRtp(
589 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700590 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000591 // If protection succeeds, let's get auth params from srtp.
592 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200593 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000594 int key_len;
595 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700596 &auth_key, &key_len,
597 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000598 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700599 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
600 updated_options.packet_time_params.srtp_auth_key.assign(
601 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000602 }
603 }
604#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 if (!res) {
606 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200607 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 GetRtpSeqNum(data, len, &seq_num);
609 GetRtpSsrc(data, len, &ssrc);
610 LOG(LS_ERROR) << "Failed to protect " << content_name_
611 << " RTP packet: size=" << len
612 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
613 return false;
614 }
615 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000616 res = srtp_filter_.ProtectRtcp(data, len,
617 static_cast<int>(packet->capacity()),
618 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 if (!res) {
620 int type = -1;
621 GetRtcpType(data, len, &type);
622 LOG(LS_ERROR) << "Failed to protect " << content_name_
623 << " RTCP packet: size=" << len << ", type=" << type;
624 return false;
625 }
626 }
627
628 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000629 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 } else if (secure_required_) {
631 // This is a double check for something that supposedly can't happen.
632 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
633 << " packet when SRTP is inactive and crypto is required";
634
635 ASSERT(false);
636 return false;
637 }
638
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000640 int ret =
stefanc1aeaf02015-10-15 07:26:07 -0700641 channel->SendPacket(packet->data<char>(), packet->size(), updated_options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000642 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
643 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 if (channel->GetError() == EWOULDBLOCK) {
645 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700646 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 }
648 return false;
649 }
650 return true;
651}
652
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000653bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 // Protect ourselves against crazy data.
655 if (!ValidPacket(rtcp, packet)) {
656 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000657 << PacketType(rtcp)
658 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 return false;
660 }
pbos482b12e2015-11-16 10:19:58 -0800661 if (rtcp) {
662 // Permit all (seemingly valid) RTCP packets.
663 return true;
664 }
665 // Check whether we handle this payload.
666 return bundle_filter_.DemuxPacket(packet->data<uint8_t>(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667}
668
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000669void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
670 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 if (!WantsPacket(rtcp, packet)) {
672 return;
673 }
674
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000675 // We are only interested in the first rtp packet because that
676 // indicates the media has started flowing.
677 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 has_received_packet_ = true;
679 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
680 }
681
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 // Unprotect the packet, if needed.
683 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200684 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000685 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 bool res;
687 if (!rtcp) {
688 res = srtp_filter_.UnprotectRtp(data, len, &len);
689 if (!res) {
690 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200691 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 GetRtpSeqNum(data, len, &seq_num);
693 GetRtpSsrc(data, len, &ssrc);
694 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
695 << " RTP packet: size=" << len
696 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
697 return;
698 }
699 } else {
700 res = srtp_filter_.UnprotectRtcp(data, len, &len);
701 if (!res) {
702 int type = -1;
703 GetRtcpType(data, len, &type);
704 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
705 << " RTCP packet: size=" << len << ", type=" << type;
706 return;
707 }
708 }
709
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000710 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 } else if (secure_required_) {
712 // Our session description indicates that SRTP is required, but we got a
713 // packet before our SRTP filter is active. This means either that
714 // a) we got SRTP packets before we received the SDES keys, in which case
715 // we can't decrypt it anyway, or
716 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
717 // channels, so we haven't yet extracted keys, even if DTLS did complete
718 // on the channel that the packets are being sent on. It's really good
719 // practice to wait for both RTP and RTCP to be good to go before sending
720 // media, to prevent weird failure modes, so it's fine for us to just eat
721 // packets here. This is all sidestepped if RTCP mux is used anyway.
722 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
723 << " packet when SRTP is inactive and crypto is required";
724 return;
725 }
726
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 // Push it down to the media channel.
728 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000729 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000731 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 }
733}
734
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000735bool BaseChannel::PushdownLocalDescription(
736 const SessionDescription* local_desc, ContentAction action,
737 std::string* error_desc) {
738 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 const MediaContentDescription* content_desc =
740 GetContentDescription(content_info);
741 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000742 !SetLocalContent(content_desc, action, error_desc)) {
743 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
744 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000746 return true;
747}
748
749bool BaseChannel::PushdownRemoteDescription(
750 const SessionDescription* remote_desc, ContentAction action,
751 std::string* error_desc) {
752 const ContentInfo* content_info = GetFirstContent(remote_desc);
753 const MediaContentDescription* content_desc =
754 GetContentDescription(content_info);
755 if (content_desc && content_info && !content_info->rejected &&
756 !SetRemoteContent(content_desc, action, error_desc)) {
757 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
758 return false;
759 }
760 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761}
762
763void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000764 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 if (enabled_)
766 return;
767
768 LOG(LS_INFO) << "Channel enabled";
769 enabled_ = true;
770 ChangeState();
771}
772
773void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000774 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 if (!enabled_)
776 return;
777
778 LOG(LS_INFO) << "Channel disabled";
779 enabled_ = false;
780 ChangeState();
781}
782
deadbeefcbecd352015-09-23 11:50:27 -0700783void BaseChannel::UpdateWritableState_w() {
784 if (transport_channel_ && transport_channel_->writable() &&
785 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
786 ChannelWritable_w();
787 } else {
788 ChannelNotWritable_w();
789 }
790}
791
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000793 ASSERT(worker_thread_ == rtc::Thread::Current());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800794 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800796 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797
deadbeefcbecd352015-09-23 11:50:27 -0700798 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 << (was_ever_writable_ ? "" : " for the first time");
800
801 std::vector<ConnectionInfo> infos;
802 transport_channel_->GetStats(&infos);
803 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
804 it != infos.end(); ++it) {
805 if (it->best_connection) {
806 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
807 << "->" << it->remote_candidate.ToSensitiveString();
808 break;
809 }
810 }
811
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 was_ever_writable_ = true;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800813 MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 writable_ = true;
815 ChangeState();
816}
817
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000818void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
819 ASSERT(worker_thread() == rtc::Thread::Current());
820 signaling_thread()->Invoke<void>(Bind(
821 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
822}
823
824void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
825 ASSERT(signaling_thread() == rtc::Thread::Current());
826 SignalDtlsSetupFailure(this, rtcp);
827}
828
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800829bool BaseChannel::SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp) {
830 std::vector<int> crypto_suites;
831 // We always use the default SRTP crypto suites for RTCP, but we may use
832 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 if (!rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800834 GetSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800836 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800838 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839}
840
841bool BaseChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800842 // Since DTLS is applied to all channels, checking RTP should be enough.
843 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844}
845
846// This function returns true if either DTLS-SRTP is not in use
847// *or* DTLS-SRTP is successfully set up.
848bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
849 bool ret = false;
850
deadbeefcbecd352015-09-23 11:50:27 -0700851 TransportChannel* channel =
852 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800854 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800856 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800858 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
859 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 return false;
861 }
862
863 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
864 << content_name() << " "
865 << PacketType(rtcp_channel);
866
867 // OK, we're now doing DTLS (RFC 5764)
868 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
869 SRTP_MASTER_KEY_SALT_LEN * 2);
870
871 // RFC 5705 exporter using the RFC 5764 parameters
872 if (!channel->ExportKeyingMaterial(
873 kDtlsSrtpExporterLabel,
874 NULL, 0, false,
875 &dtls_buffer[0], dtls_buffer.size())) {
876 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
877 ASSERT(false); // This should never happen
878 return false;
879 }
880
881 // Sync up the keys with the DTLS-SRTP interface
882 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
883 SRTP_MASTER_KEY_SALT_LEN);
884 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
885 SRTP_MASTER_KEY_SALT_LEN);
886 size_t offset = 0;
887 memcpy(&client_write_key[0], &dtls_buffer[offset],
888 SRTP_MASTER_KEY_KEY_LEN);
889 offset += SRTP_MASTER_KEY_KEY_LEN;
890 memcpy(&server_write_key[0], &dtls_buffer[offset],
891 SRTP_MASTER_KEY_KEY_LEN);
892 offset += SRTP_MASTER_KEY_KEY_LEN;
893 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
894 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
895 offset += SRTP_MASTER_KEY_SALT_LEN;
896 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
897 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
898
899 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000900 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000901 if (!channel->GetSslRole(&role)) {
902 LOG(LS_WARNING) << "GetSslRole failed";
903 return false;
904 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000906 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 send_key = &server_write_key;
908 recv_key = &client_write_key;
909 } else {
910 send_key = &client_write_key;
911 recv_key = &server_write_key;
912 }
913
914 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800915 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
916 static_cast<int>(send_key->size()),
917 selected_crypto_suite, &(*recv_key)[0],
918 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800920 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
921 static_cast<int>(send_key->size()),
922 selected_crypto_suite, &(*recv_key)[0],
923 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 }
925
926 if (!ret)
927 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
928 else
929 dtls_keyed_ = true;
930
931 return ret;
932}
933
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800934void BaseChannel::MaybeSetupDtlsSrtp_w() {
935 if (srtp_filter_.IsActive()) {
936 return;
937 }
938
939 if (!ShouldSetupDtlsSrtp()) {
940 return;
941 }
942
943 if (!SetupDtlsSrtp(false)) {
944 SignalDtlsSetupFailure_w(false);
945 return;
946 }
947
948 if (rtcp_transport_channel_) {
949 if (!SetupDtlsSrtp(true)) {
950 SignalDtlsSetupFailure_w(true);
951 return;
952 }
953 }
954}
955
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000957 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 if (!writable_)
959 return;
960
deadbeefcbecd352015-09-23 11:50:27 -0700961 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 writable_ = false;
963 ChangeState();
964}
965
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700966bool BaseChannel::SetRtpTransportParameters_w(
967 const MediaContentDescription* content,
968 ContentAction action,
969 ContentSource src,
970 std::string* error_desc) {
971 if (action == CA_UPDATE) {
972 // These parameters never get changed by a CA_UDPATE.
973 return true;
974 }
975
976 // Cache secure_required_ for belt and suspenders check on SendPacket
977 if (src == CS_LOCAL) {
978 set_secure_required(content->crypto_required() != CT_NONE);
979 }
980
981 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
982 return false;
983 }
984
985 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
986 return false;
987 }
988
989 return true;
990}
991
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000992// |dtls| will be set to true if DTLS is active for transport channel and
993// crypto is empty.
994bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000995 bool* dtls,
996 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000997 *dtls = transport_channel_->IsDtlsActive();
998 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000999 SafeSetError("Cryptos must be empty when DTLS is active.",
1000 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001001 return false;
1002 }
1003 return true;
1004}
1005
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001007 ContentAction action,
1008 ContentSource src,
1009 std::string* error_desc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001010 TRACE_EVENT0("webrtc", "BaseChannel::SetSrtp_w");
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001011 if (action == CA_UPDATE) {
1012 // no crypto params.
1013 return true;
1014 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001016 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001017 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
1018 if (!ret) {
1019 return false;
1020 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 switch (action) {
1022 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001023 // If DTLS is already active on the channel, we could be renegotiating
1024 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001025 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001026 ret = srtp_filter_.SetOffer(cryptos, src);
1027 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028 break;
1029 case CA_PRANSWER:
1030 // If we're doing DTLS-SRTP, we don't want to update the filter
1031 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001032 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1034 }
1035 break;
1036 case CA_ANSWER:
1037 // If we're doing DTLS-SRTP, we don't want to update the filter
1038 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001039 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 ret = srtp_filter_.SetAnswer(cryptos, src);
1041 }
1042 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043 default:
1044 break;
1045 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001046 if (!ret) {
1047 SafeSetError("Failed to setup SRTP filter.", error_desc);
1048 return false;
1049 }
1050 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051}
1052
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001053void BaseChannel::ActivateRtcpMux() {
1054 worker_thread_->Invoke<void>(Bind(
1055 &BaseChannel::ActivateRtcpMux_w, this));
1056}
1057
1058void BaseChannel::ActivateRtcpMux_w() {
1059 if (!rtcp_mux_filter_.IsActive()) {
1060 rtcp_mux_filter_.SetActive();
guoweis46383312015-12-17 16:45:59 -08001061 set_rtcp_transport_channel(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001062 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001063 }
1064}
1065
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001067 ContentSource src,
1068 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 bool ret = false;
1070 switch (action) {
1071 case CA_OFFER:
1072 ret = rtcp_mux_filter_.SetOffer(enable, src);
1073 break;
1074 case CA_PRANSWER:
1075 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1076 break;
1077 case CA_ANSWER:
1078 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1079 if (ret && rtcp_mux_filter_.IsActive()) {
1080 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001081 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1082 << " by destroying RTCP transport channel for "
1083 << transport_name();
guoweis46383312015-12-17 16:45:59 -08001084 set_rtcp_transport_channel(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001085 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086 }
1087 break;
1088 case CA_UPDATE:
1089 // No RTCP mux info.
1090 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001091 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 default:
1093 break;
1094 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001095 if (!ret) {
1096 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1097 return false;
1098 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1100 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1101 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001102 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 // If the RTP transport is already writable, then so are we.
1104 if (transport_channel_->writable()) {
1105 ChannelWritable_w();
1106 }
1107 }
1108
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001109 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110}
1111
1112bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001113 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001114 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115}
1116
Peter Boström0c4e06b2015-10-07 12:23:21 +02001117bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001118 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 return media_channel()->RemoveRecvStream(ssrc);
1120}
1121
1122bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001123 ContentAction action,
1124 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1126 action == CA_PRANSWER || action == CA_UPDATE))
1127 return false;
1128
1129 // If this is an update, streams only contain streams that have changed.
1130 if (action == CA_UPDATE) {
1131 for (StreamParamsVec::const_iterator it = streams.begin();
1132 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001133 const StreamParams* existing_stream =
1134 GetStreamByIds(local_streams_, it->groupid, it->id);
1135 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 if (media_channel()->AddSendStream(*it)) {
1137 local_streams_.push_back(*it);
1138 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1139 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001140 std::ostringstream desc;
1141 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1142 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143 return false;
1144 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001145 } else if (existing_stream && !it->has_ssrcs()) {
1146 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001147 std::ostringstream desc;
1148 desc << "Failed to remove send stream with ssrc "
1149 << it->first_ssrc() << ".";
1150 SafeSetError(desc.str(), error_desc);
1151 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001152 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001153 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154 } else {
1155 LOG(LS_WARNING) << "Ignore unsupported stream update";
1156 }
1157 }
1158 return true;
1159 }
1160 // Else streams are all the streams we want to send.
1161
1162 // Check for streams that have been removed.
1163 bool ret = true;
1164 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1165 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001166 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001168 std::ostringstream desc;
1169 desc << "Failed to remove send stream with ssrc "
1170 << it->first_ssrc() << ".";
1171 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172 ret = false;
1173 }
1174 }
1175 }
1176 // Check for new streams.
1177 for (StreamParamsVec::const_iterator it = streams.begin();
1178 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001179 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001181 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001183 std::ostringstream desc;
1184 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1185 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 ret = false;
1187 }
1188 }
1189 }
1190 local_streams_ = streams;
1191 return ret;
1192}
1193
1194bool BaseChannel::UpdateRemoteStreams_w(
1195 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001196 ContentAction action,
1197 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1199 action == CA_PRANSWER || action == CA_UPDATE))
1200 return false;
1201
1202 // If this is an update, streams only contain streams that have changed.
1203 if (action == CA_UPDATE) {
1204 for (StreamParamsVec::const_iterator it = streams.begin();
1205 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001206 const StreamParams* existing_stream =
1207 GetStreamByIds(remote_streams_, it->groupid, it->id);
1208 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 if (AddRecvStream_w(*it)) {
1210 remote_streams_.push_back(*it);
1211 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1212 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001213 std::ostringstream desc;
1214 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1215 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 return false;
1217 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001218 } else if (existing_stream && !it->has_ssrcs()) {
1219 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001220 std::ostringstream desc;
1221 desc << "Failed to remove remote stream with ssrc "
1222 << it->first_ssrc() << ".";
1223 SafeSetError(desc.str(), error_desc);
1224 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001226 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 } else {
1228 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001229 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 << " new stream = " << it->ToString();
1231 }
1232 }
1233 return true;
1234 }
1235 // Else streams are all the streams we want to receive.
1236
1237 // Check for streams that have been removed.
1238 bool ret = true;
1239 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1240 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001241 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001243 std::ostringstream desc;
1244 desc << "Failed to remove remote stream with ssrc "
1245 << it->first_ssrc() << ".";
1246 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 ret = false;
1248 }
1249 }
1250 }
1251 // Check for new streams.
1252 for (StreamParamsVec::const_iterator it = streams.begin();
1253 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001254 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 if (AddRecvStream_w(*it)) {
1256 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1257 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001258 std::ostringstream desc;
1259 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1260 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 ret = false;
1262 }
1263 }
1264 }
1265 remote_streams_ = streams;
1266 return ret;
1267}
1268
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001269void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1270 const std::vector<RtpHeaderExtension>& extensions) {
1271 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001272 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001273 rtp_abs_sendtime_extn_id_ =
1274 send_time_extension ? send_time_extension->id : -1;
1275}
1276
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001277void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001278 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 case MSG_RTPPACKET:
1281 case MSG_RTCPPACKET: {
1282 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
stefanc1aeaf02015-10-15 07:26:07 -07001283 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet,
1284 data->options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 delete data; // because it is Posted
1286 break;
1287 }
1288 case MSG_FIRSTPACKETRECEIVED: {
1289 SignalFirstPacketReceived(this);
1290 break;
1291 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 }
1293}
1294
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001295void BaseChannel::FlushRtcpMessages() {
1296 // Flush all remaining RTCP messages. This should only be called in
1297 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001298 ASSERT(rtc::Thread::Current() == worker_thread_);
1299 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001300 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001301 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001303 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 }
1305}
1306
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001307VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308 MediaEngineInterface* media_engine,
1309 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001310 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 const std::string& content_name,
1312 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001313 : BaseChannel(thread,
1314 media_channel,
1315 transport_controller,
1316 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001318 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001319 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320
1321VoiceChannel::~VoiceChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001322 TRACE_EVENT0("webrtc", "VoiceChannel::~VoiceChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 StopAudioMonitor();
1324 StopMediaMonitor();
1325 // this can't be done in the base class, since it calls a virtual
1326 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001327 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328}
1329
1330bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001331 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 return false;
1333 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 return true;
1335}
1336
Peter Boström0c4e06b2015-10-07 12:23:21 +02001337bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001338 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001339 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001340 AudioSource* source) {
deadbeefcbecd352015-09-23 11:50:27 -07001341 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001342 ssrc, enable, options, source));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343}
1344
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345// TODO(juberti): Handle early media the right way. We should get an explicit
1346// ringing message telling us to start playing local ringback, which we cancel
1347// if any early media actually arrives. For now, we do the opposite, which is
1348// to wait 1 second for early media, and start playing local ringback if none
1349// arrives.
1350void VoiceChannel::SetEarlyMedia(bool enable) {
1351 if (enable) {
1352 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001353 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1354 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355 } else {
1356 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001357 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358 }
1359}
1360
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001362 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1363 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364}
1365
Peter Boström0c4e06b2015-10-07 12:23:21 +02001366bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1367 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001368 int duration) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001369 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
solenberg1d63dd02015-12-02 12:35:09 -08001370 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371}
1372
solenberg4bac9c52015-10-09 02:32:53 -07001373bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1374 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1375 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001377
Tommif888bb52015-12-12 01:37:01 +01001378void VoiceChannel::SetRawAudioSink(
1379 uint32_t ssrc,
deadbeef2d110be2016-01-13 12:00:26 -08001380 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
1381 // We need to work around Bind's lack of support for scoped_ptr and ownership
1382 // passing. So we invoke to our own little routine that gets a pointer to
1383 // our local variable. This is OK since we're synchronously invoking.
1384 InvokeOnWorker(Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
Tommif888bb52015-12-12 01:37:01 +01001385}
1386
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001388 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1389 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390}
1391
1392void VoiceChannel::StartMediaMonitor(int cms) {
1393 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001394 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395 media_monitor_->SignalUpdate.connect(
1396 this, &VoiceChannel::OnMediaMonitorUpdate);
1397 media_monitor_->Start(cms);
1398}
1399
1400void VoiceChannel::StopMediaMonitor() {
1401 if (media_monitor_) {
1402 media_monitor_->Stop();
1403 media_monitor_->SignalUpdate.disconnect(this);
1404 media_monitor_.reset();
1405 }
1406}
1407
1408void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001409 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410 audio_monitor_
1411 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1412 audio_monitor_->Start(cms);
1413}
1414
1415void VoiceChannel::StopAudioMonitor() {
1416 if (audio_monitor_) {
1417 audio_monitor_->Stop();
1418 audio_monitor_.reset();
1419 }
1420}
1421
1422bool VoiceChannel::IsAudioMonitorRunning() const {
1423 return (audio_monitor_.get() != NULL);
1424}
1425
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001427 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428}
1429
1430int VoiceChannel::GetOutputLevel_w() {
1431 return media_channel()->GetOutputLevel();
1432}
1433
1434void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1435 media_channel()->GetActiveStreams(actives);
1436}
1437
1438void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001439 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001440 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001441 int flags) {
1442 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443
1444 // Set a flag when we've received an RTP packet. If we're waiting for early
1445 // media, this will disable the timeout.
1446 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1447 received_media_ = true;
1448 }
1449}
1450
1451void VoiceChannel::ChangeState() {
1452 // Render incoming data if we're the active call, and we have the local
1453 // content. We receive data on the default channel and multiplexed streams.
1454 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001455 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456
1457 // Send outgoing data if we're the active call, we have the remote content,
1458 // and we have had some form of connectivity.
1459 bool send = IsReadyToSend();
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001460 media_channel()->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461
1462 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1463}
1464
1465const ContentInfo* VoiceChannel::GetFirstContent(
1466 const SessionDescription* sdesc) {
1467 return GetFirstAudioContent(sdesc);
1468}
1469
1470bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001471 ContentAction action,
1472 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001473 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001474 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 LOG(LS_INFO) << "Setting local voice description";
1476
1477 const AudioContentDescription* audio =
1478 static_cast<const AudioContentDescription*>(content);
1479 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001480 if (!audio) {
1481 SafeSetError("Can't find audio content in local description.", error_desc);
1482 return false;
1483 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001485 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1486 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487 }
1488
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001489 AudioRecvParameters recv_params = last_recv_params_;
1490 RtpParametersFromMediaDescription(audio, &recv_params);
1491 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001492 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001493 error_desc);
1494 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001496 for (const AudioCodec& codec : audio->codecs()) {
1497 bundle_filter()->AddPayloadType(codec.id);
1498 }
1499 last_recv_params_ = recv_params;
1500
1501 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1502 // only give it to the media channel once we have a remote
1503 // description too (without a remote description, we won't be able
1504 // to send them anyway).
1505 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1506 SafeSetError("Failed to set local audio description streams.", error_desc);
1507 return false;
1508 }
1509
1510 set_local_content_direction(content->direction());
1511 ChangeState();
1512 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513}
1514
1515bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001516 ContentAction action,
1517 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001518 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001519 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001520 LOG(LS_INFO) << "Setting remote voice description";
1521
1522 const AudioContentDescription* audio =
1523 static_cast<const AudioContentDescription*>(content);
1524 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001525 if (!audio) {
1526 SafeSetError("Can't find audio content in remote description.", error_desc);
1527 return false;
1528 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001530 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1531 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 }
1533
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001534 AudioSendParameters send_params = last_send_params_;
1535 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001536 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001537 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001538 }
1539 if (!media_channel()->SetSendParameters(send_params)) {
1540 SafeSetError("Failed to set remote audio description send parameters.",
1541 error_desc);
1542 return false;
1543 }
1544 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001546 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1547 // and only give it to the media channel once we have a local
1548 // description too (without a local description, we won't be able to
1549 // recv them anyway).
1550 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1551 SafeSetError("Failed to set remote audio description streams.", error_desc);
1552 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553 }
1554
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001555 if (audio->rtp_header_extensions_set()) {
1556 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1557 }
1558
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001559 set_remote_content_direction(content->direction());
1560 ChangeState();
1561 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562}
1563
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564void VoiceChannel::HandleEarlyMediaTimeout() {
1565 // This occurs on the main thread, not the worker thread.
1566 if (!received_media_) {
1567 LOG(LS_INFO) << "No early media received before timeout";
1568 SignalEarlyMediaTimeout(this);
1569 }
1570}
1571
Peter Boström0c4e06b2015-10-07 12:23:21 +02001572bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1573 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001574 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575 if (!enabled()) {
1576 return false;
1577 }
solenberg1d63dd02015-12-02 12:35:09 -08001578 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001579}
1580
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001581void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 case MSG_EARLYMEDIATIMEOUT:
1584 HandleEarlyMediaTimeout();
1585 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 case MSG_CHANNEL_ERROR: {
1587 VoiceChannelErrorMessageData* data =
1588 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 delete data;
1590 break;
1591 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 default:
1593 BaseChannel::OnMessage(pmsg);
1594 break;
1595 }
1596}
1597
1598void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001599 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 SignalConnectionMonitor(this, infos);
1601}
1602
1603void VoiceChannel::OnMediaMonitorUpdate(
1604 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1605 ASSERT(media_channel == this->media_channel());
1606 SignalMediaMonitor(this, info);
1607}
1608
1609void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1610 const AudioInfo& info) {
1611 SignalAudioMonitor(this, info);
1612}
1613
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001614void VoiceChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1615 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616}
1617
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001618VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001620 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001622 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001623 : BaseChannel(thread,
1624 media_channel,
1625 transport_controller,
1626 content_name,
perkjc11b1842016-03-07 17:34:13 -08001627 rtcp) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628
1629bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001630 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 return false;
1632 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 return true;
1634}
1635
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636VideoChannel::~VideoChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001637 TRACE_EVENT0("webrtc", "VideoChannel::~VideoChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 StopMediaMonitor();
1639 // this can't be done in the base class, since it calls a virtual
1640 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001641
1642 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643}
1644
nisse08582ff2016-02-04 01:24:52 -08001645bool VideoChannel::SetSink(uint32_t ssrc,
1646 rtc::VideoSinkInterface<VideoFrame>* sink) {
1647 worker_thread()->Invoke<void>(
1648 Bind(&VideoMediaChannel::SetSink, media_channel(), ssrc, sink));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 return true;
1650}
1651
Peter Boström0c4e06b2015-10-07 12:23:21 +02001652bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001653 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1654 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655}
1656
Peter Boström0c4e06b2015-10-07 12:23:21 +02001657bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001658 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001659 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001660 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1661 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001662}
1663
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 // Send outgoing data if we're the active call, we have the remote content,
1666 // and we have had some form of connectivity.
1667 bool send = IsReadyToSend();
1668 if (!media_channel()->SetSend(send)) {
1669 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1670 // TODO(gangji): Report error back to server.
1671 }
1672
Peter Boström34fbfff2015-09-24 19:20:30 +02001673 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674}
1675
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001676bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1677 return InvokeOnWorker(
1678 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679}
1680
1681void VideoChannel::StartMediaMonitor(int cms) {
1682 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001683 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684 media_monitor_->SignalUpdate.connect(
1685 this, &VideoChannel::OnMediaMonitorUpdate);
1686 media_monitor_->Start(cms);
1687}
1688
1689void VideoChannel::StopMediaMonitor() {
1690 if (media_monitor_) {
1691 media_monitor_->Stop();
1692 media_monitor_.reset();
1693 }
1694}
1695
1696const ContentInfo* VideoChannel::GetFirstContent(
1697 const SessionDescription* sdesc) {
1698 return GetFirstVideoContent(sdesc);
1699}
1700
1701bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001702 ContentAction action,
1703 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001704 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001705 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 LOG(LS_INFO) << "Setting local video description";
1707
1708 const VideoContentDescription* video =
1709 static_cast<const VideoContentDescription*>(content);
1710 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001711 if (!video) {
1712 SafeSetError("Can't find video content in local description.", error_desc);
1713 return false;
1714 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001716 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1717 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 }
1719
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001720 VideoRecvParameters recv_params = last_recv_params_;
1721 RtpParametersFromMediaDescription(video, &recv_params);
1722 if (!media_channel()->SetRecvParameters(recv_params)) {
1723 SafeSetError("Failed to set local video description recv parameters.",
1724 error_desc);
1725 return false;
1726 }
1727 for (const VideoCodec& codec : video->codecs()) {
1728 bundle_filter()->AddPayloadType(codec.id);
1729 }
1730 last_recv_params_ = recv_params;
1731
1732 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1733 // only give it to the media channel once we have a remote
1734 // description too (without a remote description, we won't be able
1735 // to send them anyway).
1736 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1737 SafeSetError("Failed to set local video description streams.", error_desc);
1738 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 }
1740
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001741 set_local_content_direction(content->direction());
1742 ChangeState();
1743 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744}
1745
1746bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001747 ContentAction action,
1748 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001749 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001750 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 LOG(LS_INFO) << "Setting remote video description";
1752
1753 const VideoContentDescription* video =
1754 static_cast<const VideoContentDescription*>(content);
1755 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001756 if (!video) {
1757 SafeSetError("Can't find video content in remote description.", error_desc);
1758 return false;
1759 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001761
1762 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1763 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 }
1765
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001766 VideoSendParameters send_params = last_send_params_;
1767 RtpSendParametersFromMediaDescription(video, &send_params);
1768 if (video->conference_mode()) {
nisse4b4dc862016-02-17 05:25:36 -08001769 send_params.conference_mode = true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001770 }
1771 if (!media_channel()->SetSendParameters(send_params)) {
1772 SafeSetError("Failed to set remote video description send parameters.",
1773 error_desc);
1774 return false;
1775 }
1776 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001778 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1779 // and only give it to the media channel once we have a local
1780 // description too (without a local description, we won't be able to
1781 // recv them anyway).
1782 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1783 SafeSetError("Failed to set remote video description streams.", error_desc);
1784 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 }
1786
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001787 if (video->rtp_header_extensions_set()) {
1788 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001790
1791 set_remote_content_direction(content->direction());
1792 ChangeState();
1793 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794}
1795
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001796void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 case MSG_CHANNEL_ERROR: {
1799 const VideoChannelErrorMessageData* data =
1800 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 delete data;
1802 break;
1803 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 default:
1805 BaseChannel::OnMessage(pmsg);
1806 break;
1807 }
1808}
1809
1810void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001811 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812 SignalConnectionMonitor(this, infos);
1813}
1814
1815// TODO(pthatcher): Look into removing duplicate code between
1816// audio, video, and data, perhaps by using templates.
1817void VideoChannel::OnMediaMonitorUpdate(
1818 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1819 ASSERT(media_channel == this->media_channel());
1820 SignalMediaMonitor(this, info);
1821}
1822
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001823void VideoChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1824 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825}
1826
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001827DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001829 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 const std::string& content_name,
1831 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001832 : BaseChannel(thread,
1833 media_channel,
1834 transport_controller,
1835 content_name,
1836 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001837 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07001838 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839
1840DataChannel::~DataChannel() {
Peter Boströmca8b4042016-03-08 14:24:13 -08001841 TRACE_EVENT0("webrtc", "DataChannel::~DataChannel");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 StopMediaMonitor();
1843 // this can't be done in the base class, since it calls a virtual
1844 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001845
1846 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847}
1848
1849bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001850 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 return false;
1852 }
1853 media_channel()->SignalDataReceived.connect(
1854 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001855 media_channel()->SignalReadyToSend.connect(
1856 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001857 media_channel()->SignalStreamClosedRemotely.connect(
1858 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 return true;
1860}
1861
1862bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001863 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001865 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
1866 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867}
1868
1869const ContentInfo* DataChannel::GetFirstContent(
1870 const SessionDescription* sdesc) {
1871 return GetFirstDataContent(sdesc);
1872}
1873
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001874bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 if (data_channel_type_ == DCT_SCTP) {
1876 // TODO(pthatcher): Do this in a more robust way by checking for
1877 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00001878 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 } else if (data_channel_type_ == DCT_RTP) {
1880 return BaseChannel::WantsPacket(rtcp, packet);
1881 }
1882 return false;
1883}
1884
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001885bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
1886 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 // It hasn't been set before, so set it now.
1888 if (data_channel_type_ == DCT_NONE) {
1889 data_channel_type_ = new_data_channel_type;
1890 return true;
1891 }
1892
1893 // It's been set before, but doesn't match. That's bad.
1894 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001895 std::ostringstream desc;
1896 desc << "Data channel type mismatch."
1897 << " Expected " << data_channel_type_
1898 << " Got " << new_data_channel_type;
1899 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900 return false;
1901 }
1902
1903 // It's hasn't changed. Nothing to do.
1904 return true;
1905}
1906
1907bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001908 const DataContentDescription* content,
1909 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
1911 (content->protocol() == kMediaProtocolDtlsSctp));
1912 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001913 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914}
1915
1916bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001917 ContentAction action,
1918 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001919 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001920 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 LOG(LS_INFO) << "Setting local data description";
1922
1923 const DataContentDescription* data =
1924 static_cast<const DataContentDescription*>(content);
1925 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001926 if (!data) {
1927 SafeSetError("Can't find data content in local description.", error_desc);
1928 return false;
1929 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001931 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 return false;
1933 }
1934
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001935 if (data_channel_type_ == DCT_RTP) {
1936 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1937 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938 }
1939 }
1940
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001941 // FYI: We send the SCTP port number (not to be confused with the
1942 // underlying UDP port number) as a codec parameter. So even SCTP
1943 // data channels need codecs.
1944 DataRecvParameters recv_params = last_recv_params_;
1945 RtpParametersFromMediaDescription(data, &recv_params);
1946 if (!media_channel()->SetRecvParameters(recv_params)) {
1947 SafeSetError("Failed to set remote data description recv parameters.",
1948 error_desc);
1949 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001951 if (data_channel_type_ == DCT_RTP) {
1952 for (const DataCodec& codec : data->codecs()) {
1953 bundle_filter()->AddPayloadType(codec.id);
1954 }
1955 }
1956 last_recv_params_ = recv_params;
1957
1958 // TODO(pthatcher): Move local streams into DataSendParameters, and
1959 // only give it to the media channel once we have a remote
1960 // description too (without a remote description, we won't be able
1961 // to send them anyway).
1962 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
1963 SafeSetError("Failed to set local data description streams.", error_desc);
1964 return false;
1965 }
1966
1967 set_local_content_direction(content->direction());
1968 ChangeState();
1969 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970}
1971
1972bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001973 ContentAction action,
1974 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001975 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001976 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977
1978 const DataContentDescription* data =
1979 static_cast<const DataContentDescription*>(content);
1980 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001981 if (!data) {
1982 SafeSetError("Can't find data content in remote description.", error_desc);
1983 return false;
1984 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001986 // If the remote data doesn't have codecs and isn't an update, it
1987 // must be empty, so ignore it.
1988 if (!data->has_codecs() && action != CA_UPDATE) {
1989 return true;
1990 }
1991
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001992 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 return false;
1994 }
1995
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001996 LOG(LS_INFO) << "Setting remote data description";
1997 if (data_channel_type_ == DCT_RTP &&
1998 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1999 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000 }
2001
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002002
2003 DataSendParameters send_params = last_send_params_;
2004 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2005 if (!media_channel()->SetSendParameters(send_params)) {
2006 SafeSetError("Failed to set remote data description send parameters.",
2007 error_desc);
2008 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002010 last_send_params_ = send_params;
2011
2012 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2013 // and only give it to the media channel once we have a local
2014 // description too (without a local description, we won't be able to
2015 // recv them anyway).
2016 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2017 SafeSetError("Failed to set remote data description streams.",
2018 error_desc);
2019 return false;
2020 }
2021
2022 set_remote_content_direction(content->direction());
2023 ChangeState();
2024 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025}
2026
2027void DataChannel::ChangeState() {
2028 // Render incoming data if we're the active call, and we have the local
2029 // content. We receive data on the default channel and multiplexed streams.
2030 bool recv = IsReadyToReceive();
2031 if (!media_channel()->SetReceive(recv)) {
2032 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2033 }
2034
2035 // Send outgoing data if we're the active call, we have the remote content,
2036 // and we have had some form of connectivity.
2037 bool send = IsReadyToSend();
2038 if (!media_channel()->SetSend(send)) {
2039 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2040 }
2041
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002042 // Trigger SignalReadyToSendData asynchronously.
2043 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044
2045 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2046}
2047
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002048void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 switch (pmsg->message_id) {
2050 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002051 DataChannelReadyToSendMessageData* data =
2052 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002053 ready_to_send_data_ = data->data();
2054 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055 delete data;
2056 break;
2057 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 case MSG_DATARECEIVED: {
2059 DataReceivedMessageData* data =
2060 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2061 SignalDataReceived(this, data->params, data->payload);
2062 delete data;
2063 break;
2064 }
2065 case MSG_CHANNEL_ERROR: {
2066 const DataChannelErrorMessageData* data =
2067 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 delete data;
2069 break;
2070 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002071 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002072 rtc::TypedMessageData<uint32_t>* data =
2073 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002074 SignalStreamClosedRemotely(data->data());
2075 delete data;
2076 break;
2077 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 default:
2079 BaseChannel::OnMessage(pmsg);
2080 break;
2081 }
2082}
2083
2084void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002085 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 SignalConnectionMonitor(this, infos);
2087}
2088
2089void DataChannel::StartMediaMonitor(int cms) {
2090 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002091 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 media_monitor_->SignalUpdate.connect(
2093 this, &DataChannel::OnMediaMonitorUpdate);
2094 media_monitor_->Start(cms);
2095}
2096
2097void DataChannel::StopMediaMonitor() {
2098 if (media_monitor_) {
2099 media_monitor_->Stop();
2100 media_monitor_->SignalUpdate.disconnect(this);
2101 media_monitor_.reset();
2102 }
2103}
2104
2105void DataChannel::OnMediaMonitorUpdate(
2106 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2107 ASSERT(media_channel == this->media_channel());
2108 SignalMediaMonitor(this, info);
2109}
2110
2111void DataChannel::OnDataReceived(
2112 const ReceiveDataParams& params, const char* data, size_t len) {
2113 DataReceivedMessageData* msg = new DataReceivedMessageData(
2114 params, data, len);
2115 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2116}
2117
Peter Boström0c4e06b2015-10-07 12:23:21 +02002118void DataChannel::OnDataChannelError(uint32_t ssrc,
2119 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2121 ssrc, err);
2122 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2123}
2124
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002125void DataChannel::OnDataChannelReadyToSend(bool writable) {
2126 // This is usded for congestion control to indicate that the stream is ready
2127 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2128 // that the transport channel is ready.
2129 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2130 new DataChannelReadyToSendMessageData(writable));
2131}
2132
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002133void DataChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2134 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135}
2136
2137bool DataChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08002138 return (data_channel_type_ == DCT_RTP) && BaseChannel::ShouldSetupDtlsSrtp();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139}
2140
Peter Boström0c4e06b2015-10-07 12:23:21 +02002141void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2142 rtc::TypedMessageData<uint32_t>* message =
2143 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002144 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2145}
2146
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147} // namespace cricket