blob: 818a659c46664c2334c6a36fc232d55c83b30c05 [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()) {
270 srtp_filter_.ResetParams();
271 }
272
deadbeefcbecd352015-09-23 11:50:27 -0700273 set_transport_channel(transport_controller_->CreateTransportChannel_w(
274 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000275 if (!transport_channel()) {
276 return false;
277 }
deadbeefcbecd352015-09-23 11:50:27 -0700278 if (rtcp_transport_enabled()) {
279 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
280 << " on " << transport_name << " transport ";
281 set_rtcp_transport_channel(transport_controller_->CreateTransportChannel_w(
282 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000283 if (!rtcp_transport_channel()) {
284 return false;
285 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000286 }
287
deadbeefcbecd352015-09-23 11:50:27 -0700288 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000289 return true;
290}
291
292void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
293 ASSERT(worker_thread_ == rtc::Thread::Current());
294
295 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700296 if (!old_tc && !new_tc) {
297 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000298 return;
299 }
deadbeefcbecd352015-09-23 11:50:27 -0700300 ASSERT(old_tc != new_tc);
301
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000302 if (old_tc) {
303 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700304 transport_controller_->DestroyTransportChannel_w(
305 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000306 }
307
308 transport_channel_ = new_tc;
309
310 if (new_tc) {
311 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700312 for (const auto& pair : socket_options_) {
313 new_tc->SetOption(pair.first, pair.second);
314 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000315 }
deadbeefcbecd352015-09-23 11:50:27 -0700316
317 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
318 // setting new channel
319 UpdateWritableState_w();
320 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000321}
322
323void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
324 ASSERT(worker_thread_ == rtc::Thread::Current());
325
326 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700327 if (!old_tc && !new_tc) {
328 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000329 return;
330 }
deadbeefcbecd352015-09-23 11:50:27 -0700331 ASSERT(old_tc != new_tc);
332
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000333 if (old_tc) {
334 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700335 transport_controller_->DestroyTransportChannel_w(
336 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000337 }
338
339 rtcp_transport_channel_ = new_tc;
340
341 if (new_tc) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800342 RTC_CHECK(!(ShouldSetupDtlsSrtp() && srtp_filter_.IsActive()))
343 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
344 << "should never happen.";
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000345 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700346 for (const auto& pair : rtcp_socket_options_) {
347 new_tc->SetOption(pair.first, pair.second);
348 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000349 }
deadbeefcbecd352015-09-23 11:50:27 -0700350
351 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
352 // setting new channel
353 UpdateWritableState_w();
354 SetReadyToSend(true, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000355}
356
357void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
358 ASSERT(worker_thread_ == rtc::Thread::Current());
359
360 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
361 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
362 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800363 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000364}
365
366void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
367 ASSERT(worker_thread_ == rtc::Thread::Current());
368
369 tc->SignalWritableState.disconnect(this);
370 tc->SignalReadPacket.disconnect(this);
371 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800372 tc->SignalDtlsState.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000373}
374
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000376 worker_thread_->Invoke<void>(Bind(
377 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
378 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379 return true;
380}
381
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000383 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384}
385
Peter Boström0c4e06b2015-10-07 12:23:21 +0200386bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000387 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388}
389
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000390bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000391 return InvokeOnWorker(
392 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000393}
394
Peter Boström0c4e06b2015-10-07 12:23:21 +0200395bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000396 return InvokeOnWorker(
397 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000398}
399
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000400bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000401 ContentAction action,
402 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100403 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000404 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
405 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406}
407
408bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000409 ContentAction action,
410 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100411 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000412 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
413 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414}
415
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000417 // We pass in the BaseChannel instead of the transport_channel_
418 // because if the transport_channel_ changes, the ConnectionMonitor
419 // would be pointing to the wrong TransportChannel.
420 connection_monitor_.reset(new ConnectionMonitor(
421 this, worker_thread(), rtc::Thread::Current()));
422 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000424 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425}
426
427void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000428 if (connection_monitor_) {
429 connection_monitor_->Stop();
430 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000431 }
432}
433
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000434bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
435 ASSERT(worker_thread_ == rtc::Thread::Current());
436 return transport_channel_->GetStats(infos);
437}
438
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439bool BaseChannel::IsReadyToReceive() const {
440 // Receive data if we are enabled and have local content,
441 return enabled() && IsReceiveContentDirection(local_content_direction_);
442}
443
444bool BaseChannel::IsReadyToSend() const {
445 // Send outgoing data if we are enabled, have local and remote content,
446 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800447 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 IsSendContentDirection(local_content_direction_) &&
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800449 was_ever_writable() &&
450 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451}
452
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000453bool BaseChannel::SendPacket(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700454 const rtc::PacketOptions& options) {
455 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456}
457
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000458bool BaseChannel::SendRtcp(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700459 const rtc::PacketOptions& options) {
460 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000461}
462
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000463int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000465 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000467 case ST_RTP:
468 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700469 socket_options_.push_back(
470 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000471 break;
472 case ST_RTCP:
473 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700474 rtcp_socket_options_.push_back(
475 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000476 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000478 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479}
480
481void BaseChannel::OnWritableState(TransportChannel* channel) {
482 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
deadbeefcbecd352015-09-23 11:50:27 -0700483 UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484}
485
486void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000487 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000488 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000489 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100490 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000492 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493
494 // When using RTCP multiplexing we might get RTCP packets on the RTP
495 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
496 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000497 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000498 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499}
500
501void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700502 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
503 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504}
505
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800506void BaseChannel::OnDtlsState(TransportChannel* channel,
507 DtlsTransportState state) {
508 if (!ShouldSetupDtlsSrtp()) {
509 return;
510 }
511
512 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
513 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
514 // cover other scenarios like the whole channel is writable (not just this
515 // TransportChannel) or when TransportChannel is attached after DTLS is
516 // negotiated.
517 if (state != DTLS_TRANSPORT_CONNECTED) {
518 srtp_filter_.ResetParams();
519 }
520}
521
deadbeefcbecd352015-09-23 11:50:27 -0700522void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
523 if (rtcp) {
524 rtcp_ready_to_send_ = ready;
525 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526 rtp_ready_to_send_ = ready;
527 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528
deadbeefcbecd352015-09-23 11:50:27 -0700529 if (rtp_ready_to_send_ &&
530 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
531 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
torbjornga81a42f2015-09-23 02:16:58 -0700532 // Notify the MediaChannel when both rtp and rtcp channel can send.
533 media_channel_->OnReadyToSend(true);
deadbeefcbecd352015-09-23 11:50:27 -0700534 } else {
535 // Notify the MediaChannel when either rtp or rtcp channel can't send.
536 media_channel_->OnReadyToSend(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 }
538}
539
540bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
541 const char* data, size_t len) {
542 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000543 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000544}
545
stefanc1aeaf02015-10-15 07:26:07 -0700546bool BaseChannel::SendPacket(bool rtcp,
547 rtc::Buffer* packet,
548 const rtc::PacketOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549 // SendPacket gets called from MediaEngine, typically on an encoder thread.
550 // If the thread is not our worker thread, we will post to our worker
551 // so that the real work happens on our worker. This avoids us having to
552 // synchronize access to all the pieces of the send path, including
553 // SRTP and the inner workings of the transport channels.
554 // The only downside is that we can't return a proper failure code if
555 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000556 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 // Avoid a copy by transferring the ownership of the packet data.
558 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
559 PacketMessageData* data = new PacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800560 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700561 data->options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562 worker_thread_->Post(this, message_id, data);
563 return true;
564 }
565
566 // Now that we are on the correct thread, ensure we have a place to send this
567 // packet before doing anything. (We might get RTCP packets that we don't
568 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
569 // transport.
570 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
571 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000572 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 return false;
574 }
575
576 // Protect ourselves against crazy data.
577 if (!ValidPacket(rtcp, packet)) {
578 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000579 << PacketType(rtcp)
580 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 return false;
582 }
583
stefanc1aeaf02015-10-15 07:26:07 -0700584 rtc::PacketOptions updated_options;
585 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 // Protect if needed.
587 if (srtp_filter_.IsActive()) {
588 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200589 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000590 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000592 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
593 // inside libsrtp for a RTP packet. A external HMAC module will be writing
594 // a fake HMAC value. This is ONLY done for a RTP packet.
595 // Socket layer will update rtp sendtime extension header if present in
596 // packet with current time before updating the HMAC.
597#if !defined(ENABLE_EXTERNAL_AUTH)
598 res = srtp_filter_.ProtectRtp(
599 data, len, static_cast<int>(packet->capacity()), &len);
600#else
stefanc1aeaf02015-10-15 07:26:07 -0700601 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000602 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000603 res = srtp_filter_.ProtectRtp(
604 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700605 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000606 // If protection succeeds, let's get auth params from srtp.
607 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200608 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000609 int key_len;
610 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700611 &auth_key, &key_len,
612 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000613 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700614 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
615 updated_options.packet_time_params.srtp_auth_key.assign(
616 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000617 }
618 }
619#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 if (!res) {
621 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200622 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000623 GetRtpSeqNum(data, len, &seq_num);
624 GetRtpSsrc(data, len, &ssrc);
625 LOG(LS_ERROR) << "Failed to protect " << content_name_
626 << " RTP packet: size=" << len
627 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
628 return false;
629 }
630 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000631 res = srtp_filter_.ProtectRtcp(data, len,
632 static_cast<int>(packet->capacity()),
633 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 if (!res) {
635 int type = -1;
636 GetRtcpType(data, len, &type);
637 LOG(LS_ERROR) << "Failed to protect " << content_name_
638 << " RTCP packet: size=" << len << ", type=" << type;
639 return false;
640 }
641 }
642
643 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000644 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 } else if (secure_required_) {
646 // This is a double check for something that supposedly can't happen.
647 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
648 << " packet when SRTP is inactive and crypto is required";
649
650 ASSERT(false);
651 return false;
652 }
653
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000655 int ret =
stefanc1aeaf02015-10-15 07:26:07 -0700656 channel->SendPacket(packet->data<char>(), packet->size(), updated_options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000657 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
658 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 if (channel->GetError() == EWOULDBLOCK) {
660 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700661 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 }
663 return false;
664 }
665 return true;
666}
667
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000668bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 // Protect ourselves against crazy data.
670 if (!ValidPacket(rtcp, packet)) {
671 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000672 << PacketType(rtcp)
673 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 return false;
675 }
pbos482b12e2015-11-16 10:19:58 -0800676 if (rtcp) {
677 // Permit all (seemingly valid) RTCP packets.
678 return true;
679 }
680 // Check whether we handle this payload.
681 return bundle_filter_.DemuxPacket(packet->data<uint8_t>(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682}
683
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000684void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
685 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 if (!WantsPacket(rtcp, packet)) {
687 return;
688 }
689
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000690 // We are only interested in the first rtp packet because that
691 // indicates the media has started flowing.
692 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 has_received_packet_ = true;
694 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
695 }
696
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 // Unprotect the packet, if needed.
698 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200699 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000700 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701 bool res;
702 if (!rtcp) {
703 res = srtp_filter_.UnprotectRtp(data, len, &len);
704 if (!res) {
705 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200706 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000707 GetRtpSeqNum(data, len, &seq_num);
708 GetRtpSsrc(data, len, &ssrc);
709 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
710 << " RTP packet: size=" << len
711 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
712 return;
713 }
714 } else {
715 res = srtp_filter_.UnprotectRtcp(data, len, &len);
716 if (!res) {
717 int type = -1;
718 GetRtcpType(data, len, &type);
719 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
720 << " RTCP packet: size=" << len << ", type=" << type;
721 return;
722 }
723 }
724
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000725 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 } else if (secure_required_) {
727 // Our session description indicates that SRTP is required, but we got a
728 // packet before our SRTP filter is active. This means either that
729 // a) we got SRTP packets before we received the SDES keys, in which case
730 // we can't decrypt it anyway, or
731 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
732 // channels, so we haven't yet extracted keys, even if DTLS did complete
733 // on the channel that the packets are being sent on. It's really good
734 // practice to wait for both RTP and RTCP to be good to go before sending
735 // media, to prevent weird failure modes, so it's fine for us to just eat
736 // packets here. This is all sidestepped if RTCP mux is used anyway.
737 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
738 << " packet when SRTP is inactive and crypto is required";
739 return;
740 }
741
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 // Push it down to the media channel.
743 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000744 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000746 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 }
748}
749
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000750bool BaseChannel::PushdownLocalDescription(
751 const SessionDescription* local_desc, ContentAction action,
752 std::string* error_desc) {
753 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000754 const MediaContentDescription* content_desc =
755 GetContentDescription(content_info);
756 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000757 !SetLocalContent(content_desc, action, error_desc)) {
758 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
759 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000760 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000761 return true;
762}
763
764bool BaseChannel::PushdownRemoteDescription(
765 const SessionDescription* remote_desc, ContentAction action,
766 std::string* error_desc) {
767 const ContentInfo* content_info = GetFirstContent(remote_desc);
768 const MediaContentDescription* content_desc =
769 GetContentDescription(content_info);
770 if (content_desc && content_info && !content_info->rejected &&
771 !SetRemoteContent(content_desc, action, error_desc)) {
772 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
773 return false;
774 }
775 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776}
777
778void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000779 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 if (enabled_)
781 return;
782
783 LOG(LS_INFO) << "Channel enabled";
784 enabled_ = true;
785 ChangeState();
786}
787
788void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000789 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000790 if (!enabled_)
791 return;
792
793 LOG(LS_INFO) << "Channel disabled";
794 enabled_ = false;
795 ChangeState();
796}
797
deadbeefcbecd352015-09-23 11:50:27 -0700798void BaseChannel::UpdateWritableState_w() {
799 if (transport_channel_ && transport_channel_->writable() &&
800 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
801 ChannelWritable_w();
802 } else {
803 ChannelNotWritable_w();
804 }
805}
806
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000807void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000808 ASSERT(worker_thread_ == rtc::Thread::Current());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800809 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800811 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812
deadbeefcbecd352015-09-23 11:50:27 -0700813 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 << (was_ever_writable_ ? "" : " for the first time");
815
816 std::vector<ConnectionInfo> infos;
817 transport_channel_->GetStats(&infos);
818 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
819 it != infos.end(); ++it) {
820 if (it->best_connection) {
821 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
822 << "->" << it->remote_candidate.ToSensitiveString();
823 break;
824 }
825 }
826
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 was_ever_writable_ = true;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800828 MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000829 writable_ = true;
830 ChangeState();
831}
832
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000833void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
834 ASSERT(worker_thread() == rtc::Thread::Current());
835 signaling_thread()->Invoke<void>(Bind(
836 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
837}
838
839void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
840 ASSERT(signaling_thread() == rtc::Thread::Current());
841 SignalDtlsSetupFailure(this, rtcp);
842}
843
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800844bool BaseChannel::SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp) {
845 std::vector<int> crypto_suites;
846 // We always use the default SRTP crypto suites for RTCP, but we may use
847 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 if (!rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800849 GetSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800851 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800853 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000854}
855
856bool BaseChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800857 // Since DTLS is applied to all channels, checking RTP should be enough.
858 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859}
860
861// This function returns true if either DTLS-SRTP is not in use
862// *or* DTLS-SRTP is successfully set up.
863bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
864 bool ret = false;
865
deadbeefcbecd352015-09-23 11:50:27 -0700866 TransportChannel* channel =
867 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800869 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800871 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800873 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
874 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 return false;
876 }
877
878 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
879 << content_name() << " "
880 << PacketType(rtcp_channel);
881
882 // OK, we're now doing DTLS (RFC 5764)
883 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
884 SRTP_MASTER_KEY_SALT_LEN * 2);
885
886 // RFC 5705 exporter using the RFC 5764 parameters
887 if (!channel->ExportKeyingMaterial(
888 kDtlsSrtpExporterLabel,
889 NULL, 0, false,
890 &dtls_buffer[0], dtls_buffer.size())) {
891 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
892 ASSERT(false); // This should never happen
893 return false;
894 }
895
896 // Sync up the keys with the DTLS-SRTP interface
897 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
898 SRTP_MASTER_KEY_SALT_LEN);
899 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
900 SRTP_MASTER_KEY_SALT_LEN);
901 size_t offset = 0;
902 memcpy(&client_write_key[0], &dtls_buffer[offset],
903 SRTP_MASTER_KEY_KEY_LEN);
904 offset += SRTP_MASTER_KEY_KEY_LEN;
905 memcpy(&server_write_key[0], &dtls_buffer[offset],
906 SRTP_MASTER_KEY_KEY_LEN);
907 offset += SRTP_MASTER_KEY_KEY_LEN;
908 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
909 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
910 offset += SRTP_MASTER_KEY_SALT_LEN;
911 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
912 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
913
914 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000915 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000916 if (!channel->GetSslRole(&role)) {
917 LOG(LS_WARNING) << "GetSslRole failed";
918 return false;
919 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000921 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 send_key = &server_write_key;
923 recv_key = &client_write_key;
924 } else {
925 send_key = &client_write_key;
926 recv_key = &server_write_key;
927 }
928
929 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800930 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
931 static_cast<int>(send_key->size()),
932 selected_crypto_suite, &(*recv_key)[0],
933 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800935 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
936 static_cast<int>(send_key->size()),
937 selected_crypto_suite, &(*recv_key)[0],
938 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 }
940
941 if (!ret)
942 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
943 else
944 dtls_keyed_ = true;
945
946 return ret;
947}
948
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800949void BaseChannel::MaybeSetupDtlsSrtp_w() {
950 if (srtp_filter_.IsActive()) {
951 return;
952 }
953
954 if (!ShouldSetupDtlsSrtp()) {
955 return;
956 }
957
958 if (!SetupDtlsSrtp(false)) {
959 SignalDtlsSetupFailure_w(false);
960 return;
961 }
962
963 if (rtcp_transport_channel_) {
964 if (!SetupDtlsSrtp(true)) {
965 SignalDtlsSetupFailure_w(true);
966 return;
967 }
968 }
969}
970
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000972 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 if (!writable_)
974 return;
975
deadbeefcbecd352015-09-23 11:50:27 -0700976 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 writable_ = false;
978 ChangeState();
979}
980
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700981bool BaseChannel::SetRtpTransportParameters_w(
982 const MediaContentDescription* content,
983 ContentAction action,
984 ContentSource src,
985 std::string* error_desc) {
986 if (action == CA_UPDATE) {
987 // These parameters never get changed by a CA_UDPATE.
988 return true;
989 }
990
991 // Cache secure_required_ for belt and suspenders check on SendPacket
992 if (src == CS_LOCAL) {
993 set_secure_required(content->crypto_required() != CT_NONE);
994 }
995
996 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
997 return false;
998 }
999
1000 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
1001 return false;
1002 }
1003
1004 return true;
1005}
1006
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001007// |dtls| will be set to true if DTLS is active for transport channel and
1008// crypto is empty.
1009bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001010 bool* dtls,
1011 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001012 *dtls = transport_channel_->IsDtlsActive();
1013 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001014 SafeSetError("Cryptos must be empty when DTLS is active.",
1015 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001016 return false;
1017 }
1018 return true;
1019}
1020
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001022 ContentAction action,
1023 ContentSource src,
1024 std::string* error_desc) {
1025 if (action == CA_UPDATE) {
1026 // no crypto params.
1027 return true;
1028 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001030 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001031 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
1032 if (!ret) {
1033 return false;
1034 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035 switch (action) {
1036 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001037 // If DTLS is already active on the channel, we could be renegotiating
1038 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001039 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001040 ret = srtp_filter_.SetOffer(cryptos, src);
1041 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 break;
1043 case CA_PRANSWER:
1044 // If we're doing DTLS-SRTP, we don't want to update the filter
1045 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001046 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1048 }
1049 break;
1050 case CA_ANSWER:
1051 // If we're doing DTLS-SRTP, we don't want to update the filter
1052 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001053 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 ret = srtp_filter_.SetAnswer(cryptos, src);
1055 }
1056 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 default:
1058 break;
1059 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001060 if (!ret) {
1061 SafeSetError("Failed to setup SRTP filter.", error_desc);
1062 return false;
1063 }
1064 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065}
1066
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001067void BaseChannel::ActivateRtcpMux() {
1068 worker_thread_->Invoke<void>(Bind(
1069 &BaseChannel::ActivateRtcpMux_w, this));
1070}
1071
1072void BaseChannel::ActivateRtcpMux_w() {
1073 if (!rtcp_mux_filter_.IsActive()) {
1074 rtcp_mux_filter_.SetActive();
deadbeefcbecd352015-09-23 11:50:27 -07001075 set_rtcp_transport_channel(nullptr);
1076 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001077 }
1078}
1079
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001081 ContentSource src,
1082 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083 bool ret = false;
1084 switch (action) {
1085 case CA_OFFER:
1086 ret = rtcp_mux_filter_.SetOffer(enable, src);
1087 break;
1088 case CA_PRANSWER:
1089 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1090 break;
1091 case CA_ANSWER:
1092 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1093 if (ret && rtcp_mux_filter_.IsActive()) {
1094 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001095 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1096 << " by destroying RTCP transport channel for "
1097 << transport_name();
1098 set_rtcp_transport_channel(nullptr);
1099 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100 }
1101 break;
1102 case CA_UPDATE:
1103 // No RTCP mux info.
1104 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001105 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 default:
1107 break;
1108 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001109 if (!ret) {
1110 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1111 return false;
1112 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1114 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1115 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001116 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117 // If the RTP transport is already writable, then so are we.
1118 if (transport_channel_->writable()) {
1119 ChannelWritable_w();
1120 }
1121 }
1122
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001123 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124}
1125
1126bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001127 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001128 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129}
1130
Peter Boström0c4e06b2015-10-07 12:23:21 +02001131bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001132 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 return media_channel()->RemoveRecvStream(ssrc);
1134}
1135
1136bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001137 ContentAction action,
1138 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1140 action == CA_PRANSWER || action == CA_UPDATE))
1141 return false;
1142
1143 // If this is an update, streams only contain streams that have changed.
1144 if (action == CA_UPDATE) {
1145 for (StreamParamsVec::const_iterator it = streams.begin();
1146 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001147 const StreamParams* existing_stream =
1148 GetStreamByIds(local_streams_, it->groupid, it->id);
1149 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 if (media_channel()->AddSendStream(*it)) {
1151 local_streams_.push_back(*it);
1152 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1153 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001154 std::ostringstream desc;
1155 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1156 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 return false;
1158 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001159 } else if (existing_stream && !it->has_ssrcs()) {
1160 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001161 std::ostringstream desc;
1162 desc << "Failed to remove send stream with ssrc "
1163 << it->first_ssrc() << ".";
1164 SafeSetError(desc.str(), error_desc);
1165 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001167 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 } else {
1169 LOG(LS_WARNING) << "Ignore unsupported stream update";
1170 }
1171 }
1172 return true;
1173 }
1174 // Else streams are all the streams we want to send.
1175
1176 // Check for streams that have been removed.
1177 bool ret = true;
1178 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1179 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001180 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001182 std::ostringstream desc;
1183 desc << "Failed to remove send stream with ssrc "
1184 << it->first_ssrc() << ".";
1185 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 ret = false;
1187 }
1188 }
1189 }
1190 // Check for new streams.
1191 for (StreamParamsVec::const_iterator it = streams.begin();
1192 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001193 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001195 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001197 std::ostringstream desc;
1198 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1199 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 ret = false;
1201 }
1202 }
1203 }
1204 local_streams_ = streams;
1205 return ret;
1206}
1207
1208bool BaseChannel::UpdateRemoteStreams_w(
1209 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001210 ContentAction action,
1211 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1213 action == CA_PRANSWER || action == CA_UPDATE))
1214 return false;
1215
1216 // If this is an update, streams only contain streams that have changed.
1217 if (action == CA_UPDATE) {
1218 for (StreamParamsVec::const_iterator it = streams.begin();
1219 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001220 const StreamParams* existing_stream =
1221 GetStreamByIds(remote_streams_, it->groupid, it->id);
1222 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 if (AddRecvStream_w(*it)) {
1224 remote_streams_.push_back(*it);
1225 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1226 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001227 std::ostringstream desc;
1228 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1229 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 return false;
1231 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001232 } else if (existing_stream && !it->has_ssrcs()) {
1233 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001234 std::ostringstream desc;
1235 desc << "Failed to remove remote stream with ssrc "
1236 << it->first_ssrc() << ".";
1237 SafeSetError(desc.str(), error_desc);
1238 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001240 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 } else {
1242 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001243 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 << " new stream = " << it->ToString();
1245 }
1246 }
1247 return true;
1248 }
1249 // Else streams are all the streams we want to receive.
1250
1251 // Check for streams that have been removed.
1252 bool ret = true;
1253 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1254 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001255 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001257 std::ostringstream desc;
1258 desc << "Failed to remove remote stream with ssrc "
1259 << it->first_ssrc() << ".";
1260 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 ret = false;
1262 }
1263 }
1264 }
1265 // Check for new streams.
1266 for (StreamParamsVec::const_iterator it = streams.begin();
1267 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001268 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 if (AddRecvStream_w(*it)) {
1270 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1271 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001272 std::ostringstream desc;
1273 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1274 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 ret = false;
1276 }
1277 }
1278 }
1279 remote_streams_ = streams;
1280 return ret;
1281}
1282
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001283void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1284 const std::vector<RtpHeaderExtension>& extensions) {
1285 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001286 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001287 rtp_abs_sendtime_extn_id_ =
1288 send_time_extension ? send_time_extension->id : -1;
1289}
1290
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001291void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001292 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 case MSG_RTPPACKET:
1295 case MSG_RTCPPACKET: {
1296 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
stefanc1aeaf02015-10-15 07:26:07 -07001297 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet,
1298 data->options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 delete data; // because it is Posted
1300 break;
1301 }
1302 case MSG_FIRSTPACKETRECEIVED: {
1303 SignalFirstPacketReceived(this);
1304 break;
1305 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306 }
1307}
1308
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309void BaseChannel::FlushRtcpMessages() {
1310 // Flush all remaining RTCP messages. This should only be called in
1311 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001312 ASSERT(rtc::Thread::Current() == worker_thread_);
1313 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001314 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001315 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001317 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 }
1319}
1320
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001321VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 MediaEngineInterface* media_engine,
1323 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001324 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 const std::string& content_name,
1326 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001327 : BaseChannel(thread,
1328 media_channel,
1329 transport_controller,
1330 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001332 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001333 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334
1335VoiceChannel::~VoiceChannel() {
1336 StopAudioMonitor();
1337 StopMediaMonitor();
1338 // this can't be done in the base class, since it calls a virtual
1339 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001340 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341}
1342
1343bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001344 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 return false;
1346 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347 return true;
1348}
1349
Peter Boström0c4e06b2015-10-07 12:23:21 +02001350bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001351 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001352 const AudioOptions* options,
1353 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001354 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001355 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356}
1357
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358// TODO(juberti): Handle early media the right way. We should get an explicit
1359// ringing message telling us to start playing local ringback, which we cancel
1360// if any early media actually arrives. For now, we do the opposite, which is
1361// to wait 1 second for early media, and start playing local ringback if none
1362// arrives.
1363void VoiceChannel::SetEarlyMedia(bool enable) {
1364 if (enable) {
1365 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001366 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1367 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 } else {
1369 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001370 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371 }
1372}
1373
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001375 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1376 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377}
1378
Peter Boström0c4e06b2015-10-07 12:23:21 +02001379bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1380 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001381 int duration) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001382 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
solenberg1d63dd02015-12-02 12:35:09 -08001383 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384}
1385
solenberg4bac9c52015-10-09 02:32:53 -07001386bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1387 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1388 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001389}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001390
Tommif888bb52015-12-12 01:37:01 +01001391void VoiceChannel::SetRawAudioSink(
1392 uint32_t ssrc,
1393 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
1394 // We need to work around Bind's lack of support for scoped_ptr and ownership
1395 // passing. So we invoke to our own little routine that gets a pointer to
1396 // our local variable. This is OK since we're synchronously invoking.
1397 InvokeOnWorker(Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
1398}
1399
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001400bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001401 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1402 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403}
1404
1405void VoiceChannel::StartMediaMonitor(int cms) {
1406 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001407 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 media_monitor_->SignalUpdate.connect(
1409 this, &VoiceChannel::OnMediaMonitorUpdate);
1410 media_monitor_->Start(cms);
1411}
1412
1413void VoiceChannel::StopMediaMonitor() {
1414 if (media_monitor_) {
1415 media_monitor_->Stop();
1416 media_monitor_->SignalUpdate.disconnect(this);
1417 media_monitor_.reset();
1418 }
1419}
1420
1421void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001422 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423 audio_monitor_
1424 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1425 audio_monitor_->Start(cms);
1426}
1427
1428void VoiceChannel::StopAudioMonitor() {
1429 if (audio_monitor_) {
1430 audio_monitor_->Stop();
1431 audio_monitor_.reset();
1432 }
1433}
1434
1435bool VoiceChannel::IsAudioMonitorRunning() const {
1436 return (audio_monitor_.get() != NULL);
1437}
1438
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001440 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441}
1442
1443int VoiceChannel::GetOutputLevel_w() {
1444 return media_channel()->GetOutputLevel();
1445}
1446
1447void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1448 media_channel()->GetActiveStreams(actives);
1449}
1450
1451void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001452 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001453 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001454 int flags) {
1455 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456
1457 // Set a flag when we've received an RTP packet. If we're waiting for early
1458 // media, this will disable the timeout.
1459 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1460 received_media_ = true;
1461 }
1462}
1463
1464void VoiceChannel::ChangeState() {
1465 // Render incoming data if we're the active call, and we have the local
1466 // content. We receive data on the default channel and multiplexed streams.
1467 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001468 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469
1470 // Send outgoing data if we're the active call, we have the remote content,
1471 // and we have had some form of connectivity.
1472 bool send = IsReadyToSend();
1473 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1474 if (!media_channel()->SetSend(send_flag)) {
1475 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 }
1477
1478 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1479}
1480
1481const ContentInfo* VoiceChannel::GetFirstContent(
1482 const SessionDescription* sdesc) {
1483 return GetFirstAudioContent(sdesc);
1484}
1485
1486bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001487 ContentAction action,
1488 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001489 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001490 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491 LOG(LS_INFO) << "Setting local voice description";
1492
1493 const AudioContentDescription* audio =
1494 static_cast<const AudioContentDescription*>(content);
1495 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001496 if (!audio) {
1497 SafeSetError("Can't find audio content in local description.", error_desc);
1498 return false;
1499 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001501 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1502 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503 }
1504
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001505 AudioRecvParameters recv_params = last_recv_params_;
1506 RtpParametersFromMediaDescription(audio, &recv_params);
1507 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001508 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001509 error_desc);
1510 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001512 for (const AudioCodec& codec : audio->codecs()) {
1513 bundle_filter()->AddPayloadType(codec.id);
1514 }
1515 last_recv_params_ = recv_params;
1516
1517 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1518 // only give it to the media channel once we have a remote
1519 // description too (without a remote description, we won't be able
1520 // to send them anyway).
1521 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1522 SafeSetError("Failed to set local audio description streams.", error_desc);
1523 return false;
1524 }
1525
1526 set_local_content_direction(content->direction());
1527 ChangeState();
1528 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529}
1530
1531bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001532 ContentAction action,
1533 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001534 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001535 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 LOG(LS_INFO) << "Setting remote voice description";
1537
1538 const AudioContentDescription* audio =
1539 static_cast<const AudioContentDescription*>(content);
1540 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001541 if (!audio) {
1542 SafeSetError("Can't find audio content in remote description.", error_desc);
1543 return false;
1544 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001546 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1547 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548 }
1549
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001550 AudioSendParameters send_params = last_send_params_;
1551 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001552 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001553 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001554 }
1555 if (!media_channel()->SetSendParameters(send_params)) {
1556 SafeSetError("Failed to set remote audio description send parameters.",
1557 error_desc);
1558 return false;
1559 }
1560 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001562 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1563 // and only give it to the media channel once we have a local
1564 // description too (without a local description, we won't be able to
1565 // recv them anyway).
1566 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1567 SafeSetError("Failed to set remote audio description streams.", error_desc);
1568 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 }
1570
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001571 if (audio->rtp_header_extensions_set()) {
1572 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1573 }
1574
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001575 set_remote_content_direction(content->direction());
1576 ChangeState();
1577 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578}
1579
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580void VoiceChannel::HandleEarlyMediaTimeout() {
1581 // This occurs on the main thread, not the worker thread.
1582 if (!received_media_) {
1583 LOG(LS_INFO) << "No early media received before timeout";
1584 SignalEarlyMediaTimeout(this);
1585 }
1586}
1587
Peter Boström0c4e06b2015-10-07 12:23:21 +02001588bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1589 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001590 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 if (!enabled()) {
1592 return false;
1593 }
solenberg1d63dd02015-12-02 12:35:09 -08001594 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595}
1596
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001597void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 case MSG_EARLYMEDIATIMEOUT:
1600 HandleEarlyMediaTimeout();
1601 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 case MSG_CHANNEL_ERROR: {
1603 VoiceChannelErrorMessageData* data =
1604 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 delete data;
1606 break;
1607 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608 default:
1609 BaseChannel::OnMessage(pmsg);
1610 break;
1611 }
1612}
1613
1614void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001615 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 SignalConnectionMonitor(this, infos);
1617}
1618
1619void VoiceChannel::OnMediaMonitorUpdate(
1620 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1621 ASSERT(media_channel == this->media_channel());
1622 SignalMediaMonitor(this, info);
1623}
1624
1625void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1626 const AudioInfo& info) {
1627 SignalAudioMonitor(this, info);
1628}
1629
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001630void VoiceChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1631 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632}
1633
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001634VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001636 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001638 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001639 : BaseChannel(thread,
1640 media_channel,
1641 transport_controller,
1642 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001645 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646
1647bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001648 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 return false;
1650 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 return true;
1652}
1653
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654VideoChannel::~VideoChannel() {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001655 std::vector<uint32_t> screencast_ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 ScreencastMap::iterator iter;
1657 while (!screencast_capturers_.empty()) {
1658 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1659 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1660 << screencast_capturers_.begin()->first;
1661 ASSERT(false);
1662 break;
1663 }
1664 }
1665
1666 StopMediaMonitor();
1667 // this can't be done in the base class, since it calls a virtual
1668 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001669
1670 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671}
1672
Peter Boström0c4e06b2015-10-07 12:23:21 +02001673bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001674 worker_thread()->Invoke<void>(Bind(
1675 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676 return true;
1677}
1678
1679bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001680 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681}
1682
Peter Boström0c4e06b2015-10-07 12:23:21 +02001683bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001684 return worker_thread()->Invoke<bool>(Bind(
1685 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686}
1687
Peter Boström0c4e06b2015-10-07 12:23:21 +02001688bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001689 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1690 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001691}
1692
Peter Boström0c4e06b2015-10-07 12:23:21 +02001693bool VideoChannel::RemoveScreencast(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001694 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695}
1696
1697bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001698 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699}
1700
Peter Boström0c4e06b2015-10-07 12:23:21 +02001701int VideoChannel::GetScreencastFps(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001702 ScreencastDetailsData data(ssrc);
1703 worker_thread()->Invoke<void>(Bind(
1704 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001705 return data.fps;
1706}
1707
Peter Boström0c4e06b2015-10-07 12:23:21 +02001708int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001709 ScreencastDetailsData data(ssrc);
1710 worker_thread()->Invoke<void>(Bind(
1711 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001712 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713}
1714
1715bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001716 worker_thread()->Invoke<void>(Bind(
1717 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 return true;
1719}
1720
1721bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001722 worker_thread()->Invoke<void>(Bind(
1723 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724 return true;
1725}
1726
Peter Boström0c4e06b2015-10-07 12:23:21 +02001727bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001728 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001729 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001730 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1731 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001732}
1733
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 // Send outgoing data if we're the active call, we have the remote content,
1736 // and we have had some form of connectivity.
1737 bool send = IsReadyToSend();
1738 if (!media_channel()->SetSend(send)) {
1739 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1740 // TODO(gangji): Report error back to server.
1741 }
1742
Peter Boström34fbfff2015-09-24 19:20:30 +02001743 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744}
1745
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001746bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1747 return InvokeOnWorker(
1748 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749}
1750
1751void VideoChannel::StartMediaMonitor(int cms) {
1752 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001753 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001754 media_monitor_->SignalUpdate.connect(
1755 this, &VideoChannel::OnMediaMonitorUpdate);
1756 media_monitor_->Start(cms);
1757}
1758
1759void VideoChannel::StopMediaMonitor() {
1760 if (media_monitor_) {
1761 media_monitor_->Stop();
1762 media_monitor_.reset();
1763 }
1764}
1765
1766const ContentInfo* VideoChannel::GetFirstContent(
1767 const SessionDescription* sdesc) {
1768 return GetFirstVideoContent(sdesc);
1769}
1770
1771bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001772 ContentAction action,
1773 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001774 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001775 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 LOG(LS_INFO) << "Setting local video description";
1777
1778 const VideoContentDescription* video =
1779 static_cast<const VideoContentDescription*>(content);
1780 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001781 if (!video) {
1782 SafeSetError("Can't find video content in local description.", error_desc);
1783 return false;
1784 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001786 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1787 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 }
1789
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001790 VideoRecvParameters recv_params = last_recv_params_;
1791 RtpParametersFromMediaDescription(video, &recv_params);
1792 if (!media_channel()->SetRecvParameters(recv_params)) {
1793 SafeSetError("Failed to set local video description recv parameters.",
1794 error_desc);
1795 return false;
1796 }
1797 for (const VideoCodec& codec : video->codecs()) {
1798 bundle_filter()->AddPayloadType(codec.id);
1799 }
1800 last_recv_params_ = recv_params;
1801
1802 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1803 // only give it to the media channel once we have a remote
1804 // description too (without a remote description, we won't be able
1805 // to send them anyway).
1806 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1807 SafeSetError("Failed to set local video description streams.", error_desc);
1808 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 }
1810
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001811 set_local_content_direction(content->direction());
1812 ChangeState();
1813 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814}
1815
1816bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001817 ContentAction action,
1818 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001819 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001820 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821 LOG(LS_INFO) << "Setting remote video description";
1822
1823 const VideoContentDescription* video =
1824 static_cast<const VideoContentDescription*>(content);
1825 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001826 if (!video) {
1827 SafeSetError("Can't find video content in remote description.", error_desc);
1828 return false;
1829 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001831
1832 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1833 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 }
1835
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001836 VideoSendParameters send_params = last_send_params_;
1837 RtpSendParametersFromMediaDescription(video, &send_params);
1838 if (video->conference_mode()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001839 send_params.options.conference_mode = rtc::Optional<bool>(true);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001840 }
1841 if (!media_channel()->SetSendParameters(send_params)) {
1842 SafeSetError("Failed to set remote video description send parameters.",
1843 error_desc);
1844 return false;
1845 }
1846 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001848 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1849 // and only give it to the media channel once we have a local
1850 // description too (without a local description, we won't be able to
1851 // recv them anyway).
1852 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1853 SafeSetError("Failed to set remote video description streams.", error_desc);
1854 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 }
1856
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001857 if (video->rtp_header_extensions_set()) {
1858 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001860
1861 set_remote_content_direction(content->direction());
1862 ChangeState();
1863 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864}
1865
1866bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1867 bool ret = true;
1868 // Set the send format for each of the local streams. If the view request
1869 // does not contain a local stream, set its send format to 0x0, which will
1870 // drop all frames.
1871 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1872 it != local_streams().end(); ++it) {
1873 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1874 StaticVideoViews::const_iterator view;
1875 for (view = request.static_video_views.begin();
1876 view != request.static_video_views.end(); ++view) {
1877 if (view->selector.Matches(*it)) {
1878 format.width = view->width;
1879 format.height = view->height;
1880 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1881 break;
1882 }
1883 }
1884
1885 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1886 }
1887
1888 // Check if the view request has invalid streams.
1889 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1890 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001891 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 LOG(LS_WARNING) << "View request for ("
1893 << it->selector.ssrc << ", '"
1894 << it->selector.groupid << "', '"
1895 << it->selector.streamid << "'"
1896 << ") is not in the local streams.";
1897 }
1898 }
1899
1900 return ret;
1901}
1902
Peter Boström0c4e06b2015-10-07 12:23:21 +02001903bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001905 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001907 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1908 screencast_capturers_[ssrc] = capturer;
1909 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910}
1911
Peter Boström0c4e06b2015-10-07 12:23:21 +02001912bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1914 if (iter == screencast_capturers_.end()) {
1915 return false;
1916 }
1917 // Clean up VideoCapturer.
1918 delete iter->second;
1919 screencast_capturers_.erase(iter);
1920 return true;
1921}
1922
1923bool VideoChannel::IsScreencasting_w() const {
1924 return !screencast_capturers_.empty();
1925}
1926
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001927void VideoChannel::GetScreencastDetails_w(
1928 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001929 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001931 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 }
1933 VideoCapturer* capturer = iter->second;
1934 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001935 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1936 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937}
1938
Peter Boström0c4e06b2015-10-07 12:23:21 +02001939void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001940 rtc::WindowEvent we) {
1941 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 SignalScreencastWindowEvent(ssrc, we);
1943}
1944
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001945void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947 case MSG_SCREENCASTWINDOWEVENT: {
1948 const ScreencastEventMessageData* data =
1949 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1950 OnScreencastWindowEvent_s(data->ssrc, data->event);
1951 delete data;
1952 break;
1953 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001954 case MSG_CHANNEL_ERROR: {
1955 const VideoChannelErrorMessageData* data =
1956 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957 delete data;
1958 break;
1959 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 default:
1961 BaseChannel::OnMessage(pmsg);
1962 break;
1963 }
1964}
1965
1966void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001967 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968 SignalConnectionMonitor(this, infos);
1969}
1970
1971// TODO(pthatcher): Look into removing duplicate code between
1972// audio, video, and data, perhaps by using templates.
1973void VideoChannel::OnMediaMonitorUpdate(
1974 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1975 ASSERT(media_channel == this->media_channel());
1976 SignalMediaMonitor(this, info);
1977}
1978
Peter Boström0c4e06b2015-10-07 12:23:21 +02001979void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001980 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 ScreencastEventMessageData* pdata =
1982 new ScreencastEventMessageData(ssrc, event);
1983 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1984}
1985
1986void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1987 // Map capturer events to window events. In the future we may want to simply
1988 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001989 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001991 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001993 we = rtc::WE_MINIMIZE;
1994 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1995 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996 } else {
1997 return;
1998 }
1999 previous_we_ = we;
2000
Peter Boström0c4e06b2015-10-07 12:23:21 +02002001 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 if (!GetLocalSsrc(capturer, &ssrc)) {
2003 return;
2004 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002005
2006 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002007}
2008
Peter Boström0c4e06b2015-10-07 12:23:21 +02002009bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010 *ssrc = 0;
2011 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2012 iter != screencast_capturers_.end(); ++iter) {
2013 if (iter->second == capturer) {
2014 *ssrc = iter->first;
2015 return true;
2016 }
2017 }
2018 return false;
2019}
2020
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002021void VideoChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2022 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023}
2024
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002025DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002027 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 const std::string& content_name,
2029 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07002030 : BaseChannel(thread,
2031 media_channel,
2032 transport_controller,
2033 content_name,
2034 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002035 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002036 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037
2038DataChannel::~DataChannel() {
2039 StopMediaMonitor();
2040 // this can't be done in the base class, since it calls a virtual
2041 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002042
2043 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044}
2045
2046bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002047 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048 return false;
2049 }
2050 media_channel()->SignalDataReceived.connect(
2051 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002052 media_channel()->SignalReadyToSend.connect(
2053 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002054 media_channel()->SignalStreamClosedRemotely.connect(
2055 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056 return true;
2057}
2058
2059bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002060 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002062 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2063 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064}
2065
2066const ContentInfo* DataChannel::GetFirstContent(
2067 const SessionDescription* sdesc) {
2068 return GetFirstDataContent(sdesc);
2069}
2070
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002071bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072 if (data_channel_type_ == DCT_SCTP) {
2073 // TODO(pthatcher): Do this in a more robust way by checking for
2074 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002075 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076 } else if (data_channel_type_ == DCT_RTP) {
2077 return BaseChannel::WantsPacket(rtcp, packet);
2078 }
2079 return false;
2080}
2081
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002082bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2083 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 // It hasn't been set before, so set it now.
2085 if (data_channel_type_ == DCT_NONE) {
2086 data_channel_type_ = new_data_channel_type;
2087 return true;
2088 }
2089
2090 // It's been set before, but doesn't match. That's bad.
2091 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002092 std::ostringstream desc;
2093 desc << "Data channel type mismatch."
2094 << " Expected " << data_channel_type_
2095 << " Got " << new_data_channel_type;
2096 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 return false;
2098 }
2099
2100 // It's hasn't changed. Nothing to do.
2101 return true;
2102}
2103
2104bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002105 const DataContentDescription* content,
2106 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2108 (content->protocol() == kMediaProtocolDtlsSctp));
2109 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002110 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111}
2112
2113bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002114 ContentAction action,
2115 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002116 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002117 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118 LOG(LS_INFO) << "Setting local data description";
2119
2120 const DataContentDescription* data =
2121 static_cast<const DataContentDescription*>(content);
2122 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002123 if (!data) {
2124 SafeSetError("Can't find data content in local description.", error_desc);
2125 return false;
2126 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002127
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002128 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129 return false;
2130 }
2131
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002132 if (data_channel_type_ == DCT_RTP) {
2133 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2134 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 }
2136 }
2137
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002138 // FYI: We send the SCTP port number (not to be confused with the
2139 // underlying UDP port number) as a codec parameter. So even SCTP
2140 // data channels need codecs.
2141 DataRecvParameters recv_params = last_recv_params_;
2142 RtpParametersFromMediaDescription(data, &recv_params);
2143 if (!media_channel()->SetRecvParameters(recv_params)) {
2144 SafeSetError("Failed to set remote data description recv parameters.",
2145 error_desc);
2146 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002148 if (data_channel_type_ == DCT_RTP) {
2149 for (const DataCodec& codec : data->codecs()) {
2150 bundle_filter()->AddPayloadType(codec.id);
2151 }
2152 }
2153 last_recv_params_ = recv_params;
2154
2155 // TODO(pthatcher): Move local streams into DataSendParameters, and
2156 // only give it to the media channel once we have a remote
2157 // description too (without a remote description, we won't be able
2158 // to send them anyway).
2159 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2160 SafeSetError("Failed to set local data description streams.", error_desc);
2161 return false;
2162 }
2163
2164 set_local_content_direction(content->direction());
2165 ChangeState();
2166 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167}
2168
2169bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002170 ContentAction action,
2171 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002172 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002173 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174
2175 const DataContentDescription* data =
2176 static_cast<const DataContentDescription*>(content);
2177 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002178 if (!data) {
2179 SafeSetError("Can't find data content in remote description.", error_desc);
2180 return false;
2181 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002183 // If the remote data doesn't have codecs and isn't an update, it
2184 // must be empty, so ignore it.
2185 if (!data->has_codecs() && action != CA_UPDATE) {
2186 return true;
2187 }
2188
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002189 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002190 return false;
2191 }
2192
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002193 LOG(LS_INFO) << "Setting remote data description";
2194 if (data_channel_type_ == DCT_RTP &&
2195 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2196 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 }
2198
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002199
2200 DataSendParameters send_params = last_send_params_;
2201 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2202 if (!media_channel()->SetSendParameters(send_params)) {
2203 SafeSetError("Failed to set remote data description send parameters.",
2204 error_desc);
2205 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002207 last_send_params_ = send_params;
2208
2209 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2210 // and only give it to the media channel once we have a local
2211 // description too (without a local description, we won't be able to
2212 // recv them anyway).
2213 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2214 SafeSetError("Failed to set remote data description streams.",
2215 error_desc);
2216 return false;
2217 }
2218
2219 set_remote_content_direction(content->direction());
2220 ChangeState();
2221 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222}
2223
2224void DataChannel::ChangeState() {
2225 // Render incoming data if we're the active call, and we have the local
2226 // content. We receive data on the default channel and multiplexed streams.
2227 bool recv = IsReadyToReceive();
2228 if (!media_channel()->SetReceive(recv)) {
2229 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2230 }
2231
2232 // Send outgoing data if we're the active call, we have the remote content,
2233 // and we have had some form of connectivity.
2234 bool send = IsReadyToSend();
2235 if (!media_channel()->SetSend(send)) {
2236 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2237 }
2238
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002239 // Trigger SignalReadyToSendData asynchronously.
2240 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002241
2242 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2243}
2244
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002245void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002246 switch (pmsg->message_id) {
2247 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002248 DataChannelReadyToSendMessageData* data =
2249 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002250 ready_to_send_data_ = data->data();
2251 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252 delete data;
2253 break;
2254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002255 case MSG_DATARECEIVED: {
2256 DataReceivedMessageData* data =
2257 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2258 SignalDataReceived(this, data->params, data->payload);
2259 delete data;
2260 break;
2261 }
2262 case MSG_CHANNEL_ERROR: {
2263 const DataChannelErrorMessageData* data =
2264 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002265 delete data;
2266 break;
2267 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002268 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002269 rtc::TypedMessageData<uint32_t>* data =
2270 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002271 SignalStreamClosedRemotely(data->data());
2272 delete data;
2273 break;
2274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275 default:
2276 BaseChannel::OnMessage(pmsg);
2277 break;
2278 }
2279}
2280
2281void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002282 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002283 SignalConnectionMonitor(this, infos);
2284}
2285
2286void DataChannel::StartMediaMonitor(int cms) {
2287 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002288 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289 media_monitor_->SignalUpdate.connect(
2290 this, &DataChannel::OnMediaMonitorUpdate);
2291 media_monitor_->Start(cms);
2292}
2293
2294void DataChannel::StopMediaMonitor() {
2295 if (media_monitor_) {
2296 media_monitor_->Stop();
2297 media_monitor_->SignalUpdate.disconnect(this);
2298 media_monitor_.reset();
2299 }
2300}
2301
2302void DataChannel::OnMediaMonitorUpdate(
2303 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2304 ASSERT(media_channel == this->media_channel());
2305 SignalMediaMonitor(this, info);
2306}
2307
2308void DataChannel::OnDataReceived(
2309 const ReceiveDataParams& params, const char* data, size_t len) {
2310 DataReceivedMessageData* msg = new DataReceivedMessageData(
2311 params, data, len);
2312 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2313}
2314
Peter Boström0c4e06b2015-10-07 12:23:21 +02002315void DataChannel::OnDataChannelError(uint32_t ssrc,
2316 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2318 ssrc, err);
2319 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2320}
2321
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002322void DataChannel::OnDataChannelReadyToSend(bool writable) {
2323 // This is usded for congestion control to indicate that the stream is ready
2324 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2325 // that the transport channel is ready.
2326 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2327 new DataChannelReadyToSendMessageData(writable));
2328}
2329
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002330void DataChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2331 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332}
2333
2334bool DataChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08002335 return (data_channel_type_ == DCT_RTP) && BaseChannel::ShouldSetupDtlsSrtp();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002336}
2337
Peter Boström0c4e06b2015-10-07 12:23:21 +02002338void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2339 rtc::TypedMessageData<uint32_t>* message =
2340 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002341 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2342}
2343
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002344} // namespace cricket