blob: 4682c05a98aae7a3fda969f8383286785af4dd59 [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 }
155}
156
157template <class Codec, class Options>
158void RtpSendParametersFromMediaDescription(
159 const MediaContentDescriptionImpl<Codec>* desc,
160 RtpSendParameters<Codec, Options>* send_params) {
161 RtpParametersFromMediaDescription(desc, send_params);
162 send_params->max_bandwidth_bps = desc->bandwidth();
163}
164
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000165BaseChannel::BaseChannel(rtc::Thread* thread,
deadbeefcbecd352015-09-23 11:50:27 -0700166 MediaChannel* media_channel,
167 TransportController* transport_controller,
168 const std::string& content_name,
169 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 : worker_thread_(thread),
deadbeefcbecd352015-09-23 11:50:27 -0700171 transport_controller_(transport_controller),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172 media_channel_(media_channel),
173 content_name_(content_name),
deadbeefcbecd352015-09-23 11:50:27 -0700174 rtcp_transport_enabled_(rtcp),
175 transport_channel_(nullptr),
176 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 enabled_(false),
178 writable_(false),
179 rtp_ready_to_send_(false),
180 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 was_ever_writable_(false),
182 local_content_direction_(MD_INACTIVE),
183 remote_content_direction_(MD_INACTIVE),
184 has_received_packet_(false),
185 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000186 secure_required_(false),
187 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000188 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 LOG(LS_INFO) << "Created channel for " << content_name;
190}
191
192BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000193 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000194 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 StopConnectionMonitor();
196 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000197 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 // We must destroy the media channel before the transport channel, otherwise
199 // the media channel may try to send on the dead transport channel. NULLing
200 // is not an effective strategy since the sends will come on another thread.
201 delete media_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700202 // Note that we don't just call set_transport_channel(nullptr) because that
203 // would call a pure virtual method which we can't do from a destructor.
204 if (transport_channel_) {
205 DisconnectFromTransportChannel(transport_channel_);
206 transport_controller_->DestroyTransportChannel_w(
207 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
208 }
209 if (rtcp_transport_channel_) {
210 DisconnectFromTransportChannel(rtcp_transport_channel_);
211 transport_controller_->DestroyTransportChannel_w(
212 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
213 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 LOG(LS_INFO) << "Destroyed channel";
215}
216
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000217bool BaseChannel::Init() {
deadbeefcbecd352015-09-23 11:50:27 -0700218 if (!SetTransport(content_name())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 return false;
220 }
221
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800222 if (!SetDtlsSrtpCryptoSuites(transport_channel(), false)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000223 return false;
224 }
deadbeefcbecd352015-09-23 11:50:27 -0700225 if (rtcp_transport_enabled() &&
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800226 !SetDtlsSrtpCryptoSuites(rtcp_transport_channel(), true)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000227 return false;
228 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229
wu@webrtc.orgde305012013-10-31 15:40:38 +0000230 // Both RTP and RTCP channels are set, we can call SetInterface on
231 // media channel and it can set network options.
232 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 return true;
234}
235
wu@webrtc.org78187522013-10-07 23:32:02 +0000236void BaseChannel::Deinit() {
237 media_channel_->SetInterface(NULL);
238}
239
deadbeefcbecd352015-09-23 11:50:27 -0700240bool BaseChannel::SetTransport(const std::string& transport_name) {
241 return worker_thread_->Invoke<bool>(
242 Bind(&BaseChannel::SetTransport_w, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000243}
244
deadbeefcbecd352015-09-23 11:50:27 -0700245bool BaseChannel::SetTransport_w(const std::string& transport_name) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000246 ASSERT(worker_thread_ == rtc::Thread::Current());
247
deadbeefcbecd352015-09-23 11:50:27 -0700248 if (transport_name == transport_name_) {
249 // Nothing to do if transport name isn't changing
250 return true;
251 }
252
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800253 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
254 // changes and wait until the DTLS handshake is complete to set the newly
255 // negotiated parameters.
256 if (ShouldSetupDtlsSrtp()) {
257 srtp_filter_.ResetParams();
258 }
259
deadbeefcbecd352015-09-23 11:50:27 -0700260 set_transport_channel(transport_controller_->CreateTransportChannel_w(
261 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000262 if (!transport_channel()) {
263 return false;
264 }
deadbeefcbecd352015-09-23 11:50:27 -0700265 if (rtcp_transport_enabled()) {
266 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
267 << " on " << transport_name << " transport ";
268 set_rtcp_transport_channel(transport_controller_->CreateTransportChannel_w(
269 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000270 if (!rtcp_transport_channel()) {
271 return false;
272 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000273 }
274
deadbeefcbecd352015-09-23 11:50:27 -0700275 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000276 return true;
277}
278
279void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
280 ASSERT(worker_thread_ == rtc::Thread::Current());
281
282 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700283 if (!old_tc && !new_tc) {
284 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000285 return;
286 }
deadbeefcbecd352015-09-23 11:50:27 -0700287 ASSERT(old_tc != new_tc);
288
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000289 if (old_tc) {
290 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700291 transport_controller_->DestroyTransportChannel_w(
292 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000293 }
294
295 transport_channel_ = new_tc;
296
297 if (new_tc) {
298 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700299 for (const auto& pair : socket_options_) {
300 new_tc->SetOption(pair.first, pair.second);
301 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000302 }
deadbeefcbecd352015-09-23 11:50:27 -0700303
304 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
305 // setting new channel
306 UpdateWritableState_w();
307 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000308}
309
310void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
311 ASSERT(worker_thread_ == rtc::Thread::Current());
312
313 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700314 if (!old_tc && !new_tc) {
315 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000316 return;
317 }
deadbeefcbecd352015-09-23 11:50:27 -0700318 ASSERT(old_tc != new_tc);
319
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000320 if (old_tc) {
321 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700322 transport_controller_->DestroyTransportChannel_w(
323 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000324 }
325
326 rtcp_transport_channel_ = new_tc;
327
328 if (new_tc) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800329 RTC_CHECK(!(ShouldSetupDtlsSrtp() && srtp_filter_.IsActive()))
330 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
331 << "should never happen.";
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000332 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700333 for (const auto& pair : rtcp_socket_options_) {
334 new_tc->SetOption(pair.first, pair.second);
335 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000336 }
deadbeefcbecd352015-09-23 11:50:27 -0700337
338 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
339 // setting new channel
340 UpdateWritableState_w();
341 SetReadyToSend(true, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000342}
343
344void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
345 ASSERT(worker_thread_ == rtc::Thread::Current());
346
347 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
348 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
349 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800350 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000351}
352
353void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
354 ASSERT(worker_thread_ == rtc::Thread::Current());
355
356 tc->SignalWritableState.disconnect(this);
357 tc->SignalReadPacket.disconnect(this);
358 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800359 tc->SignalDtlsState.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000360}
361
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000363 worker_thread_->Invoke<void>(Bind(
364 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
365 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366 return true;
367}
368
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000370 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371}
372
Peter Boström0c4e06b2015-10-07 12:23:21 +0200373bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000374 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375}
376
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000377bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000378 return InvokeOnWorker(
379 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000380}
381
Peter Boström0c4e06b2015-10-07 12:23:21 +0200382bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000383 return InvokeOnWorker(
384 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000385}
386
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000388 ContentAction action,
389 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000390 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
391 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392}
393
394bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000395 ContentAction action,
396 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000397 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
398 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399}
400
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000402 // We pass in the BaseChannel instead of the transport_channel_
403 // because if the transport_channel_ changes, the ConnectionMonitor
404 // would be pointing to the wrong TransportChannel.
405 connection_monitor_.reset(new ConnectionMonitor(
406 this, worker_thread(), rtc::Thread::Current()));
407 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000408 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000409 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410}
411
412void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000413 if (connection_monitor_) {
414 connection_monitor_->Stop();
415 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 }
417}
418
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000419bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
420 ASSERT(worker_thread_ == rtc::Thread::Current());
421 return transport_channel_->GetStats(infos);
422}
423
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424bool BaseChannel::IsReadyToReceive() const {
425 // Receive data if we are enabled and have local content,
426 return enabled() && IsReceiveContentDirection(local_content_direction_);
427}
428
429bool BaseChannel::IsReadyToSend() const {
430 // Send outgoing data if we are enabled, have local and remote content,
431 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800432 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 IsSendContentDirection(local_content_direction_) &&
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800434 was_ever_writable() &&
435 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436}
437
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000438bool BaseChannel::SendPacket(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700439 const rtc::PacketOptions& options) {
440 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441}
442
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000443bool BaseChannel::SendRtcp(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700444 const rtc::PacketOptions& options) {
445 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446}
447
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000448int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000450 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000452 case ST_RTP:
453 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700454 socket_options_.push_back(
455 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000456 break;
457 case ST_RTCP:
458 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700459 rtcp_socket_options_.push_back(
460 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000461 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000463 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464}
465
466void BaseChannel::OnWritableState(TransportChannel* channel) {
467 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
deadbeefcbecd352015-09-23 11:50:27 -0700468 UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000469}
470
471void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000472 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000473 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000474 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100475 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000477 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478
479 // When using RTCP multiplexing we might get RTCP packets on the RTP
480 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
481 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000482 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000483 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484}
485
486void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700487 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
488 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489}
490
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800491void BaseChannel::OnDtlsState(TransportChannel* channel,
492 DtlsTransportState state) {
493 if (!ShouldSetupDtlsSrtp()) {
494 return;
495 }
496
497 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
498 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
499 // cover other scenarios like the whole channel is writable (not just this
500 // TransportChannel) or when TransportChannel is attached after DTLS is
501 // negotiated.
502 if (state != DTLS_TRANSPORT_CONNECTED) {
503 srtp_filter_.ResetParams();
504 }
505}
506
deadbeefcbecd352015-09-23 11:50:27 -0700507void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
508 if (rtcp) {
509 rtcp_ready_to_send_ = ready;
510 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 rtp_ready_to_send_ = ready;
512 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000513
deadbeefcbecd352015-09-23 11:50:27 -0700514 if (rtp_ready_to_send_ &&
515 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
516 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
torbjornga81a42f2015-09-23 02:16:58 -0700517 // Notify the MediaChannel when both rtp and rtcp channel can send.
518 media_channel_->OnReadyToSend(true);
deadbeefcbecd352015-09-23 11:50:27 -0700519 } else {
520 // Notify the MediaChannel when either rtp or rtcp channel can't send.
521 media_channel_->OnReadyToSend(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 }
523}
524
525bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
526 const char* data, size_t len) {
527 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000528 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529}
530
stefanc1aeaf02015-10-15 07:26:07 -0700531bool BaseChannel::SendPacket(bool rtcp,
532 rtc::Buffer* packet,
533 const rtc::PacketOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 // SendPacket gets called from MediaEngine, typically on an encoder thread.
535 // If the thread is not our worker thread, we will post to our worker
536 // so that the real work happens on our worker. This avoids us having to
537 // synchronize access to all the pieces of the send path, including
538 // SRTP and the inner workings of the transport channels.
539 // The only downside is that we can't return a proper failure code if
540 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000541 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542 // Avoid a copy by transferring the ownership of the packet data.
543 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
544 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200545 data->packet = packet->Pass();
stefanc1aeaf02015-10-15 07:26:07 -0700546 data->options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 worker_thread_->Post(this, message_id, data);
548 return true;
549 }
550
551 // Now that we are on the correct thread, ensure we have a place to send this
552 // packet before doing anything. (We might get RTCP packets that we don't
553 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
554 // transport.
555 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
556 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000557 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 return false;
559 }
560
561 // Protect ourselves against crazy data.
562 if (!ValidPacket(rtcp, packet)) {
563 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000564 << PacketType(rtcp)
565 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 return false;
567 }
568
stefanc1aeaf02015-10-15 07:26:07 -0700569 rtc::PacketOptions updated_options;
570 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 // Protect if needed.
572 if (srtp_filter_.IsActive()) {
573 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200574 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000575 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000576 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000577 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
578 // inside libsrtp for a RTP packet. A external HMAC module will be writing
579 // a fake HMAC value. This is ONLY done for a RTP packet.
580 // Socket layer will update rtp sendtime extension header if present in
581 // packet with current time before updating the HMAC.
582#if !defined(ENABLE_EXTERNAL_AUTH)
583 res = srtp_filter_.ProtectRtp(
584 data, len, static_cast<int>(packet->capacity()), &len);
585#else
stefanc1aeaf02015-10-15 07:26:07 -0700586 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000587 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000588 res = srtp_filter_.ProtectRtp(
589 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700590 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000591 // If protection succeeds, let's get auth params from srtp.
592 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200593 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000594 int key_len;
595 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700596 &auth_key, &key_len,
597 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000598 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700599 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
600 updated_options.packet_time_params.srtp_auth_key.assign(
601 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000602 }
603 }
604#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 if (!res) {
606 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200607 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608 GetRtpSeqNum(data, len, &seq_num);
609 GetRtpSsrc(data, len, &ssrc);
610 LOG(LS_ERROR) << "Failed to protect " << content_name_
611 << " RTP packet: size=" << len
612 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
613 return false;
614 }
615 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000616 res = srtp_filter_.ProtectRtcp(data, len,
617 static_cast<int>(packet->capacity()),
618 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 if (!res) {
620 int type = -1;
621 GetRtcpType(data, len, &type);
622 LOG(LS_ERROR) << "Failed to protect " << content_name_
623 << " RTCP packet: size=" << len << ", type=" << type;
624 return false;
625 }
626 }
627
628 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000629 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 } else if (secure_required_) {
631 // This is a double check for something that supposedly can't happen.
632 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
633 << " packet when SRTP is inactive and crypto is required";
634
635 ASSERT(false);
636 return false;
637 }
638
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000640 int ret =
stefanc1aeaf02015-10-15 07:26:07 -0700641 channel->SendPacket(packet->data<char>(), packet->size(), updated_options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000642 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
643 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 if (channel->GetError() == EWOULDBLOCK) {
645 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700646 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 }
648 return false;
649 }
650 return true;
651}
652
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000653bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 // Protect ourselves against crazy data.
655 if (!ValidPacket(rtcp, packet)) {
656 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000657 << PacketType(rtcp)
658 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 return false;
660 }
pbos482b12e2015-11-16 10:19:58 -0800661 if (rtcp) {
662 // Permit all (seemingly valid) RTCP packets.
663 return true;
664 }
665 // Check whether we handle this payload.
666 return bundle_filter_.DemuxPacket(packet->data<uint8_t>(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667}
668
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000669void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
670 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 if (!WantsPacket(rtcp, packet)) {
672 return;
673 }
674
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000675 // We are only interested in the first rtp packet because that
676 // indicates the media has started flowing.
677 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 has_received_packet_ = true;
679 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
680 }
681
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000682 // Unprotect the packet, if needed.
683 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200684 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000685 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 bool res;
687 if (!rtcp) {
688 res = srtp_filter_.UnprotectRtp(data, len, &len);
689 if (!res) {
690 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200691 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 GetRtpSeqNum(data, len, &seq_num);
693 GetRtpSsrc(data, len, &ssrc);
694 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
695 << " RTP packet: size=" << len
696 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
697 return;
698 }
699 } else {
700 res = srtp_filter_.UnprotectRtcp(data, len, &len);
701 if (!res) {
702 int type = -1;
703 GetRtcpType(data, len, &type);
704 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
705 << " RTCP packet: size=" << len << ", type=" << type;
706 return;
707 }
708 }
709
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000710 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 } else if (secure_required_) {
712 // Our session description indicates that SRTP is required, but we got a
713 // packet before our SRTP filter is active. This means either that
714 // a) we got SRTP packets before we received the SDES keys, in which case
715 // we can't decrypt it anyway, or
716 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
717 // channels, so we haven't yet extracted keys, even if DTLS did complete
718 // on the channel that the packets are being sent on. It's really good
719 // practice to wait for both RTP and RTCP to be good to go before sending
720 // media, to prevent weird failure modes, so it's fine for us to just eat
721 // packets here. This is all sidestepped if RTCP mux is used anyway.
722 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
723 << " packet when SRTP is inactive and crypto is required";
724 return;
725 }
726
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 // Push it down to the media channel.
728 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000729 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000730 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000731 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 }
733}
734
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000735bool BaseChannel::PushdownLocalDescription(
736 const SessionDescription* local_desc, ContentAction action,
737 std::string* error_desc) {
738 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000739 const MediaContentDescription* content_desc =
740 GetContentDescription(content_info);
741 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000742 !SetLocalContent(content_desc, action, error_desc)) {
743 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
744 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000746 return true;
747}
748
749bool BaseChannel::PushdownRemoteDescription(
750 const SessionDescription* remote_desc, ContentAction action,
751 std::string* error_desc) {
752 const ContentInfo* content_info = GetFirstContent(remote_desc);
753 const MediaContentDescription* content_desc =
754 GetContentDescription(content_info);
755 if (content_desc && content_info && !content_info->rejected &&
756 !SetRemoteContent(content_desc, action, error_desc)) {
757 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
758 return false;
759 }
760 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761}
762
763void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000764 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 if (enabled_)
766 return;
767
768 LOG(LS_INFO) << "Channel enabled";
769 enabled_ = true;
770 ChangeState();
771}
772
773void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000774 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 if (!enabled_)
776 return;
777
778 LOG(LS_INFO) << "Channel disabled";
779 enabled_ = false;
780 ChangeState();
781}
782
deadbeefcbecd352015-09-23 11:50:27 -0700783void BaseChannel::UpdateWritableState_w() {
784 if (transport_channel_ && transport_channel_->writable() &&
785 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
786 ChannelWritable_w();
787 } else {
788 ChannelNotWritable_w();
789 }
790}
791
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000793 ASSERT(worker_thread_ == rtc::Thread::Current());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800794 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800796 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797
deadbeefcbecd352015-09-23 11:50:27 -0700798 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000799 << (was_ever_writable_ ? "" : " for the first time");
800
801 std::vector<ConnectionInfo> infos;
802 transport_channel_->GetStats(&infos);
803 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
804 it != infos.end(); ++it) {
805 if (it->best_connection) {
806 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
807 << "->" << it->remote_candidate.ToSensitiveString();
808 break;
809 }
810 }
811
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 was_ever_writable_ = true;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800813 MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814 writable_ = true;
815 ChangeState();
816}
817
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000818void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
819 ASSERT(worker_thread() == rtc::Thread::Current());
820 signaling_thread()->Invoke<void>(Bind(
821 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
822}
823
824void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
825 ASSERT(signaling_thread() == rtc::Thread::Current());
826 SignalDtlsSetupFailure(this, rtcp);
827}
828
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800829bool BaseChannel::SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp) {
830 std::vector<int> crypto_suites;
831 // We always use the default SRTP crypto suites for RTCP, but we may use
832 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833 if (!rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800834 GetSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800836 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800838 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839}
840
841bool BaseChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800842 // Since DTLS is applied to all channels, checking RTP should be enough.
843 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844}
845
846// This function returns true if either DTLS-SRTP is not in use
847// *or* DTLS-SRTP is successfully set up.
848bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
849 bool ret = false;
850
deadbeefcbecd352015-09-23 11:50:27 -0700851 TransportChannel* channel =
852 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800854 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800856 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800858 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
859 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000860 return false;
861 }
862
863 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
864 << content_name() << " "
865 << PacketType(rtcp_channel);
866
867 // OK, we're now doing DTLS (RFC 5764)
868 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
869 SRTP_MASTER_KEY_SALT_LEN * 2);
870
871 // RFC 5705 exporter using the RFC 5764 parameters
872 if (!channel->ExportKeyingMaterial(
873 kDtlsSrtpExporterLabel,
874 NULL, 0, false,
875 &dtls_buffer[0], dtls_buffer.size())) {
876 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
877 ASSERT(false); // This should never happen
878 return false;
879 }
880
881 // Sync up the keys with the DTLS-SRTP interface
882 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
883 SRTP_MASTER_KEY_SALT_LEN);
884 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
885 SRTP_MASTER_KEY_SALT_LEN);
886 size_t offset = 0;
887 memcpy(&client_write_key[0], &dtls_buffer[offset],
888 SRTP_MASTER_KEY_KEY_LEN);
889 offset += SRTP_MASTER_KEY_KEY_LEN;
890 memcpy(&server_write_key[0], &dtls_buffer[offset],
891 SRTP_MASTER_KEY_KEY_LEN);
892 offset += SRTP_MASTER_KEY_KEY_LEN;
893 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
894 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
895 offset += SRTP_MASTER_KEY_SALT_LEN;
896 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
897 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
898
899 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000900 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000901 if (!channel->GetSslRole(&role)) {
902 LOG(LS_WARNING) << "GetSslRole failed";
903 return false;
904 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000906 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 send_key = &server_write_key;
908 recv_key = &client_write_key;
909 } else {
910 send_key = &client_write_key;
911 recv_key = &server_write_key;
912 }
913
914 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800915 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
916 static_cast<int>(send_key->size()),
917 selected_crypto_suite, &(*recv_key)[0],
918 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800920 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
921 static_cast<int>(send_key->size()),
922 selected_crypto_suite, &(*recv_key)[0],
923 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 }
925
926 if (!ret)
927 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
928 else
929 dtls_keyed_ = true;
930
931 return ret;
932}
933
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800934void BaseChannel::MaybeSetupDtlsSrtp_w() {
935 if (srtp_filter_.IsActive()) {
936 return;
937 }
938
939 if (!ShouldSetupDtlsSrtp()) {
940 return;
941 }
942
943 if (!SetupDtlsSrtp(false)) {
944 SignalDtlsSetupFailure_w(false);
945 return;
946 }
947
948 if (rtcp_transport_channel_) {
949 if (!SetupDtlsSrtp(true)) {
950 SignalDtlsSetupFailure_w(true);
951 return;
952 }
953 }
954}
955
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000957 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 if (!writable_)
959 return;
960
deadbeefcbecd352015-09-23 11:50:27 -0700961 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 writable_ = false;
963 ChangeState();
964}
965
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700966bool BaseChannel::SetRtpTransportParameters_w(
967 const MediaContentDescription* content,
968 ContentAction action,
969 ContentSource src,
970 std::string* error_desc) {
971 if (action == CA_UPDATE) {
972 // These parameters never get changed by a CA_UDPATE.
973 return true;
974 }
975
976 // Cache secure_required_ for belt and suspenders check on SendPacket
977 if (src == CS_LOCAL) {
978 set_secure_required(content->crypto_required() != CT_NONE);
979 }
980
981 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
982 return false;
983 }
984
985 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
986 return false;
987 }
988
989 return true;
990}
991
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000992// |dtls| will be set to true if DTLS is active for transport channel and
993// crypto is empty.
994bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000995 bool* dtls,
996 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000997 *dtls = transport_channel_->IsDtlsActive();
998 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000999 SafeSetError("Cryptos must be empty when DTLS is active.",
1000 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001001 return false;
1002 }
1003 return true;
1004}
1005
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001007 ContentAction action,
1008 ContentSource src,
1009 std::string* error_desc) {
1010 if (action == CA_UPDATE) {
1011 // no crypto params.
1012 return true;
1013 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001015 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001016 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
1017 if (!ret) {
1018 return false;
1019 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 switch (action) {
1021 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001022 // If DTLS is already active on the channel, we could be renegotiating
1023 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001024 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001025 ret = srtp_filter_.SetOffer(cryptos, src);
1026 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027 break;
1028 case CA_PRANSWER:
1029 // If we're doing DTLS-SRTP, we don't want to update the filter
1030 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001031 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001032 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1033 }
1034 break;
1035 case CA_ANSWER:
1036 // If we're doing DTLS-SRTP, we don't want to update the filter
1037 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001038 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039 ret = srtp_filter_.SetAnswer(cryptos, src);
1040 }
1041 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 default:
1043 break;
1044 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001045 if (!ret) {
1046 SafeSetError("Failed to setup SRTP filter.", error_desc);
1047 return false;
1048 }
1049 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050}
1051
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001052void BaseChannel::ActivateRtcpMux() {
1053 worker_thread_->Invoke<void>(Bind(
1054 &BaseChannel::ActivateRtcpMux_w, this));
1055}
1056
1057void BaseChannel::ActivateRtcpMux_w() {
1058 if (!rtcp_mux_filter_.IsActive()) {
1059 rtcp_mux_filter_.SetActive();
deadbeefcbecd352015-09-23 11:50:27 -07001060 set_rtcp_transport_channel(nullptr);
1061 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001062 }
1063}
1064
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001065bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001066 ContentSource src,
1067 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 bool ret = false;
1069 switch (action) {
1070 case CA_OFFER:
1071 ret = rtcp_mux_filter_.SetOffer(enable, src);
1072 break;
1073 case CA_PRANSWER:
1074 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1075 break;
1076 case CA_ANSWER:
1077 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1078 if (ret && rtcp_mux_filter_.IsActive()) {
1079 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001080 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1081 << " by destroying RTCP transport channel for "
1082 << transport_name();
1083 set_rtcp_transport_channel(nullptr);
1084 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085 }
1086 break;
1087 case CA_UPDATE:
1088 // No RTCP mux info.
1089 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001090 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091 default:
1092 break;
1093 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001094 if (!ret) {
1095 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1096 return false;
1097 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1099 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1100 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001101 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 // If the RTP transport is already writable, then so are we.
1103 if (transport_channel_->writable()) {
1104 ChannelWritable_w();
1105 }
1106 }
1107
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001108 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109}
1110
1111bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001112 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001113 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114}
1115
Peter Boström0c4e06b2015-10-07 12:23:21 +02001116bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001117 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 return media_channel()->RemoveRecvStream(ssrc);
1119}
1120
1121bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001122 ContentAction action,
1123 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1125 action == CA_PRANSWER || action == CA_UPDATE))
1126 return false;
1127
1128 // If this is an update, streams only contain streams that have changed.
1129 if (action == CA_UPDATE) {
1130 for (StreamParamsVec::const_iterator it = streams.begin();
1131 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001132 const StreamParams* existing_stream =
1133 GetStreamByIds(local_streams_, it->groupid, it->id);
1134 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 if (media_channel()->AddSendStream(*it)) {
1136 local_streams_.push_back(*it);
1137 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1138 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001139 std::ostringstream desc;
1140 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1141 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 return false;
1143 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001144 } else if (existing_stream && !it->has_ssrcs()) {
1145 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001146 std::ostringstream desc;
1147 desc << "Failed to remove send stream with ssrc "
1148 << it->first_ssrc() << ".";
1149 SafeSetError(desc.str(), error_desc);
1150 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001152 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153 } else {
1154 LOG(LS_WARNING) << "Ignore unsupported stream update";
1155 }
1156 }
1157 return true;
1158 }
1159 // Else streams are all the streams we want to send.
1160
1161 // Check for streams that have been removed.
1162 bool ret = true;
1163 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1164 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001165 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001167 std::ostringstream desc;
1168 desc << "Failed to remove send stream with ssrc "
1169 << it->first_ssrc() << ".";
1170 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 ret = false;
1172 }
1173 }
1174 }
1175 // Check for new streams.
1176 for (StreamParamsVec::const_iterator it = streams.begin();
1177 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001178 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001180 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001182 std::ostringstream desc;
1183 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1184 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001185 ret = false;
1186 }
1187 }
1188 }
1189 local_streams_ = streams;
1190 return ret;
1191}
1192
1193bool BaseChannel::UpdateRemoteStreams_w(
1194 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001195 ContentAction action,
1196 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1198 action == CA_PRANSWER || action == CA_UPDATE))
1199 return false;
1200
1201 // If this is an update, streams only contain streams that have changed.
1202 if (action == CA_UPDATE) {
1203 for (StreamParamsVec::const_iterator it = streams.begin();
1204 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001205 const StreamParams* existing_stream =
1206 GetStreamByIds(remote_streams_, it->groupid, it->id);
1207 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 if (AddRecvStream_w(*it)) {
1209 remote_streams_.push_back(*it);
1210 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1211 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001212 std::ostringstream desc;
1213 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1214 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 return false;
1216 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001217 } else if (existing_stream && !it->has_ssrcs()) {
1218 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001219 std::ostringstream desc;
1220 desc << "Failed to remove remote stream with ssrc "
1221 << it->first_ssrc() << ".";
1222 SafeSetError(desc.str(), error_desc);
1223 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001225 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 } else {
1227 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001228 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229 << " new stream = " << it->ToString();
1230 }
1231 }
1232 return true;
1233 }
1234 // Else streams are all the streams we want to receive.
1235
1236 // Check for streams that have been removed.
1237 bool ret = true;
1238 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1239 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001240 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001242 std::ostringstream desc;
1243 desc << "Failed to remove remote stream with ssrc "
1244 << it->first_ssrc() << ".";
1245 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 ret = false;
1247 }
1248 }
1249 }
1250 // Check for new streams.
1251 for (StreamParamsVec::const_iterator it = streams.begin();
1252 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001253 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 if (AddRecvStream_w(*it)) {
1255 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1256 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001257 std::ostringstream desc;
1258 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1259 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 ret = false;
1261 }
1262 }
1263 }
1264 remote_streams_ = streams;
1265 return ret;
1266}
1267
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001268void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1269 const std::vector<RtpHeaderExtension>& extensions) {
1270 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001271 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001272 rtp_abs_sendtime_extn_id_ =
1273 send_time_extension ? send_time_extension->id : -1;
1274}
1275
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001276void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001277 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 case MSG_RTPPACKET:
1280 case MSG_RTCPPACKET: {
1281 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
stefanc1aeaf02015-10-15 07:26:07 -07001282 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet,
1283 data->options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 delete data; // because it is Posted
1285 break;
1286 }
1287 case MSG_FIRSTPACKETRECEIVED: {
1288 SignalFirstPacketReceived(this);
1289 break;
1290 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 }
1292}
1293
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294void BaseChannel::FlushRtcpMessages() {
1295 // Flush all remaining RTCP messages. This should only be called in
1296 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001297 ASSERT(rtc::Thread::Current() == worker_thread_);
1298 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001299 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001300 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001302 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 }
1304}
1305
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001306VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001307 MediaEngineInterface* media_engine,
1308 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001309 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 const std::string& content_name,
1311 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001312 : BaseChannel(thread,
1313 media_channel,
1314 transport_controller,
1315 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001317 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001318 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319
1320VoiceChannel::~VoiceChannel() {
1321 StopAudioMonitor();
1322 StopMediaMonitor();
1323 // this can't be done in the base class, since it calls a virtual
1324 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001325 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326}
1327
1328bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001329 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 return false;
1331 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 return true;
1333}
1334
Peter Boström0c4e06b2015-10-07 12:23:21 +02001335bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001336 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001337 const AudioOptions* options,
1338 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001339 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001340 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341}
1342
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343// TODO(juberti): Handle early media the right way. We should get an explicit
1344// ringing message telling us to start playing local ringback, which we cancel
1345// if any early media actually arrives. For now, we do the opposite, which is
1346// to wait 1 second for early media, and start playing local ringback if none
1347// arrives.
1348void VoiceChannel::SetEarlyMedia(bool enable) {
1349 if (enable) {
1350 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001351 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1352 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 } else {
1354 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001355 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356 }
1357}
1358
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001360 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1361 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001362}
1363
Peter Boström0c4e06b2015-10-07 12:23:21 +02001364bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1365 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001366 int duration) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001367 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
solenberg1d63dd02015-12-02 12:35:09 -08001368 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369}
1370
solenberg4bac9c52015-10-09 02:32:53 -07001371bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1372 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1373 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001375
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001377 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1378 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379}
1380
1381void VoiceChannel::StartMediaMonitor(int cms) {
1382 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001383 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 media_monitor_->SignalUpdate.connect(
1385 this, &VoiceChannel::OnMediaMonitorUpdate);
1386 media_monitor_->Start(cms);
1387}
1388
1389void VoiceChannel::StopMediaMonitor() {
1390 if (media_monitor_) {
1391 media_monitor_->Stop();
1392 media_monitor_->SignalUpdate.disconnect(this);
1393 media_monitor_.reset();
1394 }
1395}
1396
1397void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001398 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001399 audio_monitor_
1400 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1401 audio_monitor_->Start(cms);
1402}
1403
1404void VoiceChannel::StopAudioMonitor() {
1405 if (audio_monitor_) {
1406 audio_monitor_->Stop();
1407 audio_monitor_.reset();
1408 }
1409}
1410
1411bool VoiceChannel::IsAudioMonitorRunning() const {
1412 return (audio_monitor_.get() != NULL);
1413}
1414
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001416 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417}
1418
1419int VoiceChannel::GetOutputLevel_w() {
1420 return media_channel()->GetOutputLevel();
1421}
1422
1423void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1424 media_channel()->GetActiveStreams(actives);
1425}
1426
1427void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001428 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001429 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001430 int flags) {
1431 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432
1433 // Set a flag when we've received an RTP packet. If we're waiting for early
1434 // media, this will disable the timeout.
1435 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1436 received_media_ = true;
1437 }
1438}
1439
1440void VoiceChannel::ChangeState() {
1441 // Render incoming data if we're the active call, and we have the local
1442 // content. We receive data on the default channel and multiplexed streams.
1443 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001444 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445
1446 // Send outgoing data if we're the active call, we have the remote content,
1447 // and we have had some form of connectivity.
1448 bool send = IsReadyToSend();
1449 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1450 if (!media_channel()->SetSend(send_flag)) {
1451 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452 }
1453
1454 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1455}
1456
1457const ContentInfo* VoiceChannel::GetFirstContent(
1458 const SessionDescription* sdesc) {
1459 return GetFirstAudioContent(sdesc);
1460}
1461
1462bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001463 ContentAction action,
1464 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001465 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 LOG(LS_INFO) << "Setting local voice description";
1467
1468 const AudioContentDescription* audio =
1469 static_cast<const AudioContentDescription*>(content);
1470 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001471 if (!audio) {
1472 SafeSetError("Can't find audio content in local description.", error_desc);
1473 return false;
1474 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001476 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1477 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 }
1479
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001480 AudioRecvParameters recv_params = last_recv_params_;
1481 RtpParametersFromMediaDescription(audio, &recv_params);
1482 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001483 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001484 error_desc);
1485 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001487 for (const AudioCodec& codec : audio->codecs()) {
1488 bundle_filter()->AddPayloadType(codec.id);
1489 }
1490 last_recv_params_ = recv_params;
1491
1492 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1493 // only give it to the media channel once we have a remote
1494 // description too (without a remote description, we won't be able
1495 // to send them anyway).
1496 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1497 SafeSetError("Failed to set local audio description streams.", error_desc);
1498 return false;
1499 }
1500
1501 set_local_content_direction(content->direction());
1502 ChangeState();
1503 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504}
1505
1506bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001507 ContentAction action,
1508 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001509 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 LOG(LS_INFO) << "Setting remote voice description";
1511
1512 const AudioContentDescription* audio =
1513 static_cast<const AudioContentDescription*>(content);
1514 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001515 if (!audio) {
1516 SafeSetError("Can't find audio content in remote description.", error_desc);
1517 return false;
1518 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001520 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1521 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 }
1523
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001524 AudioSendParameters send_params = last_send_params_;
1525 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001526 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001527 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001528 }
1529 if (!media_channel()->SetSendParameters(send_params)) {
1530 SafeSetError("Failed to set remote audio description send parameters.",
1531 error_desc);
1532 return false;
1533 }
1534 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001536 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1537 // and only give it to the media channel once we have a local
1538 // description too (without a local description, we won't be able to
1539 // recv them anyway).
1540 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1541 SafeSetError("Failed to set remote audio description streams.", error_desc);
1542 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 }
1544
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001545 if (audio->rtp_header_extensions_set()) {
1546 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1547 }
1548
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001549 set_remote_content_direction(content->direction());
1550 ChangeState();
1551 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552}
1553
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554void VoiceChannel::HandleEarlyMediaTimeout() {
1555 // This occurs on the main thread, not the worker thread.
1556 if (!received_media_) {
1557 LOG(LS_INFO) << "No early media received before timeout";
1558 SignalEarlyMediaTimeout(this);
1559 }
1560}
1561
Peter Boström0c4e06b2015-10-07 12:23:21 +02001562bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1563 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001564 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 if (!enabled()) {
1566 return false;
1567 }
solenberg1d63dd02015-12-02 12:35:09 -08001568 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569}
1570
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001571void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 case MSG_EARLYMEDIATIMEOUT:
1574 HandleEarlyMediaTimeout();
1575 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576 case MSG_CHANNEL_ERROR: {
1577 VoiceChannelErrorMessageData* data =
1578 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001579 delete data;
1580 break;
1581 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 default:
1583 BaseChannel::OnMessage(pmsg);
1584 break;
1585 }
1586}
1587
1588void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001589 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 SignalConnectionMonitor(this, infos);
1591}
1592
1593void VoiceChannel::OnMediaMonitorUpdate(
1594 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1595 ASSERT(media_channel == this->media_channel());
1596 SignalMediaMonitor(this, info);
1597}
1598
1599void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1600 const AudioInfo& info) {
1601 SignalAudioMonitor(this, info);
1602}
1603
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001604void VoiceChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1605 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606}
1607
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001608VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001610 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001612 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001613 : BaseChannel(thread,
1614 media_channel,
1615 transport_controller,
1616 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001619 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620
1621bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001622 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 return false;
1624 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 return true;
1626}
1627
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628VideoChannel::~VideoChannel() {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001629 std::vector<uint32_t> screencast_ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 ScreencastMap::iterator iter;
1631 while (!screencast_capturers_.empty()) {
1632 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1633 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1634 << screencast_capturers_.begin()->first;
1635 ASSERT(false);
1636 break;
1637 }
1638 }
1639
1640 StopMediaMonitor();
1641 // this can't be done in the base class, since it calls a virtual
1642 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001643
1644 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645}
1646
Peter Boström0c4e06b2015-10-07 12:23:21 +02001647bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001648 worker_thread()->Invoke<void>(Bind(
1649 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 return true;
1651}
1652
1653bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001654 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655}
1656
Peter Boström0c4e06b2015-10-07 12:23:21 +02001657bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001658 return worker_thread()->Invoke<bool>(Bind(
1659 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660}
1661
Peter Boström0c4e06b2015-10-07 12:23:21 +02001662bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001663 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1664 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665}
1666
Peter Boström0c4e06b2015-10-07 12:23:21 +02001667bool VideoChannel::RemoveScreencast(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001668 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669}
1670
1671bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001672 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673}
1674
Peter Boström0c4e06b2015-10-07 12:23:21 +02001675int VideoChannel::GetScreencastFps(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001676 ScreencastDetailsData data(ssrc);
1677 worker_thread()->Invoke<void>(Bind(
1678 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001679 return data.fps;
1680}
1681
Peter Boström0c4e06b2015-10-07 12:23:21 +02001682int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001683 ScreencastDetailsData data(ssrc);
1684 worker_thread()->Invoke<void>(Bind(
1685 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001686 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687}
1688
1689bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001690 worker_thread()->Invoke<void>(Bind(
1691 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 return true;
1693}
1694
1695bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001696 worker_thread()->Invoke<void>(Bind(
1697 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 return true;
1699}
1700
Peter Boström0c4e06b2015-10-07 12:23:21 +02001701bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001702 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001703 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001704 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1705 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001706}
1707
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 // Send outgoing data if we're the active call, we have the remote content,
1710 // and we have had some form of connectivity.
1711 bool send = IsReadyToSend();
1712 if (!media_channel()->SetSend(send)) {
1713 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1714 // TODO(gangji): Report error back to server.
1715 }
1716
Peter Boström34fbfff2015-09-24 19:20:30 +02001717 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718}
1719
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001720bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1721 return InvokeOnWorker(
1722 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723}
1724
1725void VideoChannel::StartMediaMonitor(int cms) {
1726 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001727 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728 media_monitor_->SignalUpdate.connect(
1729 this, &VideoChannel::OnMediaMonitorUpdate);
1730 media_monitor_->Start(cms);
1731}
1732
1733void VideoChannel::StopMediaMonitor() {
1734 if (media_monitor_) {
1735 media_monitor_->Stop();
1736 media_monitor_.reset();
1737 }
1738}
1739
1740const ContentInfo* VideoChannel::GetFirstContent(
1741 const SessionDescription* sdesc) {
1742 return GetFirstVideoContent(sdesc);
1743}
1744
1745bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001746 ContentAction action,
1747 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001748 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 LOG(LS_INFO) << "Setting local video description";
1750
1751 const VideoContentDescription* video =
1752 static_cast<const VideoContentDescription*>(content);
1753 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001754 if (!video) {
1755 SafeSetError("Can't find video content in local description.", error_desc);
1756 return false;
1757 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001759 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1760 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 }
1762
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001763 VideoRecvParameters recv_params = last_recv_params_;
1764 RtpParametersFromMediaDescription(video, &recv_params);
1765 if (!media_channel()->SetRecvParameters(recv_params)) {
1766 SafeSetError("Failed to set local video description recv parameters.",
1767 error_desc);
1768 return false;
1769 }
1770 for (const VideoCodec& codec : video->codecs()) {
1771 bundle_filter()->AddPayloadType(codec.id);
1772 }
1773 last_recv_params_ = recv_params;
1774
1775 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1776 // only give it to the media channel once we have a remote
1777 // description too (without a remote description, we won't be able
1778 // to send them anyway).
1779 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1780 SafeSetError("Failed to set local video description streams.", error_desc);
1781 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 }
1783
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001784 set_local_content_direction(content->direction());
1785 ChangeState();
1786 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787}
1788
1789bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001790 ContentAction action,
1791 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001792 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 LOG(LS_INFO) << "Setting remote video description";
1794
1795 const VideoContentDescription* video =
1796 static_cast<const VideoContentDescription*>(content);
1797 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001798 if (!video) {
1799 SafeSetError("Can't find video content in remote description.", error_desc);
1800 return false;
1801 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001803
1804 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1805 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806 }
1807
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001808 VideoSendParameters send_params = last_send_params_;
1809 RtpSendParametersFromMediaDescription(video, &send_params);
1810 if (video->conference_mode()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001811 send_params.options.conference_mode = rtc::Optional<bool>(true);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001812 }
1813 if (!media_channel()->SetSendParameters(send_params)) {
1814 SafeSetError("Failed to set remote video description send parameters.",
1815 error_desc);
1816 return false;
1817 }
1818 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001820 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1821 // and only give it to the media channel once we have a local
1822 // description too (without a local description, we won't be able to
1823 // recv them anyway).
1824 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1825 SafeSetError("Failed to set remote video description streams.", error_desc);
1826 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 }
1828
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001829 if (video->rtp_header_extensions_set()) {
1830 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001832
1833 set_remote_content_direction(content->direction());
1834 ChangeState();
1835 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001836}
1837
1838bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1839 bool ret = true;
1840 // Set the send format for each of the local streams. If the view request
1841 // does not contain a local stream, set its send format to 0x0, which will
1842 // drop all frames.
1843 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1844 it != local_streams().end(); ++it) {
1845 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1846 StaticVideoViews::const_iterator view;
1847 for (view = request.static_video_views.begin();
1848 view != request.static_video_views.end(); ++view) {
1849 if (view->selector.Matches(*it)) {
1850 format.width = view->width;
1851 format.height = view->height;
1852 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1853 break;
1854 }
1855 }
1856
1857 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1858 }
1859
1860 // Check if the view request has invalid streams.
1861 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1862 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001863 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 LOG(LS_WARNING) << "View request for ("
1865 << it->selector.ssrc << ", '"
1866 << it->selector.groupid << "', '"
1867 << it->selector.streamid << "'"
1868 << ") is not in the local streams.";
1869 }
1870 }
1871
1872 return ret;
1873}
1874
Peter Boström0c4e06b2015-10-07 12:23:21 +02001875bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001877 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001879 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1880 screencast_capturers_[ssrc] = capturer;
1881 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882}
1883
Peter Boström0c4e06b2015-10-07 12:23:21 +02001884bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1886 if (iter == screencast_capturers_.end()) {
1887 return false;
1888 }
1889 // Clean up VideoCapturer.
1890 delete iter->second;
1891 screencast_capturers_.erase(iter);
1892 return true;
1893}
1894
1895bool VideoChannel::IsScreencasting_w() const {
1896 return !screencast_capturers_.empty();
1897}
1898
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001899void VideoChannel::GetScreencastDetails_w(
1900 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001901 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001903 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 }
1905 VideoCapturer* capturer = iter->second;
1906 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001907 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1908 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909}
1910
Peter Boström0c4e06b2015-10-07 12:23:21 +02001911void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001912 rtc::WindowEvent we) {
1913 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914 SignalScreencastWindowEvent(ssrc, we);
1915}
1916
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001917void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001919 case MSG_SCREENCASTWINDOWEVENT: {
1920 const ScreencastEventMessageData* data =
1921 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1922 OnScreencastWindowEvent_s(data->ssrc, data->event);
1923 delete data;
1924 break;
1925 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926 case MSG_CHANNEL_ERROR: {
1927 const VideoChannelErrorMessageData* data =
1928 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929 delete data;
1930 break;
1931 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 default:
1933 BaseChannel::OnMessage(pmsg);
1934 break;
1935 }
1936}
1937
1938void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001939 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940 SignalConnectionMonitor(this, infos);
1941}
1942
1943// TODO(pthatcher): Look into removing duplicate code between
1944// audio, video, and data, perhaps by using templates.
1945void VideoChannel::OnMediaMonitorUpdate(
1946 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1947 ASSERT(media_channel == this->media_channel());
1948 SignalMediaMonitor(this, info);
1949}
1950
Peter Boström0c4e06b2015-10-07 12:23:21 +02001951void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001952 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 ScreencastEventMessageData* pdata =
1954 new ScreencastEventMessageData(ssrc, event);
1955 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1956}
1957
1958void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1959 // Map capturer events to window events. In the future we may want to simply
1960 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001961 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001963 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001965 we = rtc::WE_MINIMIZE;
1966 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1967 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968 } else {
1969 return;
1970 }
1971 previous_we_ = we;
1972
Peter Boström0c4e06b2015-10-07 12:23:21 +02001973 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974 if (!GetLocalSsrc(capturer, &ssrc)) {
1975 return;
1976 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001977
1978 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979}
1980
Peter Boström0c4e06b2015-10-07 12:23:21 +02001981bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 *ssrc = 0;
1983 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
1984 iter != screencast_capturers_.end(); ++iter) {
1985 if (iter->second == capturer) {
1986 *ssrc = iter->first;
1987 return true;
1988 }
1989 }
1990 return false;
1991}
1992
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001993void VideoChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1994 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995}
1996
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001997DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001999 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000 const std::string& content_name,
2001 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07002002 : BaseChannel(thread,
2003 media_channel,
2004 transport_controller,
2005 content_name,
2006 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002007 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002008 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009
2010DataChannel::~DataChannel() {
2011 StopMediaMonitor();
2012 // this can't be done in the base class, since it calls a virtual
2013 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002014
2015 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016}
2017
2018bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002019 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002020 return false;
2021 }
2022 media_channel()->SignalDataReceived.connect(
2023 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002024 media_channel()->SignalReadyToSend.connect(
2025 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002026 media_channel()->SignalStreamClosedRemotely.connect(
2027 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 return true;
2029}
2030
2031bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002032 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002034 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2035 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036}
2037
2038const ContentInfo* DataChannel::GetFirstContent(
2039 const SessionDescription* sdesc) {
2040 return GetFirstDataContent(sdesc);
2041}
2042
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002043bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044 if (data_channel_type_ == DCT_SCTP) {
2045 // TODO(pthatcher): Do this in a more robust way by checking for
2046 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002047 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048 } else if (data_channel_type_ == DCT_RTP) {
2049 return BaseChannel::WantsPacket(rtcp, packet);
2050 }
2051 return false;
2052}
2053
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002054bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2055 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056 // It hasn't been set before, so set it now.
2057 if (data_channel_type_ == DCT_NONE) {
2058 data_channel_type_ = new_data_channel_type;
2059 return true;
2060 }
2061
2062 // It's been set before, but doesn't match. That's bad.
2063 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002064 std::ostringstream desc;
2065 desc << "Data channel type mismatch."
2066 << " Expected " << data_channel_type_
2067 << " Got " << new_data_channel_type;
2068 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069 return false;
2070 }
2071
2072 // It's hasn't changed. Nothing to do.
2073 return true;
2074}
2075
2076bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002077 const DataContentDescription* content,
2078 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2080 (content->protocol() == kMediaProtocolDtlsSctp));
2081 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002082 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083}
2084
2085bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002086 ContentAction action,
2087 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002088 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089 LOG(LS_INFO) << "Setting local data description";
2090
2091 const DataContentDescription* data =
2092 static_cast<const DataContentDescription*>(content);
2093 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002094 if (!data) {
2095 SafeSetError("Can't find data content in local description.", error_desc);
2096 return false;
2097 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002099 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 return false;
2101 }
2102
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002103 if (data_channel_type_ == DCT_RTP) {
2104 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2105 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 }
2107 }
2108
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002109 // FYI: We send the SCTP port number (not to be confused with the
2110 // underlying UDP port number) as a codec parameter. So even SCTP
2111 // data channels need codecs.
2112 DataRecvParameters recv_params = last_recv_params_;
2113 RtpParametersFromMediaDescription(data, &recv_params);
2114 if (!media_channel()->SetRecvParameters(recv_params)) {
2115 SafeSetError("Failed to set remote data description recv parameters.",
2116 error_desc);
2117 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002119 if (data_channel_type_ == DCT_RTP) {
2120 for (const DataCodec& codec : data->codecs()) {
2121 bundle_filter()->AddPayloadType(codec.id);
2122 }
2123 }
2124 last_recv_params_ = recv_params;
2125
2126 // TODO(pthatcher): Move local streams into DataSendParameters, and
2127 // only give it to the media channel once we have a remote
2128 // description too (without a remote description, we won't be able
2129 // to send them anyway).
2130 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2131 SafeSetError("Failed to set local data description streams.", error_desc);
2132 return false;
2133 }
2134
2135 set_local_content_direction(content->direction());
2136 ChangeState();
2137 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138}
2139
2140bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002141 ContentAction action,
2142 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002143 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144
2145 const DataContentDescription* data =
2146 static_cast<const DataContentDescription*>(content);
2147 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002148 if (!data) {
2149 SafeSetError("Can't find data content in remote description.", error_desc);
2150 return false;
2151 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002153 // If the remote data doesn't have codecs and isn't an update, it
2154 // must be empty, so ignore it.
2155 if (!data->has_codecs() && action != CA_UPDATE) {
2156 return true;
2157 }
2158
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002159 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 return false;
2161 }
2162
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002163 LOG(LS_INFO) << "Setting remote data description";
2164 if (data_channel_type_ == DCT_RTP &&
2165 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2166 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167 }
2168
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002169
2170 DataSendParameters send_params = last_send_params_;
2171 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2172 if (!media_channel()->SetSendParameters(send_params)) {
2173 SafeSetError("Failed to set remote data description send parameters.",
2174 error_desc);
2175 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002177 last_send_params_ = send_params;
2178
2179 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2180 // and only give it to the media channel once we have a local
2181 // description too (without a local description, we won't be able to
2182 // recv them anyway).
2183 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2184 SafeSetError("Failed to set remote data description streams.",
2185 error_desc);
2186 return false;
2187 }
2188
2189 set_remote_content_direction(content->direction());
2190 ChangeState();
2191 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192}
2193
2194void DataChannel::ChangeState() {
2195 // Render incoming data if we're the active call, and we have the local
2196 // content. We receive data on the default channel and multiplexed streams.
2197 bool recv = IsReadyToReceive();
2198 if (!media_channel()->SetReceive(recv)) {
2199 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2200 }
2201
2202 // Send outgoing data if we're the active call, we have the remote content,
2203 // and we have had some form of connectivity.
2204 bool send = IsReadyToSend();
2205 if (!media_channel()->SetSend(send)) {
2206 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2207 }
2208
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002209 // Trigger SignalReadyToSendData asynchronously.
2210 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211
2212 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2213}
2214
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002215void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216 switch (pmsg->message_id) {
2217 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002218 DataChannelReadyToSendMessageData* data =
2219 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002220 ready_to_send_data_ = data->data();
2221 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222 delete data;
2223 break;
2224 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 case MSG_DATARECEIVED: {
2226 DataReceivedMessageData* data =
2227 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2228 SignalDataReceived(this, data->params, data->payload);
2229 delete data;
2230 break;
2231 }
2232 case MSG_CHANNEL_ERROR: {
2233 const DataChannelErrorMessageData* data =
2234 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235 delete data;
2236 break;
2237 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002238 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002239 rtc::TypedMessageData<uint32_t>* data =
2240 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002241 SignalStreamClosedRemotely(data->data());
2242 delete data;
2243 break;
2244 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245 default:
2246 BaseChannel::OnMessage(pmsg);
2247 break;
2248 }
2249}
2250
2251void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002252 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253 SignalConnectionMonitor(this, infos);
2254}
2255
2256void DataChannel::StartMediaMonitor(int cms) {
2257 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002258 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259 media_monitor_->SignalUpdate.connect(
2260 this, &DataChannel::OnMediaMonitorUpdate);
2261 media_monitor_->Start(cms);
2262}
2263
2264void DataChannel::StopMediaMonitor() {
2265 if (media_monitor_) {
2266 media_monitor_->Stop();
2267 media_monitor_->SignalUpdate.disconnect(this);
2268 media_monitor_.reset();
2269 }
2270}
2271
2272void DataChannel::OnMediaMonitorUpdate(
2273 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2274 ASSERT(media_channel == this->media_channel());
2275 SignalMediaMonitor(this, info);
2276}
2277
2278void DataChannel::OnDataReceived(
2279 const ReceiveDataParams& params, const char* data, size_t len) {
2280 DataReceivedMessageData* msg = new DataReceivedMessageData(
2281 params, data, len);
2282 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2283}
2284
Peter Boström0c4e06b2015-10-07 12:23:21 +02002285void DataChannel::OnDataChannelError(uint32_t ssrc,
2286 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2288 ssrc, err);
2289 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2290}
2291
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002292void DataChannel::OnDataChannelReadyToSend(bool writable) {
2293 // This is usded for congestion control to indicate that the stream is ready
2294 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2295 // that the transport channel is ready.
2296 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2297 new DataChannelReadyToSendMessageData(writable));
2298}
2299
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002300void DataChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2301 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002302}
2303
2304bool DataChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08002305 return (data_channel_type_ == DCT_RTP) && BaseChannel::ShouldSetupDtlsSrtp();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002306}
2307
Peter Boström0c4e06b2015-10-07 12:23:21 +02002308void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2309 rtc::TypedMessageData<uint32_t>* message =
2310 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002311 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2312}
2313
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002314} // namespace cricket