blob: 91808524e176be6ec0b46bd40042643048cfad59 [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
28#include "talk/session/media/channel.h"
29
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000030#include "talk/media/base/constants.h"
31#include "talk/media/base/rtputils.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000032#include "talk/session/media/channelmanager.h"
Tommif888bb52015-12-12 01:37:01 +010033#include "webrtc/audio/audio_sink.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000034#include "webrtc/base/bind.h"
35#include "webrtc/base/buffer.h"
36#include "webrtc/base/byteorder.h"
37#include "webrtc/base/common.h"
38#include "webrtc/base/dscp.h"
39#include "webrtc/base/logging.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010040#include "webrtc/base/trace_event.h"
41#include "webrtc/p2p/base/transportchannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
43namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000044using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000045
Tommif888bb52015-12-12 01:37:01 +010046namespace {
47// See comment below for why we need to use a pointer to a scoped_ptr.
48bool SetRawAudioSink_w(VoiceMediaChannel* channel,
49 uint32_t ssrc,
50 rtc::scoped_ptr<webrtc::AudioSinkInterface>* sink) {
51 channel->SetRawAudioSink(ssrc, std::move(*sink));
52 return true;
53}
54} // namespace
55
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000057 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 MSG_SCREENCASTWINDOWEVENT,
59 MSG_RTPPACKET,
60 MSG_RTCPPACKET,
61 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000065 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066};
67
68// Value specified in RFC 5764.
69static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
70
71static const int kAgcMinus10db = -10;
72
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000073static void SafeSetError(const std::string& message, std::string* error_desc) {
74 if (error_desc) {
75 *error_desc = message;
76 }
77}
78
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079struct PacketMessageData : public rtc::MessageData {
80 rtc::Buffer packet;
stefanc1aeaf02015-10-15 07:26:07 -070081 rtc::PacketOptions options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082};
83
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084struct ScreencastEventMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 ScreencastEventMessageData(uint32_t s, rtc::WindowEvent we)
86 : ssrc(s), event(we) {}
87 uint32_t ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089};
90
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020092 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020094 : ssrc(in_ssrc), error(in_error) {}
95 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 VoiceMediaChannel::Error error;
97};
98
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000099struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 : ssrc(in_ssrc), error(in_error) {}
103 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 VideoMediaChannel::Error error;
105};
106
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000107struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200108 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200110 : ssrc(in_ssrc), error(in_error) {}
111 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 DataMediaChannel::Error error;
113};
114
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000116struct VideoChannel::ScreencastDetailsData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200117 explicit ScreencastDetailsData(uint32_t s)
118 : ssrc(s), fps(0), screencast_max_pixels(0) {}
119 uint32_t ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000120 int fps;
121 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122};
123
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124static const char* PacketType(bool rtcp) {
125 return (!rtcp) ? "RTP" : "RTCP";
126}
127
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000128static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 // Check the packet size. We could check the header too if needed.
130 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000131 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
132 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133}
134
135static bool IsReceiveContentDirection(MediaContentDirection direction) {
136 return direction == MD_SENDRECV || direction == MD_RECVONLY;
137}
138
139static bool IsSendContentDirection(MediaContentDirection direction) {
140 return direction == MD_SENDRECV || direction == MD_SENDONLY;
141}
142
143static const MediaContentDescription* GetContentDescription(
144 const ContentInfo* cinfo) {
145 if (cinfo == NULL)
146 return NULL;
147 return static_cast<const MediaContentDescription*>(cinfo->description);
148}
149
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700150template <class Codec>
151void RtpParametersFromMediaDescription(
152 const MediaContentDescriptionImpl<Codec>* desc,
153 RtpParameters<Codec>* params) {
154 // TODO(pthatcher): Remove this once we're sure no one will give us
155 // a description without codecs (currently a CA_UPDATE with just
156 // streams can).
157 if (desc->has_codecs()) {
158 params->codecs = desc->codecs();
159 }
160 // TODO(pthatcher): See if we really need
161 // rtp_header_extensions_set() and remove it if we don't.
162 if (desc->rtp_header_extensions_set()) {
163 params->extensions = desc->rtp_header_extensions();
164 }
deadbeef13871492015-12-09 12:37:51 -0800165 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700166}
167
168template <class Codec, class Options>
169void RtpSendParametersFromMediaDescription(
170 const MediaContentDescriptionImpl<Codec>* desc,
171 RtpSendParameters<Codec, Options>* send_params) {
172 RtpParametersFromMediaDescription(desc, send_params);
173 send_params->max_bandwidth_bps = desc->bandwidth();
174}
175
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000176BaseChannel::BaseChannel(rtc::Thread* thread,
deadbeefcbecd352015-09-23 11:50:27 -0700177 MediaChannel* media_channel,
178 TransportController* transport_controller,
179 const std::string& content_name,
180 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 : worker_thread_(thread),
deadbeefcbecd352015-09-23 11:50:27 -0700182 transport_controller_(transport_controller),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 media_channel_(media_channel),
184 content_name_(content_name),
deadbeefcbecd352015-09-23 11:50:27 -0700185 rtcp_transport_enabled_(rtcp),
186 transport_channel_(nullptr),
187 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 enabled_(false),
189 writable_(false),
190 rtp_ready_to_send_(false),
191 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 was_ever_writable_(false),
193 local_content_direction_(MD_INACTIVE),
194 remote_content_direction_(MD_INACTIVE),
195 has_received_packet_(false),
196 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000197 secure_required_(false),
198 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000199 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 LOG(LS_INFO) << "Created channel for " << content_name;
201}
202
203BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000204 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000205 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 StopConnectionMonitor();
207 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000208 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 // We must destroy the media channel before the transport channel, otherwise
210 // the media channel may try to send on the dead transport channel. NULLing
211 // is not an effective strategy since the sends will come on another thread.
212 delete media_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700213 // Note that we don't just call set_transport_channel(nullptr) because that
214 // would call a pure virtual method which we can't do from a destructor.
215 if (transport_channel_) {
216 DisconnectFromTransportChannel(transport_channel_);
217 transport_controller_->DestroyTransportChannel_w(
218 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
219 }
220 if (rtcp_transport_channel_) {
221 DisconnectFromTransportChannel(rtcp_transport_channel_);
222 transport_controller_->DestroyTransportChannel_w(
223 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
224 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 LOG(LS_INFO) << "Destroyed channel";
226}
227
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000228bool BaseChannel::Init() {
deadbeefcbecd352015-09-23 11:50:27 -0700229 if (!SetTransport(content_name())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 return false;
231 }
232
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800233 if (!SetDtlsSrtpCryptoSuites(transport_channel(), false)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000234 return false;
235 }
deadbeefcbecd352015-09-23 11:50:27 -0700236 if (rtcp_transport_enabled() &&
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800237 !SetDtlsSrtpCryptoSuites(rtcp_transport_channel(), true)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000238 return false;
239 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240
wu@webrtc.orgde305012013-10-31 15:40:38 +0000241 // Both RTP and RTCP channels are set, we can call SetInterface on
242 // media channel and it can set network options.
243 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 return true;
245}
246
wu@webrtc.org78187522013-10-07 23:32:02 +0000247void BaseChannel::Deinit() {
248 media_channel_->SetInterface(NULL);
249}
250
deadbeefcbecd352015-09-23 11:50:27 -0700251bool BaseChannel::SetTransport(const std::string& transport_name) {
252 return worker_thread_->Invoke<bool>(
253 Bind(&BaseChannel::SetTransport_w, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000254}
255
deadbeefcbecd352015-09-23 11:50:27 -0700256bool BaseChannel::SetTransport_w(const std::string& transport_name) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000257 ASSERT(worker_thread_ == rtc::Thread::Current());
258
deadbeefcbecd352015-09-23 11:50:27 -0700259 if (transport_name == transport_name_) {
260 // Nothing to do if transport name isn't changing
261 return true;
262 }
263
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800264 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
265 // changes and wait until the DTLS handshake is complete to set the newly
266 // negotiated parameters.
267 if (ShouldSetupDtlsSrtp()) {
268 srtp_filter_.ResetParams();
269 }
270
deadbeefcbecd352015-09-23 11:50:27 -0700271 set_transport_channel(transport_controller_->CreateTransportChannel_w(
272 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000273 if (!transport_channel()) {
274 return false;
275 }
deadbeefcbecd352015-09-23 11:50:27 -0700276 if (rtcp_transport_enabled()) {
277 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
278 << " on " << transport_name << " transport ";
279 set_rtcp_transport_channel(transport_controller_->CreateTransportChannel_w(
280 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000281 if (!rtcp_transport_channel()) {
282 return false;
283 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000284 }
285
deadbeefcbecd352015-09-23 11:50:27 -0700286 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000287 return true;
288}
289
290void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
291 ASSERT(worker_thread_ == rtc::Thread::Current());
292
293 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700294 if (!old_tc && !new_tc) {
295 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000296 return;
297 }
deadbeefcbecd352015-09-23 11:50:27 -0700298 ASSERT(old_tc != new_tc);
299
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000300 if (old_tc) {
301 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700302 transport_controller_->DestroyTransportChannel_w(
303 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000304 }
305
306 transport_channel_ = new_tc;
307
308 if (new_tc) {
309 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700310 for (const auto& pair : socket_options_) {
311 new_tc->SetOption(pair.first, pair.second);
312 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000313 }
deadbeefcbecd352015-09-23 11:50:27 -0700314
315 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
316 // setting new channel
317 UpdateWritableState_w();
318 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000319}
320
321void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
322 ASSERT(worker_thread_ == rtc::Thread::Current());
323
324 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700325 if (!old_tc && !new_tc) {
326 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000327 return;
328 }
deadbeefcbecd352015-09-23 11:50:27 -0700329 ASSERT(old_tc != new_tc);
330
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000331 if (old_tc) {
332 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700333 transport_controller_->DestroyTransportChannel_w(
334 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000335 }
336
337 rtcp_transport_channel_ = new_tc;
338
339 if (new_tc) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800340 RTC_CHECK(!(ShouldSetupDtlsSrtp() && srtp_filter_.IsActive()))
341 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
342 << "should never happen.";
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000343 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700344 for (const auto& pair : rtcp_socket_options_) {
345 new_tc->SetOption(pair.first, pair.second);
346 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000347 }
deadbeefcbecd352015-09-23 11:50:27 -0700348
349 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
350 // setting new channel
351 UpdateWritableState_w();
352 SetReadyToSend(true, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000353}
354
355void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
356 ASSERT(worker_thread_ == rtc::Thread::Current());
357
358 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
359 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
360 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800361 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000362}
363
364void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
365 ASSERT(worker_thread_ == rtc::Thread::Current());
366
367 tc->SignalWritableState.disconnect(this);
368 tc->SignalReadPacket.disconnect(this);
369 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800370 tc->SignalDtlsState.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000371}
372
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000374 worker_thread_->Invoke<void>(Bind(
375 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
376 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377 return true;
378}
379
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000381 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382}
383
Peter Boström0c4e06b2015-10-07 12:23:21 +0200384bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000385 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386}
387
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000388bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000389 return InvokeOnWorker(
390 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000391}
392
Peter Boström0c4e06b2015-10-07 12:23:21 +0200393bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000394 return InvokeOnWorker(
395 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000396}
397
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000399 ContentAction action,
400 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100401 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000402 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
403 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404}
405
406bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000407 ContentAction action,
408 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100409 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000410 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
411 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412}
413
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000415 // We pass in the BaseChannel instead of the transport_channel_
416 // because if the transport_channel_ changes, the ConnectionMonitor
417 // would be pointing to the wrong TransportChannel.
418 connection_monitor_.reset(new ConnectionMonitor(
419 this, worker_thread(), rtc::Thread::Current()));
420 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000422 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423}
424
425void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000426 if (connection_monitor_) {
427 connection_monitor_->Stop();
428 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429 }
430}
431
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000432bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
433 ASSERT(worker_thread_ == rtc::Thread::Current());
434 return transport_channel_->GetStats(infos);
435}
436
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437bool BaseChannel::IsReadyToReceive() const {
438 // Receive data if we are enabled and have local content,
439 return enabled() && IsReceiveContentDirection(local_content_direction_);
440}
441
442bool BaseChannel::IsReadyToSend() const {
443 // Send outgoing data if we are enabled, have local and remote content,
444 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800445 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 IsSendContentDirection(local_content_direction_) &&
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800447 was_ever_writable() &&
448 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449}
450
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000451bool BaseChannel::SendPacket(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700452 const rtc::PacketOptions& options) {
453 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454}
455
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000456bool BaseChannel::SendRtcp(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700457 const rtc::PacketOptions& options) {
458 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459}
460
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000461int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000463 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000465 case ST_RTP:
466 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700467 socket_options_.push_back(
468 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000469 break;
470 case ST_RTCP:
471 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700472 rtcp_socket_options_.push_back(
473 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000474 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000476 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000477}
478
479void BaseChannel::OnWritableState(TransportChannel* channel) {
480 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
deadbeefcbecd352015-09-23 11:50:27 -0700481 UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482}
483
484void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000485 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000486 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000487 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100488 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000490 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
492 // When using RTCP multiplexing we might get RTCP packets on the RTP
493 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
494 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000495 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000496 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000497}
498
499void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700500 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
501 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502}
503
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800504void BaseChannel::OnDtlsState(TransportChannel* channel,
505 DtlsTransportState state) {
506 if (!ShouldSetupDtlsSrtp()) {
507 return;
508 }
509
510 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
511 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
512 // cover other scenarios like the whole channel is writable (not just this
513 // TransportChannel) or when TransportChannel is attached after DTLS is
514 // negotiated.
515 if (state != DTLS_TRANSPORT_CONNECTED) {
516 srtp_filter_.ResetParams();
517 }
518}
519
deadbeefcbecd352015-09-23 11:50:27 -0700520void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
521 if (rtcp) {
522 rtcp_ready_to_send_ = ready;
523 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 rtp_ready_to_send_ = ready;
525 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526
deadbeefcbecd352015-09-23 11:50:27 -0700527 if (rtp_ready_to_send_ &&
528 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
529 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
torbjornga81a42f2015-09-23 02:16:58 -0700530 // Notify the MediaChannel when both rtp and rtcp channel can send.
531 media_channel_->OnReadyToSend(true);
deadbeefcbecd352015-09-23 11:50:27 -0700532 } else {
533 // Notify the MediaChannel when either rtp or rtcp channel can't send.
534 media_channel_->OnReadyToSend(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 }
536}
537
538bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
539 const char* data, size_t len) {
540 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000541 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542}
543
stefanc1aeaf02015-10-15 07:26:07 -0700544bool BaseChannel::SendPacket(bool rtcp,
545 rtc::Buffer* packet,
546 const rtc::PacketOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 // SendPacket gets called from MediaEngine, typically on an encoder thread.
548 // If the thread is not our worker thread, we will post to our worker
549 // so that the real work happens on our worker. This avoids us having to
550 // synchronize access to all the pieces of the send path, including
551 // SRTP and the inner workings of the transport channels.
552 // The only downside is that we can't return a proper failure code if
553 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000554 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 // Avoid a copy by transferring the ownership of the packet data.
556 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
557 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200558 data->packet = packet->Pass();
stefanc1aeaf02015-10-15 07:26:07 -0700559 data->options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 worker_thread_->Post(this, message_id, data);
561 return true;
562 }
563
564 // Now that we are on the correct thread, ensure we have a place to send this
565 // packet before doing anything. (We might get RTCP packets that we don't
566 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
567 // transport.
568 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
569 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000570 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 return false;
572 }
573
574 // Protect ourselves against crazy data.
575 if (!ValidPacket(rtcp, packet)) {
576 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000577 << PacketType(rtcp)
578 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 return false;
580 }
581
stefanc1aeaf02015-10-15 07:26:07 -0700582 rtc::PacketOptions updated_options;
583 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000584 // Protect if needed.
585 if (srtp_filter_.IsActive()) {
586 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200587 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000588 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000590 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
591 // inside libsrtp for a RTP packet. A external HMAC module will be writing
592 // a fake HMAC value. This is ONLY done for a RTP packet.
593 // Socket layer will update rtp sendtime extension header if present in
594 // packet with current time before updating the HMAC.
595#if !defined(ENABLE_EXTERNAL_AUTH)
596 res = srtp_filter_.ProtectRtp(
597 data, len, static_cast<int>(packet->capacity()), &len);
598#else
stefanc1aeaf02015-10-15 07:26:07 -0700599 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000600 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000601 res = srtp_filter_.ProtectRtp(
602 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700603 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000604 // If protection succeeds, let's get auth params from srtp.
605 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200606 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000607 int key_len;
608 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700609 &auth_key, &key_len,
610 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000611 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700612 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
613 updated_options.packet_time_params.srtp_auth_key.assign(
614 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000615 }
616 }
617#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000618 if (!res) {
619 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200620 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 GetRtpSeqNum(data, len, &seq_num);
622 GetRtpSsrc(data, len, &ssrc);
623 LOG(LS_ERROR) << "Failed to protect " << content_name_
624 << " RTP packet: size=" << len
625 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
626 return false;
627 }
628 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000629 res = srtp_filter_.ProtectRtcp(data, len,
630 static_cast<int>(packet->capacity()),
631 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 if (!res) {
633 int type = -1;
634 GetRtcpType(data, len, &type);
635 LOG(LS_ERROR) << "Failed to protect " << content_name_
636 << " RTCP packet: size=" << len << ", type=" << type;
637 return false;
638 }
639 }
640
641 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000642 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 } else if (secure_required_) {
644 // This is a double check for something that supposedly can't happen.
645 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
646 << " packet when SRTP is inactive and crypto is required";
647
648 ASSERT(false);
649 return false;
650 }
651
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000653 int ret =
stefanc1aeaf02015-10-15 07:26:07 -0700654 channel->SendPacket(packet->data<char>(), packet->size(), updated_options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000655 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
656 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 if (channel->GetError() == EWOULDBLOCK) {
658 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700659 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 }
661 return false;
662 }
663 return true;
664}
665
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000666bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 // Protect ourselves against crazy data.
668 if (!ValidPacket(rtcp, packet)) {
669 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000670 << PacketType(rtcp)
671 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000672 return false;
673 }
pbos482b12e2015-11-16 10:19:58 -0800674 if (rtcp) {
675 // Permit all (seemingly valid) RTCP packets.
676 return true;
677 }
678 // Check whether we handle this payload.
679 return bundle_filter_.DemuxPacket(packet->data<uint8_t>(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680}
681
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000682void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
683 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 if (!WantsPacket(rtcp, packet)) {
685 return;
686 }
687
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000688 // We are only interested in the first rtp packet because that
689 // indicates the media has started flowing.
690 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000691 has_received_packet_ = true;
692 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
693 }
694
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 // Unprotect the packet, if needed.
696 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200697 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000698 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 bool res;
700 if (!rtcp) {
701 res = srtp_filter_.UnprotectRtp(data, len, &len);
702 if (!res) {
703 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200704 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 GetRtpSeqNum(data, len, &seq_num);
706 GetRtpSsrc(data, len, &ssrc);
707 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
708 << " RTP packet: size=" << len
709 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
710 return;
711 }
712 } else {
713 res = srtp_filter_.UnprotectRtcp(data, len, &len);
714 if (!res) {
715 int type = -1;
716 GetRtcpType(data, len, &type);
717 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
718 << " RTCP packet: size=" << len << ", type=" << type;
719 return;
720 }
721 }
722
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000723 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 } else if (secure_required_) {
725 // Our session description indicates that SRTP is required, but we got a
726 // packet before our SRTP filter is active. This means either that
727 // a) we got SRTP packets before we received the SDES keys, in which case
728 // we can't decrypt it anyway, or
729 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
730 // channels, so we haven't yet extracted keys, even if DTLS did complete
731 // on the channel that the packets are being sent on. It's really good
732 // practice to wait for both RTP and RTCP to be good to go before sending
733 // media, to prevent weird failure modes, so it's fine for us to just eat
734 // packets here. This is all sidestepped if RTCP mux is used anyway.
735 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
736 << " packet when SRTP is inactive and crypto is required";
737 return;
738 }
739
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 // Push it down to the media channel.
741 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000742 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000744 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 }
746}
747
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000748bool BaseChannel::PushdownLocalDescription(
749 const SessionDescription* local_desc, ContentAction action,
750 std::string* error_desc) {
751 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000752 const MediaContentDescription* content_desc =
753 GetContentDescription(content_info);
754 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000755 !SetLocalContent(content_desc, action, error_desc)) {
756 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
757 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000759 return true;
760}
761
762bool BaseChannel::PushdownRemoteDescription(
763 const SessionDescription* remote_desc, ContentAction action,
764 std::string* error_desc) {
765 const ContentInfo* content_info = GetFirstContent(remote_desc);
766 const MediaContentDescription* content_desc =
767 GetContentDescription(content_info);
768 if (content_desc && content_info && !content_info->rejected &&
769 !SetRemoteContent(content_desc, action, error_desc)) {
770 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
771 return false;
772 }
773 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774}
775
776void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000777 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000778 if (enabled_)
779 return;
780
781 LOG(LS_INFO) << "Channel enabled";
782 enabled_ = true;
783 ChangeState();
784}
785
786void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000787 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 if (!enabled_)
789 return;
790
791 LOG(LS_INFO) << "Channel disabled";
792 enabled_ = false;
793 ChangeState();
794}
795
deadbeefcbecd352015-09-23 11:50:27 -0700796void BaseChannel::UpdateWritableState_w() {
797 if (transport_channel_ && transport_channel_->writable() &&
798 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
799 ChannelWritable_w();
800 } else {
801 ChannelNotWritable_w();
802 }
803}
804
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000805void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000806 ASSERT(worker_thread_ == rtc::Thread::Current());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800807 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000808 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800809 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000810
deadbeefcbecd352015-09-23 11:50:27 -0700811 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 << (was_ever_writable_ ? "" : " for the first time");
813
814 std::vector<ConnectionInfo> infos;
815 transport_channel_->GetStats(&infos);
816 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
817 it != infos.end(); ++it) {
818 if (it->best_connection) {
819 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
820 << "->" << it->remote_candidate.ToSensitiveString();
821 break;
822 }
823 }
824
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825 was_ever_writable_ = true;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800826 MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000827 writable_ = true;
828 ChangeState();
829}
830
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000831void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
832 ASSERT(worker_thread() == rtc::Thread::Current());
833 signaling_thread()->Invoke<void>(Bind(
834 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
835}
836
837void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
838 ASSERT(signaling_thread() == rtc::Thread::Current());
839 SignalDtlsSetupFailure(this, rtcp);
840}
841
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800842bool BaseChannel::SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp) {
843 std::vector<int> crypto_suites;
844 // We always use the default SRTP crypto suites for RTCP, but we may use
845 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846 if (!rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800847 GetSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800849 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800851 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852}
853
854bool BaseChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800855 // Since DTLS is applied to all channels, checking RTP should be enough.
856 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857}
858
859// This function returns true if either DTLS-SRTP is not in use
860// *or* DTLS-SRTP is successfully set up.
861bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
862 bool ret = false;
863
deadbeefcbecd352015-09-23 11:50:27 -0700864 TransportChannel* channel =
865 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000866
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800867 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000868
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800869 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800871 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
872 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873 return false;
874 }
875
876 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
877 << content_name() << " "
878 << PacketType(rtcp_channel);
879
880 // OK, we're now doing DTLS (RFC 5764)
881 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
882 SRTP_MASTER_KEY_SALT_LEN * 2);
883
884 // RFC 5705 exporter using the RFC 5764 parameters
885 if (!channel->ExportKeyingMaterial(
886 kDtlsSrtpExporterLabel,
887 NULL, 0, false,
888 &dtls_buffer[0], dtls_buffer.size())) {
889 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
890 ASSERT(false); // This should never happen
891 return false;
892 }
893
894 // Sync up the keys with the DTLS-SRTP interface
895 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
896 SRTP_MASTER_KEY_SALT_LEN);
897 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
898 SRTP_MASTER_KEY_SALT_LEN);
899 size_t offset = 0;
900 memcpy(&client_write_key[0], &dtls_buffer[offset],
901 SRTP_MASTER_KEY_KEY_LEN);
902 offset += SRTP_MASTER_KEY_KEY_LEN;
903 memcpy(&server_write_key[0], &dtls_buffer[offset],
904 SRTP_MASTER_KEY_KEY_LEN);
905 offset += SRTP_MASTER_KEY_KEY_LEN;
906 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
907 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
908 offset += SRTP_MASTER_KEY_SALT_LEN;
909 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
910 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
911
912 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000913 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000914 if (!channel->GetSslRole(&role)) {
915 LOG(LS_WARNING) << "GetSslRole failed";
916 return false;
917 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000919 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 send_key = &server_write_key;
921 recv_key = &client_write_key;
922 } else {
923 send_key = &client_write_key;
924 recv_key = &server_write_key;
925 }
926
927 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800928 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
929 static_cast<int>(send_key->size()),
930 selected_crypto_suite, &(*recv_key)[0],
931 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800933 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
934 static_cast<int>(send_key->size()),
935 selected_crypto_suite, &(*recv_key)[0],
936 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 }
938
939 if (!ret)
940 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
941 else
942 dtls_keyed_ = true;
943
944 return ret;
945}
946
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800947void BaseChannel::MaybeSetupDtlsSrtp_w() {
948 if (srtp_filter_.IsActive()) {
949 return;
950 }
951
952 if (!ShouldSetupDtlsSrtp()) {
953 return;
954 }
955
956 if (!SetupDtlsSrtp(false)) {
957 SignalDtlsSetupFailure_w(false);
958 return;
959 }
960
961 if (rtcp_transport_channel_) {
962 if (!SetupDtlsSrtp(true)) {
963 SignalDtlsSetupFailure_w(true);
964 return;
965 }
966 }
967}
968
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000970 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 if (!writable_)
972 return;
973
deadbeefcbecd352015-09-23 11:50:27 -0700974 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 writable_ = false;
976 ChangeState();
977}
978
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700979bool BaseChannel::SetRtpTransportParameters_w(
980 const MediaContentDescription* content,
981 ContentAction action,
982 ContentSource src,
983 std::string* error_desc) {
984 if (action == CA_UPDATE) {
985 // These parameters never get changed by a CA_UDPATE.
986 return true;
987 }
988
989 // Cache secure_required_ for belt and suspenders check on SendPacket
990 if (src == CS_LOCAL) {
991 set_secure_required(content->crypto_required() != CT_NONE);
992 }
993
994 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
995 return false;
996 }
997
998 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
999 return false;
1000 }
1001
1002 return true;
1003}
1004
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001005// |dtls| will be set to true if DTLS is active for transport channel and
1006// crypto is empty.
1007bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001008 bool* dtls,
1009 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001010 *dtls = transport_channel_->IsDtlsActive();
1011 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001012 SafeSetError("Cryptos must be empty when DTLS is active.",
1013 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001014 return false;
1015 }
1016 return true;
1017}
1018
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001020 ContentAction action,
1021 ContentSource src,
1022 std::string* error_desc) {
1023 if (action == CA_UPDATE) {
1024 // no crypto params.
1025 return true;
1026 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001028 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001029 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
1030 if (!ret) {
1031 return false;
1032 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 switch (action) {
1034 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001035 // If DTLS is already active on the channel, we could be renegotiating
1036 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001037 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001038 ret = srtp_filter_.SetOffer(cryptos, src);
1039 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 break;
1041 case CA_PRANSWER:
1042 // If we're doing DTLS-SRTP, we don't want to update the filter
1043 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001044 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1046 }
1047 break;
1048 case CA_ANSWER:
1049 // If we're doing DTLS-SRTP, we don't want to update the filter
1050 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001051 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 ret = srtp_filter_.SetAnswer(cryptos, src);
1053 }
1054 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 default:
1056 break;
1057 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001058 if (!ret) {
1059 SafeSetError("Failed to setup SRTP filter.", error_desc);
1060 return false;
1061 }
1062 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063}
1064
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001065void BaseChannel::ActivateRtcpMux() {
1066 worker_thread_->Invoke<void>(Bind(
1067 &BaseChannel::ActivateRtcpMux_w, this));
1068}
1069
1070void BaseChannel::ActivateRtcpMux_w() {
1071 if (!rtcp_mux_filter_.IsActive()) {
1072 rtcp_mux_filter_.SetActive();
deadbeefcbecd352015-09-23 11:50:27 -07001073 set_rtcp_transport_channel(nullptr);
1074 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001075 }
1076}
1077
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001079 ContentSource src,
1080 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001081 bool ret = false;
1082 switch (action) {
1083 case CA_OFFER:
1084 ret = rtcp_mux_filter_.SetOffer(enable, src);
1085 break;
1086 case CA_PRANSWER:
1087 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1088 break;
1089 case CA_ANSWER:
1090 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1091 if (ret && rtcp_mux_filter_.IsActive()) {
1092 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001093 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1094 << " by destroying RTCP transport channel for "
1095 << transport_name();
1096 set_rtcp_transport_channel(nullptr);
1097 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 }
1099 break;
1100 case CA_UPDATE:
1101 // No RTCP mux info.
1102 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001103 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 default:
1105 break;
1106 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001107 if (!ret) {
1108 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1109 return false;
1110 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1112 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1113 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001114 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 // If the RTP transport is already writable, then so are we.
1116 if (transport_channel_->writable()) {
1117 ChannelWritable_w();
1118 }
1119 }
1120
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001121 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122}
1123
1124bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001125 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001126 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127}
1128
Peter Boström0c4e06b2015-10-07 12:23:21 +02001129bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001130 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 return media_channel()->RemoveRecvStream(ssrc);
1132}
1133
1134bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001135 ContentAction action,
1136 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1138 action == CA_PRANSWER || action == CA_UPDATE))
1139 return false;
1140
1141 // If this is an update, streams only contain streams that have changed.
1142 if (action == CA_UPDATE) {
1143 for (StreamParamsVec::const_iterator it = streams.begin();
1144 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001145 const StreamParams* existing_stream =
1146 GetStreamByIds(local_streams_, it->groupid, it->id);
1147 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 if (media_channel()->AddSendStream(*it)) {
1149 local_streams_.push_back(*it);
1150 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1151 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001152 std::ostringstream desc;
1153 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1154 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001155 return false;
1156 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001157 } else if (existing_stream && !it->has_ssrcs()) {
1158 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001159 std::ostringstream desc;
1160 desc << "Failed to remove send stream with ssrc "
1161 << it->first_ssrc() << ".";
1162 SafeSetError(desc.str(), error_desc);
1163 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001165 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 } else {
1167 LOG(LS_WARNING) << "Ignore unsupported stream update";
1168 }
1169 }
1170 return true;
1171 }
1172 // Else streams are all the streams we want to send.
1173
1174 // Check for streams that have been removed.
1175 bool ret = true;
1176 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1177 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001178 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001180 std::ostringstream desc;
1181 desc << "Failed to remove send stream with ssrc "
1182 << it->first_ssrc() << ".";
1183 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 ret = false;
1185 }
1186 }
1187 }
1188 // Check for new streams.
1189 for (StreamParamsVec::const_iterator it = streams.begin();
1190 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001191 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001193 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001195 std::ostringstream desc;
1196 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1197 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 ret = false;
1199 }
1200 }
1201 }
1202 local_streams_ = streams;
1203 return ret;
1204}
1205
1206bool BaseChannel::UpdateRemoteStreams_w(
1207 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001208 ContentAction action,
1209 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1211 action == CA_PRANSWER || action == CA_UPDATE))
1212 return false;
1213
1214 // If this is an update, streams only contain streams that have changed.
1215 if (action == CA_UPDATE) {
1216 for (StreamParamsVec::const_iterator it = streams.begin();
1217 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001218 const StreamParams* existing_stream =
1219 GetStreamByIds(remote_streams_, it->groupid, it->id);
1220 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 if (AddRecvStream_w(*it)) {
1222 remote_streams_.push_back(*it);
1223 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1224 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001225 std::ostringstream desc;
1226 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1227 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 return false;
1229 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001230 } else if (existing_stream && !it->has_ssrcs()) {
1231 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001232 std::ostringstream desc;
1233 desc << "Failed to remove remote stream with ssrc "
1234 << it->first_ssrc() << ".";
1235 SafeSetError(desc.str(), error_desc);
1236 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001238 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 } else {
1240 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001241 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 << " new stream = " << it->ToString();
1243 }
1244 }
1245 return true;
1246 }
1247 // Else streams are all the streams we want to receive.
1248
1249 // Check for streams that have been removed.
1250 bool ret = true;
1251 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1252 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001253 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001255 std::ostringstream desc;
1256 desc << "Failed to remove remote stream with ssrc "
1257 << it->first_ssrc() << ".";
1258 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 ret = false;
1260 }
1261 }
1262 }
1263 // Check for new streams.
1264 for (StreamParamsVec::const_iterator it = streams.begin();
1265 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001266 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267 if (AddRecvStream_w(*it)) {
1268 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1269 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001270 std::ostringstream desc;
1271 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1272 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273 ret = false;
1274 }
1275 }
1276 }
1277 remote_streams_ = streams;
1278 return ret;
1279}
1280
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001281void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1282 const std::vector<RtpHeaderExtension>& extensions) {
1283 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001284 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001285 rtp_abs_sendtime_extn_id_ =
1286 send_time_extension ? send_time_extension->id : -1;
1287}
1288
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001289void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001290 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 case MSG_RTPPACKET:
1293 case MSG_RTCPPACKET: {
1294 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
stefanc1aeaf02015-10-15 07:26:07 -07001295 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet,
1296 data->options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 delete data; // because it is Posted
1298 break;
1299 }
1300 case MSG_FIRSTPACKETRECEIVED: {
1301 SignalFirstPacketReceived(this);
1302 break;
1303 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 }
1305}
1306
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307void BaseChannel::FlushRtcpMessages() {
1308 // Flush all remaining RTCP messages. This should only be called in
1309 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001310 ASSERT(rtc::Thread::Current() == worker_thread_);
1311 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001312 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001313 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001315 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 }
1317}
1318
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001319VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320 MediaEngineInterface* media_engine,
1321 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001322 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 const std::string& content_name,
1324 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001325 : BaseChannel(thread,
1326 media_channel,
1327 transport_controller,
1328 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001330 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001331 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332
1333VoiceChannel::~VoiceChannel() {
1334 StopAudioMonitor();
1335 StopMediaMonitor();
1336 // this can't be done in the base class, since it calls a virtual
1337 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001338 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339}
1340
1341bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001342 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 return false;
1344 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 return true;
1346}
1347
Peter Boström0c4e06b2015-10-07 12:23:21 +02001348bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001349 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001350 const AudioOptions* options,
1351 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001352 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001353 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354}
1355
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356// TODO(juberti): Handle early media the right way. We should get an explicit
1357// ringing message telling us to start playing local ringback, which we cancel
1358// if any early media actually arrives. For now, we do the opposite, which is
1359// to wait 1 second for early media, and start playing local ringback if none
1360// arrives.
1361void VoiceChannel::SetEarlyMedia(bool enable) {
1362 if (enable) {
1363 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001364 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1365 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 } else {
1367 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001368 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 }
1370}
1371
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001373 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1374 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001375}
1376
Peter Boström0c4e06b2015-10-07 12:23:21 +02001377bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1378 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001379 int duration) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001380 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
solenberg1d63dd02015-12-02 12:35:09 -08001381 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382}
1383
solenberg4bac9c52015-10-09 02:32:53 -07001384bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1385 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1386 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001388
Tommif888bb52015-12-12 01:37:01 +01001389void VoiceChannel::SetRawAudioSink(
1390 uint32_t ssrc,
1391 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
1392 // We need to work around Bind's lack of support for scoped_ptr and ownership
1393 // passing. So we invoke to our own little routine that gets a pointer to
1394 // our local variable. This is OK since we're synchronously invoking.
1395 InvokeOnWorker(Bind(&SetRawAudioSink_w, media_channel(), ssrc, &sink));
1396}
1397
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001398bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001399 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1400 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001401}
1402
1403void VoiceChannel::StartMediaMonitor(int cms) {
1404 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001405 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001406 media_monitor_->SignalUpdate.connect(
1407 this, &VoiceChannel::OnMediaMonitorUpdate);
1408 media_monitor_->Start(cms);
1409}
1410
1411void VoiceChannel::StopMediaMonitor() {
1412 if (media_monitor_) {
1413 media_monitor_->Stop();
1414 media_monitor_->SignalUpdate.disconnect(this);
1415 media_monitor_.reset();
1416 }
1417}
1418
1419void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001420 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421 audio_monitor_
1422 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1423 audio_monitor_->Start(cms);
1424}
1425
1426void VoiceChannel::StopAudioMonitor() {
1427 if (audio_monitor_) {
1428 audio_monitor_->Stop();
1429 audio_monitor_.reset();
1430 }
1431}
1432
1433bool VoiceChannel::IsAudioMonitorRunning() const {
1434 return (audio_monitor_.get() != NULL);
1435}
1436
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001438 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439}
1440
1441int VoiceChannel::GetOutputLevel_w() {
1442 return media_channel()->GetOutputLevel();
1443}
1444
1445void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1446 media_channel()->GetActiveStreams(actives);
1447}
1448
1449void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001450 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001451 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001452 int flags) {
1453 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454
1455 // Set a flag when we've received an RTP packet. If we're waiting for early
1456 // media, this will disable the timeout.
1457 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1458 received_media_ = true;
1459 }
1460}
1461
1462void VoiceChannel::ChangeState() {
1463 // Render incoming data if we're the active call, and we have the local
1464 // content. We receive data on the default channel and multiplexed streams.
1465 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001466 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467
1468 // Send outgoing data if we're the active call, we have the remote content,
1469 // and we have had some form of connectivity.
1470 bool send = IsReadyToSend();
1471 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1472 if (!media_channel()->SetSend(send_flag)) {
1473 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 }
1475
1476 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1477}
1478
1479const ContentInfo* VoiceChannel::GetFirstContent(
1480 const SessionDescription* sdesc) {
1481 return GetFirstAudioContent(sdesc);
1482}
1483
1484bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001485 ContentAction action,
1486 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001487 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001488 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489 LOG(LS_INFO) << "Setting local voice description";
1490
1491 const AudioContentDescription* audio =
1492 static_cast<const AudioContentDescription*>(content);
1493 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001494 if (!audio) {
1495 SafeSetError("Can't find audio content in local description.", error_desc);
1496 return false;
1497 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001499 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1500 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501 }
1502
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001503 AudioRecvParameters recv_params = last_recv_params_;
1504 RtpParametersFromMediaDescription(audio, &recv_params);
1505 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001506 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001507 error_desc);
1508 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001510 for (const AudioCodec& codec : audio->codecs()) {
1511 bundle_filter()->AddPayloadType(codec.id);
1512 }
1513 last_recv_params_ = recv_params;
1514
1515 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1516 // only give it to the media channel once we have a remote
1517 // description too (without a remote description, we won't be able
1518 // to send them anyway).
1519 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1520 SafeSetError("Failed to set local audio description streams.", error_desc);
1521 return false;
1522 }
1523
1524 set_local_content_direction(content->direction());
1525 ChangeState();
1526 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527}
1528
1529bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001530 ContentAction action,
1531 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001532 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001533 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534 LOG(LS_INFO) << "Setting remote voice description";
1535
1536 const AudioContentDescription* audio =
1537 static_cast<const AudioContentDescription*>(content);
1538 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001539 if (!audio) {
1540 SafeSetError("Can't find audio content in remote description.", error_desc);
1541 return false;
1542 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001544 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1545 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546 }
1547
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001548 AudioSendParameters send_params = last_send_params_;
1549 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001550 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001551 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001552 }
1553 if (!media_channel()->SetSendParameters(send_params)) {
1554 SafeSetError("Failed to set remote audio description send parameters.",
1555 error_desc);
1556 return false;
1557 }
1558 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001560 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1561 // and only give it to the media channel once we have a local
1562 // description too (without a local description, we won't be able to
1563 // recv them anyway).
1564 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1565 SafeSetError("Failed to set remote audio description streams.", error_desc);
1566 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 }
1568
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001569 if (audio->rtp_header_extensions_set()) {
1570 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1571 }
1572
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001573 set_remote_content_direction(content->direction());
1574 ChangeState();
1575 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576}
1577
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578void VoiceChannel::HandleEarlyMediaTimeout() {
1579 // This occurs on the main thread, not the worker thread.
1580 if (!received_media_) {
1581 LOG(LS_INFO) << "No early media received before timeout";
1582 SignalEarlyMediaTimeout(this);
1583 }
1584}
1585
Peter Boström0c4e06b2015-10-07 12:23:21 +02001586bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1587 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001588 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 if (!enabled()) {
1590 return false;
1591 }
solenberg1d63dd02015-12-02 12:35:09 -08001592 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593}
1594
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001595void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 case MSG_EARLYMEDIATIMEOUT:
1598 HandleEarlyMediaTimeout();
1599 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 case MSG_CHANNEL_ERROR: {
1601 VoiceChannelErrorMessageData* data =
1602 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 delete data;
1604 break;
1605 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 default:
1607 BaseChannel::OnMessage(pmsg);
1608 break;
1609 }
1610}
1611
1612void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001613 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 SignalConnectionMonitor(this, infos);
1615}
1616
1617void VoiceChannel::OnMediaMonitorUpdate(
1618 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1619 ASSERT(media_channel == this->media_channel());
1620 SignalMediaMonitor(this, info);
1621}
1622
1623void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1624 const AudioInfo& info) {
1625 SignalAudioMonitor(this, info);
1626}
1627
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001628void VoiceChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1629 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630}
1631
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001632VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001634 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001636 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001637 : BaseChannel(thread,
1638 media_channel,
1639 transport_controller,
1640 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001643 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644
1645bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001646 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647 return false;
1648 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 return true;
1650}
1651
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652VideoChannel::~VideoChannel() {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001653 std::vector<uint32_t> screencast_ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 ScreencastMap::iterator iter;
1655 while (!screencast_capturers_.empty()) {
1656 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1657 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1658 << screencast_capturers_.begin()->first;
1659 ASSERT(false);
1660 break;
1661 }
1662 }
1663
1664 StopMediaMonitor();
1665 // this can't be done in the base class, since it calls a virtual
1666 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001667
1668 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669}
1670
Peter Boström0c4e06b2015-10-07 12:23:21 +02001671bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001672 worker_thread()->Invoke<void>(Bind(
1673 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 return true;
1675}
1676
1677bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001678 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679}
1680
Peter Boström0c4e06b2015-10-07 12:23:21 +02001681bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001682 return worker_thread()->Invoke<bool>(Bind(
1683 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684}
1685
Peter Boström0c4e06b2015-10-07 12:23:21 +02001686bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001687 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1688 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689}
1690
Peter Boström0c4e06b2015-10-07 12:23:21 +02001691bool VideoChannel::RemoveScreencast(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001692 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693}
1694
1695bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001696 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697}
1698
Peter Boström0c4e06b2015-10-07 12:23:21 +02001699int VideoChannel::GetScreencastFps(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001700 ScreencastDetailsData data(ssrc);
1701 worker_thread()->Invoke<void>(Bind(
1702 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001703 return data.fps;
1704}
1705
Peter Boström0c4e06b2015-10-07 12:23:21 +02001706int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001707 ScreencastDetailsData data(ssrc);
1708 worker_thread()->Invoke<void>(Bind(
1709 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001710 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711}
1712
1713bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001714 worker_thread()->Invoke<void>(Bind(
1715 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716 return true;
1717}
1718
1719bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001720 worker_thread()->Invoke<void>(Bind(
1721 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 return true;
1723}
1724
Peter Boström0c4e06b2015-10-07 12:23:21 +02001725bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001726 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001727 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001728 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1729 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001730}
1731
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 // Send outgoing data if we're the active call, we have the remote content,
1734 // and we have had some form of connectivity.
1735 bool send = IsReadyToSend();
1736 if (!media_channel()->SetSend(send)) {
1737 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1738 // TODO(gangji): Report error back to server.
1739 }
1740
Peter Boström34fbfff2015-09-24 19:20:30 +02001741 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742}
1743
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001744bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1745 return InvokeOnWorker(
1746 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747}
1748
1749void VideoChannel::StartMediaMonitor(int cms) {
1750 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001751 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 media_monitor_->SignalUpdate.connect(
1753 this, &VideoChannel::OnMediaMonitorUpdate);
1754 media_monitor_->Start(cms);
1755}
1756
1757void VideoChannel::StopMediaMonitor() {
1758 if (media_monitor_) {
1759 media_monitor_->Stop();
1760 media_monitor_.reset();
1761 }
1762}
1763
1764const ContentInfo* VideoChannel::GetFirstContent(
1765 const SessionDescription* sdesc) {
1766 return GetFirstVideoContent(sdesc);
1767}
1768
1769bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001770 ContentAction action,
1771 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001772 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001773 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 LOG(LS_INFO) << "Setting local video description";
1775
1776 const VideoContentDescription* video =
1777 static_cast<const VideoContentDescription*>(content);
1778 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001779 if (!video) {
1780 SafeSetError("Can't find video content in local description.", error_desc);
1781 return false;
1782 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001784 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1785 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786 }
1787
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001788 VideoRecvParameters recv_params = last_recv_params_;
1789 RtpParametersFromMediaDescription(video, &recv_params);
1790 if (!media_channel()->SetRecvParameters(recv_params)) {
1791 SafeSetError("Failed to set local video description recv parameters.",
1792 error_desc);
1793 return false;
1794 }
1795 for (const VideoCodec& codec : video->codecs()) {
1796 bundle_filter()->AddPayloadType(codec.id);
1797 }
1798 last_recv_params_ = recv_params;
1799
1800 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1801 // only give it to the media channel once we have a remote
1802 // description too (without a remote description, we won't be able
1803 // to send them anyway).
1804 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1805 SafeSetError("Failed to set local video description streams.", error_desc);
1806 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 }
1808
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001809 set_local_content_direction(content->direction());
1810 ChangeState();
1811 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812}
1813
1814bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001815 ContentAction action,
1816 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001817 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001818 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 LOG(LS_INFO) << "Setting remote video description";
1820
1821 const VideoContentDescription* video =
1822 static_cast<const VideoContentDescription*>(content);
1823 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001824 if (!video) {
1825 SafeSetError("Can't find video content in remote description.", error_desc);
1826 return false;
1827 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001829
1830 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1831 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 }
1833
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001834 VideoSendParameters send_params = last_send_params_;
1835 RtpSendParametersFromMediaDescription(video, &send_params);
1836 if (video->conference_mode()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001837 send_params.options.conference_mode = rtc::Optional<bool>(true);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001838 }
1839 if (!media_channel()->SetSendParameters(send_params)) {
1840 SafeSetError("Failed to set remote video description send parameters.",
1841 error_desc);
1842 return false;
1843 }
1844 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001846 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1847 // and only give it to the media channel once we have a local
1848 // description too (without a local description, we won't be able to
1849 // recv them anyway).
1850 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1851 SafeSetError("Failed to set remote video description streams.", error_desc);
1852 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 }
1854
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001855 if (video->rtp_header_extensions_set()) {
1856 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001858
1859 set_remote_content_direction(content->direction());
1860 ChangeState();
1861 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862}
1863
1864bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1865 bool ret = true;
1866 // Set the send format for each of the local streams. If the view request
1867 // does not contain a local stream, set its send format to 0x0, which will
1868 // drop all frames.
1869 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1870 it != local_streams().end(); ++it) {
1871 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1872 StaticVideoViews::const_iterator view;
1873 for (view = request.static_video_views.begin();
1874 view != request.static_video_views.end(); ++view) {
1875 if (view->selector.Matches(*it)) {
1876 format.width = view->width;
1877 format.height = view->height;
1878 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1879 break;
1880 }
1881 }
1882
1883 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1884 }
1885
1886 // Check if the view request has invalid streams.
1887 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1888 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001889 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 LOG(LS_WARNING) << "View request for ("
1891 << it->selector.ssrc << ", '"
1892 << it->selector.groupid << "', '"
1893 << it->selector.streamid << "'"
1894 << ") is not in the local streams.";
1895 }
1896 }
1897
1898 return ret;
1899}
1900
Peter Boström0c4e06b2015-10-07 12:23:21 +02001901bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001903 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001905 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1906 screencast_capturers_[ssrc] = capturer;
1907 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908}
1909
Peter Boström0c4e06b2015-10-07 12:23:21 +02001910bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1912 if (iter == screencast_capturers_.end()) {
1913 return false;
1914 }
1915 // Clean up VideoCapturer.
1916 delete iter->second;
1917 screencast_capturers_.erase(iter);
1918 return true;
1919}
1920
1921bool VideoChannel::IsScreencasting_w() const {
1922 return !screencast_capturers_.empty();
1923}
1924
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001925void VideoChannel::GetScreencastDetails_w(
1926 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001927 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001929 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 }
1931 VideoCapturer* capturer = iter->second;
1932 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001933 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1934 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935}
1936
Peter Boström0c4e06b2015-10-07 12:23:21 +02001937void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001938 rtc::WindowEvent we) {
1939 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940 SignalScreencastWindowEvent(ssrc, we);
1941}
1942
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001943void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945 case MSG_SCREENCASTWINDOWEVENT: {
1946 const ScreencastEventMessageData* data =
1947 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1948 OnScreencastWindowEvent_s(data->ssrc, data->event);
1949 delete data;
1950 break;
1951 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 case MSG_CHANNEL_ERROR: {
1953 const VideoChannelErrorMessageData* data =
1954 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 delete data;
1956 break;
1957 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 default:
1959 BaseChannel::OnMessage(pmsg);
1960 break;
1961 }
1962}
1963
1964void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001965 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 SignalConnectionMonitor(this, infos);
1967}
1968
1969// TODO(pthatcher): Look into removing duplicate code between
1970// audio, video, and data, perhaps by using templates.
1971void VideoChannel::OnMediaMonitorUpdate(
1972 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1973 ASSERT(media_channel == this->media_channel());
1974 SignalMediaMonitor(this, info);
1975}
1976
Peter Boström0c4e06b2015-10-07 12:23:21 +02001977void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001978 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 ScreencastEventMessageData* pdata =
1980 new ScreencastEventMessageData(ssrc, event);
1981 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1982}
1983
1984void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1985 // Map capturer events to window events. In the future we may want to simply
1986 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001987 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001989 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001991 we = rtc::WE_MINIMIZE;
1992 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1993 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994 } else {
1995 return;
1996 }
1997 previous_we_ = we;
1998
Peter Boström0c4e06b2015-10-07 12:23:21 +02001999 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000 if (!GetLocalSsrc(capturer, &ssrc)) {
2001 return;
2002 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002003
2004 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005}
2006
Peter Boström0c4e06b2015-10-07 12:23:21 +02002007bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 *ssrc = 0;
2009 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2010 iter != screencast_capturers_.end(); ++iter) {
2011 if (iter->second == capturer) {
2012 *ssrc = iter->first;
2013 return true;
2014 }
2015 }
2016 return false;
2017}
2018
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002019void VideoChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2020 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002021}
2022
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002023DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002025 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026 const std::string& content_name,
2027 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07002028 : BaseChannel(thread,
2029 media_channel,
2030 transport_controller,
2031 content_name,
2032 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002033 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002034 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035
2036DataChannel::~DataChannel() {
2037 StopMediaMonitor();
2038 // this can't be done in the base class, since it calls a virtual
2039 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002040
2041 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042}
2043
2044bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002045 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046 return false;
2047 }
2048 media_channel()->SignalDataReceived.connect(
2049 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002050 media_channel()->SignalReadyToSend.connect(
2051 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002052 media_channel()->SignalStreamClosedRemotely.connect(
2053 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054 return true;
2055}
2056
2057bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002058 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002060 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2061 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062}
2063
2064const ContentInfo* DataChannel::GetFirstContent(
2065 const SessionDescription* sdesc) {
2066 return GetFirstDataContent(sdesc);
2067}
2068
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002069bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070 if (data_channel_type_ == DCT_SCTP) {
2071 // TODO(pthatcher): Do this in a more robust way by checking for
2072 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002073 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 } else if (data_channel_type_ == DCT_RTP) {
2075 return BaseChannel::WantsPacket(rtcp, packet);
2076 }
2077 return false;
2078}
2079
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002080bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2081 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082 // It hasn't been set before, so set it now.
2083 if (data_channel_type_ == DCT_NONE) {
2084 data_channel_type_ = new_data_channel_type;
2085 return true;
2086 }
2087
2088 // It's been set before, but doesn't match. That's bad.
2089 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002090 std::ostringstream desc;
2091 desc << "Data channel type mismatch."
2092 << " Expected " << data_channel_type_
2093 << " Got " << new_data_channel_type;
2094 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095 return false;
2096 }
2097
2098 // It's hasn't changed. Nothing to do.
2099 return true;
2100}
2101
2102bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002103 const DataContentDescription* content,
2104 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2106 (content->protocol() == kMediaProtocolDtlsSctp));
2107 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002108 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109}
2110
2111bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002112 ContentAction action,
2113 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002114 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002115 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116 LOG(LS_INFO) << "Setting local data description";
2117
2118 const DataContentDescription* data =
2119 static_cast<const DataContentDescription*>(content);
2120 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002121 if (!data) {
2122 SafeSetError("Can't find data content in local description.", error_desc);
2123 return false;
2124 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002126 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002127 return false;
2128 }
2129
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002130 if (data_channel_type_ == DCT_RTP) {
2131 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2132 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 }
2134 }
2135
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002136 // FYI: We send the SCTP port number (not to be confused with the
2137 // underlying UDP port number) as a codec parameter. So even SCTP
2138 // data channels need codecs.
2139 DataRecvParameters recv_params = last_recv_params_;
2140 RtpParametersFromMediaDescription(data, &recv_params);
2141 if (!media_channel()->SetRecvParameters(recv_params)) {
2142 SafeSetError("Failed to set remote data description recv parameters.",
2143 error_desc);
2144 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002146 if (data_channel_type_ == DCT_RTP) {
2147 for (const DataCodec& codec : data->codecs()) {
2148 bundle_filter()->AddPayloadType(codec.id);
2149 }
2150 }
2151 last_recv_params_ = recv_params;
2152
2153 // TODO(pthatcher): Move local streams into DataSendParameters, and
2154 // only give it to the media channel once we have a remote
2155 // description too (without a remote description, we won't be able
2156 // to send them anyway).
2157 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2158 SafeSetError("Failed to set local data description streams.", error_desc);
2159 return false;
2160 }
2161
2162 set_local_content_direction(content->direction());
2163 ChangeState();
2164 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165}
2166
2167bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002168 ContentAction action,
2169 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002170 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002171 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172
2173 const DataContentDescription* data =
2174 static_cast<const DataContentDescription*>(content);
2175 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002176 if (!data) {
2177 SafeSetError("Can't find data content in remote description.", error_desc);
2178 return false;
2179 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002181 // If the remote data doesn't have codecs and isn't an update, it
2182 // must be empty, so ignore it.
2183 if (!data->has_codecs() && action != CA_UPDATE) {
2184 return true;
2185 }
2186
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002187 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 return false;
2189 }
2190
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002191 LOG(LS_INFO) << "Setting remote data description";
2192 if (data_channel_type_ == DCT_RTP &&
2193 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2194 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 }
2196
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002197
2198 DataSendParameters send_params = last_send_params_;
2199 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2200 if (!media_channel()->SetSendParameters(send_params)) {
2201 SafeSetError("Failed to set remote data description send parameters.",
2202 error_desc);
2203 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002205 last_send_params_ = send_params;
2206
2207 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2208 // and only give it to the media channel once we have a local
2209 // description too (without a local description, we won't be able to
2210 // recv them anyway).
2211 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2212 SafeSetError("Failed to set remote data description streams.",
2213 error_desc);
2214 return false;
2215 }
2216
2217 set_remote_content_direction(content->direction());
2218 ChangeState();
2219 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220}
2221
2222void DataChannel::ChangeState() {
2223 // Render incoming data if we're the active call, and we have the local
2224 // content. We receive data on the default channel and multiplexed streams.
2225 bool recv = IsReadyToReceive();
2226 if (!media_channel()->SetReceive(recv)) {
2227 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2228 }
2229
2230 // Send outgoing data if we're the active call, we have the remote content,
2231 // and we have had some form of connectivity.
2232 bool send = IsReadyToSend();
2233 if (!media_channel()->SetSend(send)) {
2234 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2235 }
2236
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002237 // Trigger SignalReadyToSendData asynchronously.
2238 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002239
2240 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2241}
2242
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002243void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002244 switch (pmsg->message_id) {
2245 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002246 DataChannelReadyToSendMessageData* data =
2247 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002248 ready_to_send_data_ = data->data();
2249 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250 delete data;
2251 break;
2252 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253 case MSG_DATARECEIVED: {
2254 DataReceivedMessageData* data =
2255 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2256 SignalDataReceived(this, data->params, data->payload);
2257 delete data;
2258 break;
2259 }
2260 case MSG_CHANNEL_ERROR: {
2261 const DataChannelErrorMessageData* data =
2262 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263 delete data;
2264 break;
2265 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002266 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002267 rtc::TypedMessageData<uint32_t>* data =
2268 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002269 SignalStreamClosedRemotely(data->data());
2270 delete data;
2271 break;
2272 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002273 default:
2274 BaseChannel::OnMessage(pmsg);
2275 break;
2276 }
2277}
2278
2279void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002280 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002281 SignalConnectionMonitor(this, infos);
2282}
2283
2284void DataChannel::StartMediaMonitor(int cms) {
2285 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002286 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 media_monitor_->SignalUpdate.connect(
2288 this, &DataChannel::OnMediaMonitorUpdate);
2289 media_monitor_->Start(cms);
2290}
2291
2292void DataChannel::StopMediaMonitor() {
2293 if (media_monitor_) {
2294 media_monitor_->Stop();
2295 media_monitor_->SignalUpdate.disconnect(this);
2296 media_monitor_.reset();
2297 }
2298}
2299
2300void DataChannel::OnMediaMonitorUpdate(
2301 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2302 ASSERT(media_channel == this->media_channel());
2303 SignalMediaMonitor(this, info);
2304}
2305
2306void DataChannel::OnDataReceived(
2307 const ReceiveDataParams& params, const char* data, size_t len) {
2308 DataReceivedMessageData* msg = new DataReceivedMessageData(
2309 params, data, len);
2310 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2311}
2312
Peter Boström0c4e06b2015-10-07 12:23:21 +02002313void DataChannel::OnDataChannelError(uint32_t ssrc,
2314 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2316 ssrc, err);
2317 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2318}
2319
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002320void DataChannel::OnDataChannelReadyToSend(bool writable) {
2321 // This is usded for congestion control to indicate that the stream is ready
2322 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2323 // that the transport channel is ready.
2324 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2325 new DataChannelReadyToSendMessageData(writable));
2326}
2327
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002328void DataChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2329 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330}
2331
2332bool DataChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08002333 return (data_channel_type_ == DCT_RTP) && BaseChannel::ShouldSetupDtlsSrtp();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334}
2335
Peter Boström0c4e06b2015-10-07 12:23:21 +02002336void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2337 rtc::TypedMessageData<uint32_t>* message =
2338 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002339 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2340}
2341
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342} // namespace cricket