blob: 95f5bc1e82da57f4ac269609b7bc724b5002e804 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/session/media/channel.h"
29
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000030#include "talk/media/base/constants.h"
31#include "talk/media/base/rtputils.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032#include "webrtc/p2p/base/transportchannel.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000033#include "talk/session/media/channelmanager.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000034#include "talk/session/media/typingmonitor.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000035#include "webrtc/base/bind.h"
36#include "webrtc/base/buffer.h"
37#include "webrtc/base/byteorder.h"
38#include "webrtc/base/common.h"
39#include "webrtc/base/dscp.h"
40#include "webrtc/base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42namespace cricket {
43
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000044using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000045
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000047 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048 MSG_SCREENCASTWINDOWEVENT,
49 MSG_RTPPACKET,
50 MSG_RTCPPACKET,
51 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000055 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056};
57
58// Value specified in RFC 5764.
59static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
60
61static const int kAgcMinus10db = -10;
62
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000063static void SetSessionError(BaseSession* session, BaseSession::Error error,
64 const std::string& error_desc) {
65 session->SetError(error, error_desc);
66}
67
68static void SafeSetError(const std::string& message, std::string* error_desc) {
69 if (error_desc) {
70 *error_desc = message;
71 }
72}
73
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074struct PacketMessageData : public rtc::MessageData {
75 rtc::Buffer packet;
76 rtc::DiffServCodePoint dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077};
78
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079struct ScreencastEventMessageData : public rtc::MessageData {
80 ScreencastEventMessageData(uint32 s, rtc::WindowEvent we)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 : ssrc(s),
82 event(we) {
83 }
84 uint32 ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000085 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086};
87
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088struct VoiceChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 VoiceChannelErrorMessageData(uint32 in_ssrc,
90 VoiceMediaChannel::Error in_error)
91 : ssrc(in_ssrc),
92 error(in_error) {
93 }
94 uint32 ssrc;
95 VoiceMediaChannel::Error error;
96};
97
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000098struct VideoChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 VideoChannelErrorMessageData(uint32 in_ssrc,
100 VideoMediaChannel::Error in_error)
101 : ssrc(in_ssrc),
102 error(in_error) {
103 }
104 uint32 ssrc;
105 VideoMediaChannel::Error error;
106};
107
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000108struct DataChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 DataChannelErrorMessageData(uint32 in_ssrc,
110 DataMediaChannel::Error in_error)
111 : ssrc(in_ssrc),
112 error(in_error) {}
113 uint32 ssrc;
114 DataMediaChannel::Error error;
115};
116
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000118struct VideoChannel::ScreencastDetailsData {
119 explicit ScreencastDetailsData(uint32 s)
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000120 : ssrc(s), fps(0), screencast_max_pixels(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 }
122 uint32 ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000123 int fps;
124 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125};
126
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127static const char* PacketType(bool rtcp) {
128 return (!rtcp) ? "RTP" : "RTCP";
129}
130
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000131static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 // Check the packet size. We could check the header too if needed.
133 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000134 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
135 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136}
137
138static bool IsReceiveContentDirection(MediaContentDirection direction) {
139 return direction == MD_SENDRECV || direction == MD_RECVONLY;
140}
141
142static bool IsSendContentDirection(MediaContentDirection direction) {
143 return direction == MD_SENDRECV || direction == MD_SENDONLY;
144}
145
146static const MediaContentDescription* GetContentDescription(
147 const ContentInfo* cinfo) {
148 if (cinfo == NULL)
149 return NULL;
150 return static_cast<const MediaContentDescription*>(cinfo->description);
151}
152
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000153BaseChannel::BaseChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 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;
Karl Wiberg94784372015-04-20 14:03:07 +0200481 data->packet = packet->Pass();
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_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000500 << PacketType(rtcp)
501 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 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_);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000508 SignalSendPacketPreCrypto(packet->data(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000509 }
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;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200515 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000516 int len = static_cast<int>(packet->size());
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.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000569 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000570 } 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_);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000582 SignalSendPacketPostCrypto(packet->data(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 }
584
585 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000586 int ret =
Karl Wiberg94784372015-04-20 14:03:07 +0200587 channel->SendPacket(packet->data<char>(), packet->size(), options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000588 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
589 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 if (channel->GetError() == EWOULDBLOCK) {
591 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
592 SetReadyToSend(channel, false);
593 }
594 return false;
595 }
596 return true;
597}
598
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000599bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 // Protect ourselves against crazy data.
601 if (!ValidPacket(rtcp, packet)) {
602 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000603 << PacketType(rtcp)
604 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 return false;
606 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000608 // Bundle filter handles both rtp and rtcp packets.
Karl Wiberg94784372015-04-20 14:03:07 +0200609 return bundle_filter_.DemuxPacket(packet->data<char>(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610}
611
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000612void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
613 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 if (!WantsPacket(rtcp, packet)) {
615 return;
616 }
617
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000618 // We are only interested in the first rtp packet because that
619 // indicates the media has started flowing.
620 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 has_received_packet_ = true;
622 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
623 }
624
625 // Signal to the media sink before unprotecting the packet.
626 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000627 rtc::CritScope cs(&signal_recv_packet_cs_);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000628 SignalRecvPacketPostCrypto(packet->data(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 }
630
631 // Unprotect the packet, if needed.
632 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200633 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000634 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 bool res;
636 if (!rtcp) {
637 res = srtp_filter_.UnprotectRtp(data, len, &len);
638 if (!res) {
639 int seq_num = -1;
640 uint32 ssrc = 0;
641 GetRtpSeqNum(data, len, &seq_num);
642 GetRtpSsrc(data, len, &ssrc);
643 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
644 << " RTP packet: size=" << len
645 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
646 return;
647 }
648 } else {
649 res = srtp_filter_.UnprotectRtcp(data, len, &len);
650 if (!res) {
651 int type = -1;
652 GetRtcpType(data, len, &type);
653 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
654 << " RTCP packet: size=" << len << ", type=" << type;
655 return;
656 }
657 }
658
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000659 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 } else if (secure_required_) {
661 // Our session description indicates that SRTP is required, but we got a
662 // packet before our SRTP filter is active. This means either that
663 // a) we got SRTP packets before we received the SDES keys, in which case
664 // we can't decrypt it anyway, or
665 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
666 // channels, so we haven't yet extracted keys, even if DTLS did complete
667 // on the channel that the packets are being sent on. It's really good
668 // practice to wait for both RTP and RTCP to be good to go before sending
669 // media, to prevent weird failure modes, so it's fine for us to just eat
670 // packets here. This is all sidestepped if RTCP mux is used anyway.
671 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
672 << " packet when SRTP is inactive and crypto is required";
673 return;
674 }
675
676 // Signal to the media sink after unprotecting the packet.
677 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000678 rtc::CritScope cs(&signal_recv_packet_cs_);
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000679 SignalRecvPacketPreCrypto(packet->data(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 }
681
682 // Push it down to the media channel.
683 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000684 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000686 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000687 }
688}
689
690void BaseChannel::OnNewLocalDescription(
691 BaseSession* session, ContentAction action) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000692 std::string error_desc;
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000693 if (!PushdownLocalDescription(
694 session->local_description(), action, &error_desc)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000695 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 }
697}
698
699void BaseChannel::OnNewRemoteDescription(
700 BaseSession* session, ContentAction action) {
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000701 std::string error_desc;
702 if (!PushdownRemoteDescription(
703 session->remote_description(), action, &error_desc)) {
704 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
705 }
706}
707
708bool BaseChannel::PushdownLocalDescription(
709 const SessionDescription* local_desc, ContentAction action,
710 std::string* error_desc) {
711 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 const MediaContentDescription* content_desc =
713 GetContentDescription(content_info);
714 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000715 !SetLocalContent(content_desc, action, error_desc)) {
716 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
717 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000719 return true;
720}
721
722bool BaseChannel::PushdownRemoteDescription(
723 const SessionDescription* remote_desc, ContentAction action,
724 std::string* error_desc) {
725 const ContentInfo* content_info = GetFirstContent(remote_desc);
726 const MediaContentDescription* content_desc =
727 GetContentDescription(content_info);
728 if (content_desc && content_info && !content_info->rejected &&
729 !SetRemoteContent(content_desc, action, error_desc)) {
730 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
731 return false;
732 }
733 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734}
735
736void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000737 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000738 if (enabled_)
739 return;
740
741 LOG(LS_INFO) << "Channel enabled";
742 enabled_ = true;
743 ChangeState();
744}
745
746void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000747 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 if (!enabled_)
749 return;
750
751 LOG(LS_INFO) << "Channel disabled";
752 enabled_ = false;
753 ChangeState();
754}
755
756bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000757 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000758 bool ret = media_channel()->MuteStream(ssrc, mute);
759 if (ret) {
760 if (mute)
761 muted_streams_.insert(ssrc);
762 else
763 muted_streams_.erase(ssrc);
764 }
765 return ret;
766}
767
768bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000769 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 return muted_streams_.find(ssrc) != muted_streams_.end();
771}
772
773void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000774 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 if (writable_)
776 return;
777
778 LOG(LS_INFO) << "Channel socket writable ("
779 << transport_channel_->content_name() << ", "
780 << transport_channel_->component() << ")"
781 << (was_ever_writable_ ? "" : " for the first time");
782
783 std::vector<ConnectionInfo> infos;
784 transport_channel_->GetStats(&infos);
785 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
786 it != infos.end(); ++it) {
787 if (it->best_connection) {
788 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
789 << "->" << it->remote_candidate.ToSensitiveString();
790 break;
791 }
792 }
793
794 // If we're doing DTLS-SRTP, now is the time.
795 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
796 if (!SetupDtlsSrtp(false)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000797 SignalDtlsSetupFailure(this, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000798 return;
799 }
800
801 if (rtcp_transport_channel_) {
802 if (!SetupDtlsSrtp(true)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000803 SignalDtlsSetupFailure(this, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000804 return;
805 }
806 }
807 }
808
809 was_ever_writable_ = true;
810 writable_ = true;
811 ChangeState();
812}
813
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000814void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
815 ASSERT(worker_thread() == rtc::Thread::Current());
816 signaling_thread()->Invoke<void>(Bind(
817 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
818}
819
820void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
821 ASSERT(signaling_thread() == rtc::Thread::Current());
822 SignalDtlsSetupFailure(this, rtcp);
823}
824
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000825bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
826 std::vector<std::string> ciphers;
827 // We always use the default SRTP ciphers for RTCP, but we may use different
828 // ciphers for RTP depending on the media type.
829 if (!rtcp) {
830 GetSrtpCiphers(&ciphers);
831 } else {
832 GetSupportedDefaultCryptoSuites(&ciphers);
833 }
834 return tc->SetSrtpCiphers(ciphers);
835}
836
837bool BaseChannel::ShouldSetupDtlsSrtp() const {
838 return true;
839}
840
841// This function returns true if either DTLS-SRTP is not in use
842// *or* DTLS-SRTP is successfully set up.
843bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
844 bool ret = false;
845
846 TransportChannel *channel = rtcp_channel ?
847 rtcp_transport_channel_ : transport_channel_;
848
849 // No DTLS
850 if (!channel->IsDtlsActive())
851 return true;
852
853 std::string selected_cipher;
854
855 if (!channel->GetSrtpCipher(&selected_cipher)) {
856 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
857 return false;
858 }
859
860 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
861 << content_name() << " "
862 << PacketType(rtcp_channel);
863
864 // OK, we're now doing DTLS (RFC 5764)
865 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
866 SRTP_MASTER_KEY_SALT_LEN * 2);
867
868 // RFC 5705 exporter using the RFC 5764 parameters
869 if (!channel->ExportKeyingMaterial(
870 kDtlsSrtpExporterLabel,
871 NULL, 0, false,
872 &dtls_buffer[0], dtls_buffer.size())) {
873 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
874 ASSERT(false); // This should never happen
875 return false;
876 }
877
878 // Sync up the keys with the DTLS-SRTP interface
879 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
880 SRTP_MASTER_KEY_SALT_LEN);
881 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
882 SRTP_MASTER_KEY_SALT_LEN);
883 size_t offset = 0;
884 memcpy(&client_write_key[0], &dtls_buffer[offset],
885 SRTP_MASTER_KEY_KEY_LEN);
886 offset += SRTP_MASTER_KEY_KEY_LEN;
887 memcpy(&server_write_key[0], &dtls_buffer[offset],
888 SRTP_MASTER_KEY_KEY_LEN);
889 offset += SRTP_MASTER_KEY_KEY_LEN;
890 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
891 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
892 offset += SRTP_MASTER_KEY_SALT_LEN;
893 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
894 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
895
896 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000897 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000898 if (!channel->GetSslRole(&role)) {
899 LOG(LS_WARNING) << "GetSslRole failed";
900 return false;
901 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000903 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 send_key = &server_write_key;
905 recv_key = &client_write_key;
906 } else {
907 send_key = &client_write_key;
908 recv_key = &server_write_key;
909 }
910
911 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000912 ret = srtp_filter_.SetRtcpParams(
913 selected_cipher,
914 &(*send_key)[0],
915 static_cast<int>(send_key->size()),
916 selected_cipher,
917 &(*recv_key)[0],
918 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000920 ret = srtp_filter_.SetRtpParams(
921 selected_cipher,
922 &(*send_key)[0],
923 static_cast<int>(send_key->size()),
924 selected_cipher,
925 &(*recv_key)[0],
926 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 }
928
929 if (!ret)
930 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
931 else
932 dtls_keyed_ = true;
933
934 return ret;
935}
936
937void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000938 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939 if (!writable_)
940 return;
941
942 LOG(LS_INFO) << "Channel socket not writable ("
943 << transport_channel_->content_name() << ", "
944 << transport_channel_->component() << ")";
945 writable_ = false;
946 ChangeState();
947}
948
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000949// |dtls| will be set to true if DTLS is active for transport channel and
950// crypto is empty.
951bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000952 bool* dtls,
953 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000954 *dtls = transport_channel_->IsDtlsActive();
955 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000956 SafeSetError("Cryptos must be empty when DTLS is active.",
957 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000958 return false;
959 }
960 return true;
961}
962
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000963bool BaseChannel::SetRecvRtpHeaderExtensions_w(
964 const MediaContentDescription* content,
965 MediaChannel* media_channel,
966 std::string* error_desc) {
967 if (content->rtp_header_extensions_set()) {
968 if (!media_channel->SetRecvRtpHeaderExtensions(
969 content->rtp_header_extensions())) {
970 std::ostringstream desc;
971 desc << "Failed to set receive rtp header extensions for "
972 << MediaTypeToString(content->type()) << " content.";
973 SafeSetError(desc.str(), error_desc);
974 return false;
975 }
976 }
977 return true;
978}
979
980bool BaseChannel::SetSendRtpHeaderExtensions_w(
981 const MediaContentDescription* content,
982 MediaChannel* media_channel,
983 std::string* error_desc) {
984 if (content->rtp_header_extensions_set()) {
985 if (!media_channel->SetSendRtpHeaderExtensions(
986 content->rtp_header_extensions())) {
987 std::ostringstream desc;
988 desc << "Failed to set send rtp header extensions for "
989 << MediaTypeToString(content->type()) << " content.";
990 SafeSetError(desc.str(), error_desc);
991 return false;
992 } else {
993 MaybeCacheRtpAbsSendTimeHeaderExtension(content->rtp_header_extensions());
994 }
995 }
996 return true;
997}
998
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001000 ContentAction action,
1001 ContentSource src,
1002 std::string* error_desc) {
1003 if (action == CA_UPDATE) {
1004 // no crypto params.
1005 return true;
1006 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001008 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001009 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
1010 if (!ret) {
1011 return false;
1012 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001013 switch (action) {
1014 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001015 // If DTLS is already active on the channel, we could be renegotiating
1016 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001017 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001018 ret = srtp_filter_.SetOffer(cryptos, src);
1019 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 break;
1021 case CA_PRANSWER:
1022 // If we're doing DTLS-SRTP, we don't want to update the filter
1023 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001024 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1026 }
1027 break;
1028 case CA_ANSWER:
1029 // If we're doing DTLS-SRTP, we don't want to update the filter
1030 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001031 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001032 ret = srtp_filter_.SetAnswer(cryptos, src);
1033 }
1034 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035 default:
1036 break;
1037 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001038 if (!ret) {
1039 SafeSetError("Failed to setup SRTP filter.", error_desc);
1040 return false;
1041 }
1042 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043}
1044
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001045void BaseChannel::ActivateRtcpMux() {
1046 worker_thread_->Invoke<void>(Bind(
1047 &BaseChannel::ActivateRtcpMux_w, this));
1048}
1049
1050void BaseChannel::ActivateRtcpMux_w() {
1051 if (!rtcp_mux_filter_.IsActive()) {
1052 rtcp_mux_filter_.SetActive();
1053 set_rtcp_transport_channel(NULL);
1054 }
1055}
1056
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001058 ContentSource src,
1059 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 bool ret = false;
1061 switch (action) {
1062 case CA_OFFER:
1063 ret = rtcp_mux_filter_.SetOffer(enable, src);
1064 break;
1065 case CA_PRANSWER:
1066 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1067 break;
1068 case CA_ANSWER:
1069 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1070 if (ret && rtcp_mux_filter_.IsActive()) {
1071 // We activated RTCP mux, close down the RTCP transport.
1072 set_rtcp_transport_channel(NULL);
1073 }
1074 break;
1075 case CA_UPDATE:
1076 // No RTCP mux info.
1077 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001078 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079 default:
1080 break;
1081 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001082 if (!ret) {
1083 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1084 return false;
1085 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1087 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1088 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001089 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 // If the RTP transport is already writable, then so are we.
1091 if (transport_channel_->writable()) {
1092 ChannelWritable_w();
1093 }
1094 }
1095
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001096 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097}
1098
1099bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001100 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101 if (!media_channel()->AddRecvStream(sp))
1102 return false;
1103
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001104 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105}
1106
1107bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001108 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001109 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 return media_channel()->RemoveRecvStream(ssrc);
1111}
1112
1113bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001114 ContentAction action,
1115 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001116 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1117 action == CA_PRANSWER || action == CA_UPDATE))
1118 return false;
1119
1120 // If this is an update, streams only contain streams that have changed.
1121 if (action == CA_UPDATE) {
1122 for (StreamParamsVec::const_iterator it = streams.begin();
1123 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001124 const StreamParams* existing_stream =
1125 GetStreamByIds(local_streams_, it->groupid, it->id);
1126 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 if (media_channel()->AddSendStream(*it)) {
1128 local_streams_.push_back(*it);
1129 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1130 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001131 std::ostringstream desc;
1132 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1133 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 return false;
1135 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001136 } else if (existing_stream && !it->has_ssrcs()) {
1137 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001138 std::ostringstream desc;
1139 desc << "Failed to remove send stream with ssrc "
1140 << it->first_ssrc() << ".";
1141 SafeSetError(desc.str(), error_desc);
1142 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001144 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 } else {
1146 LOG(LS_WARNING) << "Ignore unsupported stream update";
1147 }
1148 }
1149 return true;
1150 }
1151 // Else streams are all the streams we want to send.
1152
1153 // Check for streams that have been removed.
1154 bool ret = true;
1155 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1156 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001157 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001159 std::ostringstream desc;
1160 desc << "Failed to remove send stream with ssrc "
1161 << it->first_ssrc() << ".";
1162 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001163 ret = false;
1164 }
1165 }
1166 }
1167 // Check for new streams.
1168 for (StreamParamsVec::const_iterator it = streams.begin();
1169 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001170 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 if (media_channel()->AddSendStream(*it)) {
1172 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1173 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001174 std::ostringstream desc;
1175 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1176 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 ret = false;
1178 }
1179 }
1180 }
1181 local_streams_ = streams;
1182 return ret;
1183}
1184
1185bool BaseChannel::UpdateRemoteStreams_w(
1186 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001187 ContentAction action,
1188 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1190 action == CA_PRANSWER || action == CA_UPDATE))
1191 return false;
1192
1193 // If this is an update, streams only contain streams that have changed.
1194 if (action == CA_UPDATE) {
1195 for (StreamParamsVec::const_iterator it = streams.begin();
1196 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001197 const StreamParams* existing_stream =
1198 GetStreamByIds(remote_streams_, it->groupid, it->id);
1199 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 if (AddRecvStream_w(*it)) {
1201 remote_streams_.push_back(*it);
1202 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1203 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001204 std::ostringstream desc;
1205 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1206 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 return false;
1208 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001209 } else if (existing_stream && !it->has_ssrcs()) {
1210 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001211 std::ostringstream desc;
1212 desc << "Failed to remove remote stream with ssrc "
1213 << it->first_ssrc() << ".";
1214 SafeSetError(desc.str(), error_desc);
1215 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001217 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 } else {
1219 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001220 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 << " new stream = " << it->ToString();
1222 }
1223 }
1224 return true;
1225 }
1226 // Else streams are all the streams we want to receive.
1227
1228 // Check for streams that have been removed.
1229 bool ret = true;
1230 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1231 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001232 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001234 std::ostringstream desc;
1235 desc << "Failed to remove remote stream with ssrc "
1236 << it->first_ssrc() << ".";
1237 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238 ret = false;
1239 }
1240 }
1241 }
1242 // Check for new streams.
1243 for (StreamParamsVec::const_iterator it = streams.begin();
1244 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001245 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 if (AddRecvStream_w(*it)) {
1247 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1248 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001249 std::ostringstream desc;
1250 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1251 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 ret = false;
1253 }
1254 }
1255 }
1256 remote_streams_ = streams;
1257 return ret;
1258}
1259
1260bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001261 ContentAction action,
1262 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 // Cache secure_required_ for belt and suspenders check on SendPacket
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001264 secure_required_ = content->crypto_required() != CT_NONE;
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001265 // Set local RTP header extensions.
1266 bool ret = SetRecvRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001267 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001268 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001270 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001271
1272 // Call UpdateLocalStreams_w last to make sure as many settings as possible
1273 // are already set when creating streams.
1274 ret &= UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 set_local_content_direction(content->direction());
1276 return ret;
1277}
1278
1279bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001280 ContentAction action,
1281 std::string* error_desc) {
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001282 // Set remote RTP header extensions.
1283 bool ret = SetSendRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001285 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001287 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001288 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1289 std::ostringstream desc;
1290 desc << "Failed to set max send bandwidth for "
1291 << MediaTypeToString(content->type()) << " content.";
1292 SafeSetError(desc.str(), error_desc);
1293 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294 }
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001295
1296 // Call UpdateRemoteStreams_w last to make sure as many settings as possible
1297 // are already set when creating streams.
1298 ret &= UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 set_remote_content_direction(content->direction());
1300 return ret;
1301}
1302
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001303void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1304 const std::vector<RtpHeaderExtension>& extensions) {
1305 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001306 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001307 rtp_abs_sendtime_extn_id_ =
1308 send_time_extension ? send_time_extension->id : -1;
1309}
1310
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001311void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 case MSG_RTPPACKET:
1314 case MSG_RTCPPACKET: {
1315 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001316 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 delete data; // because it is Posted
1318 break;
1319 }
1320 case MSG_FIRSTPACKETRECEIVED: {
1321 SignalFirstPacketReceived(this);
1322 break;
1323 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 }
1325}
1326
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327void BaseChannel::FlushRtcpMessages() {
1328 // Flush all remaining RTCP messages. This should only be called in
1329 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001330 ASSERT(rtc::Thread::Current() == worker_thread_);
1331 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001332 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001333 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001335 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336 }
1337}
1338
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001339VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340 MediaEngineInterface* media_engine,
1341 VoiceMediaChannel* media_channel,
1342 BaseSession* session,
1343 const std::string& content_name,
1344 bool rtcp)
1345 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1346 rtcp),
1347 received_media_(false) {
1348}
1349
1350VoiceChannel::~VoiceChannel() {
1351 StopAudioMonitor();
1352 StopMediaMonitor();
1353 // this can't be done in the base class, since it calls a virtual
1354 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001355 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356}
1357
1358bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001359 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 return false;
1361 }
1362 media_channel()->SignalMediaError.connect(
1363 this, &VoiceChannel::OnVoiceChannelError);
1364 srtp_filter()->SignalSrtpError.connect(
1365 this, &VoiceChannel::OnSrtpError);
1366 return true;
1367}
1368
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001369bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001370 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1371 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001372}
1373
1374bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001375 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1376 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377}
1378
1379bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001380 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381}
1382
1383// TODO(juberti): Handle early media the right way. We should get an explicit
1384// ringing message telling us to start playing local ringback, which we cancel
1385// if any early media actually arrives. For now, we do the opposite, which is
1386// to wait 1 second for early media, and start playing local ringback if none
1387// arrives.
1388void VoiceChannel::SetEarlyMedia(bool enable) {
1389 if (enable) {
1390 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001391 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1392 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393 } else {
1394 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001395 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001396 }
1397}
1398
1399bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001400 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1401 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402}
1403
1404bool VoiceChannel::PressDTMF(int digit, bool playout) {
1405 int flags = DF_SEND;
1406 if (playout) {
1407 flags |= DF_PLAY;
1408 }
1409 int duration_ms = 160;
1410 return InsertDtmf(0, digit, duration_ms, flags);
1411}
1412
1413bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001414 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1415 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416}
1417
1418bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1419 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001420 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1421 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422}
1423
1424bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001425 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1426 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001428
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001430 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1431 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432}
1433
1434void VoiceChannel::StartMediaMonitor(int cms) {
1435 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001436 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 media_monitor_->SignalUpdate.connect(
1438 this, &VoiceChannel::OnMediaMonitorUpdate);
1439 media_monitor_->Start(cms);
1440}
1441
1442void VoiceChannel::StopMediaMonitor() {
1443 if (media_monitor_) {
1444 media_monitor_->Stop();
1445 media_monitor_->SignalUpdate.disconnect(this);
1446 media_monitor_.reset();
1447 }
1448}
1449
1450void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001451 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452 audio_monitor_
1453 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1454 audio_monitor_->Start(cms);
1455}
1456
1457void VoiceChannel::StopAudioMonitor() {
1458 if (audio_monitor_) {
1459 audio_monitor_->Stop();
1460 audio_monitor_.reset();
1461 }
1462}
1463
1464bool VoiceChannel::IsAudioMonitorRunning() const {
1465 return (audio_monitor_.get() != NULL);
1466}
1467
1468void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1469 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1470 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1471}
1472
1473void VoiceChannel::StopTypingMonitor() {
1474 typing_monitor_.reset();
1475}
1476
1477bool VoiceChannel::IsTypingMonitorRunning() const {
1478 return typing_monitor_;
1479}
1480
1481bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1482 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1483 if (typing_monitor_ && mute)
1484 typing_monitor_->OnChannelMuted();
1485 return ret;
1486}
1487
1488int VoiceChannel::GetInputLevel_w() {
1489 return media_engine()->GetInputLevel();
1490}
1491
1492int VoiceChannel::GetOutputLevel_w() {
1493 return media_channel()->GetOutputLevel();
1494}
1495
1496void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1497 media_channel()->GetActiveStreams(actives);
1498}
1499
1500void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001501 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001502 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001503 int flags) {
1504 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505
1506 // Set a flag when we've received an RTP packet. If we're waiting for early
1507 // media, this will disable the timeout.
1508 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1509 received_media_ = true;
1510 }
1511}
1512
1513void VoiceChannel::ChangeState() {
1514 // Render incoming data if we're the active call, and we have the local
1515 // content. We receive data on the default channel and multiplexed streams.
1516 bool recv = IsReadyToReceive();
1517 if (!media_channel()->SetPlayout(recv)) {
1518 SendLastMediaError();
1519 }
1520
1521 // Send outgoing data if we're the active call, we have the remote content,
1522 // and we have had some form of connectivity.
1523 bool send = IsReadyToSend();
1524 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1525 if (!media_channel()->SetSend(send_flag)) {
1526 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1527 SendLastMediaError();
1528 }
1529
1530 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1531}
1532
1533const ContentInfo* VoiceChannel::GetFirstContent(
1534 const SessionDescription* sdesc) {
1535 return GetFirstAudioContent(sdesc);
1536}
1537
1538bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001539 ContentAction action,
1540 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001541 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 LOG(LS_INFO) << "Setting local voice description";
1543
1544 const AudioContentDescription* audio =
1545 static_cast<const AudioContentDescription*>(content);
1546 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001547 if (!audio) {
1548 SafeSetError("Can't find audio content in local description.", error_desc);
1549 return false;
1550 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001552 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553 // Set local audio codecs (what we want to receive).
1554 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1555 // is set properly.
1556 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001557 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1558 SafeSetError("Failed to set audio receive codecs.", error_desc);
1559 ret = false;
1560 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 }
1562
1563 // If everything worked, see if we can start receiving.
1564 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001565 std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1566 for (; it != audio->codecs().end(); ++it) {
1567 bundle_filter()->AddPayloadType(it->id);
1568 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 ChangeState();
1570 } else {
1571 LOG(LS_WARNING) << "Failed to set local voice description";
1572 }
1573 return ret;
1574}
1575
1576bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001577 ContentAction action,
1578 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001579 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 LOG(LS_INFO) << "Setting remote voice description";
1581
1582 const AudioContentDescription* audio =
1583 static_cast<const AudioContentDescription*>(content);
1584 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001585 if (!audio) {
1586 SafeSetError("Can't find audio content in remote description.", error_desc);
1587 return false;
1588 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589
1590 bool ret = true;
1591 // Set remote video codecs (what the other side wants to receive).
1592 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001593 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1594 SafeSetError("Failed to set audio send codecs.", error_desc);
1595 ret = false;
1596 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 }
1598
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001599 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600
1601 if (action != CA_UPDATE) {
1602 // Tweak our audio processing settings, if needed.
1603 AudioOptions audio_options;
1604 if (!media_channel()->GetOptions(&audio_options)) {
1605 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1606 } else {
1607 if (audio->conference_mode()) {
1608 audio_options.conference_mode.Set(true);
1609 }
1610 if (audio->agc_minus_10db()) {
1611 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1612 }
1613 if (!media_channel()->SetOptions(audio_options)) {
1614 // Log an error on failure, but don't abort the call.
1615 LOG(LS_ERROR) << "Failed to set voice channel options";
1616 }
1617 }
1618 }
1619
1620 // If everything worked, see if we can start sending.
1621 if (ret) {
1622 ChangeState();
1623 } else {
1624 LOG(LS_WARNING) << "Failed to set remote voice description";
1625 }
1626 return ret;
1627}
1628
1629bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001630 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1632}
1633
1634bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001635 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 if (play) {
1637 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1638 } else {
1639 LOG(LS_INFO) << "Stopping ringback tone";
1640 }
1641 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1642}
1643
1644void VoiceChannel::HandleEarlyMediaTimeout() {
1645 // This occurs on the main thread, not the worker thread.
1646 if (!received_media_) {
1647 LOG(LS_INFO) << "No early media received before timeout";
1648 SignalEarlyMediaTimeout(this);
1649 }
1650}
1651
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1653 int flags) {
1654 if (!enabled()) {
1655 return false;
1656 }
1657
1658 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1659}
1660
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001662 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1663 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664}
1665
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001666void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668 case MSG_EARLYMEDIATIMEOUT:
1669 HandleEarlyMediaTimeout();
1670 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 case MSG_CHANNEL_ERROR: {
1672 VoiceChannelErrorMessageData* data =
1673 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1674 SignalMediaError(this, data->ssrc, data->error);
1675 delete data;
1676 break;
1677 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 default:
1679 BaseChannel::OnMessage(pmsg);
1680 break;
1681 }
1682}
1683
1684void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001685 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686 SignalConnectionMonitor(this, infos);
1687}
1688
1689void VoiceChannel::OnMediaMonitorUpdate(
1690 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1691 ASSERT(media_channel == this->media_channel());
1692 SignalMediaMonitor(this, info);
1693}
1694
1695void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1696 const AudioInfo& info) {
1697 SignalAudioMonitor(this, info);
1698}
1699
1700void VoiceChannel::OnVoiceChannelError(
1701 uint32 ssrc, VoiceMediaChannel::Error err) {
1702 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1703 ssrc, err);
1704 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1705}
1706
1707void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1708 SrtpFilter::Error error) {
1709 switch (error) {
1710 case SrtpFilter::ERROR_FAIL:
1711 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1712 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1713 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1714 break;
1715 case SrtpFilter::ERROR_AUTH:
1716 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1717 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1718 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1719 break;
1720 case SrtpFilter::ERROR_REPLAY:
1721 // Only receving channel should have this error.
1722 ASSERT(mode == SrtpFilter::UNPROTECT);
1723 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1724 break;
1725 default:
1726 break;
1727 }
1728}
1729
1730void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1731 GetSupportedAudioCryptoSuites(ciphers);
1732}
1733
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001734VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 MediaEngineInterface* media_engine,
1736 VideoMediaChannel* media_channel,
1737 BaseSession* session,
1738 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001739 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1741 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001743 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744}
1745
1746bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001747 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 return false;
1749 }
1750 media_channel()->SignalMediaError.connect(
1751 this, &VideoChannel::OnVideoChannelError);
1752 srtp_filter()->SignalSrtpError.connect(
1753 this, &VideoChannel::OnSrtpError);
1754 return true;
1755}
1756
1757void VoiceChannel::SendLastMediaError() {
1758 uint32 ssrc;
1759 VoiceMediaChannel::Error error;
1760 media_channel()->GetLastMediaError(&ssrc, &error);
1761 SignalMediaError(this, ssrc, error);
1762}
1763
1764VideoChannel::~VideoChannel() {
1765 std::vector<uint32> screencast_ssrcs;
1766 ScreencastMap::iterator iter;
1767 while (!screencast_capturers_.empty()) {
1768 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1769 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1770 << screencast_capturers_.begin()->first;
1771 ASSERT(false);
1772 break;
1773 }
1774 }
1775
1776 StopMediaMonitor();
1777 // this can't be done in the base class, since it calls a virtual
1778 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001779
1780 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001781}
1782
1783bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001784 worker_thread()->Invoke<void>(Bind(
1785 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786 return true;
1787}
1788
1789bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001790 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791}
1792
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001793bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1794 return worker_thread()->Invoke<bool>(Bind(
1795 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796}
1797
1798bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001799 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1800 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801}
1802
1803bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001804 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805}
1806
1807bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001808 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809}
1810
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001811int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001812 ScreencastDetailsData data(ssrc);
1813 worker_thread()->Invoke<void>(Bind(
1814 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001815 return data.fps;
1816}
1817
1818int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001819 ScreencastDetailsData data(ssrc);
1820 worker_thread()->Invoke<void>(Bind(
1821 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001822 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823}
1824
1825bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001826 worker_thread()->Invoke<void>(Bind(
1827 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 return true;
1829}
1830
1831bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001832 worker_thread()->Invoke<void>(Bind(
1833 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 return true;
1835}
1836
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837void VideoChannel::ChangeState() {
1838 // Render incoming data if we're the active call, and we have the local
1839 // content. We receive data on the default channel and multiplexed streams.
1840 bool recv = IsReadyToReceive();
1841 if (!media_channel()->SetRender(recv)) {
1842 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1843 // TODO(gangji): Report error back to server.
1844 }
1845
1846 // Send outgoing data if we're the active call, we have the remote content,
1847 // and we have had some form of connectivity.
1848 bool send = IsReadyToSend();
1849 if (!media_channel()->SetSend(send)) {
1850 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1851 // TODO(gangji): Report error back to server.
1852 }
1853
1854 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1855}
1856
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001857bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1858 return InvokeOnWorker(
1859 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860}
1861
1862void VideoChannel::StartMediaMonitor(int cms) {
1863 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001864 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 media_monitor_->SignalUpdate.connect(
1866 this, &VideoChannel::OnMediaMonitorUpdate);
1867 media_monitor_->Start(cms);
1868}
1869
1870void VideoChannel::StopMediaMonitor() {
1871 if (media_monitor_) {
1872 media_monitor_->Stop();
1873 media_monitor_.reset();
1874 }
1875}
1876
1877const ContentInfo* VideoChannel::GetFirstContent(
1878 const SessionDescription* sdesc) {
1879 return GetFirstVideoContent(sdesc);
1880}
1881
1882bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001883 ContentAction action,
1884 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001885 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 LOG(LS_INFO) << "Setting local video description";
1887
1888 const VideoContentDescription* video =
1889 static_cast<const VideoContentDescription*>(content);
1890 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001891 if (!video) {
1892 SafeSetError("Can't find video content in local description.", error_desc);
1893 return false;
1894 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001896 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001897 // Set local video codecs (what we want to receive).
1898 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001899 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1900 SafeSetError("Failed to set video receive codecs.", error_desc);
1901 ret = false;
1902 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903 }
1904
1905 if (action != CA_UPDATE) {
1906 VideoOptions video_options;
1907 media_channel()->GetOptions(&video_options);
1908 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1909
1910 if (!media_channel()->SetOptions(video_options)) {
1911 // Log an error on failure, but don't abort the call.
1912 LOG(LS_ERROR) << "Failed to set video channel options";
1913 }
1914 }
1915
1916 // If everything worked, see if we can start receiving.
1917 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001918 std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1919 for (; it != video->codecs().end(); ++it) {
1920 bundle_filter()->AddPayloadType(it->id);
1921 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922 ChangeState();
1923 } else {
1924 LOG(LS_WARNING) << "Failed to set local video description";
1925 }
1926 return ret;
1927}
1928
1929bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001930 ContentAction action,
1931 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001932 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 LOG(LS_INFO) << "Setting remote video description";
1934
1935 const VideoContentDescription* video =
1936 static_cast<const VideoContentDescription*>(content);
1937 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001938 if (!video) {
1939 SafeSetError("Can't find video content in remote description.", error_desc);
1940 return false;
1941 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942
1943 bool ret = true;
1944 // Set remote video codecs (what the other side wants to receive).
1945 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001946 if (!media_channel()->SetSendCodecs(video->codecs())) {
1947 SafeSetError("Failed to set video send codecs.", error_desc);
1948 ret = false;
1949 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 }
1951
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001952 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953
1954 if (action != CA_UPDATE) {
1955 // Tweak our video processing settings, if needed.
1956 VideoOptions video_options;
1957 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001958 if (video->conference_mode()) {
1959 video_options.conference_mode.Set(true);
1960 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1962
1963 if (!media_channel()->SetOptions(video_options)) {
1964 // Log an error on failure, but don't abort the call.
1965 LOG(LS_ERROR) << "Failed to set video channel options";
1966 }
1967 }
1968
1969 // If everything worked, see if we can start sending.
1970 if (ret) {
1971 ChangeState();
1972 } else {
1973 LOG(LS_WARNING) << "Failed to set remote video description";
1974 }
1975 return ret;
1976}
1977
1978bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1979 bool ret = true;
1980 // Set the send format for each of the local streams. If the view request
1981 // does not contain a local stream, set its send format to 0x0, which will
1982 // drop all frames.
1983 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1984 it != local_streams().end(); ++it) {
1985 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1986 StaticVideoViews::const_iterator view;
1987 for (view = request.static_video_views.begin();
1988 view != request.static_video_views.end(); ++view) {
1989 if (view->selector.Matches(*it)) {
1990 format.width = view->width;
1991 format.height = view->height;
1992 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1993 break;
1994 }
1995 }
1996
1997 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1998 }
1999
2000 // Check if the view request has invalid streams.
2001 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
2002 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00002003 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004 LOG(LS_WARNING) << "View request for ("
2005 << it->selector.ssrc << ", '"
2006 << it->selector.groupid << "', '"
2007 << it->selector.streamid << "'"
2008 << ") is not in the local streams.";
2009 }
2010 }
2011
2012 return ret;
2013}
2014
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002015bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002017 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002019 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
2020 screencast_capturers_[ssrc] = capturer;
2021 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022}
2023
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
2025 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
2026 if (iter == screencast_capturers_.end()) {
2027 return false;
2028 }
2029 // Clean up VideoCapturer.
2030 delete iter->second;
2031 screencast_capturers_.erase(iter);
2032 return true;
2033}
2034
2035bool VideoChannel::IsScreencasting_w() const {
2036 return !screencast_capturers_.empty();
2037}
2038
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002039void VideoChannel::GetScreencastDetails_w(
2040 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002041 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002043 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044 }
2045 VideoCapturer* capturer = iter->second;
2046 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002047 data->fps = VideoFormat::IntervalToFps(video_format->interval);
2048 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049}
2050
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002052 rtc::WindowEvent we) {
2053 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054 SignalScreencastWindowEvent(ssrc, we);
2055}
2056
2057bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002058 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
2059 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060}
2061
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002062void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 case MSG_SCREENCASTWINDOWEVENT: {
2065 const ScreencastEventMessageData* data =
2066 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
2067 OnScreencastWindowEvent_s(data->ssrc, data->event);
2068 delete data;
2069 break;
2070 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002071 case MSG_CHANNEL_ERROR: {
2072 const VideoChannelErrorMessageData* data =
2073 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
2074 SignalMediaError(this, data->ssrc, data->error);
2075 delete data;
2076 break;
2077 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 default:
2079 BaseChannel::OnMessage(pmsg);
2080 break;
2081 }
2082}
2083
2084void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002085 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 SignalConnectionMonitor(this, infos);
2087}
2088
2089// TODO(pthatcher): Look into removing duplicate code between
2090// audio, video, and data, perhaps by using templates.
2091void VideoChannel::OnMediaMonitorUpdate(
2092 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2093 ASSERT(media_channel == this->media_channel());
2094 SignalMediaMonitor(this, info);
2095}
2096
2097void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002098 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099 ScreencastEventMessageData* pdata =
2100 new ScreencastEventMessageData(ssrc, event);
2101 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2102}
2103
2104void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2105 // Map capturer events to window events. In the future we may want to simply
2106 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002107 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002109 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002110 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002111 we = rtc::WE_MINIMIZE;
2112 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2113 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 } else {
2115 return;
2116 }
2117 previous_we_ = we;
2118
2119 uint32 ssrc = 0;
2120 if (!GetLocalSsrc(capturer, &ssrc)) {
2121 return;
2122 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002123
2124 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125}
2126
2127bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2128 *ssrc = 0;
2129 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2130 iter != screencast_capturers_.end(); ++iter) {
2131 if (iter->second == capturer) {
2132 *ssrc = iter->first;
2133 return true;
2134 }
2135 }
2136 return false;
2137}
2138
2139void VideoChannel::OnVideoChannelError(uint32 ssrc,
2140 VideoMediaChannel::Error error) {
2141 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2142 ssrc, error);
2143 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2144}
2145
2146void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2147 SrtpFilter::Error error) {
2148 switch (error) {
2149 case SrtpFilter::ERROR_FAIL:
2150 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2151 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2152 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2153 break;
2154 case SrtpFilter::ERROR_AUTH:
2155 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2156 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2157 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2158 break;
2159 case SrtpFilter::ERROR_REPLAY:
2160 // Only receving channel should have this error.
2161 ASSERT(mode == SrtpFilter::UNPROTECT);
2162 // TODO(gangji): Turn on the signaling of replay error once we have
2163 // switched to the new mechanism for doing video retransmissions.
2164 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2165 break;
2166 default:
2167 break;
2168 }
2169}
2170
2171
2172void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2173 GetSupportedVideoCryptoSuites(ciphers);
2174}
2175
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002176DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177 DataMediaChannel* media_channel,
2178 BaseSession* session,
2179 const std::string& content_name,
2180 bool rtcp)
2181 // MediaEngine is NULL
2182 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002183 data_channel_type_(cricket::DCT_NONE),
2184 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185}
2186
2187DataChannel::~DataChannel() {
2188 StopMediaMonitor();
2189 // this can't be done in the base class, since it calls a virtual
2190 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002191
2192 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193}
2194
2195bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002196 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 return false;
2198 }
2199 media_channel()->SignalDataReceived.connect(
2200 this, &DataChannel::OnDataReceived);
2201 media_channel()->SignalMediaError.connect(
2202 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002203 media_channel()->SignalReadyToSend.connect(
2204 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002205 media_channel()->SignalStreamClosedRemotely.connect(
2206 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207 srtp_filter()->SignalSrtpError.connect(
2208 this, &DataChannel::OnSrtpError);
2209 return true;
2210}
2211
2212bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002213 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002215 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2216 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217}
2218
2219const ContentInfo* DataChannel::GetFirstContent(
2220 const SessionDescription* sdesc) {
2221 return GetFirstDataContent(sdesc);
2222}
2223
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002224bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 if (data_channel_type_ == DCT_SCTP) {
2226 // TODO(pthatcher): Do this in a more robust way by checking for
2227 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002228 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002229 } else if (data_channel_type_ == DCT_RTP) {
2230 return BaseChannel::WantsPacket(rtcp, packet);
2231 }
2232 return false;
2233}
2234
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002235bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2236 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237 // It hasn't been set before, so set it now.
2238 if (data_channel_type_ == DCT_NONE) {
2239 data_channel_type_ = new_data_channel_type;
2240 return true;
2241 }
2242
2243 // It's been set before, but doesn't match. That's bad.
2244 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002245 std::ostringstream desc;
2246 desc << "Data channel type mismatch."
2247 << " Expected " << data_channel_type_
2248 << " Got " << new_data_channel_type;
2249 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250 return false;
2251 }
2252
2253 // It's hasn't changed. Nothing to do.
2254 return true;
2255}
2256
2257bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002258 const DataContentDescription* content,
2259 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002260 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2261 (content->protocol() == kMediaProtocolDtlsSctp));
2262 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002263 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002264}
2265
2266bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002267 ContentAction action,
2268 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002269 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270 LOG(LS_INFO) << "Setting local data description";
2271
2272 const DataContentDescription* data =
2273 static_cast<const DataContentDescription*>(content);
2274 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002275 if (!data) {
2276 SafeSetError("Can't find data content in local description.", error_desc);
2277 return false;
2278 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279
2280 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002281 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 return false;
2283 }
2284
2285 if (data_channel_type_ == DCT_SCTP) {
2286 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002287 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288 if (ret) {
2289 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002290 // As in SetRemoteContent_w, make sure we set the local SCTP port
2291 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002292 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2293 SafeSetError("Failed to set data receive codecs.", error_desc);
2294 ret = false;
2295 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 }
2297 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002298 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002300 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2301 SafeSetError("Failed to set data receive codecs.", error_desc);
2302 ret = false;
2303 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002304 }
2305 }
2306
2307 // If everything worked, see if we can start receiving.
2308 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00002309 std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2310 for (; it != data->codecs().end(); ++it) {
2311 bundle_filter()->AddPayloadType(it->id);
2312 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313 ChangeState();
2314 } else {
2315 LOG(LS_WARNING) << "Failed to set local data description";
2316 }
2317 return ret;
2318}
2319
2320bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002321 ContentAction action,
2322 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002323 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324
2325 const DataContentDescription* data =
2326 static_cast<const DataContentDescription*>(content);
2327 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002328 if (!data) {
2329 SafeSetError("Can't find data content in remote description.", error_desc);
2330 return false;
2331 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332
2333 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002334 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002335 return false;
2336 }
2337
2338 if (data_channel_type_ == DCT_SCTP) {
2339 LOG(LS_INFO) << "Setting SCTP remote data description";
2340 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002341 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342 if (ret) {
2343 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002344 // We send the SCTP port number (not to be confused with the underlying
2345 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002346 if (!media_channel()->SetSendCodecs(data->codecs())) {
2347 SafeSetError("Failed to set data send codecs.", error_desc);
2348 ret = false;
2349 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350 }
2351 } else {
2352 // If the remote data doesn't have codecs and isn't an update, it
2353 // must be empty, so ignore it.
2354 if (action != CA_UPDATE && !data->has_codecs()) {
2355 return true;
2356 }
2357 LOG(LS_INFO) << "Setting remote data description";
2358
2359 // Set remote video codecs (what the other side wants to receive).
2360 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002361 if (!media_channel()->SetSendCodecs(data->codecs())) {
2362 SafeSetError("Failed to set data send codecs.", error_desc);
2363 ret = false;
2364 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002365 }
2366
2367 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002368 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369 }
2370
2371 if (action != CA_UPDATE) {
2372 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002373 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2374 std::ostringstream desc;
2375 desc << "Failed to set max send bandwidth for data content.";
2376 SafeSetError(desc.str(), error_desc);
2377 ret = false;
2378 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002379 }
2380 }
2381
2382 // If everything worked, see if we can start sending.
2383 if (ret) {
2384 ChangeState();
2385 } else {
2386 LOG(LS_WARNING) << "Failed to set remote data description";
2387 }
2388 return ret;
2389}
2390
2391void DataChannel::ChangeState() {
2392 // Render incoming data if we're the active call, and we have the local
2393 // content. We receive data on the default channel and multiplexed streams.
2394 bool recv = IsReadyToReceive();
2395 if (!media_channel()->SetReceive(recv)) {
2396 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2397 }
2398
2399 // Send outgoing data if we're the active call, we have the remote content,
2400 // and we have had some form of connectivity.
2401 bool send = IsReadyToSend();
2402 if (!media_channel()->SetSend(send)) {
2403 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2404 }
2405
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002406 // Trigger SignalReadyToSendData asynchronously.
2407 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408
2409 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2410}
2411
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002412void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413 switch (pmsg->message_id) {
2414 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002415 DataChannelReadyToSendMessageData* data =
2416 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002417 ready_to_send_data_ = data->data();
2418 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002419 delete data;
2420 break;
2421 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002422 case MSG_DATARECEIVED: {
2423 DataReceivedMessageData* data =
2424 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2425 SignalDataReceived(this, data->params, data->payload);
2426 delete data;
2427 break;
2428 }
2429 case MSG_CHANNEL_ERROR: {
2430 const DataChannelErrorMessageData* data =
2431 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2432 SignalMediaError(this, data->ssrc, data->error);
2433 delete data;
2434 break;
2435 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002436 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002437 rtc::TypedMessageData<uint32>* data =
2438 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002439 SignalStreamClosedRemotely(data->data());
2440 delete data;
2441 break;
2442 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002443 default:
2444 BaseChannel::OnMessage(pmsg);
2445 break;
2446 }
2447}
2448
2449void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002450 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002451 SignalConnectionMonitor(this, infos);
2452}
2453
2454void DataChannel::StartMediaMonitor(int cms) {
2455 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002456 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002457 media_monitor_->SignalUpdate.connect(
2458 this, &DataChannel::OnMediaMonitorUpdate);
2459 media_monitor_->Start(cms);
2460}
2461
2462void DataChannel::StopMediaMonitor() {
2463 if (media_monitor_) {
2464 media_monitor_->Stop();
2465 media_monitor_->SignalUpdate.disconnect(this);
2466 media_monitor_.reset();
2467 }
2468}
2469
2470void DataChannel::OnMediaMonitorUpdate(
2471 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2472 ASSERT(media_channel == this->media_channel());
2473 SignalMediaMonitor(this, info);
2474}
2475
2476void DataChannel::OnDataReceived(
2477 const ReceiveDataParams& params, const char* data, size_t len) {
2478 DataReceivedMessageData* msg = new DataReceivedMessageData(
2479 params, data, len);
2480 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2481}
2482
2483void DataChannel::OnDataChannelError(
2484 uint32 ssrc, DataMediaChannel::Error err) {
2485 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2486 ssrc, err);
2487 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2488}
2489
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002490void DataChannel::OnDataChannelReadyToSend(bool writable) {
2491 // This is usded for congestion control to indicate that the stream is ready
2492 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2493 // that the transport channel is ready.
2494 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2495 new DataChannelReadyToSendMessageData(writable));
2496}
2497
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002498void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2499 SrtpFilter::Error error) {
2500 switch (error) {
2501 case SrtpFilter::ERROR_FAIL:
2502 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2503 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2504 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2505 break;
2506 case SrtpFilter::ERROR_AUTH:
2507 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2508 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2509 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2510 break;
2511 case SrtpFilter::ERROR_REPLAY:
2512 // Only receving channel should have this error.
2513 ASSERT(mode == SrtpFilter::UNPROTECT);
2514 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2515 break;
2516 default:
2517 break;
2518 }
2519}
2520
2521void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2522 GetSupportedDataCryptoSuites(ciphers);
2523}
2524
2525bool DataChannel::ShouldSetupDtlsSrtp() const {
2526 return (data_channel_type_ == DCT_RTP);
2527}
2528
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002529void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002530 rtc::TypedMessageData<uint32>* message =
2531 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002532 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2533}
2534
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002535} // namespace cricket