blob: 8de8d379962e2aec5936ce9a2e04d11860c629e1 [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
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700153template <class Codec>
154void RtpParametersFromMediaDescription(
155 const MediaContentDescriptionImpl<Codec>* desc,
156 RtpParameters<Codec>* params) {
157 // TODO(pthatcher): Remove this once we're sure no one will give us
158 // a description without codecs (currently a CA_UPDATE with just
159 // streams can).
160 if (desc->has_codecs()) {
161 params->codecs = desc->codecs();
162 }
163 // TODO(pthatcher): See if we really need
164 // rtp_header_extensions_set() and remove it if we don't.
165 if (desc->rtp_header_extensions_set()) {
166 params->extensions = desc->rtp_header_extensions();
167 }
168}
169
170template <class Codec, class Options>
171void RtpSendParametersFromMediaDescription(
172 const MediaContentDescriptionImpl<Codec>* desc,
173 RtpSendParameters<Codec, Options>* send_params) {
174 RtpParametersFromMediaDescription(desc, send_params);
175 send_params->max_bandwidth_bps = desc->bandwidth();
176}
177
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000178BaseChannel::BaseChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 MediaChannel* media_channel, BaseSession* session,
180 const std::string& content_name, bool rtcp)
181 : worker_thread_(thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 session_(session),
183 media_channel_(media_channel),
184 content_name_(content_name),
185 rtcp_(rtcp),
186 transport_channel_(NULL),
187 rtcp_transport_channel_(NULL),
188 enabled_(false),
189 writable_(false),
190 rtp_ready_to_send_(false),
191 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 was_ever_writable_(false),
193 local_content_direction_(MD_INACTIVE),
194 remote_content_direction_(MD_INACTIVE),
195 has_received_packet_(false),
196 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000197 secure_required_(false),
198 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000199 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 LOG(LS_INFO) << "Created channel for " << content_name;
201}
202
203BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000204 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000205 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 StopConnectionMonitor();
207 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000208 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 // We must destroy the media channel before the transport channel, otherwise
210 // the media channel may try to send on the dead transport channel. NULLing
211 // is not an effective strategy since the sends will come on another thread.
212 delete media_channel_;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000213 set_transport_channel(nullptr);
214 set_rtcp_transport_channel(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 LOG(LS_INFO) << "Destroyed channel";
216}
217
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000218bool BaseChannel::Init() {
219 if (!SetTransportChannels(session(), rtcp())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 return false;
221 }
222
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000223 if (!SetDtlsSrtpCiphers(transport_channel(), false)) {
224 return false;
225 }
226 if (rtcp() && !SetDtlsSrtpCiphers(rtcp_transport_channel(), true)) {
227 return false;
228 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229
wu@webrtc.orgde305012013-10-31 15:40:38 +0000230 // Both RTP and RTCP channels are set, we can call SetInterface on
231 // media channel and it can set network options.
232 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233 return true;
234}
235
wu@webrtc.org78187522013-10-07 23:32:02 +0000236void BaseChannel::Deinit() {
237 media_channel_->SetInterface(NULL);
238}
239
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000240bool BaseChannel::SetTransportChannels(BaseSession* session, bool rtcp) {
241 return worker_thread_->Invoke<bool>(Bind(
242 &BaseChannel::SetTransportChannels_w, this, session, rtcp));
243}
244
245bool BaseChannel::SetTransportChannels_w(BaseSession* session, bool rtcp) {
246 ASSERT(worker_thread_ == rtc::Thread::Current());
247
248 set_transport_channel(session->CreateChannel(
249 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTP));
250 if (!transport_channel()) {
251 return false;
252 }
253 if (rtcp) {
254 set_rtcp_transport_channel(session->CreateChannel(
255 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTCP));
256 if (!rtcp_transport_channel()) {
257 return false;
258 }
259 } else {
260 set_rtcp_transport_channel(nullptr);
261 }
262
263 return true;
264}
265
266void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
267 ASSERT(worker_thread_ == rtc::Thread::Current());
268
269 TransportChannel* old_tc = transport_channel_;
270
271 if (old_tc == new_tc) {
272 return;
273 }
274 if (old_tc) {
275 DisconnectFromTransportChannel(old_tc);
276 session()->DestroyChannel(
277 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTP);
278 }
279
280 transport_channel_ = new_tc;
281
282 if (new_tc) {
283 ConnectToTransportChannel(new_tc);
284 }
285}
286
287void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
288 ASSERT(worker_thread_ == rtc::Thread::Current());
289
290 TransportChannel* old_tc = rtcp_transport_channel_;
291
292 if (old_tc == new_tc) {
293 return;
294 }
295 if (old_tc) {
296 DisconnectFromTransportChannel(old_tc);
297 session()->DestroyChannel(
298 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTCP);
299 }
300
301 rtcp_transport_channel_ = new_tc;
302
303 if (new_tc) {
304 ConnectToTransportChannel(new_tc);
305 }
306}
307
308void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
309 ASSERT(worker_thread_ == rtc::Thread::Current());
310
311 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
312 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
313 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
314}
315
316void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
317 ASSERT(worker_thread_ == rtc::Thread::Current());
318
319 tc->SignalWritableState.disconnect(this);
320 tc->SignalReadPacket.disconnect(this);
321 tc->SignalReadyToSend.disconnect(this);
322}
323
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000324bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000325 worker_thread_->Invoke<void>(Bind(
326 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
327 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 return true;
329}
330
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331bool BaseChannel::MuteStream(uint32 ssrc, bool mute) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000332 return InvokeOnWorker(Bind(&BaseChannel::MuteStream_w, this, ssrc, mute));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333}
334
335bool BaseChannel::IsStreamMuted(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000336 return InvokeOnWorker(Bind(&BaseChannel::IsStreamMuted_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000337}
338
339bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000340 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341}
342
343bool BaseChannel::RemoveRecvStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000344 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345}
346
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000347bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000348 return InvokeOnWorker(
349 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000350}
351
352bool BaseChannel::RemoveSendStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000353 return InvokeOnWorker(
354 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000355}
356
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000358 ContentAction action,
359 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000360 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
361 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362}
363
364bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000365 ContentAction action,
366 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000367 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
368 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369}
370
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000372 // We pass in the BaseChannel instead of the transport_channel_
373 // because if the transport_channel_ changes, the ConnectionMonitor
374 // would be pointing to the wrong TransportChannel.
375 connection_monitor_.reset(new ConnectionMonitor(
376 this, worker_thread(), rtc::Thread::Current()));
377 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000379 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380}
381
382void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000383 if (connection_monitor_) {
384 connection_monitor_->Stop();
385 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386 }
387}
388
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000389bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
390 ASSERT(worker_thread_ == rtc::Thread::Current());
391 return transport_channel_->GetStats(infos);
392}
393
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394bool BaseChannel::IsReadyToReceive() const {
395 // Receive data if we are enabled and have local content,
396 return enabled() && IsReceiveContentDirection(local_content_direction_);
397}
398
399bool BaseChannel::IsReadyToSend() const {
400 // Send outgoing data if we are enabled, have local and remote content,
401 // and we have had some form of connectivity.
402 return enabled() &&
403 IsReceiveContentDirection(remote_content_direction_) &&
404 IsSendContentDirection(local_content_direction_) &&
405 was_ever_writable();
406}
407
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000408bool BaseChannel::SendPacket(rtc::Buffer* packet,
409 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000410 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411}
412
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000413bool BaseChannel::SendRtcp(rtc::Buffer* packet,
414 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000415 return SendPacket(true, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416}
417
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000418int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000420 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000421 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000422 case ST_RTP:
423 channel = transport_channel_;
424 break;
425 case ST_RTCP:
426 channel = rtcp_transport_channel_;
427 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000429 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430}
431
432void BaseChannel::OnWritableState(TransportChannel* channel) {
433 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
434 if (transport_channel_->writable()
435 && (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
436 ChannelWritable_w();
437 } else {
438 ChannelNotWritable_w();
439 }
440}
441
442void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000443 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000444 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000445 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000447 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448
449 // When using RTCP multiplexing we might get RTCP packets on the RTP
450 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
451 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000452 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000453 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454}
455
456void BaseChannel::OnReadyToSend(TransportChannel* channel) {
457 SetReadyToSend(channel, true);
458}
459
460void BaseChannel::SetReadyToSend(TransportChannel* channel, bool ready) {
461 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
462 if (channel == transport_channel_) {
463 rtp_ready_to_send_ = ready;
464 }
465 if (channel == rtcp_transport_channel_) {
466 rtcp_ready_to_send_ = ready;
467 }
468
469 if (!ready) {
470 // Notify the MediaChannel when either rtp or rtcp channel can't send.
471 media_channel_->OnReadyToSend(false);
472 } else if (rtp_ready_to_send_ &&
473 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
474 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
475 // Notify the MediaChannel when both rtp and rtcp channel can send.
476 media_channel_->OnReadyToSend(true);
477 }
478}
479
480bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
481 const char* data, size_t len) {
482 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000483 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000484}
485
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000486bool BaseChannel::SendPacket(bool rtcp, rtc::Buffer* packet,
487 rtc::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000488 // SendPacket gets called from MediaEngine, typically on an encoder thread.
489 // If the thread is not our worker thread, we will post to our worker
490 // so that the real work happens on our worker. This avoids us having to
491 // synchronize access to all the pieces of the send path, including
492 // SRTP and the inner workings of the transport channels.
493 // The only downside is that we can't return a proper failure code if
494 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000495 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 // Avoid a copy by transferring the ownership of the packet data.
497 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
498 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200499 data->packet = packet->Pass();
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000500 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 worker_thread_->Post(this, message_id, data);
502 return true;
503 }
504
505 // Now that we are on the correct thread, ensure we have a place to send this
506 // packet before doing anything. (We might get RTCP packets that we don't
507 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
508 // transport.
509 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
510 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000511 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 return false;
513 }
514
515 // Protect ourselves against crazy data.
516 if (!ValidPacket(rtcp, packet)) {
517 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000518 << PacketType(rtcp)
519 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000520 return false;
521 }
522
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000523 rtc::PacketOptions options(dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 // Protect if needed.
525 if (srtp_filter_.IsActive()) {
526 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200527 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000528 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000530 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
531 // inside libsrtp for a RTP packet. A external HMAC module will be writing
532 // a fake HMAC value. This is ONLY done for a RTP packet.
533 // Socket layer will update rtp sendtime extension header if present in
534 // packet with current time before updating the HMAC.
535#if !defined(ENABLE_EXTERNAL_AUTH)
536 res = srtp_filter_.ProtectRtp(
537 data, len, static_cast<int>(packet->capacity()), &len);
538#else
henrike@webrtc.org05376342014-03-10 15:53:12 +0000539 options.packet_time_params.rtp_sendtime_extension_id =
540 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000541 res = srtp_filter_.ProtectRtp(
542 data, len, static_cast<int>(packet->capacity()), &len,
543 &options.packet_time_params.srtp_packet_index);
544 // If protection succeeds, let's get auth params from srtp.
545 if (res) {
546 uint8* auth_key = NULL;
547 int key_len;
548 res = srtp_filter_.GetRtpAuthParams(
549 &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
550 if (res) {
551 options.packet_time_params.srtp_auth_key.resize(key_len);
552 options.packet_time_params.srtp_auth_key.assign(auth_key,
553 auth_key + key_len);
554 }
555 }
556#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 if (!res) {
558 int seq_num = -1;
559 uint32 ssrc = 0;
560 GetRtpSeqNum(data, len, &seq_num);
561 GetRtpSsrc(data, len, &ssrc);
562 LOG(LS_ERROR) << "Failed to protect " << content_name_
563 << " RTP packet: size=" << len
564 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
565 return false;
566 }
567 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000568 res = srtp_filter_.ProtectRtcp(data, len,
569 static_cast<int>(packet->capacity()),
570 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000571 if (!res) {
572 int type = -1;
573 GetRtcpType(data, len, &type);
574 LOG(LS_ERROR) << "Failed to protect " << content_name_
575 << " RTCP packet: size=" << len << ", type=" << type;
576 return false;
577 }
578 }
579
580 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000581 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 } else if (secure_required_) {
583 // This is a double check for something that supposedly can't happen.
584 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
585 << " packet when SRTP is inactive and crypto is required";
586
587 ASSERT(false);
588 return false;
589 }
590
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000592 int ret =
Karl Wiberg94784372015-04-20 14:03:07 +0200593 channel->SendPacket(packet->data<char>(), packet->size(), options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000594 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
595 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000596 if (channel->GetError() == EWOULDBLOCK) {
597 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
598 SetReadyToSend(channel, false);
599 }
600 return false;
601 }
602 return true;
603}
604
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000605bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 // Protect ourselves against crazy data.
607 if (!ValidPacket(rtcp, packet)) {
608 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000609 << PacketType(rtcp)
610 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611 return false;
612 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000614 // Bundle filter handles both rtp and rtcp packets.
Karl Wiberg94784372015-04-20 14:03:07 +0200615 return bundle_filter_.DemuxPacket(packet->data<char>(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616}
617
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000618void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
619 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000620 if (!WantsPacket(rtcp, packet)) {
621 return;
622 }
623
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000624 // We are only interested in the first rtp packet because that
625 // indicates the media has started flowing.
626 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 has_received_packet_ = true;
628 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
629 }
630
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 // Unprotect the packet, if needed.
632 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200633 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000634 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000635 bool res;
636 if (!rtcp) {
637 res = srtp_filter_.UnprotectRtp(data, len, &len);
638 if (!res) {
639 int seq_num = -1;
640 uint32 ssrc = 0;
641 GetRtpSeqNum(data, len, &seq_num);
642 GetRtpSsrc(data, len, &ssrc);
643 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
644 << " RTP packet: size=" << len
645 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
646 return;
647 }
648 } else {
649 res = srtp_filter_.UnprotectRtcp(data, len, &len);
650 if (!res) {
651 int type = -1;
652 GetRtcpType(data, len, &type);
653 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
654 << " RTCP packet: size=" << len << ", type=" << type;
655 return;
656 }
657 }
658
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000659 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 } else if (secure_required_) {
661 // Our session description indicates that SRTP is required, but we got a
662 // packet before our SRTP filter is active. This means either that
663 // a) we got SRTP packets before we received the SDES keys, in which case
664 // we can't decrypt it anyway, or
665 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
666 // channels, so we haven't yet extracted keys, even if DTLS did complete
667 // on the channel that the packets are being sent on. It's really good
668 // practice to wait for both RTP and RTCP to be good to go before sending
669 // media, to prevent weird failure modes, so it's fine for us to just eat
670 // packets here. This is all sidestepped if RTCP mux is used anyway.
671 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
672 << " packet when SRTP is inactive and crypto is required";
673 return;
674 }
675
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 // Push it down to the media channel.
677 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000678 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000680 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 }
682}
683
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000684bool BaseChannel::PushdownLocalDescription(
685 const SessionDescription* local_desc, ContentAction action,
686 std::string* error_desc) {
687 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688 const MediaContentDescription* content_desc =
689 GetContentDescription(content_info);
690 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000691 !SetLocalContent(content_desc, action, error_desc)) {
692 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
693 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000695 return true;
696}
697
698bool BaseChannel::PushdownRemoteDescription(
699 const SessionDescription* remote_desc, ContentAction action,
700 std::string* error_desc) {
701 const ContentInfo* content_info = GetFirstContent(remote_desc);
702 const MediaContentDescription* content_desc =
703 GetContentDescription(content_info);
704 if (content_desc && content_info && !content_info->rejected &&
705 !SetRemoteContent(content_desc, action, error_desc)) {
706 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
707 return false;
708 }
709 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710}
711
712void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000713 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000714 if (enabled_)
715 return;
716
717 LOG(LS_INFO) << "Channel enabled";
718 enabled_ = true;
719 ChangeState();
720}
721
722void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000723 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 if (!enabled_)
725 return;
726
727 LOG(LS_INFO) << "Channel disabled";
728 enabled_ = false;
729 ChangeState();
730}
731
732bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000733 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 bool ret = media_channel()->MuteStream(ssrc, mute);
735 if (ret) {
736 if (mute)
737 muted_streams_.insert(ssrc);
738 else
739 muted_streams_.erase(ssrc);
740 }
741 return ret;
742}
743
744bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000745 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 return muted_streams_.find(ssrc) != muted_streams_.end();
747}
748
749void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000750 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000751 if (writable_)
752 return;
753
754 LOG(LS_INFO) << "Channel socket writable ("
755 << transport_channel_->content_name() << ", "
756 << transport_channel_->component() << ")"
757 << (was_ever_writable_ ? "" : " for the first time");
758
759 std::vector<ConnectionInfo> infos;
760 transport_channel_->GetStats(&infos);
761 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
762 it != infos.end(); ++it) {
763 if (it->best_connection) {
764 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
765 << "->" << it->remote_candidate.ToSensitiveString();
766 break;
767 }
768 }
769
770 // If we're doing DTLS-SRTP, now is the time.
771 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
772 if (!SetupDtlsSrtp(false)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000773 SignalDtlsSetupFailure(this, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000774 return;
775 }
776
777 if (rtcp_transport_channel_) {
778 if (!SetupDtlsSrtp(true)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000779 SignalDtlsSetupFailure(this, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 return;
781 }
782 }
783 }
784
785 was_ever_writable_ = true;
786 writable_ = true;
787 ChangeState();
788}
789
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000790void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
791 ASSERT(worker_thread() == rtc::Thread::Current());
792 signaling_thread()->Invoke<void>(Bind(
793 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
794}
795
796void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
797 ASSERT(signaling_thread() == rtc::Thread::Current());
798 SignalDtlsSetupFailure(this, rtcp);
799}
800
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000801bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
802 std::vector<std::string> ciphers;
803 // We always use the default SRTP ciphers for RTCP, but we may use different
804 // ciphers for RTP depending on the media type.
805 if (!rtcp) {
806 GetSrtpCiphers(&ciphers);
807 } else {
808 GetSupportedDefaultCryptoSuites(&ciphers);
809 }
810 return tc->SetSrtpCiphers(ciphers);
811}
812
813bool BaseChannel::ShouldSetupDtlsSrtp() const {
814 return true;
815}
816
817// This function returns true if either DTLS-SRTP is not in use
818// *or* DTLS-SRTP is successfully set up.
819bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
820 bool ret = false;
821
822 TransportChannel *channel = rtcp_channel ?
823 rtcp_transport_channel_ : transport_channel_;
824
825 // No DTLS
826 if (!channel->IsDtlsActive())
827 return true;
828
829 std::string selected_cipher;
830
831 if (!channel->GetSrtpCipher(&selected_cipher)) {
832 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
833 return false;
834 }
835
836 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
837 << content_name() << " "
838 << PacketType(rtcp_channel);
839
840 // OK, we're now doing DTLS (RFC 5764)
841 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
842 SRTP_MASTER_KEY_SALT_LEN * 2);
843
844 // RFC 5705 exporter using the RFC 5764 parameters
845 if (!channel->ExportKeyingMaterial(
846 kDtlsSrtpExporterLabel,
847 NULL, 0, false,
848 &dtls_buffer[0], dtls_buffer.size())) {
849 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
850 ASSERT(false); // This should never happen
851 return false;
852 }
853
854 // Sync up the keys with the DTLS-SRTP interface
855 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
856 SRTP_MASTER_KEY_SALT_LEN);
857 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
858 SRTP_MASTER_KEY_SALT_LEN);
859 size_t offset = 0;
860 memcpy(&client_write_key[0], &dtls_buffer[offset],
861 SRTP_MASTER_KEY_KEY_LEN);
862 offset += SRTP_MASTER_KEY_KEY_LEN;
863 memcpy(&server_write_key[0], &dtls_buffer[offset],
864 SRTP_MASTER_KEY_KEY_LEN);
865 offset += SRTP_MASTER_KEY_KEY_LEN;
866 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
867 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
868 offset += SRTP_MASTER_KEY_SALT_LEN;
869 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
870 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
871
872 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000873 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000874 if (!channel->GetSslRole(&role)) {
875 LOG(LS_WARNING) << "GetSslRole failed";
876 return false;
877 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000879 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 send_key = &server_write_key;
881 recv_key = &client_write_key;
882 } else {
883 send_key = &client_write_key;
884 recv_key = &server_write_key;
885 }
886
887 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000888 ret = srtp_filter_.SetRtcpParams(
889 selected_cipher,
890 &(*send_key)[0],
891 static_cast<int>(send_key->size()),
892 selected_cipher,
893 &(*recv_key)[0],
894 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000896 ret = srtp_filter_.SetRtpParams(
897 selected_cipher,
898 &(*send_key)[0],
899 static_cast<int>(send_key->size()),
900 selected_cipher,
901 &(*recv_key)[0],
902 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 }
904
905 if (!ret)
906 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
907 else
908 dtls_keyed_ = true;
909
910 return ret;
911}
912
913void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000914 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 if (!writable_)
916 return;
917
918 LOG(LS_INFO) << "Channel socket not writable ("
919 << transport_channel_->content_name() << ", "
920 << transport_channel_->component() << ")";
921 writable_ = false;
922 ChangeState();
923}
924
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700925bool BaseChannel::SetRtpTransportParameters_w(
926 const MediaContentDescription* content,
927 ContentAction action,
928 ContentSource src,
929 std::string* error_desc) {
930 if (action == CA_UPDATE) {
931 // These parameters never get changed by a CA_UDPATE.
932 return true;
933 }
934
935 // Cache secure_required_ for belt and suspenders check on SendPacket
936 if (src == CS_LOCAL) {
937 set_secure_required(content->crypto_required() != CT_NONE);
938 }
939
940 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
941 return false;
942 }
943
944 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
945 return false;
946 }
947
948 return true;
949}
950
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000951// |dtls| will be set to true if DTLS is active for transport channel and
952// crypto is empty.
953bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000954 bool* dtls,
955 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000956 *dtls = transport_channel_->IsDtlsActive();
957 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000958 SafeSetError("Cryptos must be empty when DTLS is active.",
959 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000960 return false;
961 }
962 return true;
963}
964
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000966 ContentAction action,
967 ContentSource src,
968 std::string* error_desc) {
969 if (action == CA_UPDATE) {
970 // no crypto params.
971 return true;
972 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000974 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000975 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
976 if (!ret) {
977 return false;
978 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 switch (action) {
980 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000981 // If DTLS is already active on the channel, we could be renegotiating
982 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000983 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000984 ret = srtp_filter_.SetOffer(cryptos, src);
985 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 break;
987 case CA_PRANSWER:
988 // If we're doing DTLS-SRTP, we don't want to update the filter
989 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000990 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
992 }
993 break;
994 case CA_ANSWER:
995 // If we're doing DTLS-SRTP, we don't want to update the filter
996 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000997 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 ret = srtp_filter_.SetAnswer(cryptos, src);
999 }
1000 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 default:
1002 break;
1003 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001004 if (!ret) {
1005 SafeSetError("Failed to setup SRTP filter.", error_desc);
1006 return false;
1007 }
1008 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009}
1010
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001011void BaseChannel::ActivateRtcpMux() {
1012 worker_thread_->Invoke<void>(Bind(
1013 &BaseChannel::ActivateRtcpMux_w, this));
1014}
1015
1016void BaseChannel::ActivateRtcpMux_w() {
1017 if (!rtcp_mux_filter_.IsActive()) {
1018 rtcp_mux_filter_.SetActive();
1019 set_rtcp_transport_channel(NULL);
1020 }
1021}
1022
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001024 ContentSource src,
1025 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026 bool ret = false;
1027 switch (action) {
1028 case CA_OFFER:
1029 ret = rtcp_mux_filter_.SetOffer(enable, src);
1030 break;
1031 case CA_PRANSWER:
1032 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1033 break;
1034 case CA_ANSWER:
1035 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1036 if (ret && rtcp_mux_filter_.IsActive()) {
1037 // We activated RTCP mux, close down the RTCP transport.
1038 set_rtcp_transport_channel(NULL);
1039 }
1040 break;
1041 case CA_UPDATE:
1042 // No RTCP mux info.
1043 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001044 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 default:
1046 break;
1047 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001048 if (!ret) {
1049 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1050 return false;
1051 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1053 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1054 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001055 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 // If the RTP transport is already writable, then so are we.
1057 if (transport_channel_->writable()) {
1058 ChannelWritable_w();
1059 }
1060 }
1061
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001062 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063}
1064
1065bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001066 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 if (!media_channel()->AddRecvStream(sp))
1068 return false;
1069
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001070 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071}
1072
1073bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001074 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001075 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 return media_channel()->RemoveRecvStream(ssrc);
1077}
1078
1079bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001080 ContentAction action,
1081 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1083 action == CA_PRANSWER || action == CA_UPDATE))
1084 return false;
1085
1086 // If this is an update, streams only contain streams that have changed.
1087 if (action == CA_UPDATE) {
1088 for (StreamParamsVec::const_iterator it = streams.begin();
1089 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001090 const StreamParams* existing_stream =
1091 GetStreamByIds(local_streams_, it->groupid, it->id);
1092 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 if (media_channel()->AddSendStream(*it)) {
1094 local_streams_.push_back(*it);
1095 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1096 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001097 std::ostringstream desc;
1098 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1099 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100 return false;
1101 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001102 } else if (existing_stream && !it->has_ssrcs()) {
1103 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001104 std::ostringstream desc;
1105 desc << "Failed to remove send stream with ssrc "
1106 << it->first_ssrc() << ".";
1107 SafeSetError(desc.str(), error_desc);
1108 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001110 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 } else {
1112 LOG(LS_WARNING) << "Ignore unsupported stream update";
1113 }
1114 }
1115 return true;
1116 }
1117 // Else streams are all the streams we want to send.
1118
1119 // Check for streams that have been removed.
1120 bool ret = true;
1121 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1122 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001123 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 if (!media_channel()->RemoveSendStream(it->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);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 ret = false;
1130 }
1131 }
1132 }
1133 // Check for new streams.
1134 for (StreamParamsVec::const_iterator it = streams.begin();
1135 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001136 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001137 if (media_channel()->AddSendStream(*it)) {
1138 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1139 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001140 std::ostringstream desc;
1141 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1142 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143 ret = false;
1144 }
1145 }
1146 }
1147 local_streams_ = streams;
1148 return ret;
1149}
1150
1151bool BaseChannel::UpdateRemoteStreams_w(
1152 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001153 ContentAction action,
1154 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001155 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1156 action == CA_PRANSWER || action == CA_UPDATE))
1157 return false;
1158
1159 // If this is an update, streams only contain streams that have changed.
1160 if (action == CA_UPDATE) {
1161 for (StreamParamsVec::const_iterator it = streams.begin();
1162 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001163 const StreamParams* existing_stream =
1164 GetStreamByIds(remote_streams_, it->groupid, it->id);
1165 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 if (AddRecvStream_w(*it)) {
1167 remote_streams_.push_back(*it);
1168 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1169 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001170 std::ostringstream desc;
1171 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1172 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173 return false;
1174 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001175 } else if (existing_stream && !it->has_ssrcs()) {
1176 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001177 std::ostringstream desc;
1178 desc << "Failed to remove remote stream with ssrc "
1179 << it->first_ssrc() << ".";
1180 SafeSetError(desc.str(), error_desc);
1181 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001183 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 } else {
1185 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001186 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 << " new stream = " << it->ToString();
1188 }
1189 }
1190 return true;
1191 }
1192 // Else streams are all the streams we want to receive.
1193
1194 // Check for streams that have been removed.
1195 bool ret = true;
1196 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1197 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001198 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001200 std::ostringstream desc;
1201 desc << "Failed to remove remote stream with ssrc "
1202 << it->first_ssrc() << ".";
1203 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 ret = false;
1205 }
1206 }
1207 }
1208 // Check for new streams.
1209 for (StreamParamsVec::const_iterator it = streams.begin();
1210 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001211 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 if (AddRecvStream_w(*it)) {
1213 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1214 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001215 std::ostringstream desc;
1216 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1217 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001218 ret = false;
1219 }
1220 }
1221 }
1222 remote_streams_ = streams;
1223 return ret;
1224}
1225
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001226void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1227 const std::vector<RtpHeaderExtension>& extensions) {
1228 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001229 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001230 rtp_abs_sendtime_extn_id_ =
1231 send_time_extension ? send_time_extension->id : -1;
1232}
1233
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001234void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236 case MSG_RTPPACKET:
1237 case MSG_RTCPPACKET: {
1238 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001239 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 delete data; // because it is Posted
1241 break;
1242 }
1243 case MSG_FIRSTPACKETRECEIVED: {
1244 SignalFirstPacketReceived(this);
1245 break;
1246 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 }
1248}
1249
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001250void BaseChannel::FlushRtcpMessages() {
1251 // Flush all remaining RTCP messages. This should only be called in
1252 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001253 ASSERT(rtc::Thread::Current() == worker_thread_);
1254 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001255 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001256 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001257 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001258 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 }
1260}
1261
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001262VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 MediaEngineInterface* media_engine,
1264 VoiceMediaChannel* media_channel,
1265 BaseSession* session,
1266 const std::string& content_name,
1267 bool rtcp)
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001268 : BaseChannel(thread, media_channel, session, content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001270 media_engine_(media_engine),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 received_media_(false) {
1272}
1273
1274VoiceChannel::~VoiceChannel() {
1275 StopAudioMonitor();
1276 StopMediaMonitor();
1277 // this can't be done in the base class, since it calls a virtual
1278 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001279 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280}
1281
1282bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001283 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 return false;
1285 }
1286 media_channel()->SignalMediaError.connect(
1287 this, &VoiceChannel::OnVoiceChannelError);
1288 srtp_filter()->SignalSrtpError.connect(
1289 this, &VoiceChannel::OnSrtpError);
1290 return true;
1291}
1292
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001293bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001294 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1295 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001296}
1297
1298bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001299 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1300 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301}
1302
1303bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001304 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305}
1306
1307// TODO(juberti): Handle early media the right way. We should get an explicit
1308// ringing message telling us to start playing local ringback, which we cancel
1309// if any early media actually arrives. For now, we do the opposite, which is
1310// to wait 1 second for early media, and start playing local ringback if none
1311// arrives.
1312void VoiceChannel::SetEarlyMedia(bool enable) {
1313 if (enable) {
1314 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001315 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1316 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 } else {
1318 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001319 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320 }
1321}
1322
1323bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001324 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1325 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001326}
1327
1328bool VoiceChannel::PressDTMF(int digit, bool playout) {
1329 int flags = DF_SEND;
1330 if (playout) {
1331 flags |= DF_PLAY;
1332 }
1333 int duration_ms = 160;
1334 return InsertDtmf(0, digit, duration_ms, flags);
1335}
1336
1337bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001338 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1339 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340}
1341
1342bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1343 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001344 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1345 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346}
1347
1348bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001349 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1350 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001352
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001354 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1355 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356}
1357
1358void VoiceChannel::StartMediaMonitor(int cms) {
1359 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001360 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361 media_monitor_->SignalUpdate.connect(
1362 this, &VoiceChannel::OnMediaMonitorUpdate);
1363 media_monitor_->Start(cms);
1364}
1365
1366void VoiceChannel::StopMediaMonitor() {
1367 if (media_monitor_) {
1368 media_monitor_->Stop();
1369 media_monitor_->SignalUpdate.disconnect(this);
1370 media_monitor_.reset();
1371 }
1372}
1373
1374void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001375 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 audio_monitor_
1377 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1378 audio_monitor_->Start(cms);
1379}
1380
1381void VoiceChannel::StopAudioMonitor() {
1382 if (audio_monitor_) {
1383 audio_monitor_->Stop();
1384 audio_monitor_.reset();
1385 }
1386}
1387
1388bool VoiceChannel::IsAudioMonitorRunning() const {
1389 return (audio_monitor_.get() != NULL);
1390}
1391
1392void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1393 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1394 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1395}
1396
1397void VoiceChannel::StopTypingMonitor() {
1398 typing_monitor_.reset();
1399}
1400
1401bool VoiceChannel::IsTypingMonitorRunning() const {
1402 return typing_monitor_;
1403}
1404
1405bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1406 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1407 if (typing_monitor_ && mute)
1408 typing_monitor_->OnChannelMuted();
1409 return ret;
1410}
1411
1412int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001413 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001414}
1415
1416int VoiceChannel::GetOutputLevel_w() {
1417 return media_channel()->GetOutputLevel();
1418}
1419
1420void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1421 media_channel()->GetActiveStreams(actives);
1422}
1423
1424void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001425 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001426 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001427 int flags) {
1428 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429
1430 // Set a flag when we've received an RTP packet. If we're waiting for early
1431 // media, this will disable the timeout.
1432 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1433 received_media_ = true;
1434 }
1435}
1436
1437void VoiceChannel::ChangeState() {
1438 // Render incoming data if we're the active call, and we have the local
1439 // content. We receive data on the default channel and multiplexed streams.
1440 bool recv = IsReadyToReceive();
1441 if (!media_channel()->SetPlayout(recv)) {
1442 SendLastMediaError();
1443 }
1444
1445 // Send outgoing data if we're the active call, we have the remote content,
1446 // and we have had some form of connectivity.
1447 bool send = IsReadyToSend();
1448 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1449 if (!media_channel()->SetSend(send_flag)) {
1450 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1451 SendLastMediaError();
1452 }
1453
1454 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1455}
1456
1457const ContentInfo* VoiceChannel::GetFirstContent(
1458 const SessionDescription* sdesc) {
1459 return GetFirstAudioContent(sdesc);
1460}
1461
1462bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001463 ContentAction action,
1464 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001465 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 LOG(LS_INFO) << "Setting local voice description";
1467
1468 const AudioContentDescription* audio =
1469 static_cast<const AudioContentDescription*>(content);
1470 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001471 if (!audio) {
1472 SafeSetError("Can't find audio content in local description.", error_desc);
1473 return false;
1474 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001476 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1477 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 }
1479
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001480 AudioRecvParameters recv_params = last_recv_params_;
1481 RtpParametersFromMediaDescription(audio, &recv_params);
1482 if (!media_channel()->SetRecvParameters(recv_params)) {
1483 SafeSetError("Failed to set local video description recv parameters.",
1484 error_desc);
1485 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001487 for (const AudioCodec& codec : audio->codecs()) {
1488 bundle_filter()->AddPayloadType(codec.id);
1489 }
1490 last_recv_params_ = recv_params;
1491
1492 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1493 // only give it to the media channel once we have a remote
1494 // description too (without a remote description, we won't be able
1495 // to send them anyway).
1496 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1497 SafeSetError("Failed to set local audio description streams.", error_desc);
1498 return false;
1499 }
1500
1501 set_local_content_direction(content->direction());
1502 ChangeState();
1503 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504}
1505
1506bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001507 ContentAction action,
1508 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001509 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 LOG(LS_INFO) << "Setting remote voice description";
1511
1512 const AudioContentDescription* audio =
1513 static_cast<const AudioContentDescription*>(content);
1514 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001515 if (!audio) {
1516 SafeSetError("Can't find audio content in remote description.", error_desc);
1517 return false;
1518 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001520 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1521 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 }
1523
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001524 AudioSendParameters send_params = last_send_params_;
1525 RtpSendParametersFromMediaDescription(audio, &send_params);
1526 if (audio->conference_mode()) {
1527 send_params.options.conference_mode.Set(true);
1528 }
1529 if (audio->agc_minus_10db()) {
1530 send_params.options.adjust_agc_delta.Set(kAgcMinus10db);
1531 }
1532 if (!media_channel()->SetSendParameters(send_params)) {
1533 SafeSetError("Failed to set remote audio description send parameters.",
1534 error_desc);
1535 return false;
1536 }
1537 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001539 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1540 // and only give it to the media channel once we have a local
1541 // description too (without a local description, we won't be able to
1542 // recv them anyway).
1543 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1544 SafeSetError("Failed to set remote audio description streams.", error_desc);
1545 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546 }
1547
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001548 set_remote_content_direction(content->direction());
1549 ChangeState();
1550 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551}
1552
1553bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001554 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1556}
1557
1558bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001559 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 if (play) {
1561 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1562 } else {
1563 LOG(LS_INFO) << "Stopping ringback tone";
1564 }
1565 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1566}
1567
1568void VoiceChannel::HandleEarlyMediaTimeout() {
1569 // This occurs on the main thread, not the worker thread.
1570 if (!received_media_) {
1571 LOG(LS_INFO) << "No early media received before timeout";
1572 SignalEarlyMediaTimeout(this);
1573 }
1574}
1575
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001576bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1577 int flags) {
1578 if (!enabled()) {
1579 return false;
1580 }
1581
1582 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1583}
1584
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001586 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1587 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588}
1589
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001590void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 case MSG_EARLYMEDIATIMEOUT:
1593 HandleEarlyMediaTimeout();
1594 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 case MSG_CHANNEL_ERROR: {
1596 VoiceChannelErrorMessageData* data =
1597 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1598 SignalMediaError(this, data->ssrc, data->error);
1599 delete data;
1600 break;
1601 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 default:
1603 BaseChannel::OnMessage(pmsg);
1604 break;
1605 }
1606}
1607
1608void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001609 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 SignalConnectionMonitor(this, infos);
1611}
1612
1613void VoiceChannel::OnMediaMonitorUpdate(
1614 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1615 ASSERT(media_channel == this->media_channel());
1616 SignalMediaMonitor(this, info);
1617}
1618
1619void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1620 const AudioInfo& info) {
1621 SignalAudioMonitor(this, info);
1622}
1623
1624void VoiceChannel::OnVoiceChannelError(
1625 uint32 ssrc, VoiceMediaChannel::Error err) {
1626 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1627 ssrc, err);
1628 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1629}
1630
1631void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1632 SrtpFilter::Error error) {
1633 switch (error) {
1634 case SrtpFilter::ERROR_FAIL:
1635 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1636 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1637 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1638 break;
1639 case SrtpFilter::ERROR_AUTH:
1640 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1641 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1642 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1643 break;
1644 case SrtpFilter::ERROR_REPLAY:
1645 // Only receving channel should have this error.
1646 ASSERT(mode == SrtpFilter::UNPROTECT);
1647 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1648 break;
1649 default:
1650 break;
1651 }
1652}
1653
1654void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1655 GetSupportedAudioCryptoSuites(ciphers);
1656}
1657
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001658VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 VideoMediaChannel* media_channel,
1660 BaseSession* session,
1661 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001662 bool rtcp)
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001663 : BaseChannel(thread, media_channel, session, content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001666 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667}
1668
1669bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001670 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 return false;
1672 }
1673 media_channel()->SignalMediaError.connect(
1674 this, &VideoChannel::OnVideoChannelError);
1675 srtp_filter()->SignalSrtpError.connect(
1676 this, &VideoChannel::OnSrtpError);
1677 return true;
1678}
1679
1680void VoiceChannel::SendLastMediaError() {
1681 uint32 ssrc;
1682 VoiceMediaChannel::Error error;
1683 media_channel()->GetLastMediaError(&ssrc, &error);
1684 SignalMediaError(this, ssrc, error);
1685}
1686
1687VideoChannel::~VideoChannel() {
1688 std::vector<uint32> screencast_ssrcs;
1689 ScreencastMap::iterator iter;
1690 while (!screencast_capturers_.empty()) {
1691 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1692 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1693 << screencast_capturers_.begin()->first;
1694 ASSERT(false);
1695 break;
1696 }
1697 }
1698
1699 StopMediaMonitor();
1700 // this can't be done in the base class, since it calls a virtual
1701 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001702
1703 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704}
1705
1706bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001707 worker_thread()->Invoke<void>(Bind(
1708 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 return true;
1710}
1711
1712bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001713 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714}
1715
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001716bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1717 return worker_thread()->Invoke<bool>(Bind(
1718 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719}
1720
1721bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001722 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1723 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724}
1725
1726bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001727 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728}
1729
1730bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001731 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732}
1733
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001734int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001735 ScreencastDetailsData data(ssrc);
1736 worker_thread()->Invoke<void>(Bind(
1737 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001738 return data.fps;
1739}
1740
1741int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001742 ScreencastDetailsData data(ssrc);
1743 worker_thread()->Invoke<void>(Bind(
1744 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001745 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746}
1747
1748bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001749 worker_thread()->Invoke<void>(Bind(
1750 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 return true;
1752}
1753
1754bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001755 worker_thread()->Invoke<void>(Bind(
1756 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757 return true;
1758}
1759
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760void VideoChannel::ChangeState() {
1761 // Render incoming data if we're the active call, and we have the local
1762 // content. We receive data on the default channel and multiplexed streams.
1763 bool recv = IsReadyToReceive();
1764 if (!media_channel()->SetRender(recv)) {
1765 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1766 // TODO(gangji): Report error back to server.
1767 }
1768
1769 // Send outgoing data if we're the active call, we have the remote content,
1770 // and we have had some form of connectivity.
1771 bool send = IsReadyToSend();
1772 if (!media_channel()->SetSend(send)) {
1773 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1774 // TODO(gangji): Report error back to server.
1775 }
1776
1777 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1778}
1779
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001780bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1781 return InvokeOnWorker(
1782 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783}
1784
1785void VideoChannel::StartMediaMonitor(int cms) {
1786 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001787 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 media_monitor_->SignalUpdate.connect(
1789 this, &VideoChannel::OnMediaMonitorUpdate);
1790 media_monitor_->Start(cms);
1791}
1792
1793void VideoChannel::StopMediaMonitor() {
1794 if (media_monitor_) {
1795 media_monitor_->Stop();
1796 media_monitor_.reset();
1797 }
1798}
1799
1800const ContentInfo* VideoChannel::GetFirstContent(
1801 const SessionDescription* sdesc) {
1802 return GetFirstVideoContent(sdesc);
1803}
1804
1805bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001806 ContentAction action,
1807 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001808 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 LOG(LS_INFO) << "Setting local video description";
1810
1811 const VideoContentDescription* video =
1812 static_cast<const VideoContentDescription*>(content);
1813 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001814 if (!video) {
1815 SafeSetError("Can't find video content in local description.", error_desc);
1816 return false;
1817 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001819 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1820 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001821 }
1822
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001823 VideoRecvParameters recv_params = last_recv_params_;
1824 RtpParametersFromMediaDescription(video, &recv_params);
1825 if (!media_channel()->SetRecvParameters(recv_params)) {
1826 SafeSetError("Failed to set local video description recv parameters.",
1827 error_desc);
1828 return false;
1829 }
1830 for (const VideoCodec& codec : video->codecs()) {
1831 bundle_filter()->AddPayloadType(codec.id);
1832 }
1833 last_recv_params_ = recv_params;
1834
1835 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1836 // only give it to the media channel once we have a remote
1837 // description too (without a remote description, we won't be able
1838 // to send them anyway).
1839 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1840 SafeSetError("Failed to set local video description streams.", error_desc);
1841 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 }
1843
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001844 set_local_content_direction(content->direction());
1845 ChangeState();
1846 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847}
1848
1849bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001850 ContentAction action,
1851 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001852 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 LOG(LS_INFO) << "Setting remote video description";
1854
1855 const VideoContentDescription* video =
1856 static_cast<const VideoContentDescription*>(content);
1857 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001858 if (!video) {
1859 SafeSetError("Can't find video content in remote description.", error_desc);
1860 return false;
1861 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001863
1864 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1865 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 }
1867
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001868 VideoSendParameters send_params = last_send_params_;
1869 RtpSendParametersFromMediaDescription(video, &send_params);
1870 if (video->conference_mode()) {
1871 send_params.options.conference_mode.Set(true);
1872 }
1873 if (!media_channel()->SetSendParameters(send_params)) {
1874 SafeSetError("Failed to set remote video description send parameters.",
1875 error_desc);
1876 return false;
1877 }
1878 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001880 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1881 // and only give it to the media channel once we have a local
1882 // description too (without a local description, we won't be able to
1883 // recv them anyway).
1884 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1885 SafeSetError("Failed to set remote video description streams.", error_desc);
1886 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 }
1888
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001889 if (video->rtp_header_extensions_set()) {
1890 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001892
1893 set_remote_content_direction(content->direction());
1894 ChangeState();
1895 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896}
1897
1898bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1899 bool ret = true;
1900 // Set the send format for each of the local streams. If the view request
1901 // does not contain a local stream, set its send format to 0x0, which will
1902 // drop all frames.
1903 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1904 it != local_streams().end(); ++it) {
1905 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1906 StaticVideoViews::const_iterator view;
1907 for (view = request.static_video_views.begin();
1908 view != request.static_video_views.end(); ++view) {
1909 if (view->selector.Matches(*it)) {
1910 format.width = view->width;
1911 format.height = view->height;
1912 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1913 break;
1914 }
1915 }
1916
1917 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1918 }
1919
1920 // Check if the view request has invalid streams.
1921 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1922 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001923 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 LOG(LS_WARNING) << "View request for ("
1925 << it->selector.ssrc << ", '"
1926 << it->selector.groupid << "', '"
1927 << it->selector.streamid << "'"
1928 << ") is not in the local streams.";
1929 }
1930 }
1931
1932 return ret;
1933}
1934
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001935bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001937 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001939 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1940 screencast_capturers_[ssrc] = capturer;
1941 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942}
1943
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1945 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1946 if (iter == screencast_capturers_.end()) {
1947 return false;
1948 }
1949 // Clean up VideoCapturer.
1950 delete iter->second;
1951 screencast_capturers_.erase(iter);
1952 return true;
1953}
1954
1955bool VideoChannel::IsScreencasting_w() const {
1956 return !screencast_capturers_.empty();
1957}
1958
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001959void VideoChannel::GetScreencastDetails_w(
1960 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001961 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001963 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 }
1965 VideoCapturer* capturer = iter->second;
1966 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001967 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1968 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969}
1970
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001972 rtc::WindowEvent we) {
1973 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974 SignalScreencastWindowEvent(ssrc, we);
1975}
1976
1977bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001978 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
1979 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980}
1981
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001982void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 case MSG_SCREENCASTWINDOWEVENT: {
1985 const ScreencastEventMessageData* data =
1986 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1987 OnScreencastWindowEvent_s(data->ssrc, data->event);
1988 delete data;
1989 break;
1990 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991 case MSG_CHANNEL_ERROR: {
1992 const VideoChannelErrorMessageData* data =
1993 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
1994 SignalMediaError(this, data->ssrc, data->error);
1995 delete data;
1996 break;
1997 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 default:
1999 BaseChannel::OnMessage(pmsg);
2000 break;
2001 }
2002}
2003
2004void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002005 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 SignalConnectionMonitor(this, infos);
2007}
2008
2009// TODO(pthatcher): Look into removing duplicate code between
2010// audio, video, and data, perhaps by using templates.
2011void VideoChannel::OnMediaMonitorUpdate(
2012 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2013 ASSERT(media_channel == this->media_channel());
2014 SignalMediaMonitor(this, info);
2015}
2016
2017void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002018 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019 ScreencastEventMessageData* pdata =
2020 new ScreencastEventMessageData(ssrc, event);
2021 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2022}
2023
2024void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2025 // Map capturer events to window events. In the future we may want to simply
2026 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002027 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002029 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002031 we = rtc::WE_MINIMIZE;
2032 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2033 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 } else {
2035 return;
2036 }
2037 previous_we_ = we;
2038
2039 uint32 ssrc = 0;
2040 if (!GetLocalSsrc(capturer, &ssrc)) {
2041 return;
2042 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002043
2044 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002045}
2046
2047bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2048 *ssrc = 0;
2049 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2050 iter != screencast_capturers_.end(); ++iter) {
2051 if (iter->second == capturer) {
2052 *ssrc = iter->first;
2053 return true;
2054 }
2055 }
2056 return false;
2057}
2058
2059void VideoChannel::OnVideoChannelError(uint32 ssrc,
2060 VideoMediaChannel::Error error) {
2061 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2062 ssrc, error);
2063 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2064}
2065
2066void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2067 SrtpFilter::Error error) {
2068 switch (error) {
2069 case SrtpFilter::ERROR_FAIL:
2070 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2071 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2072 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2073 break;
2074 case SrtpFilter::ERROR_AUTH:
2075 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2076 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2077 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2078 break;
2079 case SrtpFilter::ERROR_REPLAY:
2080 // Only receving channel should have this error.
2081 ASSERT(mode == SrtpFilter::UNPROTECT);
2082 // TODO(gangji): Turn on the signaling of replay error once we have
2083 // switched to the new mechanism for doing video retransmissions.
2084 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2085 break;
2086 default:
2087 break;
2088 }
2089}
2090
2091
2092void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2093 GetSupportedVideoCryptoSuites(ciphers);
2094}
2095
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002096DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 DataMediaChannel* media_channel,
2098 BaseSession* session,
2099 const std::string& content_name,
2100 bool rtcp)
Fredrik Solenberg0c022642015-08-05 12:25:22 +02002101 : BaseChannel(thread, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002102 data_channel_type_(cricket::DCT_NONE),
2103 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104}
2105
2106DataChannel::~DataChannel() {
2107 StopMediaMonitor();
2108 // this can't be done in the base class, since it calls a virtual
2109 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002110
2111 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112}
2113
2114bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002115 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116 return false;
2117 }
2118 media_channel()->SignalDataReceived.connect(
2119 this, &DataChannel::OnDataReceived);
2120 media_channel()->SignalMediaError.connect(
2121 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002122 media_channel()->SignalReadyToSend.connect(
2123 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002124 media_channel()->SignalStreamClosedRemotely.connect(
2125 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 srtp_filter()->SignalSrtpError.connect(
2127 this, &DataChannel::OnSrtpError);
2128 return true;
2129}
2130
2131bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002132 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002134 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2135 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136}
2137
2138const ContentInfo* DataChannel::GetFirstContent(
2139 const SessionDescription* sdesc) {
2140 return GetFirstDataContent(sdesc);
2141}
2142
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002143bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144 if (data_channel_type_ == DCT_SCTP) {
2145 // TODO(pthatcher): Do this in a more robust way by checking for
2146 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002147 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148 } else if (data_channel_type_ == DCT_RTP) {
2149 return BaseChannel::WantsPacket(rtcp, packet);
2150 }
2151 return false;
2152}
2153
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002154bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2155 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156 // It hasn't been set before, so set it now.
2157 if (data_channel_type_ == DCT_NONE) {
2158 data_channel_type_ = new_data_channel_type;
2159 return true;
2160 }
2161
2162 // It's been set before, but doesn't match. That's bad.
2163 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002164 std::ostringstream desc;
2165 desc << "Data channel type mismatch."
2166 << " Expected " << data_channel_type_
2167 << " Got " << new_data_channel_type;
2168 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 return false;
2170 }
2171
2172 // It's hasn't changed. Nothing to do.
2173 return true;
2174}
2175
2176bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002177 const DataContentDescription* content,
2178 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2180 (content->protocol() == kMediaProtocolDtlsSctp));
2181 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002182 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183}
2184
2185bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002186 ContentAction action,
2187 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002188 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189 LOG(LS_INFO) << "Setting local data description";
2190
2191 const DataContentDescription* data =
2192 static_cast<const DataContentDescription*>(content);
2193 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002194 if (!data) {
2195 SafeSetError("Can't find data content in local description.", error_desc);
2196 return false;
2197 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002199 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200 return false;
2201 }
2202
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002203 if (data_channel_type_ == DCT_RTP) {
2204 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2205 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 }
2207 }
2208
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002209 // FYI: We send the SCTP port number (not to be confused with the
2210 // underlying UDP port number) as a codec parameter. So even SCTP
2211 // data channels need codecs.
2212 DataRecvParameters recv_params = last_recv_params_;
2213 RtpParametersFromMediaDescription(data, &recv_params);
2214 if (!media_channel()->SetRecvParameters(recv_params)) {
2215 SafeSetError("Failed to set remote data description recv parameters.",
2216 error_desc);
2217 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002219 if (data_channel_type_ == DCT_RTP) {
2220 for (const DataCodec& codec : data->codecs()) {
2221 bundle_filter()->AddPayloadType(codec.id);
2222 }
2223 }
2224 last_recv_params_ = recv_params;
2225
2226 // TODO(pthatcher): Move local streams into DataSendParameters, and
2227 // only give it to the media channel once we have a remote
2228 // description too (without a remote description, we won't be able
2229 // to send them anyway).
2230 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2231 SafeSetError("Failed to set local data description streams.", error_desc);
2232 return false;
2233 }
2234
2235 set_local_content_direction(content->direction());
2236 ChangeState();
2237 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002238}
2239
2240bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002241 ContentAction action,
2242 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002243 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002244
2245 const DataContentDescription* data =
2246 static_cast<const DataContentDescription*>(content);
2247 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002248 if (!data) {
2249 SafeSetError("Can't find data content in remote description.", error_desc);
2250 return false;
2251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002253 // If the remote data doesn't have codecs and isn't an update, it
2254 // must be empty, so ignore it.
2255 if (!data->has_codecs() && action != CA_UPDATE) {
2256 return true;
2257 }
2258
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002259 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002260 return false;
2261 }
2262
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002263 LOG(LS_INFO) << "Setting remote data description";
2264 if (data_channel_type_ == DCT_RTP &&
2265 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2266 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267 }
2268
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002269
2270 DataSendParameters send_params = last_send_params_;
2271 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2272 if (!media_channel()->SetSendParameters(send_params)) {
2273 SafeSetError("Failed to set remote data description send parameters.",
2274 error_desc);
2275 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002277 last_send_params_ = send_params;
2278
2279 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2280 // and only give it to the media channel once we have a local
2281 // description too (without a local description, we won't be able to
2282 // recv them anyway).
2283 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2284 SafeSetError("Failed to set remote data description streams.",
2285 error_desc);
2286 return false;
2287 }
2288
2289 set_remote_content_direction(content->direction());
2290 ChangeState();
2291 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292}
2293
2294void DataChannel::ChangeState() {
2295 // Render incoming data if we're the active call, and we have the local
2296 // content. We receive data on the default channel and multiplexed streams.
2297 bool recv = IsReadyToReceive();
2298 if (!media_channel()->SetReceive(recv)) {
2299 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2300 }
2301
2302 // Send outgoing data if we're the active call, we have the remote content,
2303 // and we have had some form of connectivity.
2304 bool send = IsReadyToSend();
2305 if (!media_channel()->SetSend(send)) {
2306 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2307 }
2308
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002309 // Trigger SignalReadyToSendData asynchronously.
2310 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311
2312 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2313}
2314
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002315void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002316 switch (pmsg->message_id) {
2317 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002318 DataChannelReadyToSendMessageData* data =
2319 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002320 ready_to_send_data_ = data->data();
2321 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002322 delete data;
2323 break;
2324 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002325 case MSG_DATARECEIVED: {
2326 DataReceivedMessageData* data =
2327 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2328 SignalDataReceived(this, data->params, data->payload);
2329 delete data;
2330 break;
2331 }
2332 case MSG_CHANNEL_ERROR: {
2333 const DataChannelErrorMessageData* data =
2334 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2335 SignalMediaError(this, data->ssrc, data->error);
2336 delete data;
2337 break;
2338 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002339 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002340 rtc::TypedMessageData<uint32>* data =
2341 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002342 SignalStreamClosedRemotely(data->data());
2343 delete data;
2344 break;
2345 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002346 default:
2347 BaseChannel::OnMessage(pmsg);
2348 break;
2349 }
2350}
2351
2352void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002353 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354 SignalConnectionMonitor(this, infos);
2355}
2356
2357void DataChannel::StartMediaMonitor(int cms) {
2358 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002359 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002360 media_monitor_->SignalUpdate.connect(
2361 this, &DataChannel::OnMediaMonitorUpdate);
2362 media_monitor_->Start(cms);
2363}
2364
2365void DataChannel::StopMediaMonitor() {
2366 if (media_monitor_) {
2367 media_monitor_->Stop();
2368 media_monitor_->SignalUpdate.disconnect(this);
2369 media_monitor_.reset();
2370 }
2371}
2372
2373void DataChannel::OnMediaMonitorUpdate(
2374 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2375 ASSERT(media_channel == this->media_channel());
2376 SignalMediaMonitor(this, info);
2377}
2378
2379void DataChannel::OnDataReceived(
2380 const ReceiveDataParams& params, const char* data, size_t len) {
2381 DataReceivedMessageData* msg = new DataReceivedMessageData(
2382 params, data, len);
2383 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2384}
2385
2386void DataChannel::OnDataChannelError(
2387 uint32 ssrc, DataMediaChannel::Error err) {
2388 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2389 ssrc, err);
2390 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2391}
2392
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002393void DataChannel::OnDataChannelReadyToSend(bool writable) {
2394 // This is usded for congestion control to indicate that the stream is ready
2395 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2396 // that the transport channel is ready.
2397 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2398 new DataChannelReadyToSendMessageData(writable));
2399}
2400
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002401void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2402 SrtpFilter::Error error) {
2403 switch (error) {
2404 case SrtpFilter::ERROR_FAIL:
2405 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2406 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2407 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2408 break;
2409 case SrtpFilter::ERROR_AUTH:
2410 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2411 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2412 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2413 break;
2414 case SrtpFilter::ERROR_REPLAY:
2415 // Only receving channel should have this error.
2416 ASSERT(mode == SrtpFilter::UNPROTECT);
2417 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2418 break;
2419 default:
2420 break;
2421 }
2422}
2423
2424void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2425 GetSupportedDataCryptoSuites(ciphers);
2426}
2427
2428bool DataChannel::ShouldSetupDtlsSrtp() const {
2429 return (data_channel_type_ == DCT_RTP);
2430}
2431
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002432void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002433 rtc::TypedMessageData<uint32>* message =
2434 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002435 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2436}
2437
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002438} // namespace cricket