blob: 5f4c275ff2610dc34547d0cef244c2c1e3f8de2c [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
kwiberg0eb15ed2015-12-17 03:04:15 -080028#include <utility>
29
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030#include "talk/session/media/channel.h"
31
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000032#include "talk/media/base/constants.h"
33#include "talk/media/base/rtputils.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000034#include "talk/session/media/channelmanager.h"
Tommif888bb52015-12-12 01:37:01 +010035#include "webrtc/audio/audio_sink.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000036#include "webrtc/base/bind.h"
37#include "webrtc/base/buffer.h"
38#include "webrtc/base/byteorder.h"
39#include "webrtc/base/common.h"
40#include "webrtc/base/dscp.h"
41#include "webrtc/base/logging.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010042#include "webrtc/base/trace_event.h"
43#include "webrtc/p2p/base/transportchannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000046using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000047
Tommif888bb52015-12-12 01:37:01 +010048namespace {
49// See comment below for why we need to use a pointer to a scoped_ptr.
50bool SetRawAudioSink_w(VoiceMediaChannel* channel,
51 uint32_t ssrc,
52 rtc::scoped_ptr<webrtc::AudioSinkInterface>* sink) {
53 channel->SetRawAudioSink(ssrc, std::move(*sink));
54 return true;
55}
56} // namespace
57
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000059 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060 MSG_SCREENCASTWINDOWEVENT,
61 MSG_RTPPACKET,
62 MSG_RTCPPACKET,
63 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000067 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068};
69
70// Value specified in RFC 5764.
71static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
72
73static const int kAgcMinus10db = -10;
74
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000075static void SafeSetError(const std::string& message, std::string* error_desc) {
76 if (error_desc) {
77 *error_desc = message;
78 }
79}
80
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000081struct PacketMessageData : public rtc::MessageData {
82 rtc::Buffer packet;
stefanc1aeaf02015-10-15 07:26:07 -070083 rtc::PacketOptions options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084};
85
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000086struct ScreencastEventMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020087 ScreencastEventMessageData(uint32_t s, rtc::WindowEvent we)
88 : ssrc(s), event(we) {}
89 uint32_t ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000090 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091};
92
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020094 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020096 : ssrc(in_ssrc), error(in_error) {}
97 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 VoiceMediaChannel::Error error;
99};
100
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000101struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200104 : ssrc(in_ssrc), error(in_error) {}
105 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 VideoMediaChannel::Error error;
107};
108
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000109struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200110 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200112 : ssrc(in_ssrc), error(in_error) {}
113 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 DataMediaChannel::Error error;
115};
116
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000118struct VideoChannel::ScreencastDetailsData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200119 explicit ScreencastDetailsData(uint32_t s)
120 : ssrc(s), fps(0), screencast_max_pixels(0) {}
121 uint32_t ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000122 int fps;
123 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124};
125
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126static const char* PacketType(bool rtcp) {
127 return (!rtcp) ? "RTP" : "RTCP";
128}
129
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000130static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 // Check the packet size. We could check the header too if needed.
132 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000133 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
134 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135}
136
137static bool IsReceiveContentDirection(MediaContentDirection direction) {
138 return direction == MD_SENDRECV || direction == MD_RECVONLY;
139}
140
141static bool IsSendContentDirection(MediaContentDirection direction) {
142 return direction == MD_SENDRECV || direction == MD_SENDONLY;
143}
144
145static const MediaContentDescription* GetContentDescription(
146 const ContentInfo* cinfo) {
147 if (cinfo == NULL)
148 return NULL;
149 return static_cast<const MediaContentDescription*>(cinfo->description);
150}
151
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700152template <class Codec>
153void RtpParametersFromMediaDescription(
154 const MediaContentDescriptionImpl<Codec>* desc,
155 RtpParameters<Codec>* params) {
156 // TODO(pthatcher): Remove this once we're sure no one will give us
157 // a description without codecs (currently a CA_UPDATE with just
158 // streams can).
159 if (desc->has_codecs()) {
160 params->codecs = desc->codecs();
161 }
162 // TODO(pthatcher): See if we really need
163 // rtp_header_extensions_set() and remove it if we don't.
164 if (desc->rtp_header_extensions_set()) {
165 params->extensions = desc->rtp_header_extensions();
166 }
deadbeef13871492015-12-09 12:37:51 -0800167 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700168}
169
170template <class Codec, class Options>
171void RtpSendParametersFromMediaDescription(
172 const MediaContentDescriptionImpl<Codec>* desc,
173 RtpSendParameters<Codec, Options>* send_params) {
174 RtpParametersFromMediaDescription(desc, send_params);
175 send_params->max_bandwidth_bps = desc->bandwidth();
176}
177
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000178BaseChannel::BaseChannel(rtc::Thread* thread,
deadbeefcbecd352015-09-23 11:50:27 -0700179 MediaChannel* media_channel,
180 TransportController* transport_controller,
181 const std::string& content_name,
182 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 : worker_thread_(thread),
deadbeefcbecd352015-09-23 11:50:27 -0700184 transport_controller_(transport_controller),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 media_channel_(media_channel),
186 content_name_(content_name),
deadbeefcbecd352015-09-23 11:50:27 -0700187 rtcp_transport_enabled_(rtcp),
188 transport_channel_(nullptr),
189 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 enabled_(false),
191 writable_(false),
192 rtp_ready_to_send_(false),
193 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 was_ever_writable_(false),
195 local_content_direction_(MD_INACTIVE),
196 remote_content_direction_(MD_INACTIVE),
197 has_received_packet_(false),
198 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000199 secure_required_(false),
200 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000201 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 LOG(LS_INFO) << "Created channel for " << content_name;
203}
204
205BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000206 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000207 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 StopConnectionMonitor();
209 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000210 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 // We must destroy the media channel before the transport channel, otherwise
212 // the media channel may try to send on the dead transport channel. NULLing
213 // is not an effective strategy since the sends will come on another thread.
214 delete media_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700215 // Note that we don't just call set_transport_channel(nullptr) because that
216 // would call a pure virtual method which we can't do from a destructor.
217 if (transport_channel_) {
218 DisconnectFromTransportChannel(transport_channel_);
219 transport_controller_->DestroyTransportChannel_w(
220 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
221 }
222 if (rtcp_transport_channel_) {
223 DisconnectFromTransportChannel(rtcp_transport_channel_);
224 transport_controller_->DestroyTransportChannel_w(
225 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
226 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 LOG(LS_INFO) << "Destroyed channel";
228}
229
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000230bool BaseChannel::Init() {
deadbeefcbecd352015-09-23 11:50:27 -0700231 if (!SetTransport(content_name())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 return false;
233 }
234
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800235 if (!SetDtlsSrtpCryptoSuites(transport_channel(), false)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000236 return false;
237 }
deadbeefcbecd352015-09-23 11:50:27 -0700238 if (rtcp_transport_enabled() &&
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800239 !SetDtlsSrtpCryptoSuites(rtcp_transport_channel(), true)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000240 return false;
241 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242
wu@webrtc.orgde305012013-10-31 15:40:38 +0000243 // Both RTP and RTCP channels are set, we can call SetInterface on
244 // media channel and it can set network options.
245 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 return true;
247}
248
wu@webrtc.org78187522013-10-07 23:32:02 +0000249void BaseChannel::Deinit() {
250 media_channel_->SetInterface(NULL);
251}
252
deadbeefcbecd352015-09-23 11:50:27 -0700253bool BaseChannel::SetTransport(const std::string& transport_name) {
254 return worker_thread_->Invoke<bool>(
255 Bind(&BaseChannel::SetTransport_w, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000256}
257
deadbeefcbecd352015-09-23 11:50:27 -0700258bool BaseChannel::SetTransport_w(const std::string& transport_name) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000259 ASSERT(worker_thread_ == rtc::Thread::Current());
260
deadbeefcbecd352015-09-23 11:50:27 -0700261 if (transport_name == transport_name_) {
262 // Nothing to do if transport name isn't changing
263 return true;
264 }
265
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800266 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
267 // changes and wait until the DTLS handshake is complete to set the newly
268 // negotiated parameters.
269 if (ShouldSetupDtlsSrtp()) {
guoweis46383312015-12-17 16:45:59 -0800270 // Set |writable_| to false such that UpdateWritableState_w can set up
271 // DTLS-SRTP when the writable_ becomes true again.
272 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800273 srtp_filter_.ResetParams();
274 }
275
guoweis46383312015-12-17 16:45:59 -0800276 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
deadbeefcbecd352015-09-23 11:50:27 -0700277 if (rtcp_transport_enabled()) {
278 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
279 << " on " << transport_name << " transport ";
guoweis46383312015-12-17 16:45:59 -0800280 set_rtcp_transport_channel(
281 transport_controller_->CreateTransportChannel_w(
282 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP),
283 false /* update_writablity */);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000284 if (!rtcp_transport_channel()) {
285 return false;
286 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000287 }
288
guoweis46383312015-12-17 16:45:59 -0800289 // We're not updating the writablity during the transition state.
290 set_transport_channel(transport_controller_->CreateTransportChannel_w(
291 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
292 if (!transport_channel()) {
293 return false;
294 }
295
296 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
297 if (rtcp_transport_enabled()) {
298 // We can only update the RTCP ready to send after set_transport_channel has
299 // handled channel writability.
300 SetReadyToSend(
301 true, rtcp_transport_channel() && rtcp_transport_channel()->writable());
302 }
deadbeefcbecd352015-09-23 11:50:27 -0700303 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000304 return true;
305}
306
307void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
308 ASSERT(worker_thread_ == rtc::Thread::Current());
309
310 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700311 if (!old_tc && !new_tc) {
312 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000313 return;
314 }
deadbeefcbecd352015-09-23 11:50:27 -0700315 ASSERT(old_tc != new_tc);
316
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000317 if (old_tc) {
318 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700319 transport_controller_->DestroyTransportChannel_w(
320 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000321 }
322
323 transport_channel_ = new_tc;
324
325 if (new_tc) {
326 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700327 for (const auto& pair : socket_options_) {
328 new_tc->SetOption(pair.first, pair.second);
329 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000330 }
deadbeefcbecd352015-09-23 11:50:27 -0700331
332 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
333 // setting new channel
334 UpdateWritableState_w();
335 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000336}
337
guoweis46383312015-12-17 16:45:59 -0800338void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc,
339 bool update_writablity) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000340 ASSERT(worker_thread_ == rtc::Thread::Current());
341
342 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700343 if (!old_tc && !new_tc) {
344 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000345 return;
346 }
deadbeefcbecd352015-09-23 11:50:27 -0700347 ASSERT(old_tc != new_tc);
348
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000349 if (old_tc) {
350 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700351 transport_controller_->DestroyTransportChannel_w(
352 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000353 }
354
355 rtcp_transport_channel_ = new_tc;
356
357 if (new_tc) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800358 RTC_CHECK(!(ShouldSetupDtlsSrtp() && srtp_filter_.IsActive()))
359 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
360 << "should never happen.";
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000361 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700362 for (const auto& pair : rtcp_socket_options_) {
363 new_tc->SetOption(pair.first, pair.second);
364 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000365 }
deadbeefcbecd352015-09-23 11:50:27 -0700366
guoweis46383312015-12-17 16:45:59 -0800367 if (update_writablity) {
368 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
369 // setting new channel
370 UpdateWritableState_w();
371 SetReadyToSend(true, new_tc && new_tc->writable());
372 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000373}
374
375void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
376 ASSERT(worker_thread_ == rtc::Thread::Current());
377
378 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
379 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
380 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800381 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000382}
383
384void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
385 ASSERT(worker_thread_ == rtc::Thread::Current());
386
387 tc->SignalWritableState.disconnect(this);
388 tc->SignalReadPacket.disconnect(this);
389 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800390 tc->SignalDtlsState.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000391}
392
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000394 worker_thread_->Invoke<void>(Bind(
395 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
396 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 return true;
398}
399
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000401 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402}
403
Peter Boström0c4e06b2015-10-07 12:23:21 +0200404bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000405 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406}
407
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000408bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000409 return InvokeOnWorker(
410 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000411}
412
Peter Boström0c4e06b2015-10-07 12:23:21 +0200413bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000414 return InvokeOnWorker(
415 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000416}
417
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000419 ContentAction action,
420 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100421 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000422 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
423 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424}
425
426bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000427 ContentAction action,
428 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100429 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000430 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
431 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432}
433
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000435 // We pass in the BaseChannel instead of the transport_channel_
436 // because if the transport_channel_ changes, the ConnectionMonitor
437 // would be pointing to the wrong TransportChannel.
438 connection_monitor_.reset(new ConnectionMonitor(
439 this, worker_thread(), rtc::Thread::Current()));
440 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000442 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443}
444
445void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000446 if (connection_monitor_) {
447 connection_monitor_->Stop();
448 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 }
450}
451
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000452bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
453 ASSERT(worker_thread_ == rtc::Thread::Current());
454 return transport_channel_->GetStats(infos);
455}
456
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457bool BaseChannel::IsReadyToReceive() const {
458 // Receive data if we are enabled and have local content,
459 return enabled() && IsReceiveContentDirection(local_content_direction_);
460}
461
462bool BaseChannel::IsReadyToSend() const {
463 // Send outgoing data if we are enabled, have local and remote content,
464 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800465 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 IsSendContentDirection(local_content_direction_) &&
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800467 was_ever_writable() &&
468 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469}
470
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000471bool BaseChannel::SendPacket(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700472 const rtc::PacketOptions& options) {
473 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474}
475
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000476bool BaseChannel::SendRtcp(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700477 const rtc::PacketOptions& options) {
478 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479}
480
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000481int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000483 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000485 case ST_RTP:
486 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700487 socket_options_.push_back(
488 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000489 break;
490 case ST_RTCP:
491 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700492 rtcp_socket_options_.push_back(
493 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000494 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000496 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497}
498
499void BaseChannel::OnWritableState(TransportChannel* channel) {
500 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
deadbeefcbecd352015-09-23 11:50:27 -0700501 UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502}
503
504void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000505 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000506 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000507 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100508 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000510 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511
512 // When using RTCP multiplexing we might get RTCP packets on the RTP
513 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
514 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000515 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000516 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517}
518
519void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700520 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
521 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522}
523
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800524void BaseChannel::OnDtlsState(TransportChannel* channel,
525 DtlsTransportState state) {
526 if (!ShouldSetupDtlsSrtp()) {
527 return;
528 }
529
530 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
531 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
532 // cover other scenarios like the whole channel is writable (not just this
533 // TransportChannel) or when TransportChannel is attached after DTLS is
534 // negotiated.
535 if (state != DTLS_TRANSPORT_CONNECTED) {
536 srtp_filter_.ResetParams();
537 }
538}
539
deadbeefcbecd352015-09-23 11:50:27 -0700540void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
541 if (rtcp) {
542 rtcp_ready_to_send_ = ready;
543 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544 rtp_ready_to_send_ = ready;
545 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546
deadbeefcbecd352015-09-23 11:50:27 -0700547 if (rtp_ready_to_send_ &&
548 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
549 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
torbjornga81a42f2015-09-23 02:16:58 -0700550 // Notify the MediaChannel when both rtp and rtcp channel can send.
551 media_channel_->OnReadyToSend(true);
deadbeefcbecd352015-09-23 11:50:27 -0700552 } else {
553 // Notify the MediaChannel when either rtp or rtcp channel can't send.
554 media_channel_->OnReadyToSend(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 }
556}
557
558bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
559 const char* data, size_t len) {
560 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000561 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562}
563
stefanc1aeaf02015-10-15 07:26:07 -0700564bool BaseChannel::SendPacket(bool rtcp,
565 rtc::Buffer* packet,
566 const rtc::PacketOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 // SendPacket gets called from MediaEngine, typically on an encoder thread.
568 // If the thread is not our worker thread, we will post to our worker
569 // so that the real work happens on our worker. This avoids us having to
570 // synchronize access to all the pieces of the send path, including
571 // SRTP and the inner workings of the transport channels.
572 // The only downside is that we can't return a proper failure code if
573 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000574 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 // Avoid a copy by transferring the ownership of the packet data.
576 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
577 PacketMessageData* data = new PacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800578 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700579 data->options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 worker_thread_->Post(this, message_id, data);
581 return true;
582 }
583
584 // Now that we are on the correct thread, ensure we have a place to send this
585 // packet before doing anything. (We might get RTCP packets that we don't
586 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
587 // transport.
588 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
589 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000590 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 return false;
592 }
593
594 // Protect ourselves against crazy data.
595 if (!ValidPacket(rtcp, packet)) {
596 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000597 << PacketType(rtcp)
598 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 return false;
600 }
601
stefanc1aeaf02015-10-15 07:26:07 -0700602 rtc::PacketOptions updated_options;
603 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000604 // Protect if needed.
605 if (srtp_filter_.IsActive()) {
606 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200607 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000608 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000610 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
611 // inside libsrtp for a RTP packet. A external HMAC module will be writing
612 // a fake HMAC value. This is ONLY done for a RTP packet.
613 // Socket layer will update rtp sendtime extension header if present in
614 // packet with current time before updating the HMAC.
615#if !defined(ENABLE_EXTERNAL_AUTH)
616 res = srtp_filter_.ProtectRtp(
617 data, len, static_cast<int>(packet->capacity()), &len);
618#else
stefanc1aeaf02015-10-15 07:26:07 -0700619 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000620 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000621 res = srtp_filter_.ProtectRtp(
622 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700623 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000624 // If protection succeeds, let's get auth params from srtp.
625 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200626 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000627 int key_len;
628 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700629 &auth_key, &key_len,
630 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000631 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700632 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
633 updated_options.packet_time_params.srtp_auth_key.assign(
634 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000635 }
636 }
637#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 if (!res) {
639 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200640 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 GetRtpSeqNum(data, len, &seq_num);
642 GetRtpSsrc(data, len, &ssrc);
643 LOG(LS_ERROR) << "Failed to protect " << content_name_
644 << " RTP packet: size=" << len
645 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
646 return false;
647 }
648 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000649 res = srtp_filter_.ProtectRtcp(data, len,
650 static_cast<int>(packet->capacity()),
651 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 if (!res) {
653 int type = -1;
654 GetRtcpType(data, len, &type);
655 LOG(LS_ERROR) << "Failed to protect " << content_name_
656 << " RTCP packet: size=" << len << ", type=" << type;
657 return false;
658 }
659 }
660
661 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000662 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 } else if (secure_required_) {
664 // This is a double check for something that supposedly can't happen.
665 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
666 << " packet when SRTP is inactive and crypto is required";
667
668 ASSERT(false);
669 return false;
670 }
671
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000673 int ret =
stefanc1aeaf02015-10-15 07:26:07 -0700674 channel->SendPacket(packet->data<char>(), packet->size(), updated_options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000675 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
676 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 if (channel->GetError() == EWOULDBLOCK) {
678 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700679 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 }
681 return false;
682 }
683 return true;
684}
685
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000686bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 // Protect ourselves against crazy data.
688 if (!ValidPacket(rtcp, packet)) {
689 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000690 << PacketType(rtcp)
691 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 return false;
693 }
pbos482b12e2015-11-16 10:19:58 -0800694 if (rtcp) {
695 // Permit all (seemingly valid) RTCP packets.
696 return true;
697 }
698 // Check whether we handle this payload.
699 return bundle_filter_.DemuxPacket(packet->data<uint8_t>(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700}
701
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000702void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
703 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 if (!WantsPacket(rtcp, packet)) {
705 return;
706 }
707
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000708 // We are only interested in the first rtp packet because that
709 // indicates the media has started flowing.
710 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 has_received_packet_ = true;
712 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
713 }
714
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 // Unprotect the packet, if needed.
716 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200717 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000718 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 bool res;
720 if (!rtcp) {
721 res = srtp_filter_.UnprotectRtp(data, len, &len);
722 if (!res) {
723 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200724 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 GetRtpSeqNum(data, len, &seq_num);
726 GetRtpSsrc(data, len, &ssrc);
727 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
728 << " RTP packet: size=" << len
729 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
730 return;
731 }
732 } else {
733 res = srtp_filter_.UnprotectRtcp(data, len, &len);
734 if (!res) {
735 int type = -1;
736 GetRtcpType(data, len, &type);
737 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
738 << " RTCP packet: size=" << len << ", type=" << type;
739 return;
740 }
741 }
742
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000743 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 } else if (secure_required_) {
745 // Our session description indicates that SRTP is required, but we got a
746 // packet before our SRTP filter is active. This means either that
747 // a) we got SRTP packets before we received the SDES keys, in which case
748 // we can't decrypt it anyway, or
749 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
750 // channels, so we haven't yet extracted keys, even if DTLS did complete
751 // on the channel that the packets are being sent on. It's really good
752 // practice to wait for both RTP and RTCP to be good to go before sending
753 // media, to prevent weird failure modes, so it's fine for us to just eat
754 // packets here. This is all sidestepped if RTCP mux is used anyway.
755 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
756 << " packet when SRTP is inactive and crypto is required";
757 return;
758 }
759
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 // Push it down to the media channel.
761 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000762 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000764 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 }
766}
767
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000768bool BaseChannel::PushdownLocalDescription(
769 const SessionDescription* local_desc, ContentAction action,
770 std::string* error_desc) {
771 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772 const MediaContentDescription* content_desc =
773 GetContentDescription(content_info);
774 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000775 !SetLocalContent(content_desc, action, error_desc)) {
776 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
777 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000779 return true;
780}
781
782bool BaseChannel::PushdownRemoteDescription(
783 const SessionDescription* remote_desc, ContentAction action,
784 std::string* error_desc) {
785 const ContentInfo* content_info = GetFirstContent(remote_desc);
786 const MediaContentDescription* content_desc =
787 GetContentDescription(content_info);
788 if (content_desc && content_info && !content_info->rejected &&
789 !SetRemoteContent(content_desc, action, error_desc)) {
790 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
791 return false;
792 }
793 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794}
795
796void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000797 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 if (enabled_)
799 return;
800
801 LOG(LS_INFO) << "Channel enabled";
802 enabled_ = true;
803 ChangeState();
804}
805
806void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000807 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 if (!enabled_)
809 return;
810
811 LOG(LS_INFO) << "Channel disabled";
812 enabled_ = false;
813 ChangeState();
814}
815
deadbeefcbecd352015-09-23 11:50:27 -0700816void BaseChannel::UpdateWritableState_w() {
817 if (transport_channel_ && transport_channel_->writable() &&
818 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
819 ChannelWritable_w();
820 } else {
821 ChannelNotWritable_w();
822 }
823}
824
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000826 ASSERT(worker_thread_ == rtc::Thread::Current());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800827 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800829 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830
deadbeefcbecd352015-09-23 11:50:27 -0700831 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 << (was_ever_writable_ ? "" : " for the first time");
833
834 std::vector<ConnectionInfo> infos;
835 transport_channel_->GetStats(&infos);
836 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
837 it != infos.end(); ++it) {
838 if (it->best_connection) {
839 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
840 << "->" << it->remote_candidate.ToSensitiveString();
841 break;
842 }
843 }
844
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 was_ever_writable_ = true;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800846 MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 writable_ = true;
848 ChangeState();
849}
850
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000851void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
852 ASSERT(worker_thread() == rtc::Thread::Current());
853 signaling_thread()->Invoke<void>(Bind(
854 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
855}
856
857void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
858 ASSERT(signaling_thread() == rtc::Thread::Current());
859 SignalDtlsSetupFailure(this, rtcp);
860}
861
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800862bool BaseChannel::SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp) {
863 std::vector<int> crypto_suites;
864 // We always use the default SRTP crypto suites for RTCP, but we may use
865 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866 if (!rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800867 GetSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800869 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800871 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872}
873
874bool BaseChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800875 // Since DTLS is applied to all channels, checking RTP should be enough.
876 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877}
878
879// This function returns true if either DTLS-SRTP is not in use
880// *or* DTLS-SRTP is successfully set up.
881bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
882 bool ret = false;
883
deadbeefcbecd352015-09-23 11:50:27 -0700884 TransportChannel* channel =
885 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800887 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800889 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800891 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
892 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 return false;
894 }
895
896 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
897 << content_name() << " "
898 << PacketType(rtcp_channel);
899
900 // OK, we're now doing DTLS (RFC 5764)
901 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
902 SRTP_MASTER_KEY_SALT_LEN * 2);
903
904 // RFC 5705 exporter using the RFC 5764 parameters
905 if (!channel->ExportKeyingMaterial(
906 kDtlsSrtpExporterLabel,
907 NULL, 0, false,
908 &dtls_buffer[0], dtls_buffer.size())) {
909 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
910 ASSERT(false); // This should never happen
911 return false;
912 }
913
914 // Sync up the keys with the DTLS-SRTP interface
915 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
916 SRTP_MASTER_KEY_SALT_LEN);
917 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
918 SRTP_MASTER_KEY_SALT_LEN);
919 size_t offset = 0;
920 memcpy(&client_write_key[0], &dtls_buffer[offset],
921 SRTP_MASTER_KEY_KEY_LEN);
922 offset += SRTP_MASTER_KEY_KEY_LEN;
923 memcpy(&server_write_key[0], &dtls_buffer[offset],
924 SRTP_MASTER_KEY_KEY_LEN);
925 offset += SRTP_MASTER_KEY_KEY_LEN;
926 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
927 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
928 offset += SRTP_MASTER_KEY_SALT_LEN;
929 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
930 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
931
932 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000933 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000934 if (!channel->GetSslRole(&role)) {
935 LOG(LS_WARNING) << "GetSslRole failed";
936 return false;
937 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000939 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 send_key = &server_write_key;
941 recv_key = &client_write_key;
942 } else {
943 send_key = &client_write_key;
944 recv_key = &server_write_key;
945 }
946
947 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800948 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
949 static_cast<int>(send_key->size()),
950 selected_crypto_suite, &(*recv_key)[0],
951 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800953 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
954 static_cast<int>(send_key->size()),
955 selected_crypto_suite, &(*recv_key)[0],
956 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 }
958
959 if (!ret)
960 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
961 else
962 dtls_keyed_ = true;
963
964 return ret;
965}
966
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800967void BaseChannel::MaybeSetupDtlsSrtp_w() {
968 if (srtp_filter_.IsActive()) {
969 return;
970 }
971
972 if (!ShouldSetupDtlsSrtp()) {
973 return;
974 }
975
976 if (!SetupDtlsSrtp(false)) {
977 SignalDtlsSetupFailure_w(false);
978 return;
979 }
980
981 if (rtcp_transport_channel_) {
982 if (!SetupDtlsSrtp(true)) {
983 SignalDtlsSetupFailure_w(true);
984 return;
985 }
986 }
987}
988
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000990 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 if (!writable_)
992 return;
993
deadbeefcbecd352015-09-23 11:50:27 -0700994 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 writable_ = false;
996 ChangeState();
997}
998
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700999bool BaseChannel::SetRtpTransportParameters_w(
1000 const MediaContentDescription* content,
1001 ContentAction action,
1002 ContentSource src,
1003 std::string* error_desc) {
1004 if (action == CA_UPDATE) {
1005 // These parameters never get changed by a CA_UDPATE.
1006 return true;
1007 }
1008
1009 // Cache secure_required_ for belt and suspenders check on SendPacket
1010 if (src == CS_LOCAL) {
1011 set_secure_required(content->crypto_required() != CT_NONE);
1012 }
1013
1014 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
1015 return false;
1016 }
1017
1018 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
1019 return false;
1020 }
1021
1022 return true;
1023}
1024
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001025// |dtls| will be set to true if DTLS is active for transport channel and
1026// crypto is empty.
1027bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001028 bool* dtls,
1029 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001030 *dtls = transport_channel_->IsDtlsActive();
1031 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001032 SafeSetError("Cryptos must be empty when DTLS is active.",
1033 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001034 return false;
1035 }
1036 return true;
1037}
1038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001040 ContentAction action,
1041 ContentSource src,
1042 std::string* error_desc) {
1043 if (action == CA_UPDATE) {
1044 // no crypto params.
1045 return true;
1046 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001048 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001049 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
1050 if (!ret) {
1051 return false;
1052 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 switch (action) {
1054 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001055 // If DTLS is already active on the channel, we could be renegotiating
1056 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001057 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001058 ret = srtp_filter_.SetOffer(cryptos, src);
1059 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 break;
1061 case CA_PRANSWER:
1062 // If we're doing DTLS-SRTP, we don't want to update the filter
1063 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001064 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1066 }
1067 break;
1068 case CA_ANSWER:
1069 // If we're doing DTLS-SRTP, we don't want to update the filter
1070 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001071 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 ret = srtp_filter_.SetAnswer(cryptos, src);
1073 }
1074 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 default:
1076 break;
1077 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001078 if (!ret) {
1079 SafeSetError("Failed to setup SRTP filter.", error_desc);
1080 return false;
1081 }
1082 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083}
1084
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001085void BaseChannel::ActivateRtcpMux() {
1086 worker_thread_->Invoke<void>(Bind(
1087 &BaseChannel::ActivateRtcpMux_w, this));
1088}
1089
1090void BaseChannel::ActivateRtcpMux_w() {
1091 if (!rtcp_mux_filter_.IsActive()) {
1092 rtcp_mux_filter_.SetActive();
guoweis46383312015-12-17 16:45:59 -08001093 set_rtcp_transport_channel(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001094 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001095 }
1096}
1097
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001099 ContentSource src,
1100 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 bool ret = false;
1102 switch (action) {
1103 case CA_OFFER:
1104 ret = rtcp_mux_filter_.SetOffer(enable, src);
1105 break;
1106 case CA_PRANSWER:
1107 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1108 break;
1109 case CA_ANSWER:
1110 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1111 if (ret && rtcp_mux_filter_.IsActive()) {
1112 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001113 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1114 << " by destroying RTCP transport channel for "
1115 << transport_name();
guoweis46383312015-12-17 16:45:59 -08001116 set_rtcp_transport_channel(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001117 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 }
1119 break;
1120 case CA_UPDATE:
1121 // No RTCP mux info.
1122 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001123 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 default:
1125 break;
1126 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001127 if (!ret) {
1128 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1129 return false;
1130 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1132 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1133 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001134 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 // If the RTP transport is already writable, then so are we.
1136 if (transport_channel_->writable()) {
1137 ChannelWritable_w();
1138 }
1139 }
1140
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001141 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142}
1143
1144bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001145 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001146 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147}
1148
Peter Boström0c4e06b2015-10-07 12:23:21 +02001149bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001150 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151 return media_channel()->RemoveRecvStream(ssrc);
1152}
1153
1154bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001155 ContentAction action,
1156 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1158 action == CA_PRANSWER || action == CA_UPDATE))
1159 return false;
1160
1161 // If this is an update, streams only contain streams that have changed.
1162 if (action == CA_UPDATE) {
1163 for (StreamParamsVec::const_iterator it = streams.begin();
1164 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001165 const StreamParams* existing_stream =
1166 GetStreamByIds(local_streams_, it->groupid, it->id);
1167 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 if (media_channel()->AddSendStream(*it)) {
1169 local_streams_.push_back(*it);
1170 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1171 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001172 std::ostringstream desc;
1173 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1174 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 return false;
1176 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001177 } else if (existing_stream && !it->has_ssrcs()) {
1178 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001179 std::ostringstream desc;
1180 desc << "Failed to remove send stream with ssrc "
1181 << it->first_ssrc() << ".";
1182 SafeSetError(desc.str(), error_desc);
1183 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001185 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 } else {
1187 LOG(LS_WARNING) << "Ignore unsupported stream update";
1188 }
1189 }
1190 return true;
1191 }
1192 // Else streams are all the streams we want to send.
1193
1194 // Check for streams that have been removed.
1195 bool ret = true;
1196 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1197 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001198 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001200 std::ostringstream desc;
1201 desc << "Failed to remove send stream with ssrc "
1202 << it->first_ssrc() << ".";
1203 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 ret = false;
1205 }
1206 }
1207 }
1208 // Check for new streams.
1209 for (StreamParamsVec::const_iterator it = streams.begin();
1210 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001211 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001213 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001215 std::ostringstream desc;
1216 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1217 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 ret = false;
1219 }
1220 }
1221 }
1222 local_streams_ = streams;
1223 return ret;
1224}
1225
1226bool BaseChannel::UpdateRemoteStreams_w(
1227 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001228 ContentAction action,
1229 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1231 action == CA_PRANSWER || action == CA_UPDATE))
1232 return false;
1233
1234 // If this is an update, streams only contain streams that have changed.
1235 if (action == CA_UPDATE) {
1236 for (StreamParamsVec::const_iterator it = streams.begin();
1237 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001238 const StreamParams* existing_stream =
1239 GetStreamByIds(remote_streams_, it->groupid, it->id);
1240 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 if (AddRecvStream_w(*it)) {
1242 remote_streams_.push_back(*it);
1243 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1244 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001245 std::ostringstream desc;
1246 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1247 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 return false;
1249 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001250 } else if (existing_stream && !it->has_ssrcs()) {
1251 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001252 std::ostringstream desc;
1253 desc << "Failed to remove remote stream with ssrc "
1254 << it->first_ssrc() << ".";
1255 SafeSetError(desc.str(), error_desc);
1256 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001258 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 } else {
1260 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001261 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 << " new stream = " << it->ToString();
1263 }
1264 }
1265 return true;
1266 }
1267 // Else streams are all the streams we want to receive.
1268
1269 // Check for streams that have been removed.
1270 bool ret = true;
1271 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1272 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001273 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001275 std::ostringstream desc;
1276 desc << "Failed to remove remote stream with ssrc "
1277 << it->first_ssrc() << ".";
1278 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 ret = false;
1280 }
1281 }
1282 }
1283 // Check for new streams.
1284 for (StreamParamsVec::const_iterator it = streams.begin();
1285 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001286 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 if (AddRecvStream_w(*it)) {
1288 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1289 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001290 std::ostringstream desc;
1291 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1292 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 ret = false;
1294 }
1295 }
1296 }
1297 remote_streams_ = streams;
1298 return ret;
1299}
1300
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001301void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1302 const std::vector<RtpHeaderExtension>& extensions) {
1303 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001304 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001305 rtp_abs_sendtime_extn_id_ =
1306 send_time_extension ? send_time_extension->id : -1;
1307}
1308
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001309void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001310 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 case MSG_RTPPACKET:
1313 case MSG_RTCPPACKET: {
1314 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
stefanc1aeaf02015-10-15 07:26:07 -07001315 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet,
1316 data->options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 delete data; // because it is Posted
1318 break;
1319 }
1320 case MSG_FIRSTPACKETRECEIVED: {
1321 SignalFirstPacketReceived(this);
1322 break;
1323 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 }
1325}
1326
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327void BaseChannel::FlushRtcpMessages() {
1328 // Flush all remaining RTCP messages. This should only be called in
1329 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001330 ASSERT(rtc::Thread::Current() == worker_thread_);
1331 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001332 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001333 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001335 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336 }
1337}
1338
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001339VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 MediaEngineInterface* media_engine,
1341 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001342 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 const std::string& content_name,
1344 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001345 : BaseChannel(thread,
1346 media_channel,
1347 transport_controller,
1348 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001350 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001351 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352
1353VoiceChannel::~VoiceChannel() {
1354 StopAudioMonitor();
1355 StopMediaMonitor();
1356 // this can't be done in the base class, since it calls a virtual
1357 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001358 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359}
1360
1361bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001362 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363 return false;
1364 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365 return true;
1366}
1367
Peter Boström0c4e06b2015-10-07 12:23:21 +02001368bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001369 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001370 const AudioOptions* options,
1371 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001372 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001373 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374}
1375
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376// TODO(juberti): Handle early media the right way. We should get an explicit
1377// ringing message telling us to start playing local ringback, which we cancel
1378// if any early media actually arrives. For now, we do the opposite, which is
1379// to wait 1 second for early media, and start playing local ringback if none
1380// arrives.
1381void VoiceChannel::SetEarlyMedia(bool enable) {
1382 if (enable) {
1383 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001384 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1385 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386 } else {
1387 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001388 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001389 }
1390}
1391
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001393 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1394 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395}
1396
Peter Boström0c4e06b2015-10-07 12:23:21 +02001397bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1398 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001399 int duration) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001400 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
solenberg1d63dd02015-12-02 12:35:09 -08001401 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402}
1403
solenberg4bac9c52015-10-09 02:32:53 -07001404bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1405 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1406 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001407}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001408
Tommif888bb52015-12-12 01:37:01 +01001409void VoiceChannel::SetRawAudioSink(
1410 uint32_t ssrc,
1411 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
1412 // We need to work around Bind's lack of support for scoped_ptr and ownership
1413 // passing. So we invoke to our own little routine that gets a pointer to
1414 // our local variable. This is OK since we're synchronously invoking.
1415 InvokeOnWorker(Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
1416}
1417
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001419 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1420 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421}
1422
1423void VoiceChannel::StartMediaMonitor(int cms) {
1424 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001425 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426 media_monitor_->SignalUpdate.connect(
1427 this, &VoiceChannel::OnMediaMonitorUpdate);
1428 media_monitor_->Start(cms);
1429}
1430
1431void VoiceChannel::StopMediaMonitor() {
1432 if (media_monitor_) {
1433 media_monitor_->Stop();
1434 media_monitor_->SignalUpdate.disconnect(this);
1435 media_monitor_.reset();
1436 }
1437}
1438
1439void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001440 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 audio_monitor_
1442 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1443 audio_monitor_->Start(cms);
1444}
1445
1446void VoiceChannel::StopAudioMonitor() {
1447 if (audio_monitor_) {
1448 audio_monitor_->Stop();
1449 audio_monitor_.reset();
1450 }
1451}
1452
1453bool VoiceChannel::IsAudioMonitorRunning() const {
1454 return (audio_monitor_.get() != NULL);
1455}
1456
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001458 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459}
1460
1461int VoiceChannel::GetOutputLevel_w() {
1462 return media_channel()->GetOutputLevel();
1463}
1464
1465void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1466 media_channel()->GetActiveStreams(actives);
1467}
1468
1469void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001470 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001471 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001472 int flags) {
1473 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474
1475 // Set a flag when we've received an RTP packet. If we're waiting for early
1476 // media, this will disable the timeout.
1477 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1478 received_media_ = true;
1479 }
1480}
1481
1482void VoiceChannel::ChangeState() {
1483 // Render incoming data if we're the active call, and we have the local
1484 // content. We receive data on the default channel and multiplexed streams.
1485 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001486 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487
1488 // Send outgoing data if we're the active call, we have the remote content,
1489 // and we have had some form of connectivity.
1490 bool send = IsReadyToSend();
1491 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1492 if (!media_channel()->SetSend(send_flag)) {
1493 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 }
1495
1496 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1497}
1498
1499const ContentInfo* VoiceChannel::GetFirstContent(
1500 const SessionDescription* sdesc) {
1501 return GetFirstAudioContent(sdesc);
1502}
1503
1504bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001505 ContentAction action,
1506 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001507 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001508 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 LOG(LS_INFO) << "Setting local voice description";
1510
1511 const AudioContentDescription* audio =
1512 static_cast<const AudioContentDescription*>(content);
1513 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001514 if (!audio) {
1515 SafeSetError("Can't find audio content in local description.", error_desc);
1516 return false;
1517 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001519 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1520 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 }
1522
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001523 AudioRecvParameters recv_params = last_recv_params_;
1524 RtpParametersFromMediaDescription(audio, &recv_params);
1525 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001526 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001527 error_desc);
1528 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001530 for (const AudioCodec& codec : audio->codecs()) {
1531 bundle_filter()->AddPayloadType(codec.id);
1532 }
1533 last_recv_params_ = recv_params;
1534
1535 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1536 // only give it to the media channel once we have a remote
1537 // description too (without a remote description, we won't be able
1538 // to send them anyway).
1539 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1540 SafeSetError("Failed to set local audio description streams.", error_desc);
1541 return false;
1542 }
1543
1544 set_local_content_direction(content->direction());
1545 ChangeState();
1546 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547}
1548
1549bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001550 ContentAction action,
1551 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001552 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001553 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 LOG(LS_INFO) << "Setting remote voice description";
1555
1556 const AudioContentDescription* audio =
1557 static_cast<const AudioContentDescription*>(content);
1558 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001559 if (!audio) {
1560 SafeSetError("Can't find audio content in remote description.", error_desc);
1561 return false;
1562 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001564 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1565 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 }
1567
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001568 AudioSendParameters send_params = last_send_params_;
1569 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001570 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001571 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001572 }
1573 if (!media_channel()->SetSendParameters(send_params)) {
1574 SafeSetError("Failed to set remote audio description send parameters.",
1575 error_desc);
1576 return false;
1577 }
1578 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001579
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001580 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1581 // and only give it to the media channel once we have a local
1582 // description too (without a local description, we won't be able to
1583 // recv them anyway).
1584 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1585 SafeSetError("Failed to set remote audio description streams.", error_desc);
1586 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 }
1588
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001589 if (audio->rtp_header_extensions_set()) {
1590 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1591 }
1592
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001593 set_remote_content_direction(content->direction());
1594 ChangeState();
1595 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596}
1597
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598void VoiceChannel::HandleEarlyMediaTimeout() {
1599 // This occurs on the main thread, not the worker thread.
1600 if (!received_media_) {
1601 LOG(LS_INFO) << "No early media received before timeout";
1602 SignalEarlyMediaTimeout(this);
1603 }
1604}
1605
Peter Boström0c4e06b2015-10-07 12:23:21 +02001606bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1607 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001608 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 if (!enabled()) {
1610 return false;
1611 }
solenberg1d63dd02015-12-02 12:35:09 -08001612 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613}
1614
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001615void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 case MSG_EARLYMEDIATIMEOUT:
1618 HandleEarlyMediaTimeout();
1619 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620 case MSG_CHANNEL_ERROR: {
1621 VoiceChannelErrorMessageData* data =
1622 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 delete data;
1624 break;
1625 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 default:
1627 BaseChannel::OnMessage(pmsg);
1628 break;
1629 }
1630}
1631
1632void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001633 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 SignalConnectionMonitor(this, infos);
1635}
1636
1637void VoiceChannel::OnMediaMonitorUpdate(
1638 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1639 ASSERT(media_channel == this->media_channel());
1640 SignalMediaMonitor(this, info);
1641}
1642
1643void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1644 const AudioInfo& info) {
1645 SignalAudioMonitor(this, info);
1646}
1647
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001648void VoiceChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1649 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650}
1651
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001652VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001654 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001656 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001657 : BaseChannel(thread,
1658 media_channel,
1659 transport_controller,
1660 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001663 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664
1665bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001666 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 return false;
1668 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 return true;
1670}
1671
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672VideoChannel::~VideoChannel() {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001673 std::vector<uint32_t> screencast_ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 ScreencastMap::iterator iter;
1675 while (!screencast_capturers_.empty()) {
1676 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1677 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1678 << screencast_capturers_.begin()->first;
1679 ASSERT(false);
1680 break;
1681 }
1682 }
1683
1684 StopMediaMonitor();
1685 // this can't be done in the base class, since it calls a virtual
1686 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001687
1688 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689}
1690
Peter Boström0c4e06b2015-10-07 12:23:21 +02001691bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001692 worker_thread()->Invoke<void>(Bind(
1693 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001694 return true;
1695}
1696
1697bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001698 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699}
1700
Peter Boström0c4e06b2015-10-07 12:23:21 +02001701bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001702 return worker_thread()->Invoke<bool>(Bind(
1703 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704}
1705
Peter Boström0c4e06b2015-10-07 12:23:21 +02001706bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001707 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1708 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709}
1710
Peter Boström0c4e06b2015-10-07 12:23:21 +02001711bool VideoChannel::RemoveScreencast(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001712 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713}
1714
1715bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001716 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717}
1718
Peter Boström0c4e06b2015-10-07 12:23:21 +02001719int VideoChannel::GetScreencastFps(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001720 ScreencastDetailsData data(ssrc);
1721 worker_thread()->Invoke<void>(Bind(
1722 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001723 return data.fps;
1724}
1725
Peter Boström0c4e06b2015-10-07 12:23:21 +02001726int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001727 ScreencastDetailsData data(ssrc);
1728 worker_thread()->Invoke<void>(Bind(
1729 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001730 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731}
1732
1733bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001734 worker_thread()->Invoke<void>(Bind(
1735 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 return true;
1737}
1738
1739bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001740 worker_thread()->Invoke<void>(Bind(
1741 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742 return true;
1743}
1744
Peter Boström0c4e06b2015-10-07 12:23:21 +02001745bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001746 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001747 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001748 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1749 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001750}
1751
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 // Send outgoing data if we're the active call, we have the remote content,
1754 // and we have had some form of connectivity.
1755 bool send = IsReadyToSend();
1756 if (!media_channel()->SetSend(send)) {
1757 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1758 // TODO(gangji): Report error back to server.
1759 }
1760
Peter Boström34fbfff2015-09-24 19:20:30 +02001761 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762}
1763
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001764bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1765 return InvokeOnWorker(
1766 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767}
1768
1769void VideoChannel::StartMediaMonitor(int cms) {
1770 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001771 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 media_monitor_->SignalUpdate.connect(
1773 this, &VideoChannel::OnMediaMonitorUpdate);
1774 media_monitor_->Start(cms);
1775}
1776
1777void VideoChannel::StopMediaMonitor() {
1778 if (media_monitor_) {
1779 media_monitor_->Stop();
1780 media_monitor_.reset();
1781 }
1782}
1783
1784const ContentInfo* VideoChannel::GetFirstContent(
1785 const SessionDescription* sdesc) {
1786 return GetFirstVideoContent(sdesc);
1787}
1788
1789bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001790 ContentAction action,
1791 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001792 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001793 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 LOG(LS_INFO) << "Setting local video description";
1795
1796 const VideoContentDescription* video =
1797 static_cast<const VideoContentDescription*>(content);
1798 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001799 if (!video) {
1800 SafeSetError("Can't find video content in local description.", error_desc);
1801 return false;
1802 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001804 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1805 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806 }
1807
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001808 VideoRecvParameters recv_params = last_recv_params_;
1809 RtpParametersFromMediaDescription(video, &recv_params);
1810 if (!media_channel()->SetRecvParameters(recv_params)) {
1811 SafeSetError("Failed to set local video description recv parameters.",
1812 error_desc);
1813 return false;
1814 }
1815 for (const VideoCodec& codec : video->codecs()) {
1816 bundle_filter()->AddPayloadType(codec.id);
1817 }
1818 last_recv_params_ = recv_params;
1819
1820 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1821 // only give it to the media channel once we have a remote
1822 // description too (without a remote description, we won't be able
1823 // to send them anyway).
1824 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1825 SafeSetError("Failed to set local video description streams.", error_desc);
1826 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 }
1828
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001829 set_local_content_direction(content->direction());
1830 ChangeState();
1831 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832}
1833
1834bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001835 ContentAction action,
1836 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001837 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001838 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 LOG(LS_INFO) << "Setting remote video description";
1840
1841 const VideoContentDescription* video =
1842 static_cast<const VideoContentDescription*>(content);
1843 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001844 if (!video) {
1845 SafeSetError("Can't find video content in remote description.", error_desc);
1846 return false;
1847 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001849
1850 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1851 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 }
1853
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001854 VideoSendParameters send_params = last_send_params_;
1855 RtpSendParametersFromMediaDescription(video, &send_params);
1856 if (video->conference_mode()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001857 send_params.options.conference_mode = rtc::Optional<bool>(true);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001858 }
1859 if (!media_channel()->SetSendParameters(send_params)) {
1860 SafeSetError("Failed to set remote video description send parameters.",
1861 error_desc);
1862 return false;
1863 }
1864 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001866 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1867 // and only give it to the media channel once we have a local
1868 // description too (without a local description, we won't be able to
1869 // recv them anyway).
1870 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1871 SafeSetError("Failed to set remote video description streams.", error_desc);
1872 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 }
1874
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001875 if (video->rtp_header_extensions_set()) {
1876 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001878
1879 set_remote_content_direction(content->direction());
1880 ChangeState();
1881 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882}
1883
1884bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1885 bool ret = true;
1886 // Set the send format for each of the local streams. If the view request
1887 // does not contain a local stream, set its send format to 0x0, which will
1888 // drop all frames.
1889 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1890 it != local_streams().end(); ++it) {
1891 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1892 StaticVideoViews::const_iterator view;
1893 for (view = request.static_video_views.begin();
1894 view != request.static_video_views.end(); ++view) {
1895 if (view->selector.Matches(*it)) {
1896 format.width = view->width;
1897 format.height = view->height;
1898 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1899 break;
1900 }
1901 }
1902
1903 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1904 }
1905
1906 // Check if the view request has invalid streams.
1907 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1908 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001909 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 LOG(LS_WARNING) << "View request for ("
1911 << it->selector.ssrc << ", '"
1912 << it->selector.groupid << "', '"
1913 << it->selector.streamid << "'"
1914 << ") is not in the local streams.";
1915 }
1916 }
1917
1918 return ret;
1919}
1920
Peter Boström0c4e06b2015-10-07 12:23:21 +02001921bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001923 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001925 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1926 screencast_capturers_[ssrc] = capturer;
1927 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928}
1929
Peter Boström0c4e06b2015-10-07 12:23:21 +02001930bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1932 if (iter == screencast_capturers_.end()) {
1933 return false;
1934 }
1935 // Clean up VideoCapturer.
1936 delete iter->second;
1937 screencast_capturers_.erase(iter);
1938 return true;
1939}
1940
1941bool VideoChannel::IsScreencasting_w() const {
1942 return !screencast_capturers_.empty();
1943}
1944
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001945void VideoChannel::GetScreencastDetails_w(
1946 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001947 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001949 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 }
1951 VideoCapturer* capturer = iter->second;
1952 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001953 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1954 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955}
1956
Peter Boström0c4e06b2015-10-07 12:23:21 +02001957void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001958 rtc::WindowEvent we) {
1959 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 SignalScreencastWindowEvent(ssrc, we);
1961}
1962
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001963void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 case MSG_SCREENCASTWINDOWEVENT: {
1966 const ScreencastEventMessageData* data =
1967 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1968 OnScreencastWindowEvent_s(data->ssrc, data->event);
1969 delete data;
1970 break;
1971 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972 case MSG_CHANNEL_ERROR: {
1973 const VideoChannelErrorMessageData* data =
1974 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975 delete data;
1976 break;
1977 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 default:
1979 BaseChannel::OnMessage(pmsg);
1980 break;
1981 }
1982}
1983
1984void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001985 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 SignalConnectionMonitor(this, infos);
1987}
1988
1989// TODO(pthatcher): Look into removing duplicate code between
1990// audio, video, and data, perhaps by using templates.
1991void VideoChannel::OnMediaMonitorUpdate(
1992 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1993 ASSERT(media_channel == this->media_channel());
1994 SignalMediaMonitor(this, info);
1995}
1996
Peter Boström0c4e06b2015-10-07 12:23:21 +02001997void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001998 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 ScreencastEventMessageData* pdata =
2000 new ScreencastEventMessageData(ssrc, event);
2001 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2002}
2003
2004void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2005 // Map capturer events to window events. In the future we may want to simply
2006 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002007 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002009 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002011 we = rtc::WE_MINIMIZE;
2012 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2013 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 } else {
2015 return;
2016 }
2017 previous_we_ = we;
2018
Peter Boström0c4e06b2015-10-07 12:23:21 +02002019 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002020 if (!GetLocalSsrc(capturer, &ssrc)) {
2021 return;
2022 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002023
2024 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025}
2026
Peter Boström0c4e06b2015-10-07 12:23:21 +02002027bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 *ssrc = 0;
2029 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2030 iter != screencast_capturers_.end(); ++iter) {
2031 if (iter->second == capturer) {
2032 *ssrc = iter->first;
2033 return true;
2034 }
2035 }
2036 return false;
2037}
2038
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002039void VideoChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2040 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002041}
2042
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002043DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002045 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046 const std::string& content_name,
2047 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07002048 : BaseChannel(thread,
2049 media_channel,
2050 transport_controller,
2051 content_name,
2052 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002053 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002054 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055
2056DataChannel::~DataChannel() {
2057 StopMediaMonitor();
2058 // this can't be done in the base class, since it calls a virtual
2059 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002060
2061 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062}
2063
2064bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002065 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 return false;
2067 }
2068 media_channel()->SignalDataReceived.connect(
2069 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002070 media_channel()->SignalReadyToSend.connect(
2071 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002072 media_channel()->SignalStreamClosedRemotely.connect(
2073 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 return true;
2075}
2076
2077bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002078 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002080 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2081 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082}
2083
2084const ContentInfo* DataChannel::GetFirstContent(
2085 const SessionDescription* sdesc) {
2086 return GetFirstDataContent(sdesc);
2087}
2088
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002089bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090 if (data_channel_type_ == DCT_SCTP) {
2091 // TODO(pthatcher): Do this in a more robust way by checking for
2092 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002093 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002094 } else if (data_channel_type_ == DCT_RTP) {
2095 return BaseChannel::WantsPacket(rtcp, packet);
2096 }
2097 return false;
2098}
2099
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002100bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2101 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102 // It hasn't been set before, so set it now.
2103 if (data_channel_type_ == DCT_NONE) {
2104 data_channel_type_ = new_data_channel_type;
2105 return true;
2106 }
2107
2108 // It's been set before, but doesn't match. That's bad.
2109 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002110 std::ostringstream desc;
2111 desc << "Data channel type mismatch."
2112 << " Expected " << data_channel_type_
2113 << " Got " << new_data_channel_type;
2114 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115 return false;
2116 }
2117
2118 // It's hasn't changed. Nothing to do.
2119 return true;
2120}
2121
2122bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002123 const DataContentDescription* content,
2124 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2126 (content->protocol() == kMediaProtocolDtlsSctp));
2127 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002128 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129}
2130
2131bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002132 ContentAction action,
2133 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002134 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002135 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136 LOG(LS_INFO) << "Setting local data description";
2137
2138 const DataContentDescription* data =
2139 static_cast<const DataContentDescription*>(content);
2140 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002141 if (!data) {
2142 SafeSetError("Can't find data content in local description.", error_desc);
2143 return false;
2144 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002146 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147 return false;
2148 }
2149
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002150 if (data_channel_type_ == DCT_RTP) {
2151 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2152 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153 }
2154 }
2155
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002156 // FYI: We send the SCTP port number (not to be confused with the
2157 // underlying UDP port number) as a codec parameter. So even SCTP
2158 // data channels need codecs.
2159 DataRecvParameters recv_params = last_recv_params_;
2160 RtpParametersFromMediaDescription(data, &recv_params);
2161 if (!media_channel()->SetRecvParameters(recv_params)) {
2162 SafeSetError("Failed to set remote data description recv parameters.",
2163 error_desc);
2164 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002166 if (data_channel_type_ == DCT_RTP) {
2167 for (const DataCodec& codec : data->codecs()) {
2168 bundle_filter()->AddPayloadType(codec.id);
2169 }
2170 }
2171 last_recv_params_ = recv_params;
2172
2173 // TODO(pthatcher): Move local streams into DataSendParameters, and
2174 // only give it to the media channel once we have a remote
2175 // description too (without a remote description, we won't be able
2176 // to send them anyway).
2177 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2178 SafeSetError("Failed to set local data description streams.", error_desc);
2179 return false;
2180 }
2181
2182 set_local_content_direction(content->direction());
2183 ChangeState();
2184 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185}
2186
2187bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002188 ContentAction action,
2189 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002190 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002191 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192
2193 const DataContentDescription* data =
2194 static_cast<const DataContentDescription*>(content);
2195 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002196 if (!data) {
2197 SafeSetError("Can't find data content in remote description.", error_desc);
2198 return false;
2199 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002201 // If the remote data doesn't have codecs and isn't an update, it
2202 // must be empty, so ignore it.
2203 if (!data->has_codecs() && action != CA_UPDATE) {
2204 return true;
2205 }
2206
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002207 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208 return false;
2209 }
2210
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002211 LOG(LS_INFO) << "Setting remote data description";
2212 if (data_channel_type_ == DCT_RTP &&
2213 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2214 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215 }
2216
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002217
2218 DataSendParameters send_params = last_send_params_;
2219 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2220 if (!media_channel()->SetSendParameters(send_params)) {
2221 SafeSetError("Failed to set remote data description send parameters.",
2222 error_desc);
2223 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002225 last_send_params_ = send_params;
2226
2227 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2228 // and only give it to the media channel once we have a local
2229 // description too (without a local description, we won't be able to
2230 // recv them anyway).
2231 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2232 SafeSetError("Failed to set remote data description streams.",
2233 error_desc);
2234 return false;
2235 }
2236
2237 set_remote_content_direction(content->direction());
2238 ChangeState();
2239 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240}
2241
2242void DataChannel::ChangeState() {
2243 // Render incoming data if we're the active call, and we have the local
2244 // content. We receive data on the default channel and multiplexed streams.
2245 bool recv = IsReadyToReceive();
2246 if (!media_channel()->SetReceive(recv)) {
2247 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2248 }
2249
2250 // Send outgoing data if we're the active call, we have the remote content,
2251 // and we have had some form of connectivity.
2252 bool send = IsReadyToSend();
2253 if (!media_channel()->SetSend(send)) {
2254 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2255 }
2256
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002257 // Trigger SignalReadyToSendData asynchronously.
2258 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259
2260 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2261}
2262
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002263void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002264 switch (pmsg->message_id) {
2265 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002266 DataChannelReadyToSendMessageData* data =
2267 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002268 ready_to_send_data_ = data->data();
2269 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270 delete data;
2271 break;
2272 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002273 case MSG_DATARECEIVED: {
2274 DataReceivedMessageData* data =
2275 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2276 SignalDataReceived(this, data->params, data->payload);
2277 delete data;
2278 break;
2279 }
2280 case MSG_CHANNEL_ERROR: {
2281 const DataChannelErrorMessageData* data =
2282 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002283 delete data;
2284 break;
2285 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002286 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002287 rtc::TypedMessageData<uint32_t>* data =
2288 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002289 SignalStreamClosedRemotely(data->data());
2290 delete data;
2291 break;
2292 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002293 default:
2294 BaseChannel::OnMessage(pmsg);
2295 break;
2296 }
2297}
2298
2299void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002300 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301 SignalConnectionMonitor(this, infos);
2302}
2303
2304void DataChannel::StartMediaMonitor(int cms) {
2305 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002306 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002307 media_monitor_->SignalUpdate.connect(
2308 this, &DataChannel::OnMediaMonitorUpdate);
2309 media_monitor_->Start(cms);
2310}
2311
2312void DataChannel::StopMediaMonitor() {
2313 if (media_monitor_) {
2314 media_monitor_->Stop();
2315 media_monitor_->SignalUpdate.disconnect(this);
2316 media_monitor_.reset();
2317 }
2318}
2319
2320void DataChannel::OnMediaMonitorUpdate(
2321 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2322 ASSERT(media_channel == this->media_channel());
2323 SignalMediaMonitor(this, info);
2324}
2325
2326void DataChannel::OnDataReceived(
2327 const ReceiveDataParams& params, const char* data, size_t len) {
2328 DataReceivedMessageData* msg = new DataReceivedMessageData(
2329 params, data, len);
2330 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2331}
2332
Peter Boström0c4e06b2015-10-07 12:23:21 +02002333void DataChannel::OnDataChannelError(uint32_t ssrc,
2334 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002335 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2336 ssrc, err);
2337 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2338}
2339
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002340void DataChannel::OnDataChannelReadyToSend(bool writable) {
2341 // This is usded for congestion control to indicate that the stream is ready
2342 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2343 // that the transport channel is ready.
2344 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2345 new DataChannelReadyToSendMessageData(writable));
2346}
2347
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002348void DataChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2349 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350}
2351
2352bool DataChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08002353 return (data_channel_type_ == DCT_RTP) && BaseChannel::ShouldSetupDtlsSrtp();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354}
2355
Peter Boström0c4e06b2015-10-07 12:23:21 +02002356void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2357 rtc::TypedMessageData<uint32_t>* message =
2358 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002359 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2360}
2361
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002362} // namespace cricket