blob: 0750537eab58da6686a55bedc55aa053de1604ca [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;
481 packet->TransferTo(&data->packet);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000482 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 worker_thread_->Post(this, message_id, data);
484 return true;
485 }
486
487 // Now that we are on the correct thread, ensure we have a place to send this
488 // packet before doing anything. (We might get RTCP packets that we don't
489 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
490 // transport.
491 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
492 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000493 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 return false;
495 }
496
497 // Protect ourselves against crazy data.
498 if (!ValidPacket(rtcp, packet)) {
499 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
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;
515 char* 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 =
587 channel->SendPacket(packet->data(), packet->size(), options,
588 (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.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000609 return bundle_filter_.DemuxPacket(packet->data(), 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()) {
633 char* data = packet->data();
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;
1066 default:
1067 break;
1068 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001069 if (!ret) {
1070 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1071 return false;
1072 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1074 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1075 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001076 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 // If the RTP transport is already writable, then so are we.
1078 if (transport_channel_->writable()) {
1079 ChannelWritable_w();
1080 }
1081 }
1082
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001083 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084}
1085
1086bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001087 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088 if (!media_channel()->AddRecvStream(sp))
1089 return false;
1090
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001091 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092}
1093
1094bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001095 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001096 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 return media_channel()->RemoveRecvStream(ssrc);
1098}
1099
1100bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001101 ContentAction action,
1102 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1104 action == CA_PRANSWER || action == CA_UPDATE))
1105 return false;
1106
1107 // If this is an update, streams only contain streams that have changed.
1108 if (action == CA_UPDATE) {
1109 for (StreamParamsVec::const_iterator it = streams.begin();
1110 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001111 const StreamParams* existing_stream =
1112 GetStreamByIds(local_streams_, it->groupid, it->id);
1113 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114 if (media_channel()->AddSendStream(*it)) {
1115 local_streams_.push_back(*it);
1116 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1117 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001118 std::ostringstream desc;
1119 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1120 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121 return false;
1122 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001123 } else if (existing_stream && !it->has_ssrcs()) {
1124 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001125 std::ostringstream desc;
1126 desc << "Failed to remove send stream with ssrc "
1127 << it->first_ssrc() << ".";
1128 SafeSetError(desc.str(), error_desc);
1129 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001131 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132 } else {
1133 LOG(LS_WARNING) << "Ignore unsupported stream update";
1134 }
1135 }
1136 return true;
1137 }
1138 // Else streams are all the streams we want to send.
1139
1140 // Check for streams that have been removed.
1141 bool ret = true;
1142 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1143 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001144 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001146 std::ostringstream desc;
1147 desc << "Failed to remove send stream with ssrc "
1148 << it->first_ssrc() << ".";
1149 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 ret = false;
1151 }
1152 }
1153 }
1154 // Check for new streams.
1155 for (StreamParamsVec::const_iterator it = streams.begin();
1156 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001157 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 if (media_channel()->AddSendStream(*it)) {
1159 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1160 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001161 std::ostringstream desc;
1162 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1163 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 ret = false;
1165 }
1166 }
1167 }
1168 local_streams_ = streams;
1169 return ret;
1170}
1171
1172bool BaseChannel::UpdateRemoteStreams_w(
1173 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001174 ContentAction action,
1175 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1177 action == CA_PRANSWER || action == CA_UPDATE))
1178 return false;
1179
1180 // If this is an update, streams only contain streams that have changed.
1181 if (action == CA_UPDATE) {
1182 for (StreamParamsVec::const_iterator it = streams.begin();
1183 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001184 const StreamParams* existing_stream =
1185 GetStreamByIds(remote_streams_, it->groupid, it->id);
1186 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 if (AddRecvStream_w(*it)) {
1188 remote_streams_.push_back(*it);
1189 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1190 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001191 std::ostringstream desc;
1192 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1193 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 return false;
1195 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001196 } else if (existing_stream && !it->has_ssrcs()) {
1197 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001198 std::ostringstream desc;
1199 desc << "Failed to remove remote stream with ssrc "
1200 << it->first_ssrc() << ".";
1201 SafeSetError(desc.str(), error_desc);
1202 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001204 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 } else {
1206 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001207 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 << " new stream = " << it->ToString();
1209 }
1210 }
1211 return true;
1212 }
1213 // Else streams are all the streams we want to receive.
1214
1215 // Check for streams that have been removed.
1216 bool ret = true;
1217 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1218 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001219 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001221 std::ostringstream desc;
1222 desc << "Failed to remove remote stream with ssrc "
1223 << it->first_ssrc() << ".";
1224 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 ret = false;
1226 }
1227 }
1228 }
1229 // Check for new streams.
1230 for (StreamParamsVec::const_iterator it = streams.begin();
1231 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001232 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233 if (AddRecvStream_w(*it)) {
1234 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1235 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001236 std::ostringstream desc;
1237 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1238 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 ret = false;
1240 }
1241 }
1242 }
1243 remote_streams_ = streams;
1244 return ret;
1245}
1246
1247bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001248 ContentAction action,
1249 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250 // Cache secure_required_ for belt and suspenders check on SendPacket
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001251 secure_required_ = content->crypto_required() != CT_NONE;
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001252 // Set local RTP header extensions.
1253 bool ret = SetRecvRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001255 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001257 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001258
1259 // Call UpdateLocalStreams_w last to make sure as many settings as possible
1260 // are already set when creating streams.
1261 ret &= UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 set_local_content_direction(content->direction());
1263 return ret;
1264}
1265
1266bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001267 ContentAction action,
1268 std::string* error_desc) {
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001269 // Set remote RTP header extensions.
1270 bool ret = SetSendRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001272 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001274 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001275 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1276 std::ostringstream desc;
1277 desc << "Failed to set max send bandwidth for "
1278 << MediaTypeToString(content->type()) << " content.";
1279 SafeSetError(desc.str(), error_desc);
1280 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281 }
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001282
1283 // Call UpdateRemoteStreams_w last to make sure as many settings as possible
1284 // are already set when creating streams.
1285 ret &= UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001286 set_remote_content_direction(content->direction());
1287 return ret;
1288}
1289
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001290void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1291 const std::vector<RtpHeaderExtension>& extensions) {
1292 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001293 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001294 rtp_abs_sendtime_extn_id_ =
1295 send_time_extension ? send_time_extension->id : -1;
1296}
1297
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001298void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 case MSG_RTPPACKET:
1301 case MSG_RTCPPACKET: {
1302 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001303 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 delete data; // because it is Posted
1305 break;
1306 }
1307 case MSG_FIRSTPACKETRECEIVED: {
1308 SignalFirstPacketReceived(this);
1309 break;
1310 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 }
1312}
1313
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314void BaseChannel::FlushRtcpMessages() {
1315 // Flush all remaining RTCP messages. This should only be called in
1316 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001317 ASSERT(rtc::Thread::Current() == worker_thread_);
1318 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001319 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001320 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001322 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 }
1324}
1325
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001326VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327 MediaEngineInterface* media_engine,
1328 VoiceMediaChannel* media_channel,
1329 BaseSession* session,
1330 const std::string& content_name,
1331 bool rtcp)
1332 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1333 rtcp),
1334 received_media_(false) {
1335}
1336
1337VoiceChannel::~VoiceChannel() {
1338 StopAudioMonitor();
1339 StopMediaMonitor();
1340 // this can't be done in the base class, since it calls a virtual
1341 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001342 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343}
1344
1345bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001346 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347 return false;
1348 }
1349 media_channel()->SignalMediaError.connect(
1350 this, &VoiceChannel::OnVoiceChannelError);
1351 srtp_filter()->SignalSrtpError.connect(
1352 this, &VoiceChannel::OnSrtpError);
1353 return true;
1354}
1355
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001356bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001357 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1358 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001359}
1360
1361bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001362 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1363 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364}
1365
1366bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001367 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368}
1369
1370// TODO(juberti): Handle early media the right way. We should get an explicit
1371// ringing message telling us to start playing local ringback, which we cancel
1372// if any early media actually arrives. For now, we do the opposite, which is
1373// to wait 1 second for early media, and start playing local ringback if none
1374// arrives.
1375void VoiceChannel::SetEarlyMedia(bool enable) {
1376 if (enable) {
1377 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001378 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1379 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380 } else {
1381 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001382 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383 }
1384}
1385
1386bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001387 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1388 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001389}
1390
1391bool VoiceChannel::PressDTMF(int digit, bool playout) {
1392 int flags = DF_SEND;
1393 if (playout) {
1394 flags |= DF_PLAY;
1395 }
1396 int duration_ms = 160;
1397 return InsertDtmf(0, digit, duration_ms, flags);
1398}
1399
1400bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001401 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1402 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001403}
1404
1405bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1406 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001407 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1408 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409}
1410
1411bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001412 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1413 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001415
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001417 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1418 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419}
1420
1421void VoiceChannel::StartMediaMonitor(int cms) {
1422 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001423 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424 media_monitor_->SignalUpdate.connect(
1425 this, &VoiceChannel::OnMediaMonitorUpdate);
1426 media_monitor_->Start(cms);
1427}
1428
1429void VoiceChannel::StopMediaMonitor() {
1430 if (media_monitor_) {
1431 media_monitor_->Stop();
1432 media_monitor_->SignalUpdate.disconnect(this);
1433 media_monitor_.reset();
1434 }
1435}
1436
1437void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001438 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 audio_monitor_
1440 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1441 audio_monitor_->Start(cms);
1442}
1443
1444void VoiceChannel::StopAudioMonitor() {
1445 if (audio_monitor_) {
1446 audio_monitor_->Stop();
1447 audio_monitor_.reset();
1448 }
1449}
1450
1451bool VoiceChannel::IsAudioMonitorRunning() const {
1452 return (audio_monitor_.get() != NULL);
1453}
1454
1455void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1456 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1457 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1458}
1459
1460void VoiceChannel::StopTypingMonitor() {
1461 typing_monitor_.reset();
1462}
1463
1464bool VoiceChannel::IsTypingMonitorRunning() const {
1465 return typing_monitor_;
1466}
1467
1468bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1469 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1470 if (typing_monitor_ && mute)
1471 typing_monitor_->OnChannelMuted();
1472 return ret;
1473}
1474
1475int VoiceChannel::GetInputLevel_w() {
1476 return media_engine()->GetInputLevel();
1477}
1478
1479int VoiceChannel::GetOutputLevel_w() {
1480 return media_channel()->GetOutputLevel();
1481}
1482
1483void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1484 media_channel()->GetActiveStreams(actives);
1485}
1486
1487void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001488 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001489 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001490 int flags) {
1491 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492
1493 // Set a flag when we've received an RTP packet. If we're waiting for early
1494 // media, this will disable the timeout.
1495 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1496 received_media_ = true;
1497 }
1498}
1499
1500void VoiceChannel::ChangeState() {
1501 // Render incoming data if we're the active call, and we have the local
1502 // content. We receive data on the default channel and multiplexed streams.
1503 bool recv = IsReadyToReceive();
1504 if (!media_channel()->SetPlayout(recv)) {
1505 SendLastMediaError();
1506 }
1507
1508 // Send outgoing data if we're the active call, we have the remote content,
1509 // and we have had some form of connectivity.
1510 bool send = IsReadyToSend();
1511 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1512 if (!media_channel()->SetSend(send_flag)) {
1513 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1514 SendLastMediaError();
1515 }
1516
1517 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1518}
1519
1520const ContentInfo* VoiceChannel::GetFirstContent(
1521 const SessionDescription* sdesc) {
1522 return GetFirstAudioContent(sdesc);
1523}
1524
1525bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001526 ContentAction action,
1527 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001528 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529 LOG(LS_INFO) << "Setting local voice description";
1530
1531 const AudioContentDescription* audio =
1532 static_cast<const AudioContentDescription*>(content);
1533 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001534 if (!audio) {
1535 SafeSetError("Can't find audio content in local description.", error_desc);
1536 return false;
1537 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001539 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 // Set local audio codecs (what we want to receive).
1541 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1542 // is set properly.
1543 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001544 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1545 SafeSetError("Failed to set audio receive codecs.", error_desc);
1546 ret = false;
1547 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548 }
1549
1550 // If everything worked, see if we can start receiving.
1551 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001552 std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1553 for (; it != audio->codecs().end(); ++it) {
1554 bundle_filter()->AddPayloadType(it->id);
1555 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556 ChangeState();
1557 } else {
1558 LOG(LS_WARNING) << "Failed to set local voice description";
1559 }
1560 return ret;
1561}
1562
1563bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001564 ContentAction action,
1565 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001566 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 LOG(LS_INFO) << "Setting remote voice description";
1568
1569 const AudioContentDescription* audio =
1570 static_cast<const AudioContentDescription*>(content);
1571 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001572 if (!audio) {
1573 SafeSetError("Can't find audio content in remote description.", error_desc);
1574 return false;
1575 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576
1577 bool ret = true;
1578 // Set remote video codecs (what the other side wants to receive).
1579 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001580 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1581 SafeSetError("Failed to set audio send codecs.", error_desc);
1582 ret = false;
1583 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 }
1585
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001586 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587
1588 if (action != CA_UPDATE) {
1589 // Tweak our audio processing settings, if needed.
1590 AudioOptions audio_options;
1591 if (!media_channel()->GetOptions(&audio_options)) {
1592 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1593 } else {
1594 if (audio->conference_mode()) {
1595 audio_options.conference_mode.Set(true);
1596 }
1597 if (audio->agc_minus_10db()) {
1598 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1599 }
1600 if (!media_channel()->SetOptions(audio_options)) {
1601 // Log an error on failure, but don't abort the call.
1602 LOG(LS_ERROR) << "Failed to set voice channel options";
1603 }
1604 }
1605 }
1606
1607 // If everything worked, see if we can start sending.
1608 if (ret) {
1609 ChangeState();
1610 } else {
1611 LOG(LS_WARNING) << "Failed to set remote voice description";
1612 }
1613 return ret;
1614}
1615
1616bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001617 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1619}
1620
1621bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001622 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 if (play) {
1624 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1625 } else {
1626 LOG(LS_INFO) << "Stopping ringback tone";
1627 }
1628 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1629}
1630
1631void VoiceChannel::HandleEarlyMediaTimeout() {
1632 // This occurs on the main thread, not the worker thread.
1633 if (!received_media_) {
1634 LOG(LS_INFO) << "No early media received before timeout";
1635 SignalEarlyMediaTimeout(this);
1636 }
1637}
1638
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1640 int flags) {
1641 if (!enabled()) {
1642 return false;
1643 }
1644
1645 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1646}
1647
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001649 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1650 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651}
1652
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001653void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 case MSG_EARLYMEDIATIMEOUT:
1656 HandleEarlyMediaTimeout();
1657 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 case MSG_CHANNEL_ERROR: {
1659 VoiceChannelErrorMessageData* data =
1660 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1661 SignalMediaError(this, data->ssrc, data->error);
1662 delete data;
1663 break;
1664 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 default:
1666 BaseChannel::OnMessage(pmsg);
1667 break;
1668 }
1669}
1670
1671void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001672 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 SignalConnectionMonitor(this, infos);
1674}
1675
1676void VoiceChannel::OnMediaMonitorUpdate(
1677 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1678 ASSERT(media_channel == this->media_channel());
1679 SignalMediaMonitor(this, info);
1680}
1681
1682void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1683 const AudioInfo& info) {
1684 SignalAudioMonitor(this, info);
1685}
1686
1687void VoiceChannel::OnVoiceChannelError(
1688 uint32 ssrc, VoiceMediaChannel::Error err) {
1689 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1690 ssrc, err);
1691 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1692}
1693
1694void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1695 SrtpFilter::Error error) {
1696 switch (error) {
1697 case SrtpFilter::ERROR_FAIL:
1698 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1699 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1700 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1701 break;
1702 case SrtpFilter::ERROR_AUTH:
1703 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1704 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1705 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1706 break;
1707 case SrtpFilter::ERROR_REPLAY:
1708 // Only receving channel should have this error.
1709 ASSERT(mode == SrtpFilter::UNPROTECT);
1710 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1711 break;
1712 default:
1713 break;
1714 }
1715}
1716
1717void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1718 GetSupportedAudioCryptoSuites(ciphers);
1719}
1720
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001721VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 MediaEngineInterface* media_engine,
1723 VideoMediaChannel* media_channel,
1724 BaseSession* session,
1725 const std::string& content_name,
1726 bool rtcp,
1727 VoiceChannel* voice_channel)
1728 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1729 rtcp),
1730 voice_channel_(voice_channel),
1731 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001732 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733}
1734
1735bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001736 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 return false;
1738 }
1739 media_channel()->SignalMediaError.connect(
1740 this, &VideoChannel::OnVideoChannelError);
1741 srtp_filter()->SignalSrtpError.connect(
1742 this, &VideoChannel::OnSrtpError);
1743 return true;
1744}
1745
1746void VoiceChannel::SendLastMediaError() {
1747 uint32 ssrc;
1748 VoiceMediaChannel::Error error;
1749 media_channel()->GetLastMediaError(&ssrc, &error);
1750 SignalMediaError(this, ssrc, error);
1751}
1752
1753VideoChannel::~VideoChannel() {
1754 std::vector<uint32> screencast_ssrcs;
1755 ScreencastMap::iterator iter;
1756 while (!screencast_capturers_.empty()) {
1757 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1758 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1759 << screencast_capturers_.begin()->first;
1760 ASSERT(false);
1761 break;
1762 }
1763 }
1764
1765 StopMediaMonitor();
1766 // this can't be done in the base class, since it calls a virtual
1767 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001768
1769 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770}
1771
1772bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001773 worker_thread()->Invoke<void>(Bind(
1774 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001775 return true;
1776}
1777
1778bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001779 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780}
1781
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001782bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1783 return worker_thread()->Invoke<bool>(Bind(
1784 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785}
1786
1787bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001788 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1789 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790}
1791
1792bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001793 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794}
1795
1796bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001797 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798}
1799
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001800int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001801 ScreencastDetailsData data(ssrc);
1802 worker_thread()->Invoke<void>(Bind(
1803 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001804 return data.fps;
1805}
1806
1807int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001808 ScreencastDetailsData data(ssrc);
1809 worker_thread()->Invoke<void>(Bind(
1810 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001811 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812}
1813
1814bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001815 worker_thread()->Invoke<void>(Bind(
1816 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 return true;
1818}
1819
1820bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001821 worker_thread()->Invoke<void>(Bind(
1822 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 return true;
1824}
1825
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826void VideoChannel::ChangeState() {
1827 // Render incoming data if we're the active call, and we have the local
1828 // content. We receive data on the default channel and multiplexed streams.
1829 bool recv = IsReadyToReceive();
1830 if (!media_channel()->SetRender(recv)) {
1831 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1832 // TODO(gangji): Report error back to server.
1833 }
1834
1835 // Send outgoing data if we're the active call, we have the remote content,
1836 // and we have had some form of connectivity.
1837 bool send = IsReadyToSend();
1838 if (!media_channel()->SetSend(send)) {
1839 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1840 // TODO(gangji): Report error back to server.
1841 }
1842
1843 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1844}
1845
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001846bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1847 return InvokeOnWorker(
1848 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849}
1850
1851void VideoChannel::StartMediaMonitor(int cms) {
1852 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001853 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 media_monitor_->SignalUpdate.connect(
1855 this, &VideoChannel::OnMediaMonitorUpdate);
1856 media_monitor_->Start(cms);
1857}
1858
1859void VideoChannel::StopMediaMonitor() {
1860 if (media_monitor_) {
1861 media_monitor_->Stop();
1862 media_monitor_.reset();
1863 }
1864}
1865
1866const ContentInfo* VideoChannel::GetFirstContent(
1867 const SessionDescription* sdesc) {
1868 return GetFirstVideoContent(sdesc);
1869}
1870
1871bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001872 ContentAction action,
1873 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001874 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 LOG(LS_INFO) << "Setting local video description";
1876
1877 const VideoContentDescription* video =
1878 static_cast<const VideoContentDescription*>(content);
1879 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001880 if (!video) {
1881 SafeSetError("Can't find video content in local description.", error_desc);
1882 return false;
1883 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001885 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 // Set local video codecs (what we want to receive).
1887 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001888 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1889 SafeSetError("Failed to set video receive codecs.", error_desc);
1890 ret = false;
1891 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 }
1893
1894 if (action != CA_UPDATE) {
1895 VideoOptions video_options;
1896 media_channel()->GetOptions(&video_options);
1897 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1898
1899 if (!media_channel()->SetOptions(video_options)) {
1900 // Log an error on failure, but don't abort the call.
1901 LOG(LS_ERROR) << "Failed to set video channel options";
1902 }
1903 }
1904
1905 // If everything worked, see if we can start receiving.
1906 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001907 std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1908 for (; it != video->codecs().end(); ++it) {
1909 bundle_filter()->AddPayloadType(it->id);
1910 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 ChangeState();
1912 } else {
1913 LOG(LS_WARNING) << "Failed to set local video description";
1914 }
1915 return ret;
1916}
1917
1918bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001919 ContentAction action,
1920 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001921 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922 LOG(LS_INFO) << "Setting remote video description";
1923
1924 const VideoContentDescription* video =
1925 static_cast<const VideoContentDescription*>(content);
1926 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001927 if (!video) {
1928 SafeSetError("Can't find video content in remote description.", error_desc);
1929 return false;
1930 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931
1932 bool ret = true;
1933 // Set remote video codecs (what the other side wants to receive).
1934 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001935 if (!media_channel()->SetSendCodecs(video->codecs())) {
1936 SafeSetError("Failed to set video send codecs.", error_desc);
1937 ret = false;
1938 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 }
1940
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001941 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942
1943 if (action != CA_UPDATE) {
1944 // Tweak our video processing settings, if needed.
1945 VideoOptions video_options;
1946 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001947 if (video->conference_mode()) {
1948 video_options.conference_mode.Set(true);
1949 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1951
1952 if (!media_channel()->SetOptions(video_options)) {
1953 // Log an error on failure, but don't abort the call.
1954 LOG(LS_ERROR) << "Failed to set video channel options";
1955 }
1956 }
1957
1958 // If everything worked, see if we can start sending.
1959 if (ret) {
1960 ChangeState();
1961 } else {
1962 LOG(LS_WARNING) << "Failed to set remote video description";
1963 }
1964 return ret;
1965}
1966
1967bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1968 bool ret = true;
1969 // Set the send format for each of the local streams. If the view request
1970 // does not contain a local stream, set its send format to 0x0, which will
1971 // drop all frames.
1972 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1973 it != local_streams().end(); ++it) {
1974 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1975 StaticVideoViews::const_iterator view;
1976 for (view = request.static_video_views.begin();
1977 view != request.static_video_views.end(); ++view) {
1978 if (view->selector.Matches(*it)) {
1979 format.width = view->width;
1980 format.height = view->height;
1981 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1982 break;
1983 }
1984 }
1985
1986 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1987 }
1988
1989 // Check if the view request has invalid streams.
1990 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1991 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001992 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 LOG(LS_WARNING) << "View request for ("
1994 << it->selector.ssrc << ", '"
1995 << it->selector.groupid << "', '"
1996 << it->selector.streamid << "'"
1997 << ") is not in the local streams.";
1998 }
1999 }
2000
2001 return ret;
2002}
2003
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002004bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002006 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002007 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00002008 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
2009 screencast_capturers_[ssrc] = capturer;
2010 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011}
2012
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
2014 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
2015 if (iter == screencast_capturers_.end()) {
2016 return false;
2017 }
2018 // Clean up VideoCapturer.
2019 delete iter->second;
2020 screencast_capturers_.erase(iter);
2021 return true;
2022}
2023
2024bool VideoChannel::IsScreencasting_w() const {
2025 return !screencast_capturers_.empty();
2026}
2027
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002028void VideoChannel::GetScreencastDetails_w(
2029 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002030 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002032 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 }
2034 VideoCapturer* capturer = iter->second;
2035 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002036 data->fps = VideoFormat::IntervalToFps(video_format->interval);
2037 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038}
2039
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002041 rtc::WindowEvent we) {
2042 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002043 SignalScreencastWindowEvent(ssrc, we);
2044}
2045
2046bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002047 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
2048 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049}
2050
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002051void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053 case MSG_SCREENCASTWINDOWEVENT: {
2054 const ScreencastEventMessageData* data =
2055 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
2056 OnScreencastWindowEvent_s(data->ssrc, data->event);
2057 delete data;
2058 break;
2059 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060 case MSG_CHANNEL_ERROR: {
2061 const VideoChannelErrorMessageData* data =
2062 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
2063 SignalMediaError(this, data->ssrc, data->error);
2064 delete data;
2065 break;
2066 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067 default:
2068 BaseChannel::OnMessage(pmsg);
2069 break;
2070 }
2071}
2072
2073void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002074 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002075 SignalConnectionMonitor(this, infos);
2076}
2077
2078// TODO(pthatcher): Look into removing duplicate code between
2079// audio, video, and data, perhaps by using templates.
2080void VideoChannel::OnMediaMonitorUpdate(
2081 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2082 ASSERT(media_channel == this->media_channel());
2083 SignalMediaMonitor(this, info);
2084}
2085
2086void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002087 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088 ScreencastEventMessageData* pdata =
2089 new ScreencastEventMessageData(ssrc, event);
2090 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2091}
2092
2093void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2094 // Map capturer events to window events. In the future we may want to simply
2095 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002096 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002098 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002100 we = rtc::WE_MINIMIZE;
2101 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2102 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103 } else {
2104 return;
2105 }
2106 previous_we_ = we;
2107
2108 uint32 ssrc = 0;
2109 if (!GetLocalSsrc(capturer, &ssrc)) {
2110 return;
2111 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002112
2113 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114}
2115
2116bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2117 *ssrc = 0;
2118 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2119 iter != screencast_capturers_.end(); ++iter) {
2120 if (iter->second == capturer) {
2121 *ssrc = iter->first;
2122 return true;
2123 }
2124 }
2125 return false;
2126}
2127
2128void VideoChannel::OnVideoChannelError(uint32 ssrc,
2129 VideoMediaChannel::Error error) {
2130 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2131 ssrc, error);
2132 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2133}
2134
2135void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2136 SrtpFilter::Error error) {
2137 switch (error) {
2138 case SrtpFilter::ERROR_FAIL:
2139 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2140 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2141 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2142 break;
2143 case SrtpFilter::ERROR_AUTH:
2144 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2145 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2146 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2147 break;
2148 case SrtpFilter::ERROR_REPLAY:
2149 // Only receving channel should have this error.
2150 ASSERT(mode == SrtpFilter::UNPROTECT);
2151 // TODO(gangji): Turn on the signaling of replay error once we have
2152 // switched to the new mechanism for doing video retransmissions.
2153 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2154 break;
2155 default:
2156 break;
2157 }
2158}
2159
2160
2161void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2162 GetSupportedVideoCryptoSuites(ciphers);
2163}
2164
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002165DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166 DataMediaChannel* media_channel,
2167 BaseSession* session,
2168 const std::string& content_name,
2169 bool rtcp)
2170 // MediaEngine is NULL
2171 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002172 data_channel_type_(cricket::DCT_NONE),
2173 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174}
2175
2176DataChannel::~DataChannel() {
2177 StopMediaMonitor();
2178 // this can't be done in the base class, since it calls a virtual
2179 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002180
2181 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182}
2183
2184bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002185 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002186 return false;
2187 }
2188 media_channel()->SignalDataReceived.connect(
2189 this, &DataChannel::OnDataReceived);
2190 media_channel()->SignalMediaError.connect(
2191 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002192 media_channel()->SignalReadyToSend.connect(
2193 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002194 media_channel()->SignalStreamClosedRemotely.connect(
2195 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196 srtp_filter()->SignalSrtpError.connect(
2197 this, &DataChannel::OnSrtpError);
2198 return true;
2199}
2200
2201bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002202 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002204 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2205 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206}
2207
2208const ContentInfo* DataChannel::GetFirstContent(
2209 const SessionDescription* sdesc) {
2210 return GetFirstDataContent(sdesc);
2211}
2212
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002213bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 if (data_channel_type_ == DCT_SCTP) {
2215 // TODO(pthatcher): Do this in a more robust way by checking for
2216 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002217 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 } else if (data_channel_type_ == DCT_RTP) {
2219 return BaseChannel::WantsPacket(rtcp, packet);
2220 }
2221 return false;
2222}
2223
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002224bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2225 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226 // It hasn't been set before, so set it now.
2227 if (data_channel_type_ == DCT_NONE) {
2228 data_channel_type_ = new_data_channel_type;
2229 return true;
2230 }
2231
2232 // It's been set before, but doesn't match. That's bad.
2233 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002234 std::ostringstream desc;
2235 desc << "Data channel type mismatch."
2236 << " Expected " << data_channel_type_
2237 << " Got " << new_data_channel_type;
2238 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002239 return false;
2240 }
2241
2242 // It's hasn't changed. Nothing to do.
2243 return true;
2244}
2245
2246bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002247 const DataContentDescription* content,
2248 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002249 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2250 (content->protocol() == kMediaProtocolDtlsSctp));
2251 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002252 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253}
2254
2255bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002256 ContentAction action,
2257 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002258 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259 LOG(LS_INFO) << "Setting local data description";
2260
2261 const DataContentDescription* data =
2262 static_cast<const DataContentDescription*>(content);
2263 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002264 if (!data) {
2265 SafeSetError("Can't find data content in local description.", error_desc);
2266 return false;
2267 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002268
2269 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002270 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271 return false;
2272 }
2273
2274 if (data_channel_type_ == DCT_SCTP) {
2275 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002276 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002277 if (ret) {
2278 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002279 // As in SetRemoteContent_w, make sure we set the local SCTP port
2280 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002281 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2282 SafeSetError("Failed to set data receive codecs.", error_desc);
2283 ret = false;
2284 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 }
2286 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002287 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002289 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2290 SafeSetError("Failed to set data receive codecs.", error_desc);
2291 ret = false;
2292 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002293 }
2294 }
2295
2296 // If everything worked, see if we can start receiving.
2297 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00002298 std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2299 for (; it != data->codecs().end(); ++it) {
2300 bundle_filter()->AddPayloadType(it->id);
2301 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002302 ChangeState();
2303 } else {
2304 LOG(LS_WARNING) << "Failed to set local data description";
2305 }
2306 return ret;
2307}
2308
2309bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002310 ContentAction action,
2311 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002312 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313
2314 const DataContentDescription* data =
2315 static_cast<const DataContentDescription*>(content);
2316 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002317 if (!data) {
2318 SafeSetError("Can't find data content in remote description.", error_desc);
2319 return false;
2320 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002321
2322 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002323 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 return false;
2325 }
2326
2327 if (data_channel_type_ == DCT_SCTP) {
2328 LOG(LS_INFO) << "Setting SCTP remote data description";
2329 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002330 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002331 if (ret) {
2332 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002333 // We send the SCTP port number (not to be confused with the underlying
2334 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002335 if (!media_channel()->SetSendCodecs(data->codecs())) {
2336 SafeSetError("Failed to set data send codecs.", error_desc);
2337 ret = false;
2338 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339 }
2340 } else {
2341 // If the remote data doesn't have codecs and isn't an update, it
2342 // must be empty, so ignore it.
2343 if (action != CA_UPDATE && !data->has_codecs()) {
2344 return true;
2345 }
2346 LOG(LS_INFO) << "Setting remote data description";
2347
2348 // Set remote video codecs (what the other side wants to receive).
2349 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002350 if (!media_channel()->SetSendCodecs(data->codecs())) {
2351 SafeSetError("Failed to set data send codecs.", error_desc);
2352 ret = false;
2353 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354 }
2355
2356 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002357 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002358 }
2359
2360 if (action != CA_UPDATE) {
2361 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002362 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2363 std::ostringstream desc;
2364 desc << "Failed to set max send bandwidth for data content.";
2365 SafeSetError(desc.str(), error_desc);
2366 ret = false;
2367 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002368 }
2369 }
2370
2371 // If everything worked, see if we can start sending.
2372 if (ret) {
2373 ChangeState();
2374 } else {
2375 LOG(LS_WARNING) << "Failed to set remote data description";
2376 }
2377 return ret;
2378}
2379
2380void DataChannel::ChangeState() {
2381 // Render incoming data if we're the active call, and we have the local
2382 // content. We receive data on the default channel and multiplexed streams.
2383 bool recv = IsReadyToReceive();
2384 if (!media_channel()->SetReceive(recv)) {
2385 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2386 }
2387
2388 // Send outgoing data if we're the active call, we have the remote content,
2389 // and we have had some form of connectivity.
2390 bool send = IsReadyToSend();
2391 if (!media_channel()->SetSend(send)) {
2392 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2393 }
2394
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002395 // Trigger SignalReadyToSendData asynchronously.
2396 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002397
2398 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2399}
2400
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002401void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002402 switch (pmsg->message_id) {
2403 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002404 DataChannelReadyToSendMessageData* data =
2405 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002406 ready_to_send_data_ = data->data();
2407 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408 delete data;
2409 break;
2410 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002411 case MSG_DATARECEIVED: {
2412 DataReceivedMessageData* data =
2413 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2414 SignalDataReceived(this, data->params, data->payload);
2415 delete data;
2416 break;
2417 }
2418 case MSG_CHANNEL_ERROR: {
2419 const DataChannelErrorMessageData* data =
2420 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2421 SignalMediaError(this, data->ssrc, data->error);
2422 delete data;
2423 break;
2424 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002425 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002426 rtc::TypedMessageData<uint32>* data =
2427 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002428 SignalStreamClosedRemotely(data->data());
2429 delete data;
2430 break;
2431 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002432 default:
2433 BaseChannel::OnMessage(pmsg);
2434 break;
2435 }
2436}
2437
2438void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002439 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002440 SignalConnectionMonitor(this, infos);
2441}
2442
2443void DataChannel::StartMediaMonitor(int cms) {
2444 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002445 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002446 media_monitor_->SignalUpdate.connect(
2447 this, &DataChannel::OnMediaMonitorUpdate);
2448 media_monitor_->Start(cms);
2449}
2450
2451void DataChannel::StopMediaMonitor() {
2452 if (media_monitor_) {
2453 media_monitor_->Stop();
2454 media_monitor_->SignalUpdate.disconnect(this);
2455 media_monitor_.reset();
2456 }
2457}
2458
2459void DataChannel::OnMediaMonitorUpdate(
2460 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2461 ASSERT(media_channel == this->media_channel());
2462 SignalMediaMonitor(this, info);
2463}
2464
2465void DataChannel::OnDataReceived(
2466 const ReceiveDataParams& params, const char* data, size_t len) {
2467 DataReceivedMessageData* msg = new DataReceivedMessageData(
2468 params, data, len);
2469 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2470}
2471
2472void DataChannel::OnDataChannelError(
2473 uint32 ssrc, DataMediaChannel::Error err) {
2474 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2475 ssrc, err);
2476 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2477}
2478
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002479void DataChannel::OnDataChannelReadyToSend(bool writable) {
2480 // This is usded for congestion control to indicate that the stream is ready
2481 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2482 // that the transport channel is ready.
2483 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2484 new DataChannelReadyToSendMessageData(writable));
2485}
2486
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002487void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2488 SrtpFilter::Error error) {
2489 switch (error) {
2490 case SrtpFilter::ERROR_FAIL:
2491 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2492 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2493 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2494 break;
2495 case SrtpFilter::ERROR_AUTH:
2496 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2497 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2498 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2499 break;
2500 case SrtpFilter::ERROR_REPLAY:
2501 // Only receving channel should have this error.
2502 ASSERT(mode == SrtpFilter::UNPROTECT);
2503 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2504 break;
2505 default:
2506 break;
2507 }
2508}
2509
2510void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2511 GetSupportedDataCryptoSuites(ciphers);
2512}
2513
2514bool DataChannel::ShouldSetupDtlsSrtp() const {
2515 return (data_channel_type_ == DCT_RTP);
2516}
2517
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002518void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002519 rtc::TypedMessageData<uint32>* message =
2520 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002521 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2522}
2523
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002524} // namespace cricket