blob: eb06aef366872c58db30abcbd488a9aeb54a690c [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 &&
134 packet->length() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
135 packet->length() <= kMaxRtpPacketLen);
136}
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 MediaEngineInterface* media_engine,
155 MediaChannel* media_channel, BaseSession* session,
156 const std::string& content_name, bool rtcp)
157 : worker_thread_(thread),
158 media_engine_(media_engine),
159 session_(session),
160 media_channel_(media_channel),
161 content_name_(content_name),
162 rtcp_(rtcp),
163 transport_channel_(NULL),
164 rtcp_transport_channel_(NULL),
165 enabled_(false),
166 writable_(false),
167 rtp_ready_to_send_(false),
168 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 was_ever_writable_(false),
170 local_content_direction_(MD_INACTIVE),
171 remote_content_direction_(MD_INACTIVE),
172 has_received_packet_(false),
173 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000174 secure_required_(false),
175 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000176 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 LOG(LS_INFO) << "Created channel for " << content_name;
178}
179
180BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000181 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000182 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 StopConnectionMonitor();
184 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000185 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 // We must destroy the media channel before the transport channel, otherwise
187 // the media channel may try to send on the dead transport channel. NULLing
188 // is not an effective strategy since the sends will come on another thread.
189 delete media_channel_;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000190 set_transport_channel(nullptr);
191 set_rtcp_transport_channel(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 LOG(LS_INFO) << "Destroyed channel";
193}
194
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000195bool BaseChannel::Init() {
196 if (!SetTransportChannels(session(), rtcp())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 return false;
198 }
199
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000200 if (!SetDtlsSrtpCiphers(transport_channel(), false)) {
201 return false;
202 }
203 if (rtcp() && !SetDtlsSrtpCiphers(rtcp_transport_channel(), true)) {
204 return false;
205 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206
207 session_->SignalNewLocalDescription.connect(
208 this, &BaseChannel::OnNewLocalDescription);
209 session_->SignalNewRemoteDescription.connect(
210 this, &BaseChannel::OnNewRemoteDescription);
211
wu@webrtc.orgde305012013-10-31 15:40:38 +0000212 // Both RTP and RTCP channels are set, we can call SetInterface on
213 // media channel and it can set network options.
214 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 return true;
216}
217
wu@webrtc.org78187522013-10-07 23:32:02 +0000218void BaseChannel::Deinit() {
219 media_channel_->SetInterface(NULL);
220}
221
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000222bool BaseChannel::SetTransportChannels(BaseSession* session, bool rtcp) {
223 return worker_thread_->Invoke<bool>(Bind(
224 &BaseChannel::SetTransportChannels_w, this, session, rtcp));
225}
226
227bool BaseChannel::SetTransportChannels_w(BaseSession* session, bool rtcp) {
228 ASSERT(worker_thread_ == rtc::Thread::Current());
229
230 set_transport_channel(session->CreateChannel(
231 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTP));
232 if (!transport_channel()) {
233 return false;
234 }
235 if (rtcp) {
236 set_rtcp_transport_channel(session->CreateChannel(
237 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTCP));
238 if (!rtcp_transport_channel()) {
239 return false;
240 }
241 } else {
242 set_rtcp_transport_channel(nullptr);
243 }
244
245 return true;
246}
247
248void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
249 ASSERT(worker_thread_ == rtc::Thread::Current());
250
251 TransportChannel* old_tc = transport_channel_;
252
253 if (old_tc == new_tc) {
254 return;
255 }
256 if (old_tc) {
257 DisconnectFromTransportChannel(old_tc);
258 session()->DestroyChannel(
259 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTP);
260 }
261
262 transport_channel_ = new_tc;
263
264 if (new_tc) {
265 ConnectToTransportChannel(new_tc);
266 }
267}
268
269void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
270 ASSERT(worker_thread_ == rtc::Thread::Current());
271
272 TransportChannel* old_tc = rtcp_transport_channel_;
273
274 if (old_tc == new_tc) {
275 return;
276 }
277 if (old_tc) {
278 DisconnectFromTransportChannel(old_tc);
279 session()->DestroyChannel(
280 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTCP);
281 }
282
283 rtcp_transport_channel_ = new_tc;
284
285 if (new_tc) {
286 ConnectToTransportChannel(new_tc);
287 }
288}
289
290void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
291 ASSERT(worker_thread_ == rtc::Thread::Current());
292
293 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
294 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
295 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
296}
297
298void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
299 ASSERT(worker_thread_ == rtc::Thread::Current());
300
301 tc->SignalWritableState.disconnect(this);
302 tc->SignalReadPacket.disconnect(this);
303 tc->SignalReadyToSend.disconnect(this);
304}
305
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000307 worker_thread_->Invoke<void>(Bind(
308 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
309 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000310 return true;
311}
312
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313bool BaseChannel::MuteStream(uint32 ssrc, bool mute) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000314 return InvokeOnWorker(Bind(&BaseChannel::MuteStream_w, this, ssrc, mute));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000315}
316
317bool BaseChannel::IsStreamMuted(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000318 return InvokeOnWorker(Bind(&BaseChannel::IsStreamMuted_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319}
320
321bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000322 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323}
324
325bool BaseChannel::RemoveRecvStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000326 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327}
328
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000329bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000330 return InvokeOnWorker(
331 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000332}
333
334bool BaseChannel::RemoveSendStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000335 return InvokeOnWorker(
336 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000337}
338
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000339bool BaseChannel::SetLocalContent(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::SetLocalContent_w,
343 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000344}
345
346bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000347 ContentAction action,
348 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000349 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
350 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351}
352
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000354 // We pass in the BaseChannel instead of the transport_channel_
355 // because if the transport_channel_ changes, the ConnectionMonitor
356 // would be pointing to the wrong TransportChannel.
357 connection_monitor_.reset(new ConnectionMonitor(
358 this, worker_thread(), rtc::Thread::Current()));
359 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000361 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362}
363
364void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000365 if (connection_monitor_) {
366 connection_monitor_->Stop();
367 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 }
369}
370
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000371bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
372 ASSERT(worker_thread_ == rtc::Thread::Current());
373 return transport_channel_->GetStats(infos);
374}
375
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376bool BaseChannel::IsReadyToReceive() const {
377 // Receive data if we are enabled and have local content,
378 return enabled() && IsReceiveContentDirection(local_content_direction_);
379}
380
381bool BaseChannel::IsReadyToSend() const {
382 // Send outgoing data if we are enabled, have local and remote content,
383 // and we have had some form of connectivity.
384 return enabled() &&
385 IsReceiveContentDirection(remote_content_direction_) &&
386 IsSendContentDirection(local_content_direction_) &&
387 was_ever_writable();
388}
389
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000390bool BaseChannel::SendPacket(rtc::Buffer* packet,
391 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000392 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393}
394
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000395bool BaseChannel::SendRtcp(rtc::Buffer* packet,
396 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000397 return SendPacket(true, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398}
399
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000400int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000402 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000404 case ST_RTP:
405 channel = transport_channel_;
406 break;
407 case ST_RTCP:
408 channel = rtcp_transport_channel_;
409 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000411 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000412}
413
414void BaseChannel::OnWritableState(TransportChannel* channel) {
415 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
416 if (transport_channel_->writable()
417 && (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
418 ChannelWritable_w();
419 } else {
420 ChannelNotWritable_w();
421 }
422}
423
424void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000425 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000426 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000427 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000429 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430
431 // When using RTCP multiplexing we might get RTCP packets on the RTP
432 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
433 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000434 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000435 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436}
437
438void BaseChannel::OnReadyToSend(TransportChannel* channel) {
439 SetReadyToSend(channel, true);
440}
441
442void BaseChannel::SetReadyToSend(TransportChannel* channel, bool ready) {
443 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
444 if (channel == transport_channel_) {
445 rtp_ready_to_send_ = ready;
446 }
447 if (channel == rtcp_transport_channel_) {
448 rtcp_ready_to_send_ = ready;
449 }
450
451 if (!ready) {
452 // Notify the MediaChannel when either rtp or rtcp channel can't send.
453 media_channel_->OnReadyToSend(false);
454 } else if (rtp_ready_to_send_ &&
455 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
456 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
457 // Notify the MediaChannel when both rtp and rtcp channel can send.
458 media_channel_->OnReadyToSend(true);
459 }
460}
461
462bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
463 const char* data, size_t len) {
464 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000465 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466}
467
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000468bool BaseChannel::SendPacket(bool rtcp, rtc::Buffer* packet,
469 rtc::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 // SendPacket gets called from MediaEngine, typically on an encoder thread.
471 // If the thread is not our worker thread, we will post to our worker
472 // so that the real work happens on our worker. This avoids us having to
473 // synchronize access to all the pieces of the send path, including
474 // SRTP and the inner workings of the transport channels.
475 // The only downside is that we can't return a proper failure code if
476 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000477 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 // Avoid a copy by transferring the ownership of the packet data.
479 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
480 PacketMessageData* data = new PacketMessageData;
481 packet->TransferTo(&data->packet);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000482 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 worker_thread_->Post(this, message_id, data);
484 return true;
485 }
486
487 // Now that we are on the correct thread, ensure we have a place to send this
488 // packet before doing anything. (We might get RTCP packets that we don't
489 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
490 // transport.
491 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
492 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000493 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 return false;
495 }
496
497 // Protect ourselves against crazy data.
498 if (!ValidPacket(rtcp, packet)) {
499 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
500 << PacketType(rtcp) << " packet: wrong size="
501 << packet->length();
502 return false;
503 }
504
505 // Signal to the media sink before protecting the packet.
506 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000507 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508 SignalSendPacketPreCrypto(packet->data(), packet->length(), rtcp);
509 }
510
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000511 rtc::PacketOptions options(dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 // Protect if needed.
513 if (srtp_filter_.IsActive()) {
514 bool res;
515 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000516 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000518 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
519 // inside libsrtp for a RTP packet. A external HMAC module will be writing
520 // a fake HMAC value. This is ONLY done for a RTP packet.
521 // Socket layer will update rtp sendtime extension header if present in
522 // packet with current time before updating the HMAC.
523#if !defined(ENABLE_EXTERNAL_AUTH)
524 res = srtp_filter_.ProtectRtp(
525 data, len, static_cast<int>(packet->capacity()), &len);
526#else
henrike@webrtc.org05376342014-03-10 15:53:12 +0000527 options.packet_time_params.rtp_sendtime_extension_id =
528 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000529 res = srtp_filter_.ProtectRtp(
530 data, len, static_cast<int>(packet->capacity()), &len,
531 &options.packet_time_params.srtp_packet_index);
532 // If protection succeeds, let's get auth params from srtp.
533 if (res) {
534 uint8* auth_key = NULL;
535 int key_len;
536 res = srtp_filter_.GetRtpAuthParams(
537 &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
538 if (res) {
539 options.packet_time_params.srtp_auth_key.resize(key_len);
540 options.packet_time_params.srtp_auth_key.assign(auth_key,
541 auth_key + key_len);
542 }
543 }
544#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000545 if (!res) {
546 int seq_num = -1;
547 uint32 ssrc = 0;
548 GetRtpSeqNum(data, len, &seq_num);
549 GetRtpSsrc(data, len, &ssrc);
550 LOG(LS_ERROR) << "Failed to protect " << content_name_
551 << " RTP packet: size=" << len
552 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
553 return false;
554 }
555 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000556 res = srtp_filter_.ProtectRtcp(data, len,
557 static_cast<int>(packet->capacity()),
558 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 if (!res) {
560 int type = -1;
561 GetRtcpType(data, len, &type);
562 LOG(LS_ERROR) << "Failed to protect " << content_name_
563 << " RTCP packet: size=" << len << ", type=" << type;
564 return false;
565 }
566 }
567
568 // Update the length of the packet now that we've added the auth tag.
569 packet->SetLength(len);
570 } else if (secure_required_) {
571 // This is a double check for something that supposedly can't happen.
572 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
573 << " packet when SRTP is inactive and crypto is required";
574
575 ASSERT(false);
576 return false;
577 }
578
579 // Signal to the media sink after protecting the packet.
580 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000581 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 SignalSendPacketPostCrypto(packet->data(), packet->length(), rtcp);
583 }
584
585 // Bon voyage.
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000586 int ret = channel->SendPacket(packet->data(), packet->length(), options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000587 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
588 if (ret != static_cast<int>(packet->length())) {
589 if (channel->GetError() == EWOULDBLOCK) {
590 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
591 SetReadyToSend(channel, false);
592 }
593 return false;
594 }
595 return true;
596}
597
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000598bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599 // Protect ourselves against crazy data.
600 if (!ValidPacket(rtcp, packet)) {
601 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
602 << PacketType(rtcp) << " packet: wrong size="
603 << packet->length();
604 return false;
605 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000607 // Bundle filter handles both rtp and rtcp packets.
608 return bundle_filter_.DemuxPacket(packet->data(), packet->length(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609}
610
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000611void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
612 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 if (!WantsPacket(rtcp, packet)) {
614 return;
615 }
616
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000617 // We are only interested in the first rtp packet because that
618 // indicates the media has started flowing.
619 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 has_received_packet_ = true;
621 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
622 }
623
624 // Signal to the media sink before unprotecting the packet.
625 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000626 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 SignalRecvPacketPostCrypto(packet->data(), packet->length(), rtcp);
628 }
629
630 // Unprotect the packet, if needed.
631 if (srtp_filter_.IsActive()) {
632 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000633 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 bool res;
635 if (!rtcp) {
636 res = srtp_filter_.UnprotectRtp(data, len, &len);
637 if (!res) {
638 int seq_num = -1;
639 uint32 ssrc = 0;
640 GetRtpSeqNum(data, len, &seq_num);
641 GetRtpSsrc(data, len, &ssrc);
642 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
643 << " RTP packet: size=" << len
644 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
645 return;
646 }
647 } else {
648 res = srtp_filter_.UnprotectRtcp(data, len, &len);
649 if (!res) {
650 int type = -1;
651 GetRtcpType(data, len, &type);
652 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
653 << " RTCP packet: size=" << len << ", type=" << type;
654 return;
655 }
656 }
657
658 packet->SetLength(len);
659 } else if (secure_required_) {
660 // Our session description indicates that SRTP is required, but we got a
661 // packet before our SRTP filter is active. This means either that
662 // a) we got SRTP packets before we received the SDES keys, in which case
663 // we can't decrypt it anyway, or
664 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
665 // channels, so we haven't yet extracted keys, even if DTLS did complete
666 // on the channel that the packets are being sent on. It's really good
667 // practice to wait for both RTP and RTCP to be good to go before sending
668 // media, to prevent weird failure modes, so it's fine for us to just eat
669 // packets here. This is all sidestepped if RTCP mux is used anyway.
670 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
671 << " packet when SRTP is inactive and crypto is required";
672 return;
673 }
674
675 // Signal to the media sink after unprotecting the packet.
676 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000677 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 SignalRecvPacketPreCrypto(packet->data(), packet->length(), rtcp);
679 }
680
681 // Push it down to the media channel.
682 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000683 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000685 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 }
687}
688
689void BaseChannel::OnNewLocalDescription(
690 BaseSession* session, ContentAction action) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000691 std::string error_desc;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000692 if (!PushdownLocalDescription(
693 session->local_description(), action, &error_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000694 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 }
696}
697
698void BaseChannel::OnNewRemoteDescription(
699 BaseSession* session, ContentAction action) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000700 std::string error_desc;
701 if (!PushdownRemoteDescription(
702 session->remote_description(), action, &error_desc)) {
703 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
704 }
705}
706
707bool BaseChannel::PushdownLocalDescription(
708 const SessionDescription* local_desc, ContentAction action,
709 std::string* error_desc) {
710 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 const MediaContentDescription* content_desc =
712 GetContentDescription(content_info);
713 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000714 !SetLocalContent(content_desc, action, error_desc)) {
715 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
716 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000718 return true;
719}
720
721bool BaseChannel::PushdownRemoteDescription(
722 const SessionDescription* remote_desc, ContentAction action,
723 std::string* error_desc) {
724 const ContentInfo* content_info = GetFirstContent(remote_desc);
725 const MediaContentDescription* content_desc =
726 GetContentDescription(content_info);
727 if (content_desc && content_info && !content_info->rejected &&
728 !SetRemoteContent(content_desc, action, error_desc)) {
729 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
730 return false;
731 }
732 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733}
734
735void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000736 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 if (enabled_)
738 return;
739
740 LOG(LS_INFO) << "Channel enabled";
741 enabled_ = true;
742 ChangeState();
743}
744
745void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000746 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 if (!enabled_)
748 return;
749
750 LOG(LS_INFO) << "Channel disabled";
751 enabled_ = false;
752 ChangeState();
753}
754
755bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000756 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 bool ret = media_channel()->MuteStream(ssrc, mute);
758 if (ret) {
759 if (mute)
760 muted_streams_.insert(ssrc);
761 else
762 muted_streams_.erase(ssrc);
763 }
764 return ret;
765}
766
767bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000768 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 return muted_streams_.find(ssrc) != muted_streams_.end();
770}
771
772void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000773 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 if (writable_)
775 return;
776
777 LOG(LS_INFO) << "Channel socket writable ("
778 << transport_channel_->content_name() << ", "
779 << transport_channel_->component() << ")"
780 << (was_ever_writable_ ? "" : " for the first time");
781
782 std::vector<ConnectionInfo> infos;
783 transport_channel_->GetStats(&infos);
784 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
785 it != infos.end(); ++it) {
786 if (it->best_connection) {
787 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
788 << "->" << it->remote_candidate.ToSensitiveString();
789 break;
790 }
791 }
792
793 // If we're doing DTLS-SRTP, now is the time.
794 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
795 if (!SetupDtlsSrtp(false)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000796 SignalDtlsSetupFailure(this, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 return;
798 }
799
800 if (rtcp_transport_channel_) {
801 if (!SetupDtlsSrtp(true)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000802 SignalDtlsSetupFailure(this, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000803 return;
804 }
805 }
806 }
807
808 was_ever_writable_ = true;
809 writable_ = true;
810 ChangeState();
811}
812
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000813void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
814 ASSERT(worker_thread() == rtc::Thread::Current());
815 signaling_thread()->Invoke<void>(Bind(
816 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
817}
818
819void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
820 ASSERT(signaling_thread() == rtc::Thread::Current());
821 SignalDtlsSetupFailure(this, rtcp);
822}
823
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
825 std::vector<std::string> ciphers;
826 // We always use the default SRTP ciphers for RTCP, but we may use different
827 // ciphers for RTP depending on the media type.
828 if (!rtcp) {
829 GetSrtpCiphers(&ciphers);
830 } else {
831 GetSupportedDefaultCryptoSuites(&ciphers);
832 }
833 return tc->SetSrtpCiphers(ciphers);
834}
835
836bool BaseChannel::ShouldSetupDtlsSrtp() const {
837 return true;
838}
839
840// This function returns true if either DTLS-SRTP is not in use
841// *or* DTLS-SRTP is successfully set up.
842bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
843 bool ret = false;
844
845 TransportChannel *channel = rtcp_channel ?
846 rtcp_transport_channel_ : transport_channel_;
847
848 // No DTLS
849 if (!channel->IsDtlsActive())
850 return true;
851
852 std::string selected_cipher;
853
854 if (!channel->GetSrtpCipher(&selected_cipher)) {
855 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
856 return false;
857 }
858
859 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
860 << content_name() << " "
861 << PacketType(rtcp_channel);
862
863 // OK, we're now doing DTLS (RFC 5764)
864 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
865 SRTP_MASTER_KEY_SALT_LEN * 2);
866
867 // RFC 5705 exporter using the RFC 5764 parameters
868 if (!channel->ExportKeyingMaterial(
869 kDtlsSrtpExporterLabel,
870 NULL, 0, false,
871 &dtls_buffer[0], dtls_buffer.size())) {
872 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
873 ASSERT(false); // This should never happen
874 return false;
875 }
876
877 // Sync up the keys with the DTLS-SRTP interface
878 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
879 SRTP_MASTER_KEY_SALT_LEN);
880 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
881 SRTP_MASTER_KEY_SALT_LEN);
882 size_t offset = 0;
883 memcpy(&client_write_key[0], &dtls_buffer[offset],
884 SRTP_MASTER_KEY_KEY_LEN);
885 offset += SRTP_MASTER_KEY_KEY_LEN;
886 memcpy(&server_write_key[0], &dtls_buffer[offset],
887 SRTP_MASTER_KEY_KEY_LEN);
888 offset += SRTP_MASTER_KEY_KEY_LEN;
889 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
890 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
891 offset += SRTP_MASTER_KEY_SALT_LEN;
892 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
893 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
894
895 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000896 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000897 if (!channel->GetSslRole(&role)) {
898 LOG(LS_WARNING) << "GetSslRole failed";
899 return false;
900 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000902 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 send_key = &server_write_key;
904 recv_key = &client_write_key;
905 } else {
906 send_key = &client_write_key;
907 recv_key = &server_write_key;
908 }
909
910 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000911 ret = srtp_filter_.SetRtcpParams(
912 selected_cipher,
913 &(*send_key)[0],
914 static_cast<int>(send_key->size()),
915 selected_cipher,
916 &(*recv_key)[0],
917 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000919 ret = srtp_filter_.SetRtpParams(
920 selected_cipher,
921 &(*send_key)[0],
922 static_cast<int>(send_key->size()),
923 selected_cipher,
924 &(*recv_key)[0],
925 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 }
927
928 if (!ret)
929 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
930 else
931 dtls_keyed_ = true;
932
933 return ret;
934}
935
936void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000937 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 if (!writable_)
939 return;
940
941 LOG(LS_INFO) << "Channel socket not writable ("
942 << transport_channel_->content_name() << ", "
943 << transport_channel_->component() << ")";
944 writable_ = false;
945 ChangeState();
946}
947
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000948// |dtls| will be set to true if DTLS is active for transport channel and
949// crypto is empty.
950bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000951 bool* dtls,
952 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000953 *dtls = transport_channel_->IsDtlsActive();
954 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000955 SafeSetError("Cryptos must be empty when DTLS is active.",
956 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000957 return false;
958 }
959 return true;
960}
961
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000962bool BaseChannel::SetRecvRtpHeaderExtensions_w(
963 const MediaContentDescription* content,
964 MediaChannel* media_channel,
965 std::string* error_desc) {
966 if (content->rtp_header_extensions_set()) {
967 if (!media_channel->SetRecvRtpHeaderExtensions(
968 content->rtp_header_extensions())) {
969 std::ostringstream desc;
970 desc << "Failed to set receive rtp header extensions for "
971 << MediaTypeToString(content->type()) << " content.";
972 SafeSetError(desc.str(), error_desc);
973 return false;
974 }
975 }
976 return true;
977}
978
979bool BaseChannel::SetSendRtpHeaderExtensions_w(
980 const MediaContentDescription* content,
981 MediaChannel* media_channel,
982 std::string* error_desc) {
983 if (content->rtp_header_extensions_set()) {
984 if (!media_channel->SetSendRtpHeaderExtensions(
985 content->rtp_header_extensions())) {
986 std::ostringstream desc;
987 desc << "Failed to set send rtp header extensions for "
988 << MediaTypeToString(content->type()) << " content.";
989 SafeSetError(desc.str(), error_desc);
990 return false;
991 } else {
992 MaybeCacheRtpAbsSendTimeHeaderExtension(content->rtp_header_extensions());
993 }
994 }
995 return true;
996}
997
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000999 ContentAction action,
1000 ContentSource src,
1001 std::string* error_desc) {
1002 if (action == CA_UPDATE) {
1003 // no crypto params.
1004 return true;
1005 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001007 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001008 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
1009 if (!ret) {
1010 return false;
1011 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 switch (action) {
1013 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001014 // If DTLS is already active on the channel, we could be renegotiating
1015 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001016 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001017 ret = srtp_filter_.SetOffer(cryptos, src);
1018 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 break;
1020 case CA_PRANSWER:
1021 // If we're doing DTLS-SRTP, we don't want to update the filter
1022 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001023 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1025 }
1026 break;
1027 case CA_ANSWER:
1028 // If we're doing DTLS-SRTP, we don't want to update the filter
1029 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001030 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 ret = srtp_filter_.SetAnswer(cryptos, src);
1032 }
1033 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 default:
1035 break;
1036 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001037 if (!ret) {
1038 SafeSetError("Failed to setup SRTP filter.", error_desc);
1039 return false;
1040 }
1041 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042}
1043
1044bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001045 ContentSource src,
1046 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 bool ret = false;
1048 switch (action) {
1049 case CA_OFFER:
1050 ret = rtcp_mux_filter_.SetOffer(enable, src);
1051 break;
1052 case CA_PRANSWER:
1053 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1054 break;
1055 case CA_ANSWER:
1056 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1057 if (ret && rtcp_mux_filter_.IsActive()) {
1058 // We activated RTCP mux, close down the RTCP transport.
1059 set_rtcp_transport_channel(NULL);
1060 }
1061 break;
1062 case CA_UPDATE:
1063 // No RTCP mux info.
1064 ret = true;
1065 default:
1066 break;
1067 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001068 if (!ret) {
1069 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1070 return false;
1071 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1073 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1074 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001075 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 // If the RTP transport is already writable, then so are we.
1077 if (transport_channel_->writable()) {
1078 ChannelWritable_w();
1079 }
1080 }
1081
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001082 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083}
1084
1085bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001086 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087 if (!media_channel()->AddRecvStream(sp))
1088 return false;
1089
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001090 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091}
1092
1093bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001094 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001095 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 return media_channel()->RemoveRecvStream(ssrc);
1097}
1098
1099bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001100 ContentAction action,
1101 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001102 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1103 action == CA_PRANSWER || action == CA_UPDATE))
1104 return false;
1105
1106 // If this is an update, streams only contain streams that have changed.
1107 if (action == CA_UPDATE) {
1108 for (StreamParamsVec::const_iterator it = streams.begin();
1109 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001110 const StreamParams* existing_stream =
1111 GetStreamByIds(local_streams_, it->groupid, it->id);
1112 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 if (media_channel()->AddSendStream(*it)) {
1114 local_streams_.push_back(*it);
1115 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1116 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001117 std::ostringstream desc;
1118 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1119 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120 return false;
1121 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001122 } else if (existing_stream && !it->has_ssrcs()) {
1123 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001124 std::ostringstream desc;
1125 desc << "Failed to remove send stream with ssrc "
1126 << it->first_ssrc() << ".";
1127 SafeSetError(desc.str(), error_desc);
1128 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001130 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 } else {
1132 LOG(LS_WARNING) << "Ignore unsupported stream update";
1133 }
1134 }
1135 return true;
1136 }
1137 // Else streams are all the streams we want to send.
1138
1139 // Check for streams that have been removed.
1140 bool ret = true;
1141 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1142 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001143 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001144 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001145 std::ostringstream desc;
1146 desc << "Failed to remove send stream with ssrc "
1147 << it->first_ssrc() << ".";
1148 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149 ret = false;
1150 }
1151 }
1152 }
1153 // Check for new streams.
1154 for (StreamParamsVec::const_iterator it = streams.begin();
1155 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001156 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 if (media_channel()->AddSendStream(*it)) {
1158 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1159 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001160 std::ostringstream desc;
1161 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1162 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 ret = false;
1164 }
1165 }
1166 }
1167 local_streams_ = streams;
1168 return ret;
1169}
1170
1171bool BaseChannel::UpdateRemoteStreams_w(
1172 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001173 ContentAction action,
1174 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1176 action == CA_PRANSWER || action == CA_UPDATE))
1177 return false;
1178
1179 // If this is an update, streams only contain streams that have changed.
1180 if (action == CA_UPDATE) {
1181 for (StreamParamsVec::const_iterator it = streams.begin();
1182 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001183 const StreamParams* existing_stream =
1184 GetStreamByIds(remote_streams_, it->groupid, it->id);
1185 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 if (AddRecvStream_w(*it)) {
1187 remote_streams_.push_back(*it);
1188 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1189 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001190 std::ostringstream desc;
1191 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1192 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 return false;
1194 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001195 } else if (existing_stream && !it->has_ssrcs()) {
1196 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001197 std::ostringstream desc;
1198 desc << "Failed to remove remote stream with ssrc "
1199 << it->first_ssrc() << ".";
1200 SafeSetError(desc.str(), error_desc);
1201 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001203 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 } else {
1205 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001206 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 << " new stream = " << it->ToString();
1208 }
1209 }
1210 return true;
1211 }
1212 // Else streams are all the streams we want to receive.
1213
1214 // Check for streams that have been removed.
1215 bool ret = true;
1216 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1217 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001218 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001220 std::ostringstream desc;
1221 desc << "Failed to remove remote stream with ssrc "
1222 << it->first_ssrc() << ".";
1223 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 ret = false;
1225 }
1226 }
1227 }
1228 // Check for new streams.
1229 for (StreamParamsVec::const_iterator it = streams.begin();
1230 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001231 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 if (AddRecvStream_w(*it)) {
1233 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1234 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001235 std::ostringstream desc;
1236 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1237 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238 ret = false;
1239 }
1240 }
1241 }
1242 remote_streams_ = streams;
1243 return ret;
1244}
1245
1246bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001247 ContentAction action,
1248 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 // Cache secure_required_ for belt and suspenders check on SendPacket
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001250 secure_required_ = content->crypto_required() != CT_NONE;
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001251 // Set local RTP header extensions.
1252 bool ret = SetRecvRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001254 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001256 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001257
1258 // Call UpdateLocalStreams_w last to make sure as many settings as possible
1259 // are already set when creating streams.
1260 ret &= UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001261 set_local_content_direction(content->direction());
1262 return ret;
1263}
1264
1265bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001266 ContentAction action,
1267 std::string* error_desc) {
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001268 // Set remote RTP header extensions.
1269 bool ret = SetSendRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001270 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001271 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001273 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001274 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1275 std::ostringstream desc;
1276 desc << "Failed to set max send bandwidth for "
1277 << MediaTypeToString(content->type()) << " content.";
1278 SafeSetError(desc.str(), error_desc);
1279 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280 }
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001281
1282 // Call UpdateRemoteStreams_w last to make sure as many settings as possible
1283 // are already set when creating streams.
1284 ret &= UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285 set_remote_content_direction(content->direction());
1286 return ret;
1287}
1288
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001289void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1290 const std::vector<RtpHeaderExtension>& extensions) {
1291 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001292 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001293 rtp_abs_sendtime_extn_id_ =
1294 send_time_extension ? send_time_extension->id : -1;
1295}
1296
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001297void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 case MSG_RTPPACKET:
1300 case MSG_RTCPPACKET: {
1301 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001302 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 delete data; // because it is Posted
1304 break;
1305 }
1306 case MSG_FIRSTPACKETRECEIVED: {
1307 SignalFirstPacketReceived(this);
1308 break;
1309 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 }
1311}
1312
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313void BaseChannel::FlushRtcpMessages() {
1314 // Flush all remaining RTCP messages. This should only be called in
1315 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001316 ASSERT(rtc::Thread::Current() == worker_thread_);
1317 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001318 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001319 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001321 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 }
1323}
1324
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001325VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326 MediaEngineInterface* media_engine,
1327 VoiceMediaChannel* media_channel,
1328 BaseSession* session,
1329 const std::string& content_name,
1330 bool rtcp)
1331 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1332 rtcp),
1333 received_media_(false) {
1334}
1335
1336VoiceChannel::~VoiceChannel() {
1337 StopAudioMonitor();
1338 StopMediaMonitor();
1339 // this can't be done in the base class, since it calls a virtual
1340 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001341 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342}
1343
1344bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001345 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 return false;
1347 }
1348 media_channel()->SignalMediaError.connect(
1349 this, &VoiceChannel::OnVoiceChannelError);
1350 srtp_filter()->SignalSrtpError.connect(
1351 this, &VoiceChannel::OnSrtpError);
1352 return true;
1353}
1354
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001355bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001356 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1357 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001358}
1359
1360bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001361 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1362 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363}
1364
1365bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001366 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367}
1368
1369// TODO(juberti): Handle early media the right way. We should get an explicit
1370// ringing message telling us to start playing local ringback, which we cancel
1371// if any early media actually arrives. For now, we do the opposite, which is
1372// to wait 1 second for early media, and start playing local ringback if none
1373// arrives.
1374void VoiceChannel::SetEarlyMedia(bool enable) {
1375 if (enable) {
1376 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001377 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1378 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379 } else {
1380 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001381 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382 }
1383}
1384
1385bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001386 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1387 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388}
1389
1390bool VoiceChannel::PressDTMF(int digit, bool playout) {
1391 int flags = DF_SEND;
1392 if (playout) {
1393 flags |= DF_PLAY;
1394 }
1395 int duration_ms = 160;
1396 return InsertDtmf(0, digit, duration_ms, flags);
1397}
1398
1399bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001400 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1401 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402}
1403
1404bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1405 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001406 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1407 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408}
1409
1410bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001411 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1412 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001414
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001416 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1417 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418}
1419
1420void VoiceChannel::StartMediaMonitor(int cms) {
1421 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001422 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423 media_monitor_->SignalUpdate.connect(
1424 this, &VoiceChannel::OnMediaMonitorUpdate);
1425 media_monitor_->Start(cms);
1426}
1427
1428void VoiceChannel::StopMediaMonitor() {
1429 if (media_monitor_) {
1430 media_monitor_->Stop();
1431 media_monitor_->SignalUpdate.disconnect(this);
1432 media_monitor_.reset();
1433 }
1434}
1435
1436void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001437 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438 audio_monitor_
1439 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1440 audio_monitor_->Start(cms);
1441}
1442
1443void VoiceChannel::StopAudioMonitor() {
1444 if (audio_monitor_) {
1445 audio_monitor_->Stop();
1446 audio_monitor_.reset();
1447 }
1448}
1449
1450bool VoiceChannel::IsAudioMonitorRunning() const {
1451 return (audio_monitor_.get() != NULL);
1452}
1453
1454void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1455 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1456 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1457}
1458
1459void VoiceChannel::StopTypingMonitor() {
1460 typing_monitor_.reset();
1461}
1462
1463bool VoiceChannel::IsTypingMonitorRunning() const {
1464 return typing_monitor_;
1465}
1466
1467bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1468 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1469 if (typing_monitor_ && mute)
1470 typing_monitor_->OnChannelMuted();
1471 return ret;
1472}
1473
1474int VoiceChannel::GetInputLevel_w() {
1475 return media_engine()->GetInputLevel();
1476}
1477
1478int VoiceChannel::GetOutputLevel_w() {
1479 return media_channel()->GetOutputLevel();
1480}
1481
1482void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1483 media_channel()->GetActiveStreams(actives);
1484}
1485
1486void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001487 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001488 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001489 int flags) {
1490 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491
1492 // Set a flag when we've received an RTP packet. If we're waiting for early
1493 // media, this will disable the timeout.
1494 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1495 received_media_ = true;
1496 }
1497}
1498
1499void VoiceChannel::ChangeState() {
1500 // Render incoming data if we're the active call, and we have the local
1501 // content. We receive data on the default channel and multiplexed streams.
1502 bool recv = IsReadyToReceive();
1503 if (!media_channel()->SetPlayout(recv)) {
1504 SendLastMediaError();
1505 }
1506
1507 // Send outgoing data if we're the active call, we have the remote content,
1508 // and we have had some form of connectivity.
1509 bool send = IsReadyToSend();
1510 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1511 if (!media_channel()->SetSend(send_flag)) {
1512 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1513 SendLastMediaError();
1514 }
1515
1516 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1517}
1518
1519const ContentInfo* VoiceChannel::GetFirstContent(
1520 const SessionDescription* sdesc) {
1521 return GetFirstAudioContent(sdesc);
1522}
1523
1524bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001525 ContentAction action,
1526 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001527 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528 LOG(LS_INFO) << "Setting local voice description";
1529
1530 const AudioContentDescription* audio =
1531 static_cast<const AudioContentDescription*>(content);
1532 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001533 if (!audio) {
1534 SafeSetError("Can't find audio content in local description.", error_desc);
1535 return false;
1536 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001538 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539 // Set local audio codecs (what we want to receive).
1540 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1541 // is set properly.
1542 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001543 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1544 SafeSetError("Failed to set audio receive codecs.", error_desc);
1545 ret = false;
1546 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 }
1548
1549 // If everything worked, see if we can start receiving.
1550 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001551 std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1552 for (; it != audio->codecs().end(); ++it) {
1553 bundle_filter()->AddPayloadType(it->id);
1554 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 ChangeState();
1556 } else {
1557 LOG(LS_WARNING) << "Failed to set local voice description";
1558 }
1559 return ret;
1560}
1561
1562bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001563 ContentAction action,
1564 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001565 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 LOG(LS_INFO) << "Setting remote voice description";
1567
1568 const AudioContentDescription* audio =
1569 static_cast<const AudioContentDescription*>(content);
1570 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001571 if (!audio) {
1572 SafeSetError("Can't find audio content in remote description.", error_desc);
1573 return false;
1574 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575
1576 bool ret = true;
1577 // Set remote video codecs (what the other side wants to receive).
1578 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001579 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1580 SafeSetError("Failed to set audio send codecs.", error_desc);
1581 ret = false;
1582 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 }
1584
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001585 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586
1587 if (action != CA_UPDATE) {
1588 // Tweak our audio processing settings, if needed.
1589 AudioOptions audio_options;
1590 if (!media_channel()->GetOptions(&audio_options)) {
1591 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1592 } else {
1593 if (audio->conference_mode()) {
1594 audio_options.conference_mode.Set(true);
1595 }
1596 if (audio->agc_minus_10db()) {
1597 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1598 }
1599 if (!media_channel()->SetOptions(audio_options)) {
1600 // Log an error on failure, but don't abort the call.
1601 LOG(LS_ERROR) << "Failed to set voice channel options";
1602 }
1603 }
1604 }
1605
1606 // If everything worked, see if we can start sending.
1607 if (ret) {
1608 ChangeState();
1609 } else {
1610 LOG(LS_WARNING) << "Failed to set remote voice description";
1611 }
1612 return ret;
1613}
1614
1615bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001616 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1618}
1619
1620bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001621 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001622 if (play) {
1623 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1624 } else {
1625 LOG(LS_INFO) << "Stopping ringback tone";
1626 }
1627 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1628}
1629
1630void VoiceChannel::HandleEarlyMediaTimeout() {
1631 // This occurs on the main thread, not the worker thread.
1632 if (!received_media_) {
1633 LOG(LS_INFO) << "No early media received before timeout";
1634 SignalEarlyMediaTimeout(this);
1635 }
1636}
1637
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1639 int flags) {
1640 if (!enabled()) {
1641 return false;
1642 }
1643
1644 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1645}
1646
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001647bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001648 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1649 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650}
1651
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001652void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 case MSG_EARLYMEDIATIMEOUT:
1655 HandleEarlyMediaTimeout();
1656 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 case MSG_CHANNEL_ERROR: {
1658 VoiceChannelErrorMessageData* data =
1659 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1660 SignalMediaError(this, data->ssrc, data->error);
1661 delete data;
1662 break;
1663 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 default:
1665 BaseChannel::OnMessage(pmsg);
1666 break;
1667 }
1668}
1669
1670void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001671 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672 SignalConnectionMonitor(this, infos);
1673}
1674
1675void VoiceChannel::OnMediaMonitorUpdate(
1676 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1677 ASSERT(media_channel == this->media_channel());
1678 SignalMediaMonitor(this, info);
1679}
1680
1681void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1682 const AudioInfo& info) {
1683 SignalAudioMonitor(this, info);
1684}
1685
1686void VoiceChannel::OnVoiceChannelError(
1687 uint32 ssrc, VoiceMediaChannel::Error err) {
1688 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1689 ssrc, err);
1690 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1691}
1692
1693void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1694 SrtpFilter::Error error) {
1695 switch (error) {
1696 case SrtpFilter::ERROR_FAIL:
1697 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1698 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1699 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1700 break;
1701 case SrtpFilter::ERROR_AUTH:
1702 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1703 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1704 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1705 break;
1706 case SrtpFilter::ERROR_REPLAY:
1707 // Only receving channel should have this error.
1708 ASSERT(mode == SrtpFilter::UNPROTECT);
1709 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1710 break;
1711 default:
1712 break;
1713 }
1714}
1715
1716void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1717 GetSupportedAudioCryptoSuites(ciphers);
1718}
1719
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001720VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721 MediaEngineInterface* media_engine,
1722 VideoMediaChannel* media_channel,
1723 BaseSession* session,
1724 const std::string& content_name,
1725 bool rtcp,
1726 VoiceChannel* voice_channel)
1727 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1728 rtcp),
1729 voice_channel_(voice_channel),
1730 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001731 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732}
1733
1734bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001735 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 return false;
1737 }
1738 media_channel()->SignalMediaError.connect(
1739 this, &VideoChannel::OnVideoChannelError);
1740 srtp_filter()->SignalSrtpError.connect(
1741 this, &VideoChannel::OnSrtpError);
1742 return true;
1743}
1744
1745void VoiceChannel::SendLastMediaError() {
1746 uint32 ssrc;
1747 VoiceMediaChannel::Error error;
1748 media_channel()->GetLastMediaError(&ssrc, &error);
1749 SignalMediaError(this, ssrc, error);
1750}
1751
1752VideoChannel::~VideoChannel() {
1753 std::vector<uint32> screencast_ssrcs;
1754 ScreencastMap::iterator iter;
1755 while (!screencast_capturers_.empty()) {
1756 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1757 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1758 << screencast_capturers_.begin()->first;
1759 ASSERT(false);
1760 break;
1761 }
1762 }
1763
1764 StopMediaMonitor();
1765 // this can't be done in the base class, since it calls a virtual
1766 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001767
1768 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769}
1770
1771bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001772 worker_thread()->Invoke<void>(Bind(
1773 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 return true;
1775}
1776
1777bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001778 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779}
1780
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001781bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1782 return worker_thread()->Invoke<bool>(Bind(
1783 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784}
1785
1786bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001787 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1788 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789}
1790
1791bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001792 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793}
1794
1795bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001796 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797}
1798
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001799int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001800 ScreencastDetailsData data(ssrc);
1801 worker_thread()->Invoke<void>(Bind(
1802 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001803 return data.fps;
1804}
1805
1806int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001807 ScreencastDetailsData data(ssrc);
1808 worker_thread()->Invoke<void>(Bind(
1809 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001810 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811}
1812
1813bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001814 worker_thread()->Invoke<void>(Bind(
1815 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 return true;
1817}
1818
1819bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001820 worker_thread()->Invoke<void>(Bind(
1821 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 return true;
1823}
1824
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825void VideoChannel::ChangeState() {
1826 // Render incoming data if we're the active call, and we have the local
1827 // content. We receive data on the default channel and multiplexed streams.
1828 bool recv = IsReadyToReceive();
1829 if (!media_channel()->SetRender(recv)) {
1830 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1831 // TODO(gangji): Report error back to server.
1832 }
1833
1834 // Send outgoing data if we're the active call, we have the remote content,
1835 // and we have had some form of connectivity.
1836 bool send = IsReadyToSend();
1837 if (!media_channel()->SetSend(send)) {
1838 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1839 // TODO(gangji): Report error back to server.
1840 }
1841
1842 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1843}
1844
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001845bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1846 return InvokeOnWorker(
1847 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848}
1849
1850void VideoChannel::StartMediaMonitor(int cms) {
1851 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001852 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 media_monitor_->SignalUpdate.connect(
1854 this, &VideoChannel::OnMediaMonitorUpdate);
1855 media_monitor_->Start(cms);
1856}
1857
1858void VideoChannel::StopMediaMonitor() {
1859 if (media_monitor_) {
1860 media_monitor_->Stop();
1861 media_monitor_.reset();
1862 }
1863}
1864
1865const ContentInfo* VideoChannel::GetFirstContent(
1866 const SessionDescription* sdesc) {
1867 return GetFirstVideoContent(sdesc);
1868}
1869
1870bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001871 ContentAction action,
1872 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001873 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 LOG(LS_INFO) << "Setting local video description";
1875
1876 const VideoContentDescription* video =
1877 static_cast<const VideoContentDescription*>(content);
1878 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001879 if (!video) {
1880 SafeSetError("Can't find video content in local description.", error_desc);
1881 return false;
1882 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001884 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 // Set local video codecs (what we want to receive).
1886 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001887 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1888 SafeSetError("Failed to set video receive codecs.", error_desc);
1889 ret = false;
1890 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 }
1892
1893 if (action != CA_UPDATE) {
1894 VideoOptions video_options;
1895 media_channel()->GetOptions(&video_options);
1896 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1897
1898 if (!media_channel()->SetOptions(video_options)) {
1899 // Log an error on failure, but don't abort the call.
1900 LOG(LS_ERROR) << "Failed to set video channel options";
1901 }
1902 }
1903
1904 // If everything worked, see if we can start receiving.
1905 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001906 std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1907 for (; it != video->codecs().end(); ++it) {
1908 bundle_filter()->AddPayloadType(it->id);
1909 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 ChangeState();
1911 } else {
1912 LOG(LS_WARNING) << "Failed to set local video description";
1913 }
1914 return ret;
1915}
1916
1917bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001918 ContentAction action,
1919 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001920 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 LOG(LS_INFO) << "Setting remote video description";
1922
1923 const VideoContentDescription* video =
1924 static_cast<const VideoContentDescription*>(content);
1925 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001926 if (!video) {
1927 SafeSetError("Can't find video content in remote description.", error_desc);
1928 return false;
1929 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930
1931 bool ret = true;
1932 // Set remote video codecs (what the other side wants to receive).
1933 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001934 if (!media_channel()->SetSendCodecs(video->codecs())) {
1935 SafeSetError("Failed to set video send codecs.", error_desc);
1936 ret = false;
1937 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938 }
1939
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001940 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941
1942 if (action != CA_UPDATE) {
1943 // Tweak our video processing settings, if needed.
1944 VideoOptions video_options;
1945 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001946 if (video->conference_mode()) {
1947 video_options.conference_mode.Set(true);
1948 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1950
1951 if (!media_channel()->SetOptions(video_options)) {
1952 // Log an error on failure, but don't abort the call.
1953 LOG(LS_ERROR) << "Failed to set video channel options";
1954 }
1955 }
1956
1957 // If everything worked, see if we can start sending.
1958 if (ret) {
1959 ChangeState();
1960 } else {
1961 LOG(LS_WARNING) << "Failed to set remote video description";
1962 }
1963 return ret;
1964}
1965
1966bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1967 bool ret = true;
1968 // Set the send format for each of the local streams. If the view request
1969 // does not contain a local stream, set its send format to 0x0, which will
1970 // drop all frames.
1971 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1972 it != local_streams().end(); ++it) {
1973 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1974 StaticVideoViews::const_iterator view;
1975 for (view = request.static_video_views.begin();
1976 view != request.static_video_views.end(); ++view) {
1977 if (view->selector.Matches(*it)) {
1978 format.width = view->width;
1979 format.height = view->height;
1980 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1981 break;
1982 }
1983 }
1984
1985 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1986 }
1987
1988 // Check if the view request has invalid streams.
1989 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1990 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001991 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 LOG(LS_WARNING) << "View request for ("
1993 << it->selector.ssrc << ", '"
1994 << it->selector.groupid << "', '"
1995 << it->selector.streamid << "'"
1996 << ") is not in the local streams.";
1997 }
1998 }
1999
2000 return ret;
2001}
2002
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002003bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002005 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002007 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
2008 screencast_capturers_[ssrc] = capturer;
2009 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010}
2011
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
2013 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
2014 if (iter == screencast_capturers_.end()) {
2015 return false;
2016 }
2017 // Clean up VideoCapturer.
2018 delete iter->second;
2019 screencast_capturers_.erase(iter);
2020 return true;
2021}
2022
2023bool VideoChannel::IsScreencasting_w() const {
2024 return !screencast_capturers_.empty();
2025}
2026
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002027void VideoChannel::GetScreencastDetails_w(
2028 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002029 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002031 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 }
2033 VideoCapturer* capturer = iter->second;
2034 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002035 data->fps = VideoFormat::IntervalToFps(video_format->interval);
2036 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037}
2038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002040 rtc::WindowEvent we) {
2041 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 SignalScreencastWindowEvent(ssrc, we);
2043}
2044
2045bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002046 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
2047 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048}
2049
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002050void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 case MSG_SCREENCASTWINDOWEVENT: {
2053 const ScreencastEventMessageData* data =
2054 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
2055 OnScreencastWindowEvent_s(data->ssrc, data->event);
2056 delete data;
2057 break;
2058 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 case MSG_CHANNEL_ERROR: {
2060 const VideoChannelErrorMessageData* data =
2061 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
2062 SignalMediaError(this, data->ssrc, data->error);
2063 delete data;
2064 break;
2065 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 default:
2067 BaseChannel::OnMessage(pmsg);
2068 break;
2069 }
2070}
2071
2072void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002073 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 SignalConnectionMonitor(this, infos);
2075}
2076
2077// TODO(pthatcher): Look into removing duplicate code between
2078// audio, video, and data, perhaps by using templates.
2079void VideoChannel::OnMediaMonitorUpdate(
2080 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2081 ASSERT(media_channel == this->media_channel());
2082 SignalMediaMonitor(this, info);
2083}
2084
2085void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002086 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 ScreencastEventMessageData* pdata =
2088 new ScreencastEventMessageData(ssrc, event);
2089 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2090}
2091
2092void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2093 // Map capturer events to window events. In the future we may want to simply
2094 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002095 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002097 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002099 we = rtc::WE_MINIMIZE;
2100 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2101 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102 } else {
2103 return;
2104 }
2105 previous_we_ = we;
2106
2107 uint32 ssrc = 0;
2108 if (!GetLocalSsrc(capturer, &ssrc)) {
2109 return;
2110 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002111
2112 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113}
2114
2115bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2116 *ssrc = 0;
2117 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2118 iter != screencast_capturers_.end(); ++iter) {
2119 if (iter->second == capturer) {
2120 *ssrc = iter->first;
2121 return true;
2122 }
2123 }
2124 return false;
2125}
2126
2127void VideoChannel::OnVideoChannelError(uint32 ssrc,
2128 VideoMediaChannel::Error error) {
2129 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2130 ssrc, error);
2131 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2132}
2133
2134void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2135 SrtpFilter::Error error) {
2136 switch (error) {
2137 case SrtpFilter::ERROR_FAIL:
2138 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2139 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2140 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2141 break;
2142 case SrtpFilter::ERROR_AUTH:
2143 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2144 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2145 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2146 break;
2147 case SrtpFilter::ERROR_REPLAY:
2148 // Only receving channel should have this error.
2149 ASSERT(mode == SrtpFilter::UNPROTECT);
2150 // TODO(gangji): Turn on the signaling of replay error once we have
2151 // switched to the new mechanism for doing video retransmissions.
2152 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2153 break;
2154 default:
2155 break;
2156 }
2157}
2158
2159
2160void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2161 GetSupportedVideoCryptoSuites(ciphers);
2162}
2163
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002164DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165 DataMediaChannel* media_channel,
2166 BaseSession* session,
2167 const std::string& content_name,
2168 bool rtcp)
2169 // MediaEngine is NULL
2170 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002171 data_channel_type_(cricket::DCT_NONE),
2172 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173}
2174
2175DataChannel::~DataChannel() {
2176 StopMediaMonitor();
2177 // this can't be done in the base class, since it calls a virtual
2178 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002179
2180 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181}
2182
2183bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002184 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185 return false;
2186 }
2187 media_channel()->SignalDataReceived.connect(
2188 this, &DataChannel::OnDataReceived);
2189 media_channel()->SignalMediaError.connect(
2190 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002191 media_channel()->SignalReadyToSend.connect(
2192 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002193 media_channel()->SignalStreamClosedRemotely.connect(
2194 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 srtp_filter()->SignalSrtpError.connect(
2196 this, &DataChannel::OnSrtpError);
2197 return true;
2198}
2199
2200bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002201 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002203 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2204 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205}
2206
2207const ContentInfo* DataChannel::GetFirstContent(
2208 const SessionDescription* sdesc) {
2209 return GetFirstDataContent(sdesc);
2210}
2211
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002212bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 if (data_channel_type_ == DCT_SCTP) {
2214 // TODO(pthatcher): Do this in a more robust way by checking for
2215 // SCTP or DTLS.
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +00002216 return !IsRtpPacket(packet->data(), packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 } else if (data_channel_type_ == DCT_RTP) {
2218 return BaseChannel::WantsPacket(rtcp, packet);
2219 }
2220 return false;
2221}
2222
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002223bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2224 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 // It hasn't been set before, so set it now.
2226 if (data_channel_type_ == DCT_NONE) {
2227 data_channel_type_ = new_data_channel_type;
2228 return true;
2229 }
2230
2231 // It's been set before, but doesn't match. That's bad.
2232 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002233 std::ostringstream desc;
2234 desc << "Data channel type mismatch."
2235 << " Expected " << data_channel_type_
2236 << " Got " << new_data_channel_type;
2237 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002238 return false;
2239 }
2240
2241 // It's hasn't changed. Nothing to do.
2242 return true;
2243}
2244
2245bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002246 const DataContentDescription* content,
2247 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2249 (content->protocol() == kMediaProtocolDtlsSctp));
2250 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002251 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252}
2253
2254bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002255 ContentAction action,
2256 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002257 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002258 LOG(LS_INFO) << "Setting local data description";
2259
2260 const DataContentDescription* data =
2261 static_cast<const DataContentDescription*>(content);
2262 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002263 if (!data) {
2264 SafeSetError("Can't find data content in local description.", error_desc);
2265 return false;
2266 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267
2268 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002269 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270 return false;
2271 }
2272
2273 if (data_channel_type_ == DCT_SCTP) {
2274 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002275 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 if (ret) {
2277 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002278 // As in SetRemoteContent_w, make sure we set the local SCTP port
2279 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002280 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2281 SafeSetError("Failed to set data receive codecs.", error_desc);
2282 ret = false;
2283 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284 }
2285 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002286 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002288 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2289 SafeSetError("Failed to set data receive codecs.", error_desc);
2290 ret = false;
2291 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 }
2293 }
2294
2295 // If everything worked, see if we can start receiving.
2296 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00002297 std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2298 for (; it != data->codecs().end(); ++it) {
2299 bundle_filter()->AddPayloadType(it->id);
2300 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301 ChangeState();
2302 } else {
2303 LOG(LS_WARNING) << "Failed to set local data description";
2304 }
2305 return ret;
2306}
2307
2308bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002309 ContentAction action,
2310 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002311 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312
2313 const DataContentDescription* data =
2314 static_cast<const DataContentDescription*>(content);
2315 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002316 if (!data) {
2317 SafeSetError("Can't find data content in remote description.", error_desc);
2318 return false;
2319 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320
2321 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002322 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 return false;
2324 }
2325
2326 if (data_channel_type_ == DCT_SCTP) {
2327 LOG(LS_INFO) << "Setting SCTP remote data description";
2328 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002329 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330 if (ret) {
2331 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002332 // We send the SCTP port number (not to be confused with the underlying
2333 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002334 if (!media_channel()->SetSendCodecs(data->codecs())) {
2335 SafeSetError("Failed to set data send codecs.", error_desc);
2336 ret = false;
2337 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002338 }
2339 } else {
2340 // If the remote data doesn't have codecs and isn't an update, it
2341 // must be empty, so ignore it.
2342 if (action != CA_UPDATE && !data->has_codecs()) {
2343 return true;
2344 }
2345 LOG(LS_INFO) << "Setting remote data description";
2346
2347 // Set remote video codecs (what the other side wants to receive).
2348 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002349 if (!media_channel()->SetSendCodecs(data->codecs())) {
2350 SafeSetError("Failed to set data send codecs.", error_desc);
2351 ret = false;
2352 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353 }
2354
2355 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002356 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002357 }
2358
2359 if (action != CA_UPDATE) {
2360 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002361 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2362 std::ostringstream desc;
2363 desc << "Failed to set max send bandwidth for data content.";
2364 SafeSetError(desc.str(), error_desc);
2365 ret = false;
2366 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367 }
2368 }
2369
2370 // If everything worked, see if we can start sending.
2371 if (ret) {
2372 ChangeState();
2373 } else {
2374 LOG(LS_WARNING) << "Failed to set remote data description";
2375 }
2376 return ret;
2377}
2378
2379void DataChannel::ChangeState() {
2380 // Render incoming data if we're the active call, and we have the local
2381 // content. We receive data on the default channel and multiplexed streams.
2382 bool recv = IsReadyToReceive();
2383 if (!media_channel()->SetReceive(recv)) {
2384 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2385 }
2386
2387 // Send outgoing data if we're the active call, we have the remote content,
2388 // and we have had some form of connectivity.
2389 bool send = IsReadyToSend();
2390 if (!media_channel()->SetSend(send)) {
2391 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2392 }
2393
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002394 // Trigger SignalReadyToSendData asynchronously.
2395 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002396
2397 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2398}
2399
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002400void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002401 switch (pmsg->message_id) {
2402 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002403 DataChannelReadyToSendMessageData* data =
2404 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002405 ready_to_send_data_ = data->data();
2406 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407 delete data;
2408 break;
2409 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 case MSG_DATARECEIVED: {
2411 DataReceivedMessageData* data =
2412 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2413 SignalDataReceived(this, data->params, data->payload);
2414 delete data;
2415 break;
2416 }
2417 case MSG_CHANNEL_ERROR: {
2418 const DataChannelErrorMessageData* data =
2419 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2420 SignalMediaError(this, data->ssrc, data->error);
2421 delete data;
2422 break;
2423 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002424 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002425 rtc::TypedMessageData<uint32>* data =
2426 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002427 SignalStreamClosedRemotely(data->data());
2428 delete data;
2429 break;
2430 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002431 default:
2432 BaseChannel::OnMessage(pmsg);
2433 break;
2434 }
2435}
2436
2437void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002438 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002439 SignalConnectionMonitor(this, infos);
2440}
2441
2442void DataChannel::StartMediaMonitor(int cms) {
2443 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002444 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002445 media_monitor_->SignalUpdate.connect(
2446 this, &DataChannel::OnMediaMonitorUpdate);
2447 media_monitor_->Start(cms);
2448}
2449
2450void DataChannel::StopMediaMonitor() {
2451 if (media_monitor_) {
2452 media_monitor_->Stop();
2453 media_monitor_->SignalUpdate.disconnect(this);
2454 media_monitor_.reset();
2455 }
2456}
2457
2458void DataChannel::OnMediaMonitorUpdate(
2459 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2460 ASSERT(media_channel == this->media_channel());
2461 SignalMediaMonitor(this, info);
2462}
2463
2464void DataChannel::OnDataReceived(
2465 const ReceiveDataParams& params, const char* data, size_t len) {
2466 DataReceivedMessageData* msg = new DataReceivedMessageData(
2467 params, data, len);
2468 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2469}
2470
2471void DataChannel::OnDataChannelError(
2472 uint32 ssrc, DataMediaChannel::Error err) {
2473 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2474 ssrc, err);
2475 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2476}
2477
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002478void DataChannel::OnDataChannelReadyToSend(bool writable) {
2479 // This is usded for congestion control to indicate that the stream is ready
2480 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2481 // that the transport channel is ready.
2482 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2483 new DataChannelReadyToSendMessageData(writable));
2484}
2485
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002486void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2487 SrtpFilter::Error error) {
2488 switch (error) {
2489 case SrtpFilter::ERROR_FAIL:
2490 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2491 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2492 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2493 break;
2494 case SrtpFilter::ERROR_AUTH:
2495 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2496 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2497 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2498 break;
2499 case SrtpFilter::ERROR_REPLAY:
2500 // Only receving channel should have this error.
2501 ASSERT(mode == SrtpFilter::UNPROTECT);
2502 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2503 break;
2504 default:
2505 break;
2506 }
2507}
2508
2509void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2510 GetSupportedDataCryptoSuites(ciphers);
2511}
2512
2513bool DataChannel::ShouldSetupDtlsSrtp() const {
2514 return (data_channel_type_ == DCT_RTP);
2515}
2516
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002517void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002518 rtc::TypedMessageData<uint32>* message =
2519 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002520 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2521}
2522
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002523} // namespace cricket