blob: 0034f15d9b104d916cc2687e371e90f6c3d1dc4c [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
1045bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001046 ContentSource src,
1047 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001048 bool ret = false;
1049 switch (action) {
1050 case CA_OFFER:
1051 ret = rtcp_mux_filter_.SetOffer(enable, src);
1052 break;
1053 case CA_PRANSWER:
1054 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1055 break;
1056 case CA_ANSWER:
1057 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1058 if (ret && rtcp_mux_filter_.IsActive()) {
1059 // We activated RTCP mux, close down the RTCP transport.
1060 set_rtcp_transport_channel(NULL);
1061 }
1062 break;
1063 case CA_UPDATE:
1064 // No RTCP mux info.
1065 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001066 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 default:
1068 break;
1069 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001070 if (!ret) {
1071 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1072 return false;
1073 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1075 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1076 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001077 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 // If the RTP transport is already writable, then so are we.
1079 if (transport_channel_->writable()) {
1080 ChannelWritable_w();
1081 }
1082 }
1083
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001084 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085}
1086
1087bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001088 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 if (!media_channel()->AddRecvStream(sp))
1090 return false;
1091
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001092 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093}
1094
1095bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001096 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001097 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 return media_channel()->RemoveRecvStream(ssrc);
1099}
1100
1101bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001102 ContentAction action,
1103 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1105 action == CA_PRANSWER || action == CA_UPDATE))
1106 return false;
1107
1108 // If this is an update, streams only contain streams that have changed.
1109 if (action == CA_UPDATE) {
1110 for (StreamParamsVec::const_iterator it = streams.begin();
1111 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001112 const StreamParams* existing_stream =
1113 GetStreamByIds(local_streams_, it->groupid, it->id);
1114 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 if (media_channel()->AddSendStream(*it)) {
1116 local_streams_.push_back(*it);
1117 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1118 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001119 std::ostringstream desc;
1120 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1121 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 return false;
1123 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001124 } else if (existing_stream && !it->has_ssrcs()) {
1125 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001126 std::ostringstream desc;
1127 desc << "Failed to remove send stream with ssrc "
1128 << it->first_ssrc() << ".";
1129 SafeSetError(desc.str(), error_desc);
1130 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001132 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 } else {
1134 LOG(LS_WARNING) << "Ignore unsupported stream update";
1135 }
1136 }
1137 return true;
1138 }
1139 // Else streams are all the streams we want to send.
1140
1141 // Check for streams that have been removed.
1142 bool ret = true;
1143 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1144 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001145 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001147 std::ostringstream desc;
1148 desc << "Failed to remove send stream with ssrc "
1149 << it->first_ssrc() << ".";
1150 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151 ret = false;
1152 }
1153 }
1154 }
1155 // Check for new streams.
1156 for (StreamParamsVec::const_iterator it = streams.begin();
1157 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001158 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 if (media_channel()->AddSendStream(*it)) {
1160 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1161 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001162 std::ostringstream desc;
1163 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1164 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165 ret = false;
1166 }
1167 }
1168 }
1169 local_streams_ = streams;
1170 return ret;
1171}
1172
1173bool BaseChannel::UpdateRemoteStreams_w(
1174 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001175 ContentAction action,
1176 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1178 action == CA_PRANSWER || action == CA_UPDATE))
1179 return false;
1180
1181 // If this is an update, streams only contain streams that have changed.
1182 if (action == CA_UPDATE) {
1183 for (StreamParamsVec::const_iterator it = streams.begin();
1184 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001185 const StreamParams* existing_stream =
1186 GetStreamByIds(remote_streams_, it->groupid, it->id);
1187 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 if (AddRecvStream_w(*it)) {
1189 remote_streams_.push_back(*it);
1190 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1191 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001192 std::ostringstream desc;
1193 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1194 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 return false;
1196 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001197 } else if (existing_stream && !it->has_ssrcs()) {
1198 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001199 std::ostringstream desc;
1200 desc << "Failed to remove remote stream with ssrc "
1201 << it->first_ssrc() << ".";
1202 SafeSetError(desc.str(), error_desc);
1203 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001205 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 } else {
1207 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001208 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 << " new stream = " << it->ToString();
1210 }
1211 }
1212 return true;
1213 }
1214 // Else streams are all the streams we want to receive.
1215
1216 // Check for streams that have been removed.
1217 bool ret = true;
1218 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1219 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001220 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001222 std::ostringstream desc;
1223 desc << "Failed to remove remote stream with ssrc "
1224 << it->first_ssrc() << ".";
1225 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 ret = false;
1227 }
1228 }
1229 }
1230 // Check for new streams.
1231 for (StreamParamsVec::const_iterator it = streams.begin();
1232 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001233 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234 if (AddRecvStream_w(*it)) {
1235 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1236 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001237 std::ostringstream desc;
1238 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1239 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 ret = false;
1241 }
1242 }
1243 }
1244 remote_streams_ = streams;
1245 return ret;
1246}
1247
1248bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001249 ContentAction action,
1250 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 // Cache secure_required_ for belt and suspenders check on SendPacket
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001252 secure_required_ = content->crypto_required() != CT_NONE;
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001253 // Set local RTP header extensions.
1254 bool ret = SetRecvRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001256 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001258 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001259
1260 // Call UpdateLocalStreams_w last to make sure as many settings as possible
1261 // are already set when creating streams.
1262 ret &= UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 set_local_content_direction(content->direction());
1264 return ret;
1265}
1266
1267bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001268 ContentAction action,
1269 std::string* error_desc) {
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001270 // Set remote RTP header extensions.
1271 bool ret = SetSendRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001273 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001275 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001276 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1277 std::ostringstream desc;
1278 desc << "Failed to set max send bandwidth for "
1279 << MediaTypeToString(content->type()) << " content.";
1280 SafeSetError(desc.str(), error_desc);
1281 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 }
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001283
1284 // Call UpdateRemoteStreams_w last to make sure as many settings as possible
1285 // are already set when creating streams.
1286 ret &= UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 set_remote_content_direction(content->direction());
1288 return ret;
1289}
1290
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001291void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1292 const std::vector<RtpHeaderExtension>& extensions) {
1293 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001294 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001295 rtp_abs_sendtime_extn_id_ =
1296 send_time_extension ? send_time_extension->id : -1;
1297}
1298
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001299void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301 case MSG_RTPPACKET:
1302 case MSG_RTCPPACKET: {
1303 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001304 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305 delete data; // because it is Posted
1306 break;
1307 }
1308 case MSG_FIRSTPACKETRECEIVED: {
1309 SignalFirstPacketReceived(this);
1310 break;
1311 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 }
1313}
1314
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315void BaseChannel::FlushRtcpMessages() {
1316 // Flush all remaining RTCP messages. This should only be called in
1317 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001318 ASSERT(rtc::Thread::Current() == worker_thread_);
1319 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001320 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001321 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001323 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 }
1325}
1326
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001327VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 MediaEngineInterface* media_engine,
1329 VoiceMediaChannel* media_channel,
1330 BaseSession* session,
1331 const std::string& content_name,
1332 bool rtcp)
1333 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1334 rtcp),
1335 received_media_(false) {
1336}
1337
1338VoiceChannel::~VoiceChannel() {
1339 StopAudioMonitor();
1340 StopMediaMonitor();
1341 // this can't be done in the base class, since it calls a virtual
1342 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001343 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344}
1345
1346bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001347 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348 return false;
1349 }
1350 media_channel()->SignalMediaError.connect(
1351 this, &VoiceChannel::OnVoiceChannelError);
1352 srtp_filter()->SignalSrtpError.connect(
1353 this, &VoiceChannel::OnSrtpError);
1354 return true;
1355}
1356
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001357bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001358 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1359 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001360}
1361
1362bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001363 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1364 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365}
1366
1367bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001368 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369}
1370
1371// TODO(juberti): Handle early media the right way. We should get an explicit
1372// ringing message telling us to start playing local ringback, which we cancel
1373// if any early media actually arrives. For now, we do the opposite, which is
1374// to wait 1 second for early media, and start playing local ringback if none
1375// arrives.
1376void VoiceChannel::SetEarlyMedia(bool enable) {
1377 if (enable) {
1378 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001379 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1380 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001381 } else {
1382 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001383 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 }
1385}
1386
1387bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001388 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1389 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390}
1391
1392bool VoiceChannel::PressDTMF(int digit, bool playout) {
1393 int flags = DF_SEND;
1394 if (playout) {
1395 flags |= DF_PLAY;
1396 }
1397 int duration_ms = 160;
1398 return InsertDtmf(0, digit, duration_ms, flags);
1399}
1400
1401bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001402 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1403 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404}
1405
1406bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1407 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001408 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1409 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410}
1411
1412bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001413 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1414 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001415}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001416
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001418 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1419 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001420}
1421
1422void VoiceChannel::StartMediaMonitor(int cms) {
1423 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001424 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 media_monitor_->SignalUpdate.connect(
1426 this, &VoiceChannel::OnMediaMonitorUpdate);
1427 media_monitor_->Start(cms);
1428}
1429
1430void VoiceChannel::StopMediaMonitor() {
1431 if (media_monitor_) {
1432 media_monitor_->Stop();
1433 media_monitor_->SignalUpdate.disconnect(this);
1434 media_monitor_.reset();
1435 }
1436}
1437
1438void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001439 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440 audio_monitor_
1441 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1442 audio_monitor_->Start(cms);
1443}
1444
1445void VoiceChannel::StopAudioMonitor() {
1446 if (audio_monitor_) {
1447 audio_monitor_->Stop();
1448 audio_monitor_.reset();
1449 }
1450}
1451
1452bool VoiceChannel::IsAudioMonitorRunning() const {
1453 return (audio_monitor_.get() != NULL);
1454}
1455
1456void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1457 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1458 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1459}
1460
1461void VoiceChannel::StopTypingMonitor() {
1462 typing_monitor_.reset();
1463}
1464
1465bool VoiceChannel::IsTypingMonitorRunning() const {
1466 return typing_monitor_;
1467}
1468
1469bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1470 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1471 if (typing_monitor_ && mute)
1472 typing_monitor_->OnChannelMuted();
1473 return ret;
1474}
1475
1476int VoiceChannel::GetInputLevel_w() {
1477 return media_engine()->GetInputLevel();
1478}
1479
1480int VoiceChannel::GetOutputLevel_w() {
1481 return media_channel()->GetOutputLevel();
1482}
1483
1484void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1485 media_channel()->GetActiveStreams(actives);
1486}
1487
1488void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001489 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001490 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001491 int flags) {
1492 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493
1494 // Set a flag when we've received an RTP packet. If we're waiting for early
1495 // media, this will disable the timeout.
1496 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1497 received_media_ = true;
1498 }
1499}
1500
1501void VoiceChannel::ChangeState() {
1502 // Render incoming data if we're the active call, and we have the local
1503 // content. We receive data on the default channel and multiplexed streams.
1504 bool recv = IsReadyToReceive();
1505 if (!media_channel()->SetPlayout(recv)) {
1506 SendLastMediaError();
1507 }
1508
1509 // Send outgoing data if we're the active call, we have the remote content,
1510 // and we have had some form of connectivity.
1511 bool send = IsReadyToSend();
1512 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1513 if (!media_channel()->SetSend(send_flag)) {
1514 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1515 SendLastMediaError();
1516 }
1517
1518 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1519}
1520
1521const ContentInfo* VoiceChannel::GetFirstContent(
1522 const SessionDescription* sdesc) {
1523 return GetFirstAudioContent(sdesc);
1524}
1525
1526bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001527 ContentAction action,
1528 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001529 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 LOG(LS_INFO) << "Setting local voice description";
1531
1532 const AudioContentDescription* audio =
1533 static_cast<const AudioContentDescription*>(content);
1534 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001535 if (!audio) {
1536 SafeSetError("Can't find audio content in local description.", error_desc);
1537 return false;
1538 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001540 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 // Set local audio codecs (what we want to receive).
1542 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1543 // is set properly.
1544 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001545 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1546 SafeSetError("Failed to set audio receive codecs.", error_desc);
1547 ret = false;
1548 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 }
1550
1551 // If everything worked, see if we can start receiving.
1552 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001553 std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1554 for (; it != audio->codecs().end(); ++it) {
1555 bundle_filter()->AddPayloadType(it->id);
1556 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557 ChangeState();
1558 } else {
1559 LOG(LS_WARNING) << "Failed to set local voice description";
1560 }
1561 return ret;
1562}
1563
1564bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001565 ContentAction action,
1566 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001567 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 LOG(LS_INFO) << "Setting remote voice description";
1569
1570 const AudioContentDescription* audio =
1571 static_cast<const AudioContentDescription*>(content);
1572 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001573 if (!audio) {
1574 SafeSetError("Can't find audio content in remote description.", error_desc);
1575 return false;
1576 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577
1578 bool ret = true;
1579 // Set remote video codecs (what the other side wants to receive).
1580 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001581 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1582 SafeSetError("Failed to set audio send codecs.", error_desc);
1583 ret = false;
1584 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585 }
1586
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001587 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588
1589 if (action != CA_UPDATE) {
1590 // Tweak our audio processing settings, if needed.
1591 AudioOptions audio_options;
1592 if (!media_channel()->GetOptions(&audio_options)) {
1593 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1594 } else {
1595 if (audio->conference_mode()) {
1596 audio_options.conference_mode.Set(true);
1597 }
1598 if (audio->agc_minus_10db()) {
1599 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1600 }
1601 if (!media_channel()->SetOptions(audio_options)) {
1602 // Log an error on failure, but don't abort the call.
1603 LOG(LS_ERROR) << "Failed to set voice channel options";
1604 }
1605 }
1606 }
1607
1608 // If everything worked, see if we can start sending.
1609 if (ret) {
1610 ChangeState();
1611 } else {
1612 LOG(LS_WARNING) << "Failed to set remote voice description";
1613 }
1614 return ret;
1615}
1616
1617bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001618 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1620}
1621
1622bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001623 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624 if (play) {
1625 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1626 } else {
1627 LOG(LS_INFO) << "Stopping ringback tone";
1628 }
1629 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1630}
1631
1632void VoiceChannel::HandleEarlyMediaTimeout() {
1633 // This occurs on the main thread, not the worker thread.
1634 if (!received_media_) {
1635 LOG(LS_INFO) << "No early media received before timeout";
1636 SignalEarlyMediaTimeout(this);
1637 }
1638}
1639
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1641 int flags) {
1642 if (!enabled()) {
1643 return false;
1644 }
1645
1646 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1647}
1648
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001650 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1651 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001652}
1653
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001654void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 case MSG_EARLYMEDIATIMEOUT:
1657 HandleEarlyMediaTimeout();
1658 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 case MSG_CHANNEL_ERROR: {
1660 VoiceChannelErrorMessageData* data =
1661 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1662 SignalMediaError(this, data->ssrc, data->error);
1663 delete data;
1664 break;
1665 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 default:
1667 BaseChannel::OnMessage(pmsg);
1668 break;
1669 }
1670}
1671
1672void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001673 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674 SignalConnectionMonitor(this, infos);
1675}
1676
1677void VoiceChannel::OnMediaMonitorUpdate(
1678 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1679 ASSERT(media_channel == this->media_channel());
1680 SignalMediaMonitor(this, info);
1681}
1682
1683void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1684 const AudioInfo& info) {
1685 SignalAudioMonitor(this, info);
1686}
1687
1688void VoiceChannel::OnVoiceChannelError(
1689 uint32 ssrc, VoiceMediaChannel::Error err) {
1690 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1691 ssrc, err);
1692 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1693}
1694
1695void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1696 SrtpFilter::Error error) {
1697 switch (error) {
1698 case SrtpFilter::ERROR_FAIL:
1699 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1700 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1701 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1702 break;
1703 case SrtpFilter::ERROR_AUTH:
1704 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1705 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1706 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1707 break;
1708 case SrtpFilter::ERROR_REPLAY:
1709 // Only receving channel should have this error.
1710 ASSERT(mode == SrtpFilter::UNPROTECT);
1711 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1712 break;
1713 default:
1714 break;
1715 }
1716}
1717
1718void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1719 GetSupportedAudioCryptoSuites(ciphers);
1720}
1721
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001722VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723 MediaEngineInterface* media_engine,
1724 VideoMediaChannel* media_channel,
1725 BaseSession* session,
1726 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001727 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1729 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001731 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732}
1733
1734bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001735 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 return false;
1737 }
1738 media_channel()->SignalMediaError.connect(
1739 this, &VideoChannel::OnVideoChannelError);
1740 srtp_filter()->SignalSrtpError.connect(
1741 this, &VideoChannel::OnSrtpError);
1742 return true;
1743}
1744
1745void VoiceChannel::SendLastMediaError() {
1746 uint32 ssrc;
1747 VoiceMediaChannel::Error error;
1748 media_channel()->GetLastMediaError(&ssrc, &error);
1749 SignalMediaError(this, ssrc, error);
1750}
1751
1752VideoChannel::~VideoChannel() {
1753 std::vector<uint32> screencast_ssrcs;
1754 ScreencastMap::iterator iter;
1755 while (!screencast_capturers_.empty()) {
1756 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1757 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1758 << screencast_capturers_.begin()->first;
1759 ASSERT(false);
1760 break;
1761 }
1762 }
1763
1764 StopMediaMonitor();
1765 // this can't be done in the base class, since it calls a virtual
1766 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001767
1768 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769}
1770
1771bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001772 worker_thread()->Invoke<void>(Bind(
1773 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 return true;
1775}
1776
1777bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001778 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779}
1780
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001781bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1782 return worker_thread()->Invoke<bool>(Bind(
1783 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001784}
1785
1786bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001787 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1788 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789}
1790
1791bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001792 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793}
1794
1795bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001796 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797}
1798
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001799int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001800 ScreencastDetailsData data(ssrc);
1801 worker_thread()->Invoke<void>(Bind(
1802 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001803 return data.fps;
1804}
1805
1806int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001807 ScreencastDetailsData data(ssrc);
1808 worker_thread()->Invoke<void>(Bind(
1809 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001810 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811}
1812
1813bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001814 worker_thread()->Invoke<void>(Bind(
1815 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 return true;
1817}
1818
1819bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001820 worker_thread()->Invoke<void>(Bind(
1821 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 return true;
1823}
1824
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825void VideoChannel::ChangeState() {
1826 // Render incoming data if we're the active call, and we have the local
1827 // content. We receive data on the default channel and multiplexed streams.
1828 bool recv = IsReadyToReceive();
1829 if (!media_channel()->SetRender(recv)) {
1830 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1831 // TODO(gangji): Report error back to server.
1832 }
1833
1834 // Send outgoing data if we're the active call, we have the remote content,
1835 // and we have had some form of connectivity.
1836 bool send = IsReadyToSend();
1837 if (!media_channel()->SetSend(send)) {
1838 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1839 // TODO(gangji): Report error back to server.
1840 }
1841
1842 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1843}
1844
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001845bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1846 return InvokeOnWorker(
1847 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848}
1849
1850void VideoChannel::StartMediaMonitor(int cms) {
1851 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001852 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 media_monitor_->SignalUpdate.connect(
1854 this, &VideoChannel::OnMediaMonitorUpdate);
1855 media_monitor_->Start(cms);
1856}
1857
1858void VideoChannel::StopMediaMonitor() {
1859 if (media_monitor_) {
1860 media_monitor_->Stop();
1861 media_monitor_.reset();
1862 }
1863}
1864
1865const ContentInfo* VideoChannel::GetFirstContent(
1866 const SessionDescription* sdesc) {
1867 return GetFirstVideoContent(sdesc);
1868}
1869
1870bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001871 ContentAction action,
1872 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001873 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 LOG(LS_INFO) << "Setting local video description";
1875
1876 const VideoContentDescription* video =
1877 static_cast<const VideoContentDescription*>(content);
1878 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001879 if (!video) {
1880 SafeSetError("Can't find video content in local description.", error_desc);
1881 return false;
1882 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001884 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 // Set local video codecs (what we want to receive).
1886 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001887 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1888 SafeSetError("Failed to set video receive codecs.", error_desc);
1889 ret = false;
1890 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 }
1892
1893 if (action != CA_UPDATE) {
1894 VideoOptions video_options;
1895 media_channel()->GetOptions(&video_options);
1896 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1897
1898 if (!media_channel()->SetOptions(video_options)) {
1899 // Log an error on failure, but don't abort the call.
1900 LOG(LS_ERROR) << "Failed to set video channel options";
1901 }
1902 }
1903
1904 // If everything worked, see if we can start receiving.
1905 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001906 std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1907 for (; it != video->codecs().end(); ++it) {
1908 bundle_filter()->AddPayloadType(it->id);
1909 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 ChangeState();
1911 } else {
1912 LOG(LS_WARNING) << "Failed to set local video description";
1913 }
1914 return ret;
1915}
1916
1917bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001918 ContentAction action,
1919 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001920 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 LOG(LS_INFO) << "Setting remote video description";
1922
1923 const VideoContentDescription* video =
1924 static_cast<const VideoContentDescription*>(content);
1925 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001926 if (!video) {
1927 SafeSetError("Can't find video content in remote description.", error_desc);
1928 return false;
1929 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930
1931 bool ret = true;
1932 // Set remote video codecs (what the other side wants to receive).
1933 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001934 if (!media_channel()->SetSendCodecs(video->codecs())) {
1935 SafeSetError("Failed to set video send codecs.", error_desc);
1936 ret = false;
1937 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938 }
1939
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001940 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941
1942 if (action != CA_UPDATE) {
1943 // Tweak our video processing settings, if needed.
1944 VideoOptions video_options;
1945 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001946 if (video->conference_mode()) {
1947 video_options.conference_mode.Set(true);
1948 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1950
1951 if (!media_channel()->SetOptions(video_options)) {
1952 // Log an error on failure, but don't abort the call.
1953 LOG(LS_ERROR) << "Failed to set video channel options";
1954 }
1955 }
1956
1957 // If everything worked, see if we can start sending.
1958 if (ret) {
1959 ChangeState();
1960 } else {
1961 LOG(LS_WARNING) << "Failed to set remote video description";
1962 }
1963 return ret;
1964}
1965
1966bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1967 bool ret = true;
1968 // Set the send format for each of the local streams. If the view request
1969 // does not contain a local stream, set its send format to 0x0, which will
1970 // drop all frames.
1971 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1972 it != local_streams().end(); ++it) {
1973 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1974 StaticVideoViews::const_iterator view;
1975 for (view = request.static_video_views.begin();
1976 view != request.static_video_views.end(); ++view) {
1977 if (view->selector.Matches(*it)) {
1978 format.width = view->width;
1979 format.height = view->height;
1980 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1981 break;
1982 }
1983 }
1984
1985 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1986 }
1987
1988 // Check if the view request has invalid streams.
1989 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1990 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001991 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 LOG(LS_WARNING) << "View request for ("
1993 << it->selector.ssrc << ", '"
1994 << it->selector.groupid << "', '"
1995 << it->selector.streamid << "'"
1996 << ") is not in the local streams.";
1997 }
1998 }
1999
2000 return ret;
2001}
2002
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002003bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002005 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002007 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
2008 screencast_capturers_[ssrc] = capturer;
2009 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010}
2011
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
2013 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
2014 if (iter == screencast_capturers_.end()) {
2015 return false;
2016 }
2017 // Clean up VideoCapturer.
2018 delete iter->second;
2019 screencast_capturers_.erase(iter);
2020 return true;
2021}
2022
2023bool VideoChannel::IsScreencasting_w() const {
2024 return !screencast_capturers_.empty();
2025}
2026
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002027void VideoChannel::GetScreencastDetails_w(
2028 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002029 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002031 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 }
2033 VideoCapturer* capturer = iter->second;
2034 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002035 data->fps = VideoFormat::IntervalToFps(video_format->interval);
2036 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037}
2038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002040 rtc::WindowEvent we) {
2041 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 SignalScreencastWindowEvent(ssrc, we);
2043}
2044
2045bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002046 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
2047 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048}
2049
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002050void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 case MSG_SCREENCASTWINDOWEVENT: {
2053 const ScreencastEventMessageData* data =
2054 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
2055 OnScreencastWindowEvent_s(data->ssrc, data->event);
2056 delete data;
2057 break;
2058 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 case MSG_CHANNEL_ERROR: {
2060 const VideoChannelErrorMessageData* data =
2061 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
2062 SignalMediaError(this, data->ssrc, data->error);
2063 delete data;
2064 break;
2065 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 default:
2067 BaseChannel::OnMessage(pmsg);
2068 break;
2069 }
2070}
2071
2072void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002073 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 SignalConnectionMonitor(this, infos);
2075}
2076
2077// TODO(pthatcher): Look into removing duplicate code between
2078// audio, video, and data, perhaps by using templates.
2079void VideoChannel::OnMediaMonitorUpdate(
2080 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2081 ASSERT(media_channel == this->media_channel());
2082 SignalMediaMonitor(this, info);
2083}
2084
2085void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002086 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002087 ScreencastEventMessageData* pdata =
2088 new ScreencastEventMessageData(ssrc, event);
2089 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2090}
2091
2092void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2093 // Map capturer events to window events. In the future we may want to simply
2094 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002095 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002097 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002099 we = rtc::WE_MINIMIZE;
2100 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2101 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102 } else {
2103 return;
2104 }
2105 previous_we_ = we;
2106
2107 uint32 ssrc = 0;
2108 if (!GetLocalSsrc(capturer, &ssrc)) {
2109 return;
2110 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002111
2112 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113}
2114
2115bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2116 *ssrc = 0;
2117 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2118 iter != screencast_capturers_.end(); ++iter) {
2119 if (iter->second == capturer) {
2120 *ssrc = iter->first;
2121 return true;
2122 }
2123 }
2124 return false;
2125}
2126
2127void VideoChannel::OnVideoChannelError(uint32 ssrc,
2128 VideoMediaChannel::Error error) {
2129 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2130 ssrc, error);
2131 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2132}
2133
2134void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2135 SrtpFilter::Error error) {
2136 switch (error) {
2137 case SrtpFilter::ERROR_FAIL:
2138 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2139 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2140 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2141 break;
2142 case SrtpFilter::ERROR_AUTH:
2143 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2144 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2145 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2146 break;
2147 case SrtpFilter::ERROR_REPLAY:
2148 // Only receving channel should have this error.
2149 ASSERT(mode == SrtpFilter::UNPROTECT);
2150 // TODO(gangji): Turn on the signaling of replay error once we have
2151 // switched to the new mechanism for doing video retransmissions.
2152 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2153 break;
2154 default:
2155 break;
2156 }
2157}
2158
2159
2160void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2161 GetSupportedVideoCryptoSuites(ciphers);
2162}
2163
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002164DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165 DataMediaChannel* media_channel,
2166 BaseSession* session,
2167 const std::string& content_name,
2168 bool rtcp)
2169 // MediaEngine is NULL
2170 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002171 data_channel_type_(cricket::DCT_NONE),
2172 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173}
2174
2175DataChannel::~DataChannel() {
2176 StopMediaMonitor();
2177 // this can't be done in the base class, since it calls a virtual
2178 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002179
2180 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181}
2182
2183bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002184 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185 return false;
2186 }
2187 media_channel()->SignalDataReceived.connect(
2188 this, &DataChannel::OnDataReceived);
2189 media_channel()->SignalMediaError.connect(
2190 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002191 media_channel()->SignalReadyToSend.connect(
2192 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002193 media_channel()->SignalStreamClosedRemotely.connect(
2194 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 srtp_filter()->SignalSrtpError.connect(
2196 this, &DataChannel::OnSrtpError);
2197 return true;
2198}
2199
2200bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002201 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002203 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2204 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205}
2206
2207const ContentInfo* DataChannel::GetFirstContent(
2208 const SessionDescription* sdesc) {
2209 return GetFirstDataContent(sdesc);
2210}
2211
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002212bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 if (data_channel_type_ == DCT_SCTP) {
2214 // TODO(pthatcher): Do this in a more robust way by checking for
2215 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002216 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 } else if (data_channel_type_ == DCT_RTP) {
2218 return BaseChannel::WantsPacket(rtcp, packet);
2219 }
2220 return false;
2221}
2222
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002223bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2224 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 // It hasn't been set before, so set it now.
2226 if (data_channel_type_ == DCT_NONE) {
2227 data_channel_type_ = new_data_channel_type;
2228 return true;
2229 }
2230
2231 // It's been set before, but doesn't match. That's bad.
2232 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002233 std::ostringstream desc;
2234 desc << "Data channel type mismatch."
2235 << " Expected " << data_channel_type_
2236 << " Got " << new_data_channel_type;
2237 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002238 return false;
2239 }
2240
2241 // It's hasn't changed. Nothing to do.
2242 return true;
2243}
2244
2245bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002246 const DataContentDescription* content,
2247 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2249 (content->protocol() == kMediaProtocolDtlsSctp));
2250 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002251 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252}
2253
2254bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002255 ContentAction action,
2256 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002257 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002258 LOG(LS_INFO) << "Setting local data description";
2259
2260 const DataContentDescription* data =
2261 static_cast<const DataContentDescription*>(content);
2262 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002263 if (!data) {
2264 SafeSetError("Can't find data content in local description.", error_desc);
2265 return false;
2266 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267
2268 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002269 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270 return false;
2271 }
2272
2273 if (data_channel_type_ == DCT_SCTP) {
2274 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002275 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 if (ret) {
2277 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002278 // As in SetRemoteContent_w, make sure we set the local SCTP port
2279 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002280 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2281 SafeSetError("Failed to set data receive codecs.", error_desc);
2282 ret = false;
2283 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284 }
2285 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002286 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002288 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2289 SafeSetError("Failed to set data receive codecs.", error_desc);
2290 ret = false;
2291 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 }
2293 }
2294
2295 // If everything worked, see if we can start receiving.
2296 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00002297 std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2298 for (; it != data->codecs().end(); ++it) {
2299 bundle_filter()->AddPayloadType(it->id);
2300 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301 ChangeState();
2302 } else {
2303 LOG(LS_WARNING) << "Failed to set local data description";
2304 }
2305 return ret;
2306}
2307
2308bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002309 ContentAction action,
2310 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002311 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312
2313 const DataContentDescription* data =
2314 static_cast<const DataContentDescription*>(content);
2315 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002316 if (!data) {
2317 SafeSetError("Can't find data content in remote description.", error_desc);
2318 return false;
2319 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320
2321 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002322 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 return false;
2324 }
2325
2326 if (data_channel_type_ == DCT_SCTP) {
2327 LOG(LS_INFO) << "Setting SCTP remote data description";
2328 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002329 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330 if (ret) {
2331 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002332 // We send the SCTP port number (not to be confused with the underlying
2333 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002334 if (!media_channel()->SetSendCodecs(data->codecs())) {
2335 SafeSetError("Failed to set data send codecs.", error_desc);
2336 ret = false;
2337 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002338 }
2339 } else {
2340 // If the remote data doesn't have codecs and isn't an update, it
2341 // must be empty, so ignore it.
2342 if (action != CA_UPDATE && !data->has_codecs()) {
2343 return true;
2344 }
2345 LOG(LS_INFO) << "Setting remote data description";
2346
2347 // Set remote video codecs (what the other side wants to receive).
2348 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002349 if (!media_channel()->SetSendCodecs(data->codecs())) {
2350 SafeSetError("Failed to set data send codecs.", error_desc);
2351 ret = false;
2352 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353 }
2354
2355 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002356 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002357 }
2358
2359 if (action != CA_UPDATE) {
2360 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002361 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2362 std::ostringstream desc;
2363 desc << "Failed to set max send bandwidth for data content.";
2364 SafeSetError(desc.str(), error_desc);
2365 ret = false;
2366 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367 }
2368 }
2369
2370 // If everything worked, see if we can start sending.
2371 if (ret) {
2372 ChangeState();
2373 } else {
2374 LOG(LS_WARNING) << "Failed to set remote data description";
2375 }
2376 return ret;
2377}
2378
2379void DataChannel::ChangeState() {
2380 // Render incoming data if we're the active call, and we have the local
2381 // content. We receive data on the default channel and multiplexed streams.
2382 bool recv = IsReadyToReceive();
2383 if (!media_channel()->SetReceive(recv)) {
2384 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2385 }
2386
2387 // Send outgoing data if we're the active call, we have the remote content,
2388 // and we have had some form of connectivity.
2389 bool send = IsReadyToSend();
2390 if (!media_channel()->SetSend(send)) {
2391 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2392 }
2393
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002394 // Trigger SignalReadyToSendData asynchronously.
2395 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002396
2397 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2398}
2399
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002400void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002401 switch (pmsg->message_id) {
2402 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002403 DataChannelReadyToSendMessageData* data =
2404 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002405 ready_to_send_data_ = data->data();
2406 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407 delete data;
2408 break;
2409 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 case MSG_DATARECEIVED: {
2411 DataReceivedMessageData* data =
2412 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2413 SignalDataReceived(this, data->params, data->payload);
2414 delete data;
2415 break;
2416 }
2417 case MSG_CHANNEL_ERROR: {
2418 const DataChannelErrorMessageData* data =
2419 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2420 SignalMediaError(this, data->ssrc, data->error);
2421 delete data;
2422 break;
2423 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002424 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002425 rtc::TypedMessageData<uint32>* data =
2426 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002427 SignalStreamClosedRemotely(data->data());
2428 delete data;
2429 break;
2430 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002431 default:
2432 BaseChannel::OnMessage(pmsg);
2433 break;
2434 }
2435}
2436
2437void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002438 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002439 SignalConnectionMonitor(this, infos);
2440}
2441
2442void DataChannel::StartMediaMonitor(int cms) {
2443 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002444 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002445 media_monitor_->SignalUpdate.connect(
2446 this, &DataChannel::OnMediaMonitorUpdate);
2447 media_monitor_->Start(cms);
2448}
2449
2450void DataChannel::StopMediaMonitor() {
2451 if (media_monitor_) {
2452 media_monitor_->Stop();
2453 media_monitor_->SignalUpdate.disconnect(this);
2454 media_monitor_.reset();
2455 }
2456}
2457
2458void DataChannel::OnMediaMonitorUpdate(
2459 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2460 ASSERT(media_channel == this->media_channel());
2461 SignalMediaMonitor(this, info);
2462}
2463
2464void DataChannel::OnDataReceived(
2465 const ReceiveDataParams& params, const char* data, size_t len) {
2466 DataReceivedMessageData* msg = new DataReceivedMessageData(
2467 params, data, len);
2468 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2469}
2470
2471void DataChannel::OnDataChannelError(
2472 uint32 ssrc, DataMediaChannel::Error err) {
2473 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2474 ssrc, err);
2475 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2476}
2477
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002478void DataChannel::OnDataChannelReadyToSend(bool writable) {
2479 // This is usded for congestion control to indicate that the stream is ready
2480 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2481 // that the transport channel is ready.
2482 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2483 new DataChannelReadyToSendMessageData(writable));
2484}
2485
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002486void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2487 SrtpFilter::Error error) {
2488 switch (error) {
2489 case SrtpFilter::ERROR_FAIL:
2490 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2491 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2492 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2493 break;
2494 case SrtpFilter::ERROR_AUTH:
2495 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2496 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2497 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2498 break;
2499 case SrtpFilter::ERROR_REPLAY:
2500 // Only receving channel should have this error.
2501 ASSERT(mode == SrtpFilter::UNPROTECT);
2502 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2503 break;
2504 default:
2505 break;
2506 }
2507}
2508
2509void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2510 GetSupportedDataCryptoSuites(ciphers);
2511}
2512
2513bool DataChannel::ShouldSetupDtlsSrtp() const {
2514 return (data_channel_type_ == DCT_RTP);
2515}
2516
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002517void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002518 rtc::TypedMessageData<uint32>* message =
2519 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002520 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2521}
2522
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002523} // namespace cricket