blob: dc7a478510ccd0fb1eb482005b715467bf64081f [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"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032#include "webrtc/p2p/base/transportchannel.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000033#include "talk/session/media/channelmanager.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000034#include "talk/session/media/typingmonitor.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000035#include "webrtc/base/bind.h"
36#include "webrtc/base/buffer.h"
37#include "webrtc/base/byteorder.h"
38#include "webrtc/base/common.h"
39#include "webrtc/base/dscp.h"
40#include "webrtc/base/logging.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 SetSessionError(BaseSession* session, BaseSession::Error error,
64 const std::string& error_desc) {
65 session->SetError(error, error_desc);
66}
67
68static void SafeSetError(const std::string& message, std::string* error_desc) {
69 if (error_desc) {
70 *error_desc = message;
71 }
72}
73
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074struct PacketMessageData : public rtc::MessageData {
75 rtc::Buffer packet;
76 rtc::DiffServCodePoint dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077};
78
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079struct ScreencastEventMessageData : public rtc::MessageData {
80 ScreencastEventMessageData(uint32 s, rtc::WindowEvent we)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 : ssrc(s),
82 event(we) {
83 }
84 uint32 ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000085 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086};
87
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088struct VoiceChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 VoiceChannelErrorMessageData(uint32 in_ssrc,
90 VoiceMediaChannel::Error in_error)
91 : ssrc(in_ssrc),
92 error(in_error) {
93 }
94 uint32 ssrc;
95 VoiceMediaChannel::Error error;
96};
97
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000098struct VideoChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 VideoChannelErrorMessageData(uint32 in_ssrc,
100 VideoMediaChannel::Error in_error)
101 : ssrc(in_ssrc),
102 error(in_error) {
103 }
104 uint32 ssrc;
105 VideoMediaChannel::Error error;
106};
107
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000108struct DataChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 DataChannelErrorMessageData(uint32 in_ssrc,
110 DataMediaChannel::Error in_error)
111 : ssrc(in_ssrc),
112 error(in_error) {}
113 uint32 ssrc;
114 DataMediaChannel::Error error;
115};
116
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000118struct VideoChannel::ScreencastDetailsData {
119 explicit ScreencastDetailsData(uint32 s)
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000120 : ssrc(s), fps(0), screencast_max_pixels(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 }
122 uint32 ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000123 int fps;
124 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125};
126
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127static const char* PacketType(bool rtcp) {
128 return (!rtcp) ? "RTP" : "RTCP";
129}
130
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000131static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 // Check the packet size. We could check the header too if needed.
133 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000134 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
135 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136}
137
138static bool IsReceiveContentDirection(MediaContentDirection direction) {
139 return direction == MD_SENDRECV || direction == MD_RECVONLY;
140}
141
142static bool IsSendContentDirection(MediaContentDirection direction) {
143 return direction == MD_SENDRECV || direction == MD_SENDONLY;
144}
145
146static const MediaContentDescription* GetContentDescription(
147 const ContentInfo* cinfo) {
148 if (cinfo == NULL)
149 return NULL;
150 return static_cast<const MediaContentDescription*>(cinfo->description);
151}
152
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000153BaseChannel::BaseChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 MediaChannel* media_channel, BaseSession* session,
155 const std::string& content_name, bool rtcp)
156 : worker_thread_(thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 session_(session),
158 media_channel_(media_channel),
159 content_name_(content_name),
160 rtcp_(rtcp),
161 transport_channel_(NULL),
162 rtcp_transport_channel_(NULL),
163 enabled_(false),
164 writable_(false),
165 rtp_ready_to_send_(false),
166 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 was_ever_writable_(false),
168 local_content_direction_(MD_INACTIVE),
169 remote_content_direction_(MD_INACTIVE),
170 has_received_packet_(false),
171 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000172 secure_required_(false),
173 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000174 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 LOG(LS_INFO) << "Created channel for " << content_name;
176}
177
178BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000179 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000180 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 StopConnectionMonitor();
182 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000183 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 // We must destroy the media channel before the transport channel, otherwise
185 // the media channel may try to send on the dead transport channel. NULLing
186 // is not an effective strategy since the sends will come on another thread.
187 delete media_channel_;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000188 set_transport_channel(nullptr);
189 set_rtcp_transport_channel(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 LOG(LS_INFO) << "Destroyed channel";
191}
192
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000193bool BaseChannel::Init() {
194 if (!SetTransportChannels(session(), rtcp())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 return false;
196 }
197
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000198 if (!SetDtlsSrtpCiphers(transport_channel(), false)) {
199 return false;
200 }
201 if (rtcp() && !SetDtlsSrtpCiphers(rtcp_transport_channel(), true)) {
202 return false;
203 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204
wu@webrtc.orgde305012013-10-31 15:40:38 +0000205 // Both RTP and RTCP channels are set, we can call SetInterface on
206 // media channel and it can set network options.
207 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 return true;
209}
210
wu@webrtc.org78187522013-10-07 23:32:02 +0000211void BaseChannel::Deinit() {
212 media_channel_->SetInterface(NULL);
213}
214
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000215bool BaseChannel::SetTransportChannels(BaseSession* session, bool rtcp) {
216 return worker_thread_->Invoke<bool>(Bind(
217 &BaseChannel::SetTransportChannels_w, this, session, rtcp));
218}
219
220bool BaseChannel::SetTransportChannels_w(BaseSession* session, bool rtcp) {
221 ASSERT(worker_thread_ == rtc::Thread::Current());
222
223 set_transport_channel(session->CreateChannel(
224 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTP));
225 if (!transport_channel()) {
226 return false;
227 }
228 if (rtcp) {
229 set_rtcp_transport_channel(session->CreateChannel(
230 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTCP));
231 if (!rtcp_transport_channel()) {
232 return false;
233 }
234 } else {
235 set_rtcp_transport_channel(nullptr);
236 }
237
238 return true;
239}
240
241void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
242 ASSERT(worker_thread_ == rtc::Thread::Current());
243
244 TransportChannel* old_tc = transport_channel_;
245
246 if (old_tc == new_tc) {
247 return;
248 }
249 if (old_tc) {
250 DisconnectFromTransportChannel(old_tc);
251 session()->DestroyChannel(
252 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTP);
253 }
254
255 transport_channel_ = new_tc;
256
257 if (new_tc) {
258 ConnectToTransportChannel(new_tc);
259 }
260}
261
262void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
263 ASSERT(worker_thread_ == rtc::Thread::Current());
264
265 TransportChannel* old_tc = rtcp_transport_channel_;
266
267 if (old_tc == new_tc) {
268 return;
269 }
270 if (old_tc) {
271 DisconnectFromTransportChannel(old_tc);
272 session()->DestroyChannel(
273 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTCP);
274 }
275
276 rtcp_transport_channel_ = new_tc;
277
278 if (new_tc) {
279 ConnectToTransportChannel(new_tc);
280 }
281}
282
283void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
284 ASSERT(worker_thread_ == rtc::Thread::Current());
285
286 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
287 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
288 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
289}
290
291void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
292 ASSERT(worker_thread_ == rtc::Thread::Current());
293
294 tc->SignalWritableState.disconnect(this);
295 tc->SignalReadPacket.disconnect(this);
296 tc->SignalReadyToSend.disconnect(this);
297}
298
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000300 worker_thread_->Invoke<void>(Bind(
301 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
302 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 return true;
304}
305
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306bool BaseChannel::MuteStream(uint32 ssrc, bool mute) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000307 return InvokeOnWorker(Bind(&BaseChannel::MuteStream_w, this, ssrc, mute));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308}
309
310bool BaseChannel::IsStreamMuted(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000311 return InvokeOnWorker(Bind(&BaseChannel::IsStreamMuted_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000312}
313
314bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000315 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316}
317
318bool BaseChannel::RemoveRecvStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000319 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000320}
321
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000322bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000323 return InvokeOnWorker(
324 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000325}
326
327bool BaseChannel::RemoveSendStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000328 return InvokeOnWorker(
329 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000330}
331
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000333 ContentAction action,
334 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000335 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
336 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337}
338
339bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000340 ContentAction action,
341 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000342 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
343 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344}
345
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000346void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000347 // We pass in the BaseChannel instead of the transport_channel_
348 // because if the transport_channel_ changes, the ConnectionMonitor
349 // would be pointing to the wrong TransportChannel.
350 connection_monitor_.reset(new ConnectionMonitor(
351 this, worker_thread(), rtc::Thread::Current()));
352 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000354 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355}
356
357void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000358 if (connection_monitor_) {
359 connection_monitor_->Stop();
360 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 }
362}
363
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000364bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
365 ASSERT(worker_thread_ == rtc::Thread::Current());
366 return transport_channel_->GetStats(infos);
367}
368
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369bool BaseChannel::IsReadyToReceive() const {
370 // Receive data if we are enabled and have local content,
371 return enabled() && IsReceiveContentDirection(local_content_direction_);
372}
373
374bool BaseChannel::IsReadyToSend() const {
375 // Send outgoing data if we are enabled, have local and remote content,
376 // and we have had some form of connectivity.
377 return enabled() &&
378 IsReceiveContentDirection(remote_content_direction_) &&
379 IsSendContentDirection(local_content_direction_) &&
380 was_ever_writable();
381}
382
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000383bool BaseChannel::SendPacket(rtc::Buffer* packet,
384 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000385 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386}
387
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000388bool BaseChannel::SendRtcp(rtc::Buffer* packet,
389 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000390 return SendPacket(true, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000391}
392
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000393int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000395 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000397 case ST_RTP:
398 channel = transport_channel_;
399 break;
400 case ST_RTCP:
401 channel = rtcp_transport_channel_;
402 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000404 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405}
406
407void BaseChannel::OnWritableState(TransportChannel* channel) {
408 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
409 if (transport_channel_->writable()
410 && (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
411 ChannelWritable_w();
412 } else {
413 ChannelNotWritable_w();
414 }
415}
416
417void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000418 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000419 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000420 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000422 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423
424 // When using RTCP multiplexing we might get RTCP packets on the RTP
425 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
426 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000427 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000428 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000429}
430
431void BaseChannel::OnReadyToSend(TransportChannel* channel) {
432 SetReadyToSend(channel, true);
433}
434
435void BaseChannel::SetReadyToSend(TransportChannel* channel, bool ready) {
436 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
437 if (channel == transport_channel_) {
438 rtp_ready_to_send_ = ready;
439 }
440 if (channel == rtcp_transport_channel_) {
441 rtcp_ready_to_send_ = ready;
442 }
443
444 if (!ready) {
445 // Notify the MediaChannel when either rtp or rtcp channel can't send.
446 media_channel_->OnReadyToSend(false);
447 } else if (rtp_ready_to_send_ &&
448 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
449 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
450 // Notify the MediaChannel when both rtp and rtcp channel can send.
451 media_channel_->OnReadyToSend(true);
452 }
453}
454
455bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
456 const char* data, size_t len) {
457 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000458 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459}
460
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000461bool BaseChannel::SendPacket(bool rtcp, rtc::Buffer* packet,
462 rtc::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 // SendPacket gets called from MediaEngine, typically on an encoder thread.
464 // If the thread is not our worker thread, we will post to our worker
465 // so that the real work happens on our worker. This avoids us having to
466 // synchronize access to all the pieces of the send path, including
467 // SRTP and the inner workings of the transport channels.
468 // The only downside is that we can't return a proper failure code if
469 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000470 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 // Avoid a copy by transferring the ownership of the packet data.
472 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
473 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200474 data->packet = packet->Pass();
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000475 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 worker_thread_->Post(this, message_id, data);
477 return true;
478 }
479
480 // Now that we are on the correct thread, ensure we have a place to send this
481 // packet before doing anything. (We might get RTCP packets that we don't
482 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
483 // transport.
484 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
485 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000486 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 return false;
488 }
489
490 // Protect ourselves against crazy data.
491 if (!ValidPacket(rtcp, packet)) {
492 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000493 << PacketType(rtcp)
494 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000495 return false;
496 }
497
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000498 rtc::PacketOptions options(dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 // Protect if needed.
500 if (srtp_filter_.IsActive()) {
501 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200502 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000503 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000505 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
506 // inside libsrtp for a RTP packet. A external HMAC module will be writing
507 // a fake HMAC value. This is ONLY done for a RTP packet.
508 // Socket layer will update rtp sendtime extension header if present in
509 // packet with current time before updating the HMAC.
510#if !defined(ENABLE_EXTERNAL_AUTH)
511 res = srtp_filter_.ProtectRtp(
512 data, len, static_cast<int>(packet->capacity()), &len);
513#else
henrike@webrtc.org05376342014-03-10 15:53:12 +0000514 options.packet_time_params.rtp_sendtime_extension_id =
515 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000516 res = srtp_filter_.ProtectRtp(
517 data, len, static_cast<int>(packet->capacity()), &len,
518 &options.packet_time_params.srtp_packet_index);
519 // If protection succeeds, let's get auth params from srtp.
520 if (res) {
521 uint8* auth_key = NULL;
522 int key_len;
523 res = srtp_filter_.GetRtpAuthParams(
524 &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
525 if (res) {
526 options.packet_time_params.srtp_auth_key.resize(key_len);
527 options.packet_time_params.srtp_auth_key.assign(auth_key,
528 auth_key + key_len);
529 }
530 }
531#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000532 if (!res) {
533 int seq_num = -1;
534 uint32 ssrc = 0;
535 GetRtpSeqNum(data, len, &seq_num);
536 GetRtpSsrc(data, len, &ssrc);
537 LOG(LS_ERROR) << "Failed to protect " << content_name_
538 << " RTP packet: size=" << len
539 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
540 return false;
541 }
542 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000543 res = srtp_filter_.ProtectRtcp(data, len,
544 static_cast<int>(packet->capacity()),
545 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 if (!res) {
547 int type = -1;
548 GetRtcpType(data, len, &type);
549 LOG(LS_ERROR) << "Failed to protect " << content_name_
550 << " RTCP packet: size=" << len << ", type=" << type;
551 return false;
552 }
553 }
554
555 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000556 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 } else if (secure_required_) {
558 // This is a double check for something that supposedly can't happen.
559 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
560 << " packet when SRTP is inactive and crypto is required";
561
562 ASSERT(false);
563 return false;
564 }
565
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000567 int ret =
Karl Wiberg94784372015-04-20 14:03:07 +0200568 channel->SendPacket(packet->data<char>(), packet->size(), options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000569 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
570 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 if (channel->GetError() == EWOULDBLOCK) {
572 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
573 SetReadyToSend(channel, false);
574 }
575 return false;
576 }
577 return true;
578}
579
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000580bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 // Protect ourselves against crazy data.
582 if (!ValidPacket(rtcp, packet)) {
583 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000584 << PacketType(rtcp)
585 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 return false;
587 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000589 // Bundle filter handles both rtp and rtcp packets.
Karl Wiberg94784372015-04-20 14:03:07 +0200590 return bundle_filter_.DemuxPacket(packet->data<char>(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591}
592
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000593void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
594 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000595 if (!WantsPacket(rtcp, packet)) {
596 return;
597 }
598
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000599 // We are only interested in the first rtp packet because that
600 // indicates the media has started flowing.
601 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602 has_received_packet_ = true;
603 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
604 }
605
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 // Unprotect the packet, if needed.
607 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200608 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000609 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610 bool res;
611 if (!rtcp) {
612 res = srtp_filter_.UnprotectRtp(data, len, &len);
613 if (!res) {
614 int seq_num = -1;
615 uint32 ssrc = 0;
616 GetRtpSeqNum(data, len, &seq_num);
617 GetRtpSsrc(data, len, &ssrc);
618 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
619 << " RTP packet: size=" << len
620 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
621 return;
622 }
623 } else {
624 res = srtp_filter_.UnprotectRtcp(data, len, &len);
625 if (!res) {
626 int type = -1;
627 GetRtcpType(data, len, &type);
628 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
629 << " RTCP packet: size=" << len << ", type=" << type;
630 return;
631 }
632 }
633
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000634 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 } else if (secure_required_) {
636 // Our session description indicates that SRTP is required, but we got a
637 // packet before our SRTP filter is active. This means either that
638 // a) we got SRTP packets before we received the SDES keys, in which case
639 // we can't decrypt it anyway, or
640 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
641 // channels, so we haven't yet extracted keys, even if DTLS did complete
642 // on the channel that the packets are being sent on. It's really good
643 // practice to wait for both RTP and RTCP to be good to go before sending
644 // media, to prevent weird failure modes, so it's fine for us to just eat
645 // packets here. This is all sidestepped if RTCP mux is used anyway.
646 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
647 << " packet when SRTP is inactive and crypto is required";
648 return;
649 }
650
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651 // Push it down to the media channel.
652 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000653 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000655 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 }
657}
658
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000659bool BaseChannel::PushdownLocalDescription(
660 const SessionDescription* local_desc, ContentAction action,
661 std::string* error_desc) {
662 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 const MediaContentDescription* content_desc =
664 GetContentDescription(content_info);
665 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000666 !SetLocalContent(content_desc, action, error_desc)) {
667 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
668 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000669 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000670 return true;
671}
672
673bool BaseChannel::PushdownRemoteDescription(
674 const SessionDescription* remote_desc, ContentAction action,
675 std::string* error_desc) {
676 const ContentInfo* content_info = GetFirstContent(remote_desc);
677 const MediaContentDescription* content_desc =
678 GetContentDescription(content_info);
679 if (content_desc && content_info && !content_info->rejected &&
680 !SetRemoteContent(content_desc, action, error_desc)) {
681 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
682 return false;
683 }
684 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685}
686
687void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000688 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 if (enabled_)
690 return;
691
692 LOG(LS_INFO) << "Channel enabled";
693 enabled_ = true;
694 ChangeState();
695}
696
697void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000698 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 if (!enabled_)
700 return;
701
702 LOG(LS_INFO) << "Channel disabled";
703 enabled_ = false;
704 ChangeState();
705}
706
707bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000708 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 bool ret = media_channel()->MuteStream(ssrc, mute);
710 if (ret) {
711 if (mute)
712 muted_streams_.insert(ssrc);
713 else
714 muted_streams_.erase(ssrc);
715 }
716 return ret;
717}
718
719bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000720 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000721 return muted_streams_.find(ssrc) != muted_streams_.end();
722}
723
724void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000725 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 if (writable_)
727 return;
728
729 LOG(LS_INFO) << "Channel socket writable ("
730 << transport_channel_->content_name() << ", "
731 << transport_channel_->component() << ")"
732 << (was_ever_writable_ ? "" : " for the first time");
733
734 std::vector<ConnectionInfo> infos;
735 transport_channel_->GetStats(&infos);
736 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
737 it != infos.end(); ++it) {
738 if (it->best_connection) {
739 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
740 << "->" << it->remote_candidate.ToSensitiveString();
741 break;
742 }
743 }
744
745 // If we're doing DTLS-SRTP, now is the time.
746 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
747 if (!SetupDtlsSrtp(false)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000748 SignalDtlsSetupFailure(this, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 return;
750 }
751
752 if (rtcp_transport_channel_) {
753 if (!SetupDtlsSrtp(true)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000754 SignalDtlsSetupFailure(this, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 return;
756 }
757 }
758 }
759
760 was_ever_writable_ = true;
761 writable_ = true;
762 ChangeState();
763}
764
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000765void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
766 ASSERT(worker_thread() == rtc::Thread::Current());
767 signaling_thread()->Invoke<void>(Bind(
768 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
769}
770
771void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
772 ASSERT(signaling_thread() == rtc::Thread::Current());
773 SignalDtlsSetupFailure(this, rtcp);
774}
775
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000776bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
777 std::vector<std::string> ciphers;
778 // We always use the default SRTP ciphers for RTCP, but we may use different
779 // ciphers for RTP depending on the media type.
780 if (!rtcp) {
781 GetSrtpCiphers(&ciphers);
782 } else {
783 GetSupportedDefaultCryptoSuites(&ciphers);
784 }
785 return tc->SetSrtpCiphers(ciphers);
786}
787
788bool BaseChannel::ShouldSetupDtlsSrtp() const {
789 return true;
790}
791
792// This function returns true if either DTLS-SRTP is not in use
793// *or* DTLS-SRTP is successfully set up.
794bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
795 bool ret = false;
796
797 TransportChannel *channel = rtcp_channel ?
798 rtcp_transport_channel_ : transport_channel_;
799
800 // No DTLS
801 if (!channel->IsDtlsActive())
802 return true;
803
804 std::string selected_cipher;
805
806 if (!channel->GetSrtpCipher(&selected_cipher)) {
807 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
808 return false;
809 }
810
811 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
812 << content_name() << " "
813 << PacketType(rtcp_channel);
814
815 // OK, we're now doing DTLS (RFC 5764)
816 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
817 SRTP_MASTER_KEY_SALT_LEN * 2);
818
819 // RFC 5705 exporter using the RFC 5764 parameters
820 if (!channel->ExportKeyingMaterial(
821 kDtlsSrtpExporterLabel,
822 NULL, 0, false,
823 &dtls_buffer[0], dtls_buffer.size())) {
824 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
825 ASSERT(false); // This should never happen
826 return false;
827 }
828
829 // Sync up the keys with the DTLS-SRTP interface
830 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
831 SRTP_MASTER_KEY_SALT_LEN);
832 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
833 SRTP_MASTER_KEY_SALT_LEN);
834 size_t offset = 0;
835 memcpy(&client_write_key[0], &dtls_buffer[offset],
836 SRTP_MASTER_KEY_KEY_LEN);
837 offset += SRTP_MASTER_KEY_KEY_LEN;
838 memcpy(&server_write_key[0], &dtls_buffer[offset],
839 SRTP_MASTER_KEY_KEY_LEN);
840 offset += SRTP_MASTER_KEY_KEY_LEN;
841 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
842 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
843 offset += SRTP_MASTER_KEY_SALT_LEN;
844 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
845 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
846
847 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000848 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000849 if (!channel->GetSslRole(&role)) {
850 LOG(LS_WARNING) << "GetSslRole failed";
851 return false;
852 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000854 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 send_key = &server_write_key;
856 recv_key = &client_write_key;
857 } else {
858 send_key = &client_write_key;
859 recv_key = &server_write_key;
860 }
861
862 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000863 ret = srtp_filter_.SetRtcpParams(
864 selected_cipher,
865 &(*send_key)[0],
866 static_cast<int>(send_key->size()),
867 selected_cipher,
868 &(*recv_key)[0],
869 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000871 ret = srtp_filter_.SetRtpParams(
872 selected_cipher,
873 &(*send_key)[0],
874 static_cast<int>(send_key->size()),
875 selected_cipher,
876 &(*recv_key)[0],
877 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 }
879
880 if (!ret)
881 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
882 else
883 dtls_keyed_ = true;
884
885 return ret;
886}
887
888void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000889 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 if (!writable_)
891 return;
892
893 LOG(LS_INFO) << "Channel socket not writable ("
894 << transport_channel_->content_name() << ", "
895 << transport_channel_->component() << ")";
896 writable_ = false;
897 ChangeState();
898}
899
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000900// |dtls| will be set to true if DTLS is active for transport channel and
901// crypto is empty.
902bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000903 bool* dtls,
904 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000905 *dtls = transport_channel_->IsDtlsActive();
906 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000907 SafeSetError("Cryptos must be empty when DTLS is active.",
908 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000909 return false;
910 }
911 return true;
912}
913
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000914bool BaseChannel::SetRecvRtpHeaderExtensions_w(
915 const MediaContentDescription* content,
916 MediaChannel* media_channel,
917 std::string* error_desc) {
918 if (content->rtp_header_extensions_set()) {
919 if (!media_channel->SetRecvRtpHeaderExtensions(
920 content->rtp_header_extensions())) {
921 std::ostringstream desc;
922 desc << "Failed to set receive rtp header extensions for "
923 << MediaTypeToString(content->type()) << " content.";
924 SafeSetError(desc.str(), error_desc);
925 return false;
926 }
927 }
928 return true;
929}
930
931bool BaseChannel::SetSendRtpHeaderExtensions_w(
932 const MediaContentDescription* content,
933 MediaChannel* media_channel,
934 std::string* error_desc) {
935 if (content->rtp_header_extensions_set()) {
936 if (!media_channel->SetSendRtpHeaderExtensions(
937 content->rtp_header_extensions())) {
938 std::ostringstream desc;
939 desc << "Failed to set send rtp header extensions for "
940 << MediaTypeToString(content->type()) << " content.";
941 SafeSetError(desc.str(), error_desc);
942 return false;
943 } else {
944 MaybeCacheRtpAbsSendTimeHeaderExtension(content->rtp_header_extensions());
945 }
946 }
947 return true;
948}
949
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000951 ContentAction action,
952 ContentSource src,
953 std::string* error_desc) {
954 if (action == CA_UPDATE) {
955 // no crypto params.
956 return true;
957 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000959 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000960 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
961 if (!ret) {
962 return false;
963 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 switch (action) {
965 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000966 // If DTLS is already active on the channel, we could be renegotiating
967 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000968 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000969 ret = srtp_filter_.SetOffer(cryptos, src);
970 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 break;
972 case CA_PRANSWER:
973 // If we're doing DTLS-SRTP, we don't want to update the filter
974 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000975 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
977 }
978 break;
979 case CA_ANSWER:
980 // If we're doing DTLS-SRTP, we don't want to update the filter
981 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000982 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 ret = srtp_filter_.SetAnswer(cryptos, src);
984 }
985 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 default:
987 break;
988 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000989 if (!ret) {
990 SafeSetError("Failed to setup SRTP filter.", error_desc);
991 return false;
992 }
993 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994}
995
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700996void BaseChannel::ActivateRtcpMux() {
997 worker_thread_->Invoke<void>(Bind(
998 &BaseChannel::ActivateRtcpMux_w, this));
999}
1000
1001void BaseChannel::ActivateRtcpMux_w() {
1002 if (!rtcp_mux_filter_.IsActive()) {
1003 rtcp_mux_filter_.SetActive();
1004 set_rtcp_transport_channel(NULL);
1005 }
1006}
1007
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001009 ContentSource src,
1010 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 bool ret = false;
1012 switch (action) {
1013 case CA_OFFER:
1014 ret = rtcp_mux_filter_.SetOffer(enable, src);
1015 break;
1016 case CA_PRANSWER:
1017 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1018 break;
1019 case CA_ANSWER:
1020 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1021 if (ret && rtcp_mux_filter_.IsActive()) {
1022 // We activated RTCP mux, close down the RTCP transport.
1023 set_rtcp_transport_channel(NULL);
1024 }
1025 break;
1026 case CA_UPDATE:
1027 // No RTCP mux info.
1028 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001029 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030 default:
1031 break;
1032 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001033 if (!ret) {
1034 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1035 return false;
1036 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001037 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1038 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1039 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001040 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 // If the RTP transport is already writable, then so are we.
1042 if (transport_channel_->writable()) {
1043 ChannelWritable_w();
1044 }
1045 }
1046
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001047 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048}
1049
1050bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001051 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 if (!media_channel()->AddRecvStream(sp))
1053 return false;
1054
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001055 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056}
1057
1058bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001059 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001060 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 return media_channel()->RemoveRecvStream(ssrc);
1062}
1063
1064bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001065 ContentAction action,
1066 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1068 action == CA_PRANSWER || action == CA_UPDATE))
1069 return false;
1070
1071 // If this is an update, streams only contain streams that have changed.
1072 if (action == CA_UPDATE) {
1073 for (StreamParamsVec::const_iterator it = streams.begin();
1074 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001075 const StreamParams* existing_stream =
1076 GetStreamByIds(local_streams_, it->groupid, it->id);
1077 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 if (media_channel()->AddSendStream(*it)) {
1079 local_streams_.push_back(*it);
1080 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1081 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001082 std::ostringstream desc;
1083 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1084 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085 return false;
1086 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001087 } else if (existing_stream && !it->has_ssrcs()) {
1088 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001089 std::ostringstream desc;
1090 desc << "Failed to remove send stream with ssrc "
1091 << it->first_ssrc() << ".";
1092 SafeSetError(desc.str(), error_desc);
1093 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001095 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 } else {
1097 LOG(LS_WARNING) << "Ignore unsupported stream update";
1098 }
1099 }
1100 return true;
1101 }
1102 // Else streams are all the streams we want to send.
1103
1104 // Check for streams that have been removed.
1105 bool ret = true;
1106 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1107 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001108 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001110 std::ostringstream desc;
1111 desc << "Failed to remove send stream with ssrc "
1112 << it->first_ssrc() << ".";
1113 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114 ret = false;
1115 }
1116 }
1117 }
1118 // Check for new streams.
1119 for (StreamParamsVec::const_iterator it = streams.begin();
1120 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001121 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 if (media_channel()->AddSendStream(*it)) {
1123 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1124 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001125 std::ostringstream desc;
1126 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1127 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 ret = false;
1129 }
1130 }
1131 }
1132 local_streams_ = streams;
1133 return ret;
1134}
1135
1136bool BaseChannel::UpdateRemoteStreams_w(
1137 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001138 ContentAction action,
1139 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1141 action == CA_PRANSWER || action == CA_UPDATE))
1142 return false;
1143
1144 // If this is an update, streams only contain streams that have changed.
1145 if (action == CA_UPDATE) {
1146 for (StreamParamsVec::const_iterator it = streams.begin();
1147 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001148 const StreamParams* existing_stream =
1149 GetStreamByIds(remote_streams_, it->groupid, it->id);
1150 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151 if (AddRecvStream_w(*it)) {
1152 remote_streams_.push_back(*it);
1153 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1154 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001155 std::ostringstream desc;
1156 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1157 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 return false;
1159 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001160 } else if (existing_stream && !it->has_ssrcs()) {
1161 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001162 std::ostringstream desc;
1163 desc << "Failed to remove remote stream with ssrc "
1164 << it->first_ssrc() << ".";
1165 SafeSetError(desc.str(), error_desc);
1166 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001168 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 } else {
1170 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001171 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172 << " new stream = " << it->ToString();
1173 }
1174 }
1175 return true;
1176 }
1177 // Else streams are all the streams we want to receive.
1178
1179 // Check for streams that have been removed.
1180 bool ret = true;
1181 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1182 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001183 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001185 std::ostringstream desc;
1186 desc << "Failed to remove remote stream with ssrc "
1187 << it->first_ssrc() << ".";
1188 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 ret = false;
1190 }
1191 }
1192 }
1193 // Check for new streams.
1194 for (StreamParamsVec::const_iterator it = streams.begin();
1195 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001196 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 if (AddRecvStream_w(*it)) {
1198 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1199 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001200 std::ostringstream desc;
1201 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1202 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 ret = false;
1204 }
1205 }
1206 }
1207 remote_streams_ = streams;
1208 return ret;
1209}
1210
1211bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001212 ContentAction action,
1213 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 // Cache secure_required_ for belt and suspenders check on SendPacket
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001215 secure_required_ = content->crypto_required() != CT_NONE;
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001216 // Set local RTP header extensions.
1217 bool ret = SetRecvRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001219 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001221 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001222
1223 // Call UpdateLocalStreams_w last to make sure as many settings as possible
1224 // are already set when creating streams.
1225 ret &= UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 set_local_content_direction(content->direction());
1227 return ret;
1228}
1229
1230bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001231 ContentAction action,
1232 std::string* error_desc) {
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001233 // Set remote RTP header extensions.
1234 bool ret = SetSendRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001236 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001238 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001239 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1240 std::ostringstream desc;
1241 desc << "Failed to set max send bandwidth for "
1242 << MediaTypeToString(content->type()) << " content.";
1243 SafeSetError(desc.str(), error_desc);
1244 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 }
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001246
1247 // Call UpdateRemoteStreams_w last to make sure as many settings as possible
1248 // are already set when creating streams.
1249 ret &= UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 set_remote_content_direction(content->direction());
1251 return ret;
1252}
1253
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001254void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1255 const std::vector<RtpHeaderExtension>& extensions) {
1256 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001257 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001258 rtp_abs_sendtime_extn_id_ =
1259 send_time_extension ? send_time_extension->id : -1;
1260}
1261
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001262void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 case MSG_RTPPACKET:
1265 case MSG_RTCPPACKET: {
1266 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001267 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 delete data; // because it is Posted
1269 break;
1270 }
1271 case MSG_FIRSTPACKETRECEIVED: {
1272 SignalFirstPacketReceived(this);
1273 break;
1274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 }
1276}
1277
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278void BaseChannel::FlushRtcpMessages() {
1279 // Flush all remaining RTCP messages. This should only be called in
1280 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001281 ASSERT(rtc::Thread::Current() == worker_thread_);
1282 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001283 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001284 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001286 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 }
1288}
1289
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001290VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 MediaEngineInterface* media_engine,
1292 VoiceMediaChannel* media_channel,
1293 BaseSession* session,
1294 const std::string& content_name,
1295 bool rtcp)
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001296 : BaseChannel(thread, media_channel, session, content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001298 media_engine_(media_engine),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 received_media_(false) {
1300}
1301
1302VoiceChannel::~VoiceChannel() {
1303 StopAudioMonitor();
1304 StopMediaMonitor();
1305 // this can't be done in the base class, since it calls a virtual
1306 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001307 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308}
1309
1310bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001311 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 return false;
1313 }
1314 media_channel()->SignalMediaError.connect(
1315 this, &VoiceChannel::OnVoiceChannelError);
1316 srtp_filter()->SignalSrtpError.connect(
1317 this, &VoiceChannel::OnSrtpError);
1318 return true;
1319}
1320
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001321bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001322 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1323 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001324}
1325
1326bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001327 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1328 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329}
1330
1331bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001332 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333}
1334
1335// TODO(juberti): Handle early media the right way. We should get an explicit
1336// ringing message telling us to start playing local ringback, which we cancel
1337// if any early media actually arrives. For now, we do the opposite, which is
1338// to wait 1 second for early media, and start playing local ringback if none
1339// arrives.
1340void VoiceChannel::SetEarlyMedia(bool enable) {
1341 if (enable) {
1342 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001343 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1344 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 } else {
1346 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001347 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 }
1349}
1350
1351bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001352 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1353 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354}
1355
1356bool VoiceChannel::PressDTMF(int digit, bool playout) {
1357 int flags = DF_SEND;
1358 if (playout) {
1359 flags |= DF_PLAY;
1360 }
1361 int duration_ms = 160;
1362 return InsertDtmf(0, digit, duration_ms, flags);
1363}
1364
1365bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001366 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1367 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368}
1369
1370bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1371 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001372 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1373 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374}
1375
1376bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001377 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1378 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001380
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001382 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1383 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384}
1385
1386void VoiceChannel::StartMediaMonitor(int cms) {
1387 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001388 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001389 media_monitor_->SignalUpdate.connect(
1390 this, &VoiceChannel::OnMediaMonitorUpdate);
1391 media_monitor_->Start(cms);
1392}
1393
1394void VoiceChannel::StopMediaMonitor() {
1395 if (media_monitor_) {
1396 media_monitor_->Stop();
1397 media_monitor_->SignalUpdate.disconnect(this);
1398 media_monitor_.reset();
1399 }
1400}
1401
1402void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001403 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 audio_monitor_
1405 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1406 audio_monitor_->Start(cms);
1407}
1408
1409void VoiceChannel::StopAudioMonitor() {
1410 if (audio_monitor_) {
1411 audio_monitor_->Stop();
1412 audio_monitor_.reset();
1413 }
1414}
1415
1416bool VoiceChannel::IsAudioMonitorRunning() const {
1417 return (audio_monitor_.get() != NULL);
1418}
1419
1420void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1421 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1422 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1423}
1424
1425void VoiceChannel::StopTypingMonitor() {
1426 typing_monitor_.reset();
1427}
1428
1429bool VoiceChannel::IsTypingMonitorRunning() const {
1430 return typing_monitor_;
1431}
1432
1433bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1434 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1435 if (typing_monitor_ && mute)
1436 typing_monitor_->OnChannelMuted();
1437 return ret;
1438}
1439
1440int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001441 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001442}
1443
1444int VoiceChannel::GetOutputLevel_w() {
1445 return media_channel()->GetOutputLevel();
1446}
1447
1448void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1449 media_channel()->GetActiveStreams(actives);
1450}
1451
1452void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001453 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001454 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001455 int flags) {
1456 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457
1458 // Set a flag when we've received an RTP packet. If we're waiting for early
1459 // media, this will disable the timeout.
1460 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1461 received_media_ = true;
1462 }
1463}
1464
1465void VoiceChannel::ChangeState() {
1466 // Render incoming data if we're the active call, and we have the local
1467 // content. We receive data on the default channel and multiplexed streams.
1468 bool recv = IsReadyToReceive();
1469 if (!media_channel()->SetPlayout(recv)) {
1470 SendLastMediaError();
1471 }
1472
1473 // Send outgoing data if we're the active call, we have the remote content,
1474 // and we have had some form of connectivity.
1475 bool send = IsReadyToSend();
1476 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1477 if (!media_channel()->SetSend(send_flag)) {
1478 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1479 SendLastMediaError();
1480 }
1481
1482 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1483}
1484
1485const ContentInfo* VoiceChannel::GetFirstContent(
1486 const SessionDescription* sdesc) {
1487 return GetFirstAudioContent(sdesc);
1488}
1489
1490bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001491 ContentAction action,
1492 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001493 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 LOG(LS_INFO) << "Setting local voice description";
1495
1496 const AudioContentDescription* audio =
1497 static_cast<const AudioContentDescription*>(content);
1498 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001499 if (!audio) {
1500 SafeSetError("Can't find audio content in local description.", error_desc);
1501 return false;
1502 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001503
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001504 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505 // Set local audio codecs (what we want to receive).
1506 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1507 // is set properly.
1508 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001509 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1510 SafeSetError("Failed to set audio receive codecs.", error_desc);
1511 ret = false;
1512 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513 }
1514
1515 // If everything worked, see if we can start receiving.
1516 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001517 std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1518 for (; it != audio->codecs().end(); ++it) {
1519 bundle_filter()->AddPayloadType(it->id);
1520 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 ChangeState();
1522 } else {
1523 LOG(LS_WARNING) << "Failed to set local voice description";
1524 }
1525 return ret;
1526}
1527
1528bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001529 ContentAction action,
1530 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001531 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 LOG(LS_INFO) << "Setting remote voice description";
1533
1534 const AudioContentDescription* audio =
1535 static_cast<const AudioContentDescription*>(content);
1536 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001537 if (!audio) {
1538 SafeSetError("Can't find audio content in remote description.", error_desc);
1539 return false;
1540 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541
1542 bool ret = true;
1543 // Set remote video codecs (what the other side wants to receive).
1544 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001545 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1546 SafeSetError("Failed to set audio send codecs.", error_desc);
1547 ret = false;
1548 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 }
1550
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001551 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552
1553 if (action != CA_UPDATE) {
1554 // Tweak our audio processing settings, if needed.
1555 AudioOptions audio_options;
1556 if (!media_channel()->GetOptions(&audio_options)) {
1557 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1558 } else {
1559 if (audio->conference_mode()) {
1560 audio_options.conference_mode.Set(true);
1561 }
1562 if (audio->agc_minus_10db()) {
1563 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1564 }
1565 if (!media_channel()->SetOptions(audio_options)) {
1566 // Log an error on failure, but don't abort the call.
1567 LOG(LS_ERROR) << "Failed to set voice channel options";
1568 }
1569 }
1570 }
1571
1572 // If everything worked, see if we can start sending.
1573 if (ret) {
1574 ChangeState();
1575 } else {
1576 LOG(LS_WARNING) << "Failed to set remote voice description";
1577 }
1578 return ret;
1579}
1580
1581bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001582 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1584}
1585
1586bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001587 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588 if (play) {
1589 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1590 } else {
1591 LOG(LS_INFO) << "Stopping ringback tone";
1592 }
1593 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1594}
1595
1596void VoiceChannel::HandleEarlyMediaTimeout() {
1597 // This occurs on the main thread, not the worker thread.
1598 if (!received_media_) {
1599 LOG(LS_INFO) << "No early media received before timeout";
1600 SignalEarlyMediaTimeout(this);
1601 }
1602}
1603
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1605 int flags) {
1606 if (!enabled()) {
1607 return false;
1608 }
1609
1610 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1611}
1612
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001614 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1615 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616}
1617
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001618void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001620 case MSG_EARLYMEDIATIMEOUT:
1621 HandleEarlyMediaTimeout();
1622 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 case MSG_CHANNEL_ERROR: {
1624 VoiceChannelErrorMessageData* data =
1625 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1626 SignalMediaError(this, data->ssrc, data->error);
1627 delete data;
1628 break;
1629 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 default:
1631 BaseChannel::OnMessage(pmsg);
1632 break;
1633 }
1634}
1635
1636void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001637 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 SignalConnectionMonitor(this, infos);
1639}
1640
1641void VoiceChannel::OnMediaMonitorUpdate(
1642 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1643 ASSERT(media_channel == this->media_channel());
1644 SignalMediaMonitor(this, info);
1645}
1646
1647void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1648 const AudioInfo& info) {
1649 SignalAudioMonitor(this, info);
1650}
1651
1652void VoiceChannel::OnVoiceChannelError(
1653 uint32 ssrc, VoiceMediaChannel::Error err) {
1654 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1655 ssrc, err);
1656 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1657}
1658
1659void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1660 SrtpFilter::Error error) {
1661 switch (error) {
1662 case SrtpFilter::ERROR_FAIL:
1663 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1664 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1665 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1666 break;
1667 case SrtpFilter::ERROR_AUTH:
1668 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1669 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1670 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1671 break;
1672 case SrtpFilter::ERROR_REPLAY:
1673 // Only receving channel should have this error.
1674 ASSERT(mode == SrtpFilter::UNPROTECT);
1675 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1676 break;
1677 default:
1678 break;
1679 }
1680}
1681
1682void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1683 GetSupportedAudioCryptoSuites(ciphers);
1684}
1685
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001686VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687 VideoMediaChannel* media_channel,
1688 BaseSession* session,
1689 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001690 bool rtcp)
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001691 : BaseChannel(thread, media_channel, session, content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001694 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695}
1696
1697bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001698 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 return false;
1700 }
1701 media_channel()->SignalMediaError.connect(
1702 this, &VideoChannel::OnVideoChannelError);
1703 srtp_filter()->SignalSrtpError.connect(
1704 this, &VideoChannel::OnSrtpError);
1705 return true;
1706}
1707
1708void VoiceChannel::SendLastMediaError() {
1709 uint32 ssrc;
1710 VoiceMediaChannel::Error error;
1711 media_channel()->GetLastMediaError(&ssrc, &error);
1712 SignalMediaError(this, ssrc, error);
1713}
1714
1715VideoChannel::~VideoChannel() {
1716 std::vector<uint32> screencast_ssrcs;
1717 ScreencastMap::iterator iter;
1718 while (!screencast_capturers_.empty()) {
1719 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1720 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1721 << screencast_capturers_.begin()->first;
1722 ASSERT(false);
1723 break;
1724 }
1725 }
1726
1727 StopMediaMonitor();
1728 // this can't be done in the base class, since it calls a virtual
1729 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001730
1731 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732}
1733
1734bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001735 worker_thread()->Invoke<void>(Bind(
1736 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 return true;
1738}
1739
1740bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001741 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742}
1743
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001744bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1745 return worker_thread()->Invoke<bool>(Bind(
1746 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747}
1748
1749bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001750 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1751 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752}
1753
1754bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001755 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756}
1757
1758bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001759 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760}
1761
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001762int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001763 ScreencastDetailsData data(ssrc);
1764 worker_thread()->Invoke<void>(Bind(
1765 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001766 return data.fps;
1767}
1768
1769int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001770 ScreencastDetailsData data(ssrc);
1771 worker_thread()->Invoke<void>(Bind(
1772 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001773 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774}
1775
1776bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001777 worker_thread()->Invoke<void>(Bind(
1778 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 return true;
1780}
1781
1782bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001783 worker_thread()->Invoke<void>(Bind(
1784 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 return true;
1786}
1787
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788void VideoChannel::ChangeState() {
1789 // Render incoming data if we're the active call, and we have the local
1790 // content. We receive data on the default channel and multiplexed streams.
1791 bool recv = IsReadyToReceive();
1792 if (!media_channel()->SetRender(recv)) {
1793 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1794 // TODO(gangji): Report error back to server.
1795 }
1796
1797 // Send outgoing data if we're the active call, we have the remote content,
1798 // and we have had some form of connectivity.
1799 bool send = IsReadyToSend();
1800 if (!media_channel()->SetSend(send)) {
1801 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1802 // TODO(gangji): Report error back to server.
1803 }
1804
1805 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1806}
1807
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001808bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1809 return InvokeOnWorker(
1810 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811}
1812
1813void VideoChannel::StartMediaMonitor(int cms) {
1814 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001815 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 media_monitor_->SignalUpdate.connect(
1817 this, &VideoChannel::OnMediaMonitorUpdate);
1818 media_monitor_->Start(cms);
1819}
1820
1821void VideoChannel::StopMediaMonitor() {
1822 if (media_monitor_) {
1823 media_monitor_->Stop();
1824 media_monitor_.reset();
1825 }
1826}
1827
1828const ContentInfo* VideoChannel::GetFirstContent(
1829 const SessionDescription* sdesc) {
1830 return GetFirstVideoContent(sdesc);
1831}
1832
1833bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001834 ContentAction action,
1835 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001836 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 LOG(LS_INFO) << "Setting local video description";
1838
1839 const VideoContentDescription* video =
1840 static_cast<const VideoContentDescription*>(content);
1841 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001842 if (!video) {
1843 SafeSetError("Can't find video content in local description.", error_desc);
1844 return false;
1845 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001847 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848 // Set local video codecs (what we want to receive).
1849 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001850 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1851 SafeSetError("Failed to set video receive codecs.", error_desc);
1852 ret = false;
1853 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 }
1855
1856 if (action != CA_UPDATE) {
1857 VideoOptions video_options;
1858 media_channel()->GetOptions(&video_options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 if (!media_channel()->SetOptions(video_options)) {
1860 // Log an error on failure, but don't abort the call.
1861 LOG(LS_ERROR) << "Failed to set video channel options";
1862 }
1863 }
1864
1865 // If everything worked, see if we can start receiving.
1866 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001867 std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1868 for (; it != video->codecs().end(); ++it) {
1869 bundle_filter()->AddPayloadType(it->id);
1870 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 ChangeState();
1872 } else {
1873 LOG(LS_WARNING) << "Failed to set local video description";
1874 }
1875 return ret;
1876}
1877
1878bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001879 ContentAction action,
1880 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001881 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882 LOG(LS_INFO) << "Setting remote video description";
1883
1884 const VideoContentDescription* video =
1885 static_cast<const VideoContentDescription*>(content);
1886 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001887 if (!video) {
1888 SafeSetError("Can't find video content in remote description.", error_desc);
1889 return false;
1890 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891
1892 bool ret = true;
1893 // Set remote video codecs (what the other side wants to receive).
1894 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001895 if (!media_channel()->SetSendCodecs(video->codecs())) {
1896 SafeSetError("Failed to set video send codecs.", error_desc);
1897 ret = false;
1898 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 }
1900
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001901 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001902
1903 if (action != CA_UPDATE) {
1904 // Tweak our video processing settings, if needed.
1905 VideoOptions video_options;
1906 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001907 if (video->conference_mode()) {
1908 video_options.conference_mode.Set(true);
1909 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910
1911 if (!media_channel()->SetOptions(video_options)) {
1912 // Log an error on failure, but don't abort the call.
1913 LOG(LS_ERROR) << "Failed to set video channel options";
1914 }
1915 }
1916
1917 // If everything worked, see if we can start sending.
1918 if (ret) {
1919 ChangeState();
1920 } else {
1921 LOG(LS_WARNING) << "Failed to set remote video description";
1922 }
1923 return ret;
1924}
1925
1926bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1927 bool ret = true;
1928 // Set the send format for each of the local streams. If the view request
1929 // does not contain a local stream, set its send format to 0x0, which will
1930 // drop all frames.
1931 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1932 it != local_streams().end(); ++it) {
1933 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1934 StaticVideoViews::const_iterator view;
1935 for (view = request.static_video_views.begin();
1936 view != request.static_video_views.end(); ++view) {
1937 if (view->selector.Matches(*it)) {
1938 format.width = view->width;
1939 format.height = view->height;
1940 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1941 break;
1942 }
1943 }
1944
1945 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1946 }
1947
1948 // Check if the view request has invalid streams.
1949 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1950 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001951 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 LOG(LS_WARNING) << "View request for ("
1953 << it->selector.ssrc << ", '"
1954 << it->selector.groupid << "', '"
1955 << it->selector.streamid << "'"
1956 << ") is not in the local streams.";
1957 }
1958 }
1959
1960 return ret;
1961}
1962
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001963bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001965 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001967 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1968 screencast_capturers_[ssrc] = capturer;
1969 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970}
1971
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1973 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1974 if (iter == screencast_capturers_.end()) {
1975 return false;
1976 }
1977 // Clean up VideoCapturer.
1978 delete iter->second;
1979 screencast_capturers_.erase(iter);
1980 return true;
1981}
1982
1983bool VideoChannel::IsScreencasting_w() const {
1984 return !screencast_capturers_.empty();
1985}
1986
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001987void VideoChannel::GetScreencastDetails_w(
1988 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001989 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001991 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 }
1993 VideoCapturer* capturer = iter->second;
1994 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001995 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1996 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997}
1998
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002000 rtc::WindowEvent we) {
2001 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 SignalScreencastWindowEvent(ssrc, we);
2003}
2004
2005bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002006 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
2007 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008}
2009
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002010void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 case MSG_SCREENCASTWINDOWEVENT: {
2013 const ScreencastEventMessageData* data =
2014 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
2015 OnScreencastWindowEvent_s(data->ssrc, data->event);
2016 delete data;
2017 break;
2018 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019 case MSG_CHANNEL_ERROR: {
2020 const VideoChannelErrorMessageData* data =
2021 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
2022 SignalMediaError(this, data->ssrc, data->error);
2023 delete data;
2024 break;
2025 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026 default:
2027 BaseChannel::OnMessage(pmsg);
2028 break;
2029 }
2030}
2031
2032void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002033 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 SignalConnectionMonitor(this, infos);
2035}
2036
2037// TODO(pthatcher): Look into removing duplicate code between
2038// audio, video, and data, perhaps by using templates.
2039void VideoChannel::OnMediaMonitorUpdate(
2040 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2041 ASSERT(media_channel == this->media_channel());
2042 SignalMediaMonitor(this, info);
2043}
2044
2045void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002046 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047 ScreencastEventMessageData* pdata =
2048 new ScreencastEventMessageData(ssrc, event);
2049 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2050}
2051
2052void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2053 // Map capturer events to window events. In the future we may want to simply
2054 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002055 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002057 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002058 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002059 we = rtc::WE_MINIMIZE;
2060 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2061 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 } else {
2063 return;
2064 }
2065 previous_we_ = we;
2066
2067 uint32 ssrc = 0;
2068 if (!GetLocalSsrc(capturer, &ssrc)) {
2069 return;
2070 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002071
2072 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073}
2074
2075bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2076 *ssrc = 0;
2077 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2078 iter != screencast_capturers_.end(); ++iter) {
2079 if (iter->second == capturer) {
2080 *ssrc = iter->first;
2081 return true;
2082 }
2083 }
2084 return false;
2085}
2086
2087void VideoChannel::OnVideoChannelError(uint32 ssrc,
2088 VideoMediaChannel::Error error) {
2089 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2090 ssrc, error);
2091 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2092}
2093
2094void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2095 SrtpFilter::Error error) {
2096 switch (error) {
2097 case SrtpFilter::ERROR_FAIL:
2098 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2099 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2100 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2101 break;
2102 case SrtpFilter::ERROR_AUTH:
2103 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2104 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2105 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2106 break;
2107 case SrtpFilter::ERROR_REPLAY:
2108 // Only receving channel should have this error.
2109 ASSERT(mode == SrtpFilter::UNPROTECT);
2110 // TODO(gangji): Turn on the signaling of replay error once we have
2111 // switched to the new mechanism for doing video retransmissions.
2112 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2113 break;
2114 default:
2115 break;
2116 }
2117}
2118
2119
2120void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2121 GetSupportedVideoCryptoSuites(ciphers);
2122}
2123
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002124DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 DataMediaChannel* media_channel,
2126 BaseSession* session,
2127 const std::string& content_name,
2128 bool rtcp)
Fredrik Solenberg0c022642015-08-05 12:25:22 +02002129 : BaseChannel(thread, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002130 data_channel_type_(cricket::DCT_NONE),
2131 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132}
2133
2134DataChannel::~DataChannel() {
2135 StopMediaMonitor();
2136 // this can't be done in the base class, since it calls a virtual
2137 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002138
2139 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140}
2141
2142bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002143 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144 return false;
2145 }
2146 media_channel()->SignalDataReceived.connect(
2147 this, &DataChannel::OnDataReceived);
2148 media_channel()->SignalMediaError.connect(
2149 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002150 media_channel()->SignalReadyToSend.connect(
2151 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002152 media_channel()->SignalStreamClosedRemotely.connect(
2153 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154 srtp_filter()->SignalSrtpError.connect(
2155 this, &DataChannel::OnSrtpError);
2156 return true;
2157}
2158
2159bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002160 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002162 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2163 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164}
2165
2166const ContentInfo* DataChannel::GetFirstContent(
2167 const SessionDescription* sdesc) {
2168 return GetFirstDataContent(sdesc);
2169}
2170
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002171bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172 if (data_channel_type_ == DCT_SCTP) {
2173 // TODO(pthatcher): Do this in a more robust way by checking for
2174 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002175 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176 } else if (data_channel_type_ == DCT_RTP) {
2177 return BaseChannel::WantsPacket(rtcp, packet);
2178 }
2179 return false;
2180}
2181
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002182bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2183 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002184 // It hasn't been set before, so set it now.
2185 if (data_channel_type_ == DCT_NONE) {
2186 data_channel_type_ = new_data_channel_type;
2187 return true;
2188 }
2189
2190 // It's been set before, but doesn't match. That's bad.
2191 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002192 std::ostringstream desc;
2193 desc << "Data channel type mismatch."
2194 << " Expected " << data_channel_type_
2195 << " Got " << new_data_channel_type;
2196 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 return false;
2198 }
2199
2200 // It's hasn't changed. Nothing to do.
2201 return true;
2202}
2203
2204bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002205 const DataContentDescription* content,
2206 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2208 (content->protocol() == kMediaProtocolDtlsSctp));
2209 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002210 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211}
2212
2213bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002214 ContentAction action,
2215 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002216 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 LOG(LS_INFO) << "Setting local data description";
2218
2219 const DataContentDescription* data =
2220 static_cast<const DataContentDescription*>(content);
2221 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002222 if (!data) {
2223 SafeSetError("Can't find data content in local description.", error_desc);
2224 return false;
2225 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226
2227 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002228 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002229 return false;
2230 }
2231
2232 if (data_channel_type_ == DCT_SCTP) {
2233 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002234 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235 if (ret) {
2236 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002237 // As in SetRemoteContent_w, make sure we set the local SCTP port
2238 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002239 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2240 SafeSetError("Failed to set data receive codecs.", error_desc);
2241 ret = false;
2242 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002243 }
2244 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002245 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002246 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002247 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2248 SafeSetError("Failed to set data receive codecs.", error_desc);
2249 ret = false;
2250 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251 }
2252 }
2253
2254 // If everything worked, see if we can start receiving.
2255 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00002256 std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2257 for (; it != data->codecs().end(); ++it) {
2258 bundle_filter()->AddPayloadType(it->id);
2259 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002260 ChangeState();
2261 } else {
2262 LOG(LS_WARNING) << "Failed to set local data description";
2263 }
2264 return ret;
2265}
2266
2267bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002268 ContentAction action,
2269 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002270 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271
2272 const DataContentDescription* data =
2273 static_cast<const DataContentDescription*>(content);
2274 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002275 if (!data) {
2276 SafeSetError("Can't find data content in remote description.", error_desc);
2277 return false;
2278 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279
2280 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002281 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 return false;
2283 }
2284
2285 if (data_channel_type_ == DCT_SCTP) {
2286 LOG(LS_INFO) << "Setting SCTP remote data description";
2287 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002288 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289 if (ret) {
2290 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002291 // We send the SCTP port number (not to be confused with the underlying
2292 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002293 if (!media_channel()->SetSendCodecs(data->codecs())) {
2294 SafeSetError("Failed to set data send codecs.", error_desc);
2295 ret = false;
2296 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 }
2298 } else {
2299 // If the remote data doesn't have codecs and isn't an update, it
2300 // must be empty, so ignore it.
2301 if (action != CA_UPDATE && !data->has_codecs()) {
2302 return true;
2303 }
2304 LOG(LS_INFO) << "Setting remote data description";
2305
2306 // Set remote video codecs (what the other side wants to receive).
2307 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002308 if (!media_channel()->SetSendCodecs(data->codecs())) {
2309 SafeSetError("Failed to set data send codecs.", error_desc);
2310 ret = false;
2311 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312 }
2313
2314 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002315 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002316 }
2317
2318 if (action != CA_UPDATE) {
2319 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002320 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2321 std::ostringstream desc;
2322 desc << "Failed to set max send bandwidth for data content.";
2323 SafeSetError(desc.str(), error_desc);
2324 ret = false;
2325 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002326 }
2327 }
2328
2329 // If everything worked, see if we can start sending.
2330 if (ret) {
2331 ChangeState();
2332 } else {
2333 LOG(LS_WARNING) << "Failed to set remote data description";
2334 }
2335 return ret;
2336}
2337
2338void DataChannel::ChangeState() {
2339 // Render incoming data if we're the active call, and we have the local
2340 // content. We receive data on the default channel and multiplexed streams.
2341 bool recv = IsReadyToReceive();
2342 if (!media_channel()->SetReceive(recv)) {
2343 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2344 }
2345
2346 // Send outgoing data if we're the active call, we have the remote content,
2347 // and we have had some form of connectivity.
2348 bool send = IsReadyToSend();
2349 if (!media_channel()->SetSend(send)) {
2350 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2351 }
2352
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002353 // Trigger SignalReadyToSendData asynchronously.
2354 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355
2356 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2357}
2358
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002359void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 switch (pmsg->message_id) {
2361 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002362 DataChannelReadyToSendMessageData* data =
2363 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002364 ready_to_send_data_ = data->data();
2365 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002366 delete data;
2367 break;
2368 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369 case MSG_DATARECEIVED: {
2370 DataReceivedMessageData* data =
2371 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2372 SignalDataReceived(this, data->params, data->payload);
2373 delete data;
2374 break;
2375 }
2376 case MSG_CHANNEL_ERROR: {
2377 const DataChannelErrorMessageData* data =
2378 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2379 SignalMediaError(this, data->ssrc, data->error);
2380 delete data;
2381 break;
2382 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002383 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002384 rtc::TypedMessageData<uint32>* data =
2385 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002386 SignalStreamClosedRemotely(data->data());
2387 delete data;
2388 break;
2389 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002390 default:
2391 BaseChannel::OnMessage(pmsg);
2392 break;
2393 }
2394}
2395
2396void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002397 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002398 SignalConnectionMonitor(this, infos);
2399}
2400
2401void DataChannel::StartMediaMonitor(int cms) {
2402 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002403 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002404 media_monitor_->SignalUpdate.connect(
2405 this, &DataChannel::OnMediaMonitorUpdate);
2406 media_monitor_->Start(cms);
2407}
2408
2409void DataChannel::StopMediaMonitor() {
2410 if (media_monitor_) {
2411 media_monitor_->Stop();
2412 media_monitor_->SignalUpdate.disconnect(this);
2413 media_monitor_.reset();
2414 }
2415}
2416
2417void DataChannel::OnMediaMonitorUpdate(
2418 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2419 ASSERT(media_channel == this->media_channel());
2420 SignalMediaMonitor(this, info);
2421}
2422
2423void DataChannel::OnDataReceived(
2424 const ReceiveDataParams& params, const char* data, size_t len) {
2425 DataReceivedMessageData* msg = new DataReceivedMessageData(
2426 params, data, len);
2427 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2428}
2429
2430void DataChannel::OnDataChannelError(
2431 uint32 ssrc, DataMediaChannel::Error err) {
2432 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2433 ssrc, err);
2434 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2435}
2436
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002437void DataChannel::OnDataChannelReadyToSend(bool writable) {
2438 // This is usded for congestion control to indicate that the stream is ready
2439 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2440 // that the transport channel is ready.
2441 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2442 new DataChannelReadyToSendMessageData(writable));
2443}
2444
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002445void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2446 SrtpFilter::Error error) {
2447 switch (error) {
2448 case SrtpFilter::ERROR_FAIL:
2449 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2450 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2451 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2452 break;
2453 case SrtpFilter::ERROR_AUTH:
2454 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2455 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2456 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2457 break;
2458 case SrtpFilter::ERROR_REPLAY:
2459 // Only receving channel should have this error.
2460 ASSERT(mode == SrtpFilter::UNPROTECT);
2461 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2462 break;
2463 default:
2464 break;
2465 }
2466}
2467
2468void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2469 GetSupportedDataCryptoSuites(ciphers);
2470}
2471
2472bool DataChannel::ShouldSetupDtlsSrtp() const {
2473 return (data_channel_type_ == DCT_RTP);
2474}
2475
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002476void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002477 rtc::TypedMessageData<uint32>* message =
2478 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002479 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2480}
2481
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002482} // namespace cricket