blob: 588a03661cf1e84e8d3d75e8cd058abd273c3e73 [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"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000033#include "webrtc/base/bind.h"
34#include "webrtc/base/buffer.h"
35#include "webrtc/base/byteorder.h"
36#include "webrtc/base/common.h"
37#include "webrtc/base/dscp.h"
38#include "webrtc/base/logging.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010039#include "webrtc/base/trace_event.h"
40#include "webrtc/p2p/base/transportchannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42namespace cricket {
43
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000044using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000045
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000047 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048 MSG_SCREENCASTWINDOWEVENT,
49 MSG_RTPPACKET,
50 MSG_RTCPPACKET,
51 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000055 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056};
57
58// Value specified in RFC 5764.
59static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
60
61static const int kAgcMinus10db = -10;
62
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000063static void SafeSetError(const std::string& message, std::string* error_desc) {
64 if (error_desc) {
65 *error_desc = message;
66 }
67}
68
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000069struct PacketMessageData : public rtc::MessageData {
70 rtc::Buffer packet;
stefanc1aeaf02015-10-15 07:26:07 -070071 rtc::PacketOptions options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072};
73
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074struct ScreencastEventMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020075 ScreencastEventMessageData(uint32_t s, rtc::WindowEvent we)
76 : ssrc(s), event(we) {}
77 uint32_t ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079};
80
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000081struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020082 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020084 : ssrc(in_ssrc), error(in_error) {}
85 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 VoiceMediaChannel::Error error;
87};
88
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000089struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020090 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020092 : ssrc(in_ssrc), error(in_error) {}
93 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 VideoMediaChannel::Error error;
95};
96
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000097struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020098 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100 : ssrc(in_ssrc), error(in_error) {}
101 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 DataMediaChannel::Error error;
103};
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000106struct VideoChannel::ScreencastDetailsData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200107 explicit ScreencastDetailsData(uint32_t s)
108 : ssrc(s), fps(0), screencast_max_pixels(0) {}
109 uint32_t ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000110 int fps;
111 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112};
113
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114static const char* PacketType(bool rtcp) {
115 return (!rtcp) ? "RTP" : "RTCP";
116}
117
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000118static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119 // Check the packet size. We could check the header too if needed.
120 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000121 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
122 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123}
124
125static bool IsReceiveContentDirection(MediaContentDirection direction) {
126 return direction == MD_SENDRECV || direction == MD_RECVONLY;
127}
128
129static bool IsSendContentDirection(MediaContentDirection direction) {
130 return direction == MD_SENDRECV || direction == MD_SENDONLY;
131}
132
133static const MediaContentDescription* GetContentDescription(
134 const ContentInfo* cinfo) {
135 if (cinfo == NULL)
136 return NULL;
137 return static_cast<const MediaContentDescription*>(cinfo->description);
138}
139
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700140template <class Codec>
141void RtpParametersFromMediaDescription(
142 const MediaContentDescriptionImpl<Codec>* desc,
143 RtpParameters<Codec>* params) {
144 // TODO(pthatcher): Remove this once we're sure no one will give us
145 // a description without codecs (currently a CA_UPDATE with just
146 // streams can).
147 if (desc->has_codecs()) {
148 params->codecs = desc->codecs();
149 }
150 // TODO(pthatcher): See if we really need
151 // rtp_header_extensions_set() and remove it if we don't.
152 if (desc->rtp_header_extensions_set()) {
153 params->extensions = desc->rtp_header_extensions();
154 }
deadbeef13871492015-12-09 12:37:51 -0800155 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700156}
157
158template <class Codec, class Options>
159void RtpSendParametersFromMediaDescription(
160 const MediaContentDescriptionImpl<Codec>* desc,
161 RtpSendParameters<Codec, Options>* send_params) {
162 RtpParametersFromMediaDescription(desc, send_params);
163 send_params->max_bandwidth_bps = desc->bandwidth();
164}
165
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000166BaseChannel::BaseChannel(rtc::Thread* thread,
deadbeefcbecd352015-09-23 11:50:27 -0700167 MediaChannel* media_channel,
168 TransportController* transport_controller,
169 const std::string& content_name,
170 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 : worker_thread_(thread),
deadbeefcbecd352015-09-23 11:50:27 -0700172 transport_controller_(transport_controller),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 media_channel_(media_channel),
174 content_name_(content_name),
deadbeefcbecd352015-09-23 11:50:27 -0700175 rtcp_transport_enabled_(rtcp),
176 transport_channel_(nullptr),
177 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 enabled_(false),
179 writable_(false),
180 rtp_ready_to_send_(false),
181 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 was_ever_writable_(false),
183 local_content_direction_(MD_INACTIVE),
184 remote_content_direction_(MD_INACTIVE),
185 has_received_packet_(false),
186 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000187 secure_required_(false),
188 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000189 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 LOG(LS_INFO) << "Created channel for " << content_name;
191}
192
193BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000194 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000195 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 StopConnectionMonitor();
197 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000198 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 // We must destroy the media channel before the transport channel, otherwise
200 // the media channel may try to send on the dead transport channel. NULLing
201 // is not an effective strategy since the sends will come on another thread.
202 delete media_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700203 // Note that we don't just call set_transport_channel(nullptr) because that
204 // would call a pure virtual method which we can't do from a destructor.
205 if (transport_channel_) {
206 DisconnectFromTransportChannel(transport_channel_);
207 transport_controller_->DestroyTransportChannel_w(
208 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
209 }
210 if (rtcp_transport_channel_) {
211 DisconnectFromTransportChannel(rtcp_transport_channel_);
212 transport_controller_->DestroyTransportChannel_w(
213 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
214 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 LOG(LS_INFO) << "Destroyed channel";
216}
217
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000218bool BaseChannel::Init() {
deadbeefcbecd352015-09-23 11:50:27 -0700219 if (!SetTransport(content_name())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 return false;
221 }
222
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800223 if (!SetDtlsSrtpCryptoSuites(transport_channel(), false)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000224 return false;
225 }
deadbeefcbecd352015-09-23 11:50:27 -0700226 if (rtcp_transport_enabled() &&
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800227 !SetDtlsSrtpCryptoSuites(rtcp_transport_channel(), true)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000228 return false;
229 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230
wu@webrtc.orgde305012013-10-31 15:40:38 +0000231 // Both RTP and RTCP channels are set, we can call SetInterface on
232 // media channel and it can set network options.
233 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 return true;
235}
236
wu@webrtc.org78187522013-10-07 23:32:02 +0000237void BaseChannel::Deinit() {
238 media_channel_->SetInterface(NULL);
239}
240
deadbeefcbecd352015-09-23 11:50:27 -0700241bool BaseChannel::SetTransport(const std::string& transport_name) {
242 return worker_thread_->Invoke<bool>(
243 Bind(&BaseChannel::SetTransport_w, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000244}
245
deadbeefcbecd352015-09-23 11:50:27 -0700246bool BaseChannel::SetTransport_w(const std::string& transport_name) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000247 ASSERT(worker_thread_ == rtc::Thread::Current());
248
deadbeefcbecd352015-09-23 11:50:27 -0700249 if (transport_name == transport_name_) {
250 // Nothing to do if transport name isn't changing
251 return true;
252 }
253
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800254 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
255 // changes and wait until the DTLS handshake is complete to set the newly
256 // negotiated parameters.
257 if (ShouldSetupDtlsSrtp()) {
258 srtp_filter_.ResetParams();
259 }
260
deadbeefcbecd352015-09-23 11:50:27 -0700261 set_transport_channel(transport_controller_->CreateTransportChannel_w(
262 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000263 if (!transport_channel()) {
264 return false;
265 }
deadbeefcbecd352015-09-23 11:50:27 -0700266 if (rtcp_transport_enabled()) {
267 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
268 << " on " << transport_name << " transport ";
269 set_rtcp_transport_channel(transport_controller_->CreateTransportChannel_w(
270 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000271 if (!rtcp_transport_channel()) {
272 return false;
273 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000274 }
275
deadbeefcbecd352015-09-23 11:50:27 -0700276 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000277 return true;
278}
279
280void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
281 ASSERT(worker_thread_ == rtc::Thread::Current());
282
283 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700284 if (!old_tc && !new_tc) {
285 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000286 return;
287 }
deadbeefcbecd352015-09-23 11:50:27 -0700288 ASSERT(old_tc != new_tc);
289
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000290 if (old_tc) {
291 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700292 transport_controller_->DestroyTransportChannel_w(
293 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000294 }
295
296 transport_channel_ = new_tc;
297
298 if (new_tc) {
299 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700300 for (const auto& pair : socket_options_) {
301 new_tc->SetOption(pair.first, pair.second);
302 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000303 }
deadbeefcbecd352015-09-23 11:50:27 -0700304
305 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
306 // setting new channel
307 UpdateWritableState_w();
308 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000309}
310
311void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
312 ASSERT(worker_thread_ == rtc::Thread::Current());
313
314 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700315 if (!old_tc && !new_tc) {
316 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000317 return;
318 }
deadbeefcbecd352015-09-23 11:50:27 -0700319 ASSERT(old_tc != new_tc);
320
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000321 if (old_tc) {
322 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700323 transport_controller_->DestroyTransportChannel_w(
324 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000325 }
326
327 rtcp_transport_channel_ = new_tc;
328
329 if (new_tc) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800330 RTC_CHECK(!(ShouldSetupDtlsSrtp() && srtp_filter_.IsActive()))
331 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
332 << "should never happen.";
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000333 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700334 for (const auto& pair : rtcp_socket_options_) {
335 new_tc->SetOption(pair.first, pair.second);
336 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000337 }
deadbeefcbecd352015-09-23 11:50:27 -0700338
339 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
340 // setting new channel
341 UpdateWritableState_w();
342 SetReadyToSend(true, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000343}
344
345void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
346 ASSERT(worker_thread_ == rtc::Thread::Current());
347
348 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
349 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
350 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800351 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000352}
353
354void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
355 ASSERT(worker_thread_ == rtc::Thread::Current());
356
357 tc->SignalWritableState.disconnect(this);
358 tc->SignalReadPacket.disconnect(this);
359 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800360 tc->SignalDtlsState.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000361}
362
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000364 worker_thread_->Invoke<void>(Bind(
365 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
366 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000367 return true;
368}
369
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000371 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372}
373
Peter Boström0c4e06b2015-10-07 12:23:21 +0200374bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000375 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376}
377
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000378bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000379 return InvokeOnWorker(
380 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000381}
382
Peter Boström0c4e06b2015-10-07 12:23:21 +0200383bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000384 return InvokeOnWorker(
385 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000386}
387
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000389 ContentAction action,
390 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100391 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000392 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
393 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394}
395
396bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000397 ContentAction action,
398 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100399 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000400 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
401 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402}
403
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000404void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000405 // We pass in the BaseChannel instead of the transport_channel_
406 // because if the transport_channel_ changes, the ConnectionMonitor
407 // would be pointing to the wrong TransportChannel.
408 connection_monitor_.reset(new ConnectionMonitor(
409 this, worker_thread(), rtc::Thread::Current()));
410 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000412 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413}
414
415void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000416 if (connection_monitor_) {
417 connection_monitor_->Stop();
418 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 }
420}
421
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000422bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
423 ASSERT(worker_thread_ == rtc::Thread::Current());
424 return transport_channel_->GetStats(infos);
425}
426
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000427bool BaseChannel::IsReadyToReceive() const {
428 // Receive data if we are enabled and have local content,
429 return enabled() && IsReceiveContentDirection(local_content_direction_);
430}
431
432bool BaseChannel::IsReadyToSend() const {
433 // Send outgoing data if we are enabled, have local and remote content,
434 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800435 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 IsSendContentDirection(local_content_direction_) &&
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800437 was_ever_writable() &&
438 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439}
440
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000441bool BaseChannel::SendPacket(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700442 const rtc::PacketOptions& options) {
443 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444}
445
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000446bool BaseChannel::SendRtcp(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700447 const rtc::PacketOptions& options) {
448 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449}
450
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000451int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000453 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000455 case ST_RTP:
456 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700457 socket_options_.push_back(
458 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000459 break;
460 case ST_RTCP:
461 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700462 rtcp_socket_options_.push_back(
463 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000464 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000466 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467}
468
469void BaseChannel::OnWritableState(TransportChannel* channel) {
470 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
deadbeefcbecd352015-09-23 11:50:27 -0700471 UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472}
473
474void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000475 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000476 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000477 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100478 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000480 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481
482 // When using RTCP multiplexing we might get RTCP packets on the RTP
483 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
484 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000485 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000486 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487}
488
489void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700490 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
491 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492}
493
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800494void BaseChannel::OnDtlsState(TransportChannel* channel,
495 DtlsTransportState state) {
496 if (!ShouldSetupDtlsSrtp()) {
497 return;
498 }
499
500 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
501 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
502 // cover other scenarios like the whole channel is writable (not just this
503 // TransportChannel) or when TransportChannel is attached after DTLS is
504 // negotiated.
505 if (state != DTLS_TRANSPORT_CONNECTED) {
506 srtp_filter_.ResetParams();
507 }
508}
509
deadbeefcbecd352015-09-23 11:50:27 -0700510void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
511 if (rtcp) {
512 rtcp_ready_to_send_ = ready;
513 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000514 rtp_ready_to_send_ = ready;
515 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000516
deadbeefcbecd352015-09-23 11:50:27 -0700517 if (rtp_ready_to_send_ &&
518 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
519 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
torbjornga81a42f2015-09-23 02:16:58 -0700520 // Notify the MediaChannel when both rtp and rtcp channel can send.
521 media_channel_->OnReadyToSend(true);
deadbeefcbecd352015-09-23 11:50:27 -0700522 } else {
523 // Notify the MediaChannel when either rtp or rtcp channel can't send.
524 media_channel_->OnReadyToSend(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 }
526}
527
528bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
529 const char* data, size_t len) {
530 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000531 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532}
533
stefanc1aeaf02015-10-15 07:26:07 -0700534bool BaseChannel::SendPacket(bool rtcp,
535 rtc::Buffer* packet,
536 const rtc::PacketOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000537 // SendPacket gets called from MediaEngine, typically on an encoder thread.
538 // If the thread is not our worker thread, we will post to our worker
539 // so that the real work happens on our worker. This avoids us having to
540 // synchronize access to all the pieces of the send path, including
541 // SRTP and the inner workings of the transport channels.
542 // The only downside is that we can't return a proper failure code if
543 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000544 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 // Avoid a copy by transferring the ownership of the packet data.
546 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
547 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200548 data->packet = packet->Pass();
stefanc1aeaf02015-10-15 07:26:07 -0700549 data->options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 worker_thread_->Post(this, message_id, data);
551 return true;
552 }
553
554 // Now that we are on the correct thread, ensure we have a place to send this
555 // packet before doing anything. (We might get RTCP packets that we don't
556 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
557 // transport.
558 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
559 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000560 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 return false;
562 }
563
564 // Protect ourselves against crazy data.
565 if (!ValidPacket(rtcp, packet)) {
566 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000567 << PacketType(rtcp)
568 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 return false;
570 }
571
stefanc1aeaf02015-10-15 07:26:07 -0700572 rtc::PacketOptions updated_options;
573 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 // Protect if needed.
575 if (srtp_filter_.IsActive()) {
576 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200577 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000578 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000579 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000580 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
581 // inside libsrtp for a RTP packet. A external HMAC module will be writing
582 // a fake HMAC value. This is ONLY done for a RTP packet.
583 // Socket layer will update rtp sendtime extension header if present in
584 // packet with current time before updating the HMAC.
585#if !defined(ENABLE_EXTERNAL_AUTH)
586 res = srtp_filter_.ProtectRtp(
587 data, len, static_cast<int>(packet->capacity()), &len);
588#else
stefanc1aeaf02015-10-15 07:26:07 -0700589 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000590 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000591 res = srtp_filter_.ProtectRtp(
592 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700593 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000594 // If protection succeeds, let's get auth params from srtp.
595 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200596 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000597 int key_len;
598 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700599 &auth_key, &key_len,
600 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000601 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700602 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
603 updated_options.packet_time_params.srtp_auth_key.assign(
604 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000605 }
606 }
607#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 if (!res) {
609 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200610 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 GetRtpSeqNum(data, len, &seq_num);
612 GetRtpSsrc(data, len, &ssrc);
613 LOG(LS_ERROR) << "Failed to protect " << content_name_
614 << " RTP packet: size=" << len
615 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
616 return false;
617 }
618 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000619 res = srtp_filter_.ProtectRtcp(data, len,
620 static_cast<int>(packet->capacity()),
621 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 if (!res) {
623 int type = -1;
624 GetRtcpType(data, len, &type);
625 LOG(LS_ERROR) << "Failed to protect " << content_name_
626 << " RTCP packet: size=" << len << ", type=" << type;
627 return false;
628 }
629 }
630
631 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000632 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 } else if (secure_required_) {
634 // This is a double check for something that supposedly can't happen.
635 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
636 << " packet when SRTP is inactive and crypto is required";
637
638 ASSERT(false);
639 return false;
640 }
641
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000643 int ret =
stefanc1aeaf02015-10-15 07:26:07 -0700644 channel->SendPacket(packet->data<char>(), packet->size(), updated_options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000645 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
646 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 if (channel->GetError() == EWOULDBLOCK) {
648 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700649 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 }
651 return false;
652 }
653 return true;
654}
655
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000656bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 // Protect ourselves against crazy data.
658 if (!ValidPacket(rtcp, packet)) {
659 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000660 << PacketType(rtcp)
661 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 return false;
663 }
pbos482b12e2015-11-16 10:19:58 -0800664 if (rtcp) {
665 // Permit all (seemingly valid) RTCP packets.
666 return true;
667 }
668 // Check whether we handle this payload.
669 return bundle_filter_.DemuxPacket(packet->data<uint8_t>(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670}
671
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000672void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
673 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 if (!WantsPacket(rtcp, packet)) {
675 return;
676 }
677
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000678 // We are only interested in the first rtp packet because that
679 // indicates the media has started flowing.
680 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 has_received_packet_ = true;
682 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
683 }
684
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 // Unprotect the packet, if needed.
686 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200687 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000688 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 bool res;
690 if (!rtcp) {
691 res = srtp_filter_.UnprotectRtp(data, len, &len);
692 if (!res) {
693 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200694 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 GetRtpSeqNum(data, len, &seq_num);
696 GetRtpSsrc(data, len, &ssrc);
697 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
698 << " RTP packet: size=" << len
699 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
700 return;
701 }
702 } else {
703 res = srtp_filter_.UnprotectRtcp(data, len, &len);
704 if (!res) {
705 int type = -1;
706 GetRtcpType(data, len, &type);
707 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
708 << " RTCP packet: size=" << len << ", type=" << type;
709 return;
710 }
711 }
712
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000713 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 } else if (secure_required_) {
715 // Our session description indicates that SRTP is required, but we got a
716 // packet before our SRTP filter is active. This means either that
717 // a) we got SRTP packets before we received the SDES keys, in which case
718 // we can't decrypt it anyway, or
719 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
720 // channels, so we haven't yet extracted keys, even if DTLS did complete
721 // on the channel that the packets are being sent on. It's really good
722 // practice to wait for both RTP and RTCP to be good to go before sending
723 // media, to prevent weird failure modes, so it's fine for us to just eat
724 // packets here. This is all sidestepped if RTCP mux is used anyway.
725 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
726 << " packet when SRTP is inactive and crypto is required";
727 return;
728 }
729
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 // Push it down to the media channel.
731 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000732 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000734 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 }
736}
737
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000738bool BaseChannel::PushdownLocalDescription(
739 const SessionDescription* local_desc, ContentAction action,
740 std::string* error_desc) {
741 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 const MediaContentDescription* content_desc =
743 GetContentDescription(content_info);
744 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000745 !SetLocalContent(content_desc, action, error_desc)) {
746 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
747 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000749 return true;
750}
751
752bool BaseChannel::PushdownRemoteDescription(
753 const SessionDescription* remote_desc, ContentAction action,
754 std::string* error_desc) {
755 const ContentInfo* content_info = GetFirstContent(remote_desc);
756 const MediaContentDescription* content_desc =
757 GetContentDescription(content_info);
758 if (content_desc && content_info && !content_info->rejected &&
759 !SetRemoteContent(content_desc, action, error_desc)) {
760 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
761 return false;
762 }
763 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764}
765
766void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000767 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 if (enabled_)
769 return;
770
771 LOG(LS_INFO) << "Channel enabled";
772 enabled_ = true;
773 ChangeState();
774}
775
776void BaseChannel::DisableMedia_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 disabled";
782 enabled_ = false;
783 ChangeState();
784}
785
deadbeefcbecd352015-09-23 11:50:27 -0700786void BaseChannel::UpdateWritableState_w() {
787 if (transport_channel_ && transport_channel_->writable() &&
788 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
789 ChannelWritable_w();
790 } else {
791 ChannelNotWritable_w();
792 }
793}
794
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000796 ASSERT(worker_thread_ == rtc::Thread::Current());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800797 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800799 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000800
deadbeefcbecd352015-09-23 11:50:27 -0700801 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000802 << (was_ever_writable_ ? "" : " for the first time");
803
804 std::vector<ConnectionInfo> infos;
805 transport_channel_->GetStats(&infos);
806 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
807 it != infos.end(); ++it) {
808 if (it->best_connection) {
809 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
810 << "->" << it->remote_candidate.ToSensitiveString();
811 break;
812 }
813 }
814
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 was_ever_writable_ = true;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800816 MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 writable_ = true;
818 ChangeState();
819}
820
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000821void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
822 ASSERT(worker_thread() == rtc::Thread::Current());
823 signaling_thread()->Invoke<void>(Bind(
824 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
825}
826
827void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
828 ASSERT(signaling_thread() == rtc::Thread::Current());
829 SignalDtlsSetupFailure(this, rtcp);
830}
831
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800832bool BaseChannel::SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp) {
833 std::vector<int> crypto_suites;
834 // We always use the default SRTP crypto suites for RTCP, but we may use
835 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 if (!rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800837 GetSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000838 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800839 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800841 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842}
843
844bool BaseChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800845 // Since DTLS is applied to all channels, checking RTP should be enough.
846 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847}
848
849// This function returns true if either DTLS-SRTP is not in use
850// *or* DTLS-SRTP is successfully set up.
851bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
852 bool ret = false;
853
deadbeefcbecd352015-09-23 11:50:27 -0700854 TransportChannel* channel =
855 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000856
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800857 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800859 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800861 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
862 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 return false;
864 }
865
866 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
867 << content_name() << " "
868 << PacketType(rtcp_channel);
869
870 // OK, we're now doing DTLS (RFC 5764)
871 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
872 SRTP_MASTER_KEY_SALT_LEN * 2);
873
874 // RFC 5705 exporter using the RFC 5764 parameters
875 if (!channel->ExportKeyingMaterial(
876 kDtlsSrtpExporterLabel,
877 NULL, 0, false,
878 &dtls_buffer[0], dtls_buffer.size())) {
879 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
880 ASSERT(false); // This should never happen
881 return false;
882 }
883
884 // Sync up the keys with the DTLS-SRTP interface
885 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
886 SRTP_MASTER_KEY_SALT_LEN);
887 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
888 SRTP_MASTER_KEY_SALT_LEN);
889 size_t offset = 0;
890 memcpy(&client_write_key[0], &dtls_buffer[offset],
891 SRTP_MASTER_KEY_KEY_LEN);
892 offset += SRTP_MASTER_KEY_KEY_LEN;
893 memcpy(&server_write_key[0], &dtls_buffer[offset],
894 SRTP_MASTER_KEY_KEY_LEN);
895 offset += SRTP_MASTER_KEY_KEY_LEN;
896 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
897 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
898 offset += SRTP_MASTER_KEY_SALT_LEN;
899 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
900 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
901
902 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000903 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000904 if (!channel->GetSslRole(&role)) {
905 LOG(LS_WARNING) << "GetSslRole failed";
906 return false;
907 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000909 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 send_key = &server_write_key;
911 recv_key = &client_write_key;
912 } else {
913 send_key = &client_write_key;
914 recv_key = &server_write_key;
915 }
916
917 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800918 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
919 static_cast<int>(send_key->size()),
920 selected_crypto_suite, &(*recv_key)[0],
921 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800923 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
924 static_cast<int>(send_key->size()),
925 selected_crypto_suite, &(*recv_key)[0],
926 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 }
928
929 if (!ret)
930 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
931 else
932 dtls_keyed_ = true;
933
934 return ret;
935}
936
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800937void BaseChannel::MaybeSetupDtlsSrtp_w() {
938 if (srtp_filter_.IsActive()) {
939 return;
940 }
941
942 if (!ShouldSetupDtlsSrtp()) {
943 return;
944 }
945
946 if (!SetupDtlsSrtp(false)) {
947 SignalDtlsSetupFailure_w(false);
948 return;
949 }
950
951 if (rtcp_transport_channel_) {
952 if (!SetupDtlsSrtp(true)) {
953 SignalDtlsSetupFailure_w(true);
954 return;
955 }
956 }
957}
958
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000960 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 if (!writable_)
962 return;
963
deadbeefcbecd352015-09-23 11:50:27 -0700964 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 writable_ = false;
966 ChangeState();
967}
968
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700969bool BaseChannel::SetRtpTransportParameters_w(
970 const MediaContentDescription* content,
971 ContentAction action,
972 ContentSource src,
973 std::string* error_desc) {
974 if (action == CA_UPDATE) {
975 // These parameters never get changed by a CA_UDPATE.
976 return true;
977 }
978
979 // Cache secure_required_ for belt and suspenders check on SendPacket
980 if (src == CS_LOCAL) {
981 set_secure_required(content->crypto_required() != CT_NONE);
982 }
983
984 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
985 return false;
986 }
987
988 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
989 return false;
990 }
991
992 return true;
993}
994
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000995// |dtls| will be set to true if DTLS is active for transport channel and
996// crypto is empty.
997bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000998 bool* dtls,
999 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001000 *dtls = transport_channel_->IsDtlsActive();
1001 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001002 SafeSetError("Cryptos must be empty when DTLS is active.",
1003 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001004 return false;
1005 }
1006 return true;
1007}
1008
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001010 ContentAction action,
1011 ContentSource src,
1012 std::string* error_desc) {
1013 if (action == CA_UPDATE) {
1014 // no crypto params.
1015 return true;
1016 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001018 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001019 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
1020 if (!ret) {
1021 return false;
1022 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 switch (action) {
1024 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001025 // If DTLS is already active on the channel, we could be renegotiating
1026 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001027 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001028 ret = srtp_filter_.SetOffer(cryptos, src);
1029 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030 break;
1031 case CA_PRANSWER:
1032 // If we're doing DTLS-SRTP, we don't want to update the filter
1033 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001034 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1036 }
1037 break;
1038 case CA_ANSWER:
1039 // If we're doing DTLS-SRTP, we don't want to update the filter
1040 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001041 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 ret = srtp_filter_.SetAnswer(cryptos, src);
1043 }
1044 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 default:
1046 break;
1047 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001048 if (!ret) {
1049 SafeSetError("Failed to setup SRTP filter.", error_desc);
1050 return false;
1051 }
1052 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053}
1054
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001055void BaseChannel::ActivateRtcpMux() {
1056 worker_thread_->Invoke<void>(Bind(
1057 &BaseChannel::ActivateRtcpMux_w, this));
1058}
1059
1060void BaseChannel::ActivateRtcpMux_w() {
1061 if (!rtcp_mux_filter_.IsActive()) {
1062 rtcp_mux_filter_.SetActive();
deadbeefcbecd352015-09-23 11:50:27 -07001063 set_rtcp_transport_channel(nullptr);
1064 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001065 }
1066}
1067
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001069 ContentSource src,
1070 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 bool ret = false;
1072 switch (action) {
1073 case CA_OFFER:
1074 ret = rtcp_mux_filter_.SetOffer(enable, src);
1075 break;
1076 case CA_PRANSWER:
1077 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1078 break;
1079 case CA_ANSWER:
1080 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1081 if (ret && rtcp_mux_filter_.IsActive()) {
1082 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001083 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1084 << " by destroying RTCP transport channel for "
1085 << transport_name();
1086 set_rtcp_transport_channel(nullptr);
1087 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088 }
1089 break;
1090 case CA_UPDATE:
1091 // No RTCP mux info.
1092 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001093 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 default:
1095 break;
1096 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001097 if (!ret) {
1098 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1099 return false;
1100 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1102 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1103 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001104 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 // If the RTP transport is already writable, then so are we.
1106 if (transport_channel_->writable()) {
1107 ChannelWritable_w();
1108 }
1109 }
1110
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001111 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112}
1113
1114bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001115 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001116 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117}
1118
Peter Boström0c4e06b2015-10-07 12:23:21 +02001119bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001120 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121 return media_channel()->RemoveRecvStream(ssrc);
1122}
1123
1124bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001125 ContentAction action,
1126 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1128 action == CA_PRANSWER || action == CA_UPDATE))
1129 return false;
1130
1131 // If this is an update, streams only contain streams that have changed.
1132 if (action == CA_UPDATE) {
1133 for (StreamParamsVec::const_iterator it = streams.begin();
1134 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001135 const StreamParams* existing_stream =
1136 GetStreamByIds(local_streams_, it->groupid, it->id);
1137 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 if (media_channel()->AddSendStream(*it)) {
1139 local_streams_.push_back(*it);
1140 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1141 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001142 std::ostringstream desc;
1143 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1144 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 return false;
1146 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001147 } else if (existing_stream && !it->has_ssrcs()) {
1148 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001149 std::ostringstream desc;
1150 desc << "Failed to remove send stream with ssrc "
1151 << it->first_ssrc() << ".";
1152 SafeSetError(desc.str(), error_desc);
1153 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001155 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 } else {
1157 LOG(LS_WARNING) << "Ignore unsupported stream update";
1158 }
1159 }
1160 return true;
1161 }
1162 // Else streams are all the streams we want to send.
1163
1164 // Check for streams that have been removed.
1165 bool ret = true;
1166 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1167 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001168 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001170 std::ostringstream desc;
1171 desc << "Failed to remove send stream with ssrc "
1172 << it->first_ssrc() << ".";
1173 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001174 ret = false;
1175 }
1176 }
1177 }
1178 // Check for new streams.
1179 for (StreamParamsVec::const_iterator it = streams.begin();
1180 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001181 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001183 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001185 std::ostringstream desc;
1186 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1187 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 ret = false;
1189 }
1190 }
1191 }
1192 local_streams_ = streams;
1193 return ret;
1194}
1195
1196bool BaseChannel::UpdateRemoteStreams_w(
1197 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001198 ContentAction action,
1199 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1201 action == CA_PRANSWER || action == CA_UPDATE))
1202 return false;
1203
1204 // If this is an update, streams only contain streams that have changed.
1205 if (action == CA_UPDATE) {
1206 for (StreamParamsVec::const_iterator it = streams.begin();
1207 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001208 const StreamParams* existing_stream =
1209 GetStreamByIds(remote_streams_, it->groupid, it->id);
1210 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 if (AddRecvStream_w(*it)) {
1212 remote_streams_.push_back(*it);
1213 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1214 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001215 std::ostringstream desc;
1216 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1217 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 return false;
1219 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001220 } else if (existing_stream && !it->has_ssrcs()) {
1221 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001222 std::ostringstream desc;
1223 desc << "Failed to remove remote stream with ssrc "
1224 << it->first_ssrc() << ".";
1225 SafeSetError(desc.str(), error_desc);
1226 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001228 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229 } else {
1230 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001231 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 << " new stream = " << it->ToString();
1233 }
1234 }
1235 return true;
1236 }
1237 // Else streams are all the streams we want to receive.
1238
1239 // Check for streams that have been removed.
1240 bool ret = true;
1241 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1242 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001243 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001245 std::ostringstream desc;
1246 desc << "Failed to remove remote stream with ssrc "
1247 << it->first_ssrc() << ".";
1248 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 ret = false;
1250 }
1251 }
1252 }
1253 // Check for new streams.
1254 for (StreamParamsVec::const_iterator it = streams.begin();
1255 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001256 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 if (AddRecvStream_w(*it)) {
1258 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1259 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001260 std::ostringstream desc;
1261 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1262 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 ret = false;
1264 }
1265 }
1266 }
1267 remote_streams_ = streams;
1268 return ret;
1269}
1270
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001271void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1272 const std::vector<RtpHeaderExtension>& extensions) {
1273 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001274 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001275 rtp_abs_sendtime_extn_id_ =
1276 send_time_extension ? send_time_extension->id : -1;
1277}
1278
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001279void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001280 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 case MSG_RTPPACKET:
1283 case MSG_RTCPPACKET: {
1284 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
stefanc1aeaf02015-10-15 07:26:07 -07001285 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet,
1286 data->options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 delete data; // because it is Posted
1288 break;
1289 }
1290 case MSG_FIRSTPACKETRECEIVED: {
1291 SignalFirstPacketReceived(this);
1292 break;
1293 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 }
1295}
1296
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297void BaseChannel::FlushRtcpMessages() {
1298 // Flush all remaining RTCP messages. This should only be called in
1299 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001300 ASSERT(rtc::Thread::Current() == worker_thread_);
1301 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001302 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001303 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001305 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306 }
1307}
1308
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001309VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 MediaEngineInterface* media_engine,
1311 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001312 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 const std::string& content_name,
1314 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001315 : BaseChannel(thread,
1316 media_channel,
1317 transport_controller,
1318 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001320 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001321 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322
1323VoiceChannel::~VoiceChannel() {
1324 StopAudioMonitor();
1325 StopMediaMonitor();
1326 // this can't be done in the base class, since it calls a virtual
1327 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001328 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329}
1330
1331bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001332 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 return false;
1334 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335 return true;
1336}
1337
Peter Boström0c4e06b2015-10-07 12:23:21 +02001338bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001339 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001340 const AudioOptions* options,
1341 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001342 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001343 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344}
1345
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346// TODO(juberti): Handle early media the right way. We should get an explicit
1347// ringing message telling us to start playing local ringback, which we cancel
1348// if any early media actually arrives. For now, we do the opposite, which is
1349// to wait 1 second for early media, and start playing local ringback if none
1350// arrives.
1351void VoiceChannel::SetEarlyMedia(bool enable) {
1352 if (enable) {
1353 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001354 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1355 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356 } else {
1357 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001358 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359 }
1360}
1361
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001362bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001363 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1364 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365}
1366
Peter Boström0c4e06b2015-10-07 12:23:21 +02001367bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1368 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001369 int duration) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001370 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
solenberg1d63dd02015-12-02 12:35:09 -08001371 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001372}
1373
solenberg4bac9c52015-10-09 02:32:53 -07001374bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1375 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1376 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001378
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001380 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1381 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382}
1383
1384void VoiceChannel::StartMediaMonitor(int cms) {
1385 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001386 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001387 media_monitor_->SignalUpdate.connect(
1388 this, &VoiceChannel::OnMediaMonitorUpdate);
1389 media_monitor_->Start(cms);
1390}
1391
1392void VoiceChannel::StopMediaMonitor() {
1393 if (media_monitor_) {
1394 media_monitor_->Stop();
1395 media_monitor_->SignalUpdate.disconnect(this);
1396 media_monitor_.reset();
1397 }
1398}
1399
1400void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001401 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402 audio_monitor_
1403 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1404 audio_monitor_->Start(cms);
1405}
1406
1407void VoiceChannel::StopAudioMonitor() {
1408 if (audio_monitor_) {
1409 audio_monitor_->Stop();
1410 audio_monitor_.reset();
1411 }
1412}
1413
1414bool VoiceChannel::IsAudioMonitorRunning() const {
1415 return (audio_monitor_.get() != NULL);
1416}
1417
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001419 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001420}
1421
1422int VoiceChannel::GetOutputLevel_w() {
1423 return media_channel()->GetOutputLevel();
1424}
1425
1426void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1427 media_channel()->GetActiveStreams(actives);
1428}
1429
1430void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001431 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001432 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001433 int flags) {
1434 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435
1436 // Set a flag when we've received an RTP packet. If we're waiting for early
1437 // media, this will disable the timeout.
1438 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1439 received_media_ = true;
1440 }
1441}
1442
1443void VoiceChannel::ChangeState() {
1444 // Render incoming data if we're the active call, and we have the local
1445 // content. We receive data on the default channel and multiplexed streams.
1446 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001447 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001448
1449 // Send outgoing data if we're the active call, we have the remote content,
1450 // and we have had some form of connectivity.
1451 bool send = IsReadyToSend();
1452 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1453 if (!media_channel()->SetSend(send_flag)) {
1454 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 }
1456
1457 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1458}
1459
1460const ContentInfo* VoiceChannel::GetFirstContent(
1461 const SessionDescription* sdesc) {
1462 return GetFirstAudioContent(sdesc);
1463}
1464
1465bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001466 ContentAction action,
1467 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001468 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001469 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470 LOG(LS_INFO) << "Setting local voice description";
1471
1472 const AudioContentDescription* audio =
1473 static_cast<const AudioContentDescription*>(content);
1474 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001475 if (!audio) {
1476 SafeSetError("Can't find audio content in local description.", error_desc);
1477 return false;
1478 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001480 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1481 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482 }
1483
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001484 AudioRecvParameters recv_params = last_recv_params_;
1485 RtpParametersFromMediaDescription(audio, &recv_params);
1486 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001487 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001488 error_desc);
1489 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001491 for (const AudioCodec& codec : audio->codecs()) {
1492 bundle_filter()->AddPayloadType(codec.id);
1493 }
1494 last_recv_params_ = recv_params;
1495
1496 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1497 // only give it to the media channel once we have a remote
1498 // description too (without a remote description, we won't be able
1499 // to send them anyway).
1500 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1501 SafeSetError("Failed to set local audio description streams.", error_desc);
1502 return false;
1503 }
1504
1505 set_local_content_direction(content->direction());
1506 ChangeState();
1507 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508}
1509
1510bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001511 ContentAction action,
1512 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001513 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001514 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 LOG(LS_INFO) << "Setting remote voice description";
1516
1517 const AudioContentDescription* audio =
1518 static_cast<const AudioContentDescription*>(content);
1519 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001520 if (!audio) {
1521 SafeSetError("Can't find audio content in remote description.", error_desc);
1522 return false;
1523 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001525 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1526 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527 }
1528
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001529 AudioSendParameters send_params = last_send_params_;
1530 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001531 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001532 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001533 }
1534 if (!media_channel()->SetSendParameters(send_params)) {
1535 SafeSetError("Failed to set remote audio description send parameters.",
1536 error_desc);
1537 return false;
1538 }
1539 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001541 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1542 // and only give it to the media channel once we have a local
1543 // description too (without a local description, we won't be able to
1544 // recv them anyway).
1545 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1546 SafeSetError("Failed to set remote audio description streams.", error_desc);
1547 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548 }
1549
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001550 if (audio->rtp_header_extensions_set()) {
1551 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1552 }
1553
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001554 set_remote_content_direction(content->direction());
1555 ChangeState();
1556 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557}
1558
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559void VoiceChannel::HandleEarlyMediaTimeout() {
1560 // This occurs on the main thread, not the worker thread.
1561 if (!received_media_) {
1562 LOG(LS_INFO) << "No early media received before timeout";
1563 SignalEarlyMediaTimeout(this);
1564 }
1565}
1566
Peter Boström0c4e06b2015-10-07 12:23:21 +02001567bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1568 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001569 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 if (!enabled()) {
1571 return false;
1572 }
solenberg1d63dd02015-12-02 12:35:09 -08001573 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574}
1575
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001576void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 case MSG_EARLYMEDIATIMEOUT:
1579 HandleEarlyMediaTimeout();
1580 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 case MSG_CHANNEL_ERROR: {
1582 VoiceChannelErrorMessageData* data =
1583 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 delete data;
1585 break;
1586 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 default:
1588 BaseChannel::OnMessage(pmsg);
1589 break;
1590 }
1591}
1592
1593void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001594 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 SignalConnectionMonitor(this, infos);
1596}
1597
1598void VoiceChannel::OnMediaMonitorUpdate(
1599 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1600 ASSERT(media_channel == this->media_channel());
1601 SignalMediaMonitor(this, info);
1602}
1603
1604void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1605 const AudioInfo& info) {
1606 SignalAudioMonitor(this, info);
1607}
1608
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001609void VoiceChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1610 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611}
1612
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001613VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001615 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001617 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001618 : BaseChannel(thread,
1619 media_channel,
1620 transport_controller,
1621 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001624 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625
1626bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001627 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 return false;
1629 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 return true;
1631}
1632
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633VideoChannel::~VideoChannel() {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001634 std::vector<uint32_t> screencast_ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 ScreencastMap::iterator iter;
1636 while (!screencast_capturers_.empty()) {
1637 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1638 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1639 << screencast_capturers_.begin()->first;
1640 ASSERT(false);
1641 break;
1642 }
1643 }
1644
1645 StopMediaMonitor();
1646 // this can't be done in the base class, since it calls a virtual
1647 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001648
1649 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650}
1651
Peter Boström0c4e06b2015-10-07 12:23:21 +02001652bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001653 worker_thread()->Invoke<void>(Bind(
1654 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 return true;
1656}
1657
1658bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001659 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660}
1661
Peter Boström0c4e06b2015-10-07 12:23:21 +02001662bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001663 return worker_thread()->Invoke<bool>(Bind(
1664 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665}
1666
Peter Boström0c4e06b2015-10-07 12:23:21 +02001667bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001668 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1669 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670}
1671
Peter Boström0c4e06b2015-10-07 12:23:21 +02001672bool VideoChannel::RemoveScreencast(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001673 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674}
1675
1676bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001677 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678}
1679
Peter Boström0c4e06b2015-10-07 12:23:21 +02001680int VideoChannel::GetScreencastFps(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001681 ScreencastDetailsData data(ssrc);
1682 worker_thread()->Invoke<void>(Bind(
1683 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001684 return data.fps;
1685}
1686
Peter Boström0c4e06b2015-10-07 12:23:21 +02001687int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001688 ScreencastDetailsData data(ssrc);
1689 worker_thread()->Invoke<void>(Bind(
1690 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001691 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692}
1693
1694bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001695 worker_thread()->Invoke<void>(Bind(
1696 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697 return true;
1698}
1699
1700bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001701 worker_thread()->Invoke<void>(Bind(
1702 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703 return true;
1704}
1705
Peter Boström0c4e06b2015-10-07 12:23:21 +02001706bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001707 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001708 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001709 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1710 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001711}
1712
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 // Send outgoing data if we're the active call, we have the remote content,
1715 // and we have had some form of connectivity.
1716 bool send = IsReadyToSend();
1717 if (!media_channel()->SetSend(send)) {
1718 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1719 // TODO(gangji): Report error back to server.
1720 }
1721
Peter Boström34fbfff2015-09-24 19:20:30 +02001722 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723}
1724
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001725bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1726 return InvokeOnWorker(
1727 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728}
1729
1730void VideoChannel::StartMediaMonitor(int cms) {
1731 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001732 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733 media_monitor_->SignalUpdate.connect(
1734 this, &VideoChannel::OnMediaMonitorUpdate);
1735 media_monitor_->Start(cms);
1736}
1737
1738void VideoChannel::StopMediaMonitor() {
1739 if (media_monitor_) {
1740 media_monitor_->Stop();
1741 media_monitor_.reset();
1742 }
1743}
1744
1745const ContentInfo* VideoChannel::GetFirstContent(
1746 const SessionDescription* sdesc) {
1747 return GetFirstVideoContent(sdesc);
1748}
1749
1750bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001751 ContentAction action,
1752 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001753 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001754 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 LOG(LS_INFO) << "Setting local video description";
1756
1757 const VideoContentDescription* video =
1758 static_cast<const VideoContentDescription*>(content);
1759 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001760 if (!video) {
1761 SafeSetError("Can't find video content in local description.", error_desc);
1762 return false;
1763 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001765 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1766 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 }
1768
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001769 VideoRecvParameters recv_params = last_recv_params_;
1770 RtpParametersFromMediaDescription(video, &recv_params);
1771 if (!media_channel()->SetRecvParameters(recv_params)) {
1772 SafeSetError("Failed to set local video description recv parameters.",
1773 error_desc);
1774 return false;
1775 }
1776 for (const VideoCodec& codec : video->codecs()) {
1777 bundle_filter()->AddPayloadType(codec.id);
1778 }
1779 last_recv_params_ = recv_params;
1780
1781 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1782 // only give it to the media channel once we have a remote
1783 // description too (without a remote description, we won't be able
1784 // to send them anyway).
1785 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1786 SafeSetError("Failed to set local video description streams.", error_desc);
1787 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 }
1789
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001790 set_local_content_direction(content->direction());
1791 ChangeState();
1792 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793}
1794
1795bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001796 ContentAction action,
1797 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001798 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001799 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800 LOG(LS_INFO) << "Setting remote video description";
1801
1802 const VideoContentDescription* video =
1803 static_cast<const VideoContentDescription*>(content);
1804 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001805 if (!video) {
1806 SafeSetError("Can't find video content in remote description.", error_desc);
1807 return false;
1808 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001810
1811 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1812 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 }
1814
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001815 VideoSendParameters send_params = last_send_params_;
1816 RtpSendParametersFromMediaDescription(video, &send_params);
1817 if (video->conference_mode()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001818 send_params.options.conference_mode = rtc::Optional<bool>(true);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001819 }
1820 if (!media_channel()->SetSendParameters(send_params)) {
1821 SafeSetError("Failed to set remote video description send parameters.",
1822 error_desc);
1823 return false;
1824 }
1825 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001827 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1828 // and only give it to the media channel once we have a local
1829 // description too (without a local description, we won't be able to
1830 // recv them anyway).
1831 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1832 SafeSetError("Failed to set remote video description streams.", error_desc);
1833 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 }
1835
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001836 if (video->rtp_header_extensions_set()) {
1837 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001838 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001839
1840 set_remote_content_direction(content->direction());
1841 ChangeState();
1842 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843}
1844
1845bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1846 bool ret = true;
1847 // Set the send format for each of the local streams. If the view request
1848 // does not contain a local stream, set its send format to 0x0, which will
1849 // drop all frames.
1850 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1851 it != local_streams().end(); ++it) {
1852 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1853 StaticVideoViews::const_iterator view;
1854 for (view = request.static_video_views.begin();
1855 view != request.static_video_views.end(); ++view) {
1856 if (view->selector.Matches(*it)) {
1857 format.width = view->width;
1858 format.height = view->height;
1859 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1860 break;
1861 }
1862 }
1863
1864 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1865 }
1866
1867 // Check if the view request has invalid streams.
1868 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1869 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001870 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 LOG(LS_WARNING) << "View request for ("
1872 << it->selector.ssrc << ", '"
1873 << it->selector.groupid << "', '"
1874 << it->selector.streamid << "'"
1875 << ") is not in the local streams.";
1876 }
1877 }
1878
1879 return ret;
1880}
1881
Peter Boström0c4e06b2015-10-07 12:23:21 +02001882bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001884 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001886 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1887 screencast_capturers_[ssrc] = capturer;
1888 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889}
1890
Peter Boström0c4e06b2015-10-07 12:23:21 +02001891bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1893 if (iter == screencast_capturers_.end()) {
1894 return false;
1895 }
1896 // Clean up VideoCapturer.
1897 delete iter->second;
1898 screencast_capturers_.erase(iter);
1899 return true;
1900}
1901
1902bool VideoChannel::IsScreencasting_w() const {
1903 return !screencast_capturers_.empty();
1904}
1905
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001906void VideoChannel::GetScreencastDetails_w(
1907 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001908 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001910 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 }
1912 VideoCapturer* capturer = iter->second;
1913 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001914 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1915 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916}
1917
Peter Boström0c4e06b2015-10-07 12:23:21 +02001918void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001919 rtc::WindowEvent we) {
1920 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 SignalScreencastWindowEvent(ssrc, we);
1922}
1923
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001924void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001925 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926 case MSG_SCREENCASTWINDOWEVENT: {
1927 const ScreencastEventMessageData* data =
1928 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1929 OnScreencastWindowEvent_s(data->ssrc, data->event);
1930 delete data;
1931 break;
1932 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 case MSG_CHANNEL_ERROR: {
1934 const VideoChannelErrorMessageData* data =
1935 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 delete data;
1937 break;
1938 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 default:
1940 BaseChannel::OnMessage(pmsg);
1941 break;
1942 }
1943}
1944
1945void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001946 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947 SignalConnectionMonitor(this, infos);
1948}
1949
1950// TODO(pthatcher): Look into removing duplicate code between
1951// audio, video, and data, perhaps by using templates.
1952void VideoChannel::OnMediaMonitorUpdate(
1953 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1954 ASSERT(media_channel == this->media_channel());
1955 SignalMediaMonitor(this, info);
1956}
1957
Peter Boström0c4e06b2015-10-07 12:23:21 +02001958void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001959 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 ScreencastEventMessageData* pdata =
1961 new ScreencastEventMessageData(ssrc, event);
1962 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1963}
1964
1965void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1966 // Map capturer events to window events. In the future we may want to simply
1967 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001968 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001970 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001972 we = rtc::WE_MINIMIZE;
1973 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1974 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975 } else {
1976 return;
1977 }
1978 previous_we_ = we;
1979
Peter Boström0c4e06b2015-10-07 12:23:21 +02001980 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 if (!GetLocalSsrc(capturer, &ssrc)) {
1982 return;
1983 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001984
1985 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986}
1987
Peter Boström0c4e06b2015-10-07 12:23:21 +02001988bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989 *ssrc = 0;
1990 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
1991 iter != screencast_capturers_.end(); ++iter) {
1992 if (iter->second == capturer) {
1993 *ssrc = iter->first;
1994 return true;
1995 }
1996 }
1997 return false;
1998}
1999
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002000void VideoChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2001 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002}
2003
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002004DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002006 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002007 const std::string& content_name,
2008 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07002009 : BaseChannel(thread,
2010 media_channel,
2011 transport_controller,
2012 content_name,
2013 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002014 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002015 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016
2017DataChannel::~DataChannel() {
2018 StopMediaMonitor();
2019 // this can't be done in the base class, since it calls a virtual
2020 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002021
2022 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023}
2024
2025bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002026 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027 return false;
2028 }
2029 media_channel()->SignalDataReceived.connect(
2030 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002031 media_channel()->SignalReadyToSend.connect(
2032 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002033 media_channel()->SignalStreamClosedRemotely.connect(
2034 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035 return true;
2036}
2037
2038bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002039 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002041 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2042 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002043}
2044
2045const ContentInfo* DataChannel::GetFirstContent(
2046 const SessionDescription* sdesc) {
2047 return GetFirstDataContent(sdesc);
2048}
2049
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002050bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051 if (data_channel_type_ == DCT_SCTP) {
2052 // TODO(pthatcher): Do this in a more robust way by checking for
2053 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002054 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055 } else if (data_channel_type_ == DCT_RTP) {
2056 return BaseChannel::WantsPacket(rtcp, packet);
2057 }
2058 return false;
2059}
2060
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002061bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2062 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063 // It hasn't been set before, so set it now.
2064 if (data_channel_type_ == DCT_NONE) {
2065 data_channel_type_ = new_data_channel_type;
2066 return true;
2067 }
2068
2069 // It's been set before, but doesn't match. That's bad.
2070 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002071 std::ostringstream desc;
2072 desc << "Data channel type mismatch."
2073 << " Expected " << data_channel_type_
2074 << " Got " << new_data_channel_type;
2075 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076 return false;
2077 }
2078
2079 // It's hasn't changed. Nothing to do.
2080 return true;
2081}
2082
2083bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002084 const DataContentDescription* content,
2085 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2087 (content->protocol() == kMediaProtocolDtlsSctp));
2088 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002089 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090}
2091
2092bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002093 ContentAction action,
2094 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002095 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002096 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 LOG(LS_INFO) << "Setting local data description";
2098
2099 const DataContentDescription* data =
2100 static_cast<const DataContentDescription*>(content);
2101 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002102 if (!data) {
2103 SafeSetError("Can't find data content in local description.", error_desc);
2104 return false;
2105 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002107 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108 return false;
2109 }
2110
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002111 if (data_channel_type_ == DCT_RTP) {
2112 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2113 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 }
2115 }
2116
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002117 // FYI: We send the SCTP port number (not to be confused with the
2118 // underlying UDP port number) as a codec parameter. So even SCTP
2119 // data channels need codecs.
2120 DataRecvParameters recv_params = last_recv_params_;
2121 RtpParametersFromMediaDescription(data, &recv_params);
2122 if (!media_channel()->SetRecvParameters(recv_params)) {
2123 SafeSetError("Failed to set remote data description recv parameters.",
2124 error_desc);
2125 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002127 if (data_channel_type_ == DCT_RTP) {
2128 for (const DataCodec& codec : data->codecs()) {
2129 bundle_filter()->AddPayloadType(codec.id);
2130 }
2131 }
2132 last_recv_params_ = recv_params;
2133
2134 // TODO(pthatcher): Move local streams into DataSendParameters, and
2135 // only give it to the media channel once we have a remote
2136 // description too (without a remote description, we won't be able
2137 // to send them anyway).
2138 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2139 SafeSetError("Failed to set local data description streams.", error_desc);
2140 return false;
2141 }
2142
2143 set_local_content_direction(content->direction());
2144 ChangeState();
2145 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146}
2147
2148bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002149 ContentAction action,
2150 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002151 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002152 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153
2154 const DataContentDescription* data =
2155 static_cast<const DataContentDescription*>(content);
2156 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002157 if (!data) {
2158 SafeSetError("Can't find data content in remote description.", error_desc);
2159 return false;
2160 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002162 // If the remote data doesn't have codecs and isn't an update, it
2163 // must be empty, so ignore it.
2164 if (!data->has_codecs() && action != CA_UPDATE) {
2165 return true;
2166 }
2167
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002168 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 return false;
2170 }
2171
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002172 LOG(LS_INFO) << "Setting remote data description";
2173 if (data_channel_type_ == DCT_RTP &&
2174 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2175 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176 }
2177
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002178
2179 DataSendParameters send_params = last_send_params_;
2180 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2181 if (!media_channel()->SetSendParameters(send_params)) {
2182 SafeSetError("Failed to set remote data description send parameters.",
2183 error_desc);
2184 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002186 last_send_params_ = send_params;
2187
2188 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2189 // and only give it to the media channel once we have a local
2190 // description too (without a local description, we won't be able to
2191 // recv them anyway).
2192 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2193 SafeSetError("Failed to set remote data description streams.",
2194 error_desc);
2195 return false;
2196 }
2197
2198 set_remote_content_direction(content->direction());
2199 ChangeState();
2200 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201}
2202
2203void DataChannel::ChangeState() {
2204 // Render incoming data if we're the active call, and we have the local
2205 // content. We receive data on the default channel and multiplexed streams.
2206 bool recv = IsReadyToReceive();
2207 if (!media_channel()->SetReceive(recv)) {
2208 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2209 }
2210
2211 // Send outgoing data if we're the active call, we have the remote content,
2212 // and we have had some form of connectivity.
2213 bool send = IsReadyToSend();
2214 if (!media_channel()->SetSend(send)) {
2215 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2216 }
2217
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002218 // Trigger SignalReadyToSendData asynchronously.
2219 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220
2221 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2222}
2223
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002224void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 switch (pmsg->message_id) {
2226 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002227 DataChannelReadyToSendMessageData* data =
2228 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002229 ready_to_send_data_ = data->data();
2230 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231 delete data;
2232 break;
2233 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 case MSG_DATARECEIVED: {
2235 DataReceivedMessageData* data =
2236 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2237 SignalDataReceived(this, data->params, data->payload);
2238 delete data;
2239 break;
2240 }
2241 case MSG_CHANNEL_ERROR: {
2242 const DataChannelErrorMessageData* data =
2243 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002244 delete data;
2245 break;
2246 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002247 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002248 rtc::TypedMessageData<uint32_t>* data =
2249 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002250 SignalStreamClosedRemotely(data->data());
2251 delete data;
2252 break;
2253 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002254 default:
2255 BaseChannel::OnMessage(pmsg);
2256 break;
2257 }
2258}
2259
2260void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002261 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002262 SignalConnectionMonitor(this, infos);
2263}
2264
2265void DataChannel::StartMediaMonitor(int cms) {
2266 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002267 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002268 media_monitor_->SignalUpdate.connect(
2269 this, &DataChannel::OnMediaMonitorUpdate);
2270 media_monitor_->Start(cms);
2271}
2272
2273void DataChannel::StopMediaMonitor() {
2274 if (media_monitor_) {
2275 media_monitor_->Stop();
2276 media_monitor_->SignalUpdate.disconnect(this);
2277 media_monitor_.reset();
2278 }
2279}
2280
2281void DataChannel::OnMediaMonitorUpdate(
2282 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2283 ASSERT(media_channel == this->media_channel());
2284 SignalMediaMonitor(this, info);
2285}
2286
2287void DataChannel::OnDataReceived(
2288 const ReceiveDataParams& params, const char* data, size_t len) {
2289 DataReceivedMessageData* msg = new DataReceivedMessageData(
2290 params, data, len);
2291 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2292}
2293
Peter Boström0c4e06b2015-10-07 12:23:21 +02002294void DataChannel::OnDataChannelError(uint32_t ssrc,
2295 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2297 ssrc, err);
2298 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2299}
2300
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002301void DataChannel::OnDataChannelReadyToSend(bool writable) {
2302 // This is usded for congestion control to indicate that the stream is ready
2303 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2304 // that the transport channel is ready.
2305 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2306 new DataChannelReadyToSendMessageData(writable));
2307}
2308
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002309void DataChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2310 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311}
2312
2313bool DataChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08002314 return (data_channel_type_ == DCT_RTP) && BaseChannel::ShouldSetupDtlsSrtp();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315}
2316
Peter Boström0c4e06b2015-10-07 12:23:21 +02002317void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2318 rtc::TypedMessageData<uint32_t>* message =
2319 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002320 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2321}
2322
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323} // namespace cricket