blob: f65579f98b221b2caac5e0feea8a0d3a395bc0f0 [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 SafeSetError(const std::string& message, std::string* error_desc) {
64 if (error_desc) {
65 *error_desc = message;
66 }
67}
68
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000069struct PacketMessageData : public rtc::MessageData {
70 rtc::Buffer packet;
71 rtc::DiffServCodePoint dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072};
73
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074struct ScreencastEventMessageData : public rtc::MessageData {
75 ScreencastEventMessageData(uint32 s, rtc::WindowEvent we)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 : ssrc(s),
77 event(we) {
78 }
79 uint32 ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081};
82
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000083struct VoiceChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 VoiceChannelErrorMessageData(uint32 in_ssrc,
85 VoiceMediaChannel::Error in_error)
86 : ssrc(in_ssrc),
87 error(in_error) {
88 }
89 uint32 ssrc;
90 VoiceMediaChannel::Error error;
91};
92
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093struct VideoChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 VideoChannelErrorMessageData(uint32 in_ssrc,
95 VideoMediaChannel::Error in_error)
96 : ssrc(in_ssrc),
97 error(in_error) {
98 }
99 uint32 ssrc;
100 VideoMediaChannel::Error error;
101};
102
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000103struct DataChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 DataChannelErrorMessageData(uint32 in_ssrc,
105 DataMediaChannel::Error in_error)
106 : ssrc(in_ssrc),
107 error(in_error) {}
108 uint32 ssrc;
109 DataMediaChannel::Error error;
110};
111
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000113struct VideoChannel::ScreencastDetailsData {
114 explicit ScreencastDetailsData(uint32 s)
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000115 : ssrc(s), fps(0), screencast_max_pixels(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 }
117 uint32 ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000118 int fps;
119 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000120};
121
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122static const char* PacketType(bool rtcp) {
123 return (!rtcp) ? "RTP" : "RTCP";
124}
125
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000126static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 // Check the packet size. We could check the header too if needed.
128 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000129 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
130 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131}
132
133static bool IsReceiveContentDirection(MediaContentDirection direction) {
134 return direction == MD_SENDRECV || direction == MD_RECVONLY;
135}
136
137static bool IsSendContentDirection(MediaContentDirection direction) {
138 return direction == MD_SENDRECV || direction == MD_SENDONLY;
139}
140
141static const MediaContentDescription* GetContentDescription(
142 const ContentInfo* cinfo) {
143 if (cinfo == NULL)
144 return NULL;
145 return static_cast<const MediaContentDescription*>(cinfo->description);
146}
147
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700148template <class Codec>
149void RtpParametersFromMediaDescription(
150 const MediaContentDescriptionImpl<Codec>* desc,
151 RtpParameters<Codec>* params) {
152 // TODO(pthatcher): Remove this once we're sure no one will give us
153 // a description without codecs (currently a CA_UPDATE with just
154 // streams can).
155 if (desc->has_codecs()) {
156 params->codecs = desc->codecs();
157 }
158 // TODO(pthatcher): See if we really need
159 // rtp_header_extensions_set() and remove it if we don't.
160 if (desc->rtp_header_extensions_set()) {
161 params->extensions = desc->rtp_header_extensions();
162 }
163}
164
165template <class Codec, class Options>
166void RtpSendParametersFromMediaDescription(
167 const MediaContentDescriptionImpl<Codec>* desc,
168 RtpSendParameters<Codec, Options>* send_params) {
169 RtpParametersFromMediaDescription(desc, send_params);
170 send_params->max_bandwidth_bps = desc->bandwidth();
171}
172
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000173BaseChannel::BaseChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 MediaChannel* media_channel, BaseSession* session,
175 const std::string& content_name, bool rtcp)
176 : worker_thread_(thread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 session_(session),
178 media_channel_(media_channel),
179 content_name_(content_name),
180 rtcp_(rtcp),
181 transport_channel_(NULL),
182 rtcp_transport_channel_(NULL),
183 enabled_(false),
184 writable_(false),
185 rtp_ready_to_send_(false),
186 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 was_ever_writable_(false),
188 local_content_direction_(MD_INACTIVE),
189 remote_content_direction_(MD_INACTIVE),
190 has_received_packet_(false),
191 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000192 secure_required_(false),
193 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000194 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 LOG(LS_INFO) << "Created channel for " << content_name;
196}
197
198BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000199 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000200 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 StopConnectionMonitor();
202 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000203 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204 // We must destroy the media channel before the transport channel, otherwise
205 // the media channel may try to send on the dead transport channel. NULLing
206 // is not an effective strategy since the sends will come on another thread.
207 delete media_channel_;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000208 set_transport_channel(nullptr);
209 set_rtcp_transport_channel(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 LOG(LS_INFO) << "Destroyed channel";
211}
212
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000213bool BaseChannel::Init() {
214 if (!SetTransportChannels(session(), rtcp())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 return false;
216 }
217
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000218 if (!SetDtlsSrtpCiphers(transport_channel(), false)) {
219 return false;
220 }
221 if (rtcp() && !SetDtlsSrtpCiphers(rtcp_transport_channel(), true)) {
222 return false;
223 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224
wu@webrtc.orgde305012013-10-31 15:40:38 +0000225 // Both RTP and RTCP channels are set, we can call SetInterface on
226 // media channel and it can set network options.
227 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228 return true;
229}
230
wu@webrtc.org78187522013-10-07 23:32:02 +0000231void BaseChannel::Deinit() {
232 media_channel_->SetInterface(NULL);
233}
234
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000235bool BaseChannel::SetTransportChannels(BaseSession* session, bool rtcp) {
236 return worker_thread_->Invoke<bool>(Bind(
237 &BaseChannel::SetTransportChannels_w, this, session, rtcp));
238}
239
240bool BaseChannel::SetTransportChannels_w(BaseSession* session, bool rtcp) {
241 ASSERT(worker_thread_ == rtc::Thread::Current());
242
243 set_transport_channel(session->CreateChannel(
244 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTP));
245 if (!transport_channel()) {
246 return false;
247 }
248 if (rtcp) {
249 set_rtcp_transport_channel(session->CreateChannel(
250 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTCP));
251 if (!rtcp_transport_channel()) {
252 return false;
253 }
254 } else {
255 set_rtcp_transport_channel(nullptr);
256 }
257
258 return true;
259}
260
261void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
262 ASSERT(worker_thread_ == rtc::Thread::Current());
263
264 TransportChannel* old_tc = transport_channel_;
265
266 if (old_tc == new_tc) {
267 return;
268 }
269 if (old_tc) {
270 DisconnectFromTransportChannel(old_tc);
271 session()->DestroyChannel(
272 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTP);
273 }
274
275 transport_channel_ = new_tc;
276
277 if (new_tc) {
278 ConnectToTransportChannel(new_tc);
279 }
280}
281
282void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
283 ASSERT(worker_thread_ == rtc::Thread::Current());
284
285 TransportChannel* old_tc = rtcp_transport_channel_;
286
287 if (old_tc == new_tc) {
288 return;
289 }
290 if (old_tc) {
291 DisconnectFromTransportChannel(old_tc);
292 session()->DestroyChannel(
293 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTCP);
294 }
295
296 rtcp_transport_channel_ = new_tc;
297
298 if (new_tc) {
299 ConnectToTransportChannel(new_tc);
300 }
301}
302
303void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
304 ASSERT(worker_thread_ == rtc::Thread::Current());
305
306 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
307 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
308 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
309}
310
311void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
312 ASSERT(worker_thread_ == rtc::Thread::Current());
313
314 tc->SignalWritableState.disconnect(this);
315 tc->SignalReadPacket.disconnect(this);
316 tc->SignalReadyToSend.disconnect(this);
317}
318
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000320 worker_thread_->Invoke<void>(Bind(
321 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
322 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000323 return true;
324}
325
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326bool BaseChannel::MuteStream(uint32 ssrc, bool mute) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000327 return InvokeOnWorker(Bind(&BaseChannel::MuteStream_w, this, ssrc, mute));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328}
329
330bool BaseChannel::IsStreamMuted(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000331 return InvokeOnWorker(Bind(&BaseChannel::IsStreamMuted_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332}
333
334bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000335 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000336}
337
338bool BaseChannel::RemoveRecvStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000339 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340}
341
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000342bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000343 return InvokeOnWorker(
344 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000345}
346
347bool BaseChannel::RemoveSendStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000348 return InvokeOnWorker(
349 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000350}
351
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000353 ContentAction action,
354 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000355 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
356 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357}
358
359bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000360 ContentAction action,
361 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000362 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
363 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364}
365
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000367 // We pass in the BaseChannel instead of the transport_channel_
368 // because if the transport_channel_ changes, the ConnectionMonitor
369 // would be pointing to the wrong TransportChannel.
370 connection_monitor_.reset(new ConnectionMonitor(
371 this, worker_thread(), rtc::Thread::Current()));
372 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000374 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375}
376
377void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000378 if (connection_monitor_) {
379 connection_monitor_->Stop();
380 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381 }
382}
383
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000384bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
385 ASSERT(worker_thread_ == rtc::Thread::Current());
386 return transport_channel_->GetStats(infos);
387}
388
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389bool BaseChannel::IsReadyToReceive() const {
390 // Receive data if we are enabled and have local content,
391 return enabled() && IsReceiveContentDirection(local_content_direction_);
392}
393
394bool BaseChannel::IsReadyToSend() const {
395 // Send outgoing data if we are enabled, have local and remote content,
396 // and we have had some form of connectivity.
397 return enabled() &&
398 IsReceiveContentDirection(remote_content_direction_) &&
399 IsSendContentDirection(local_content_direction_) &&
400 was_ever_writable();
401}
402
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000403bool BaseChannel::SendPacket(rtc::Buffer* packet,
404 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000405 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406}
407
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000408bool BaseChannel::SendRtcp(rtc::Buffer* packet,
409 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000410 return SendPacket(true, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411}
412
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000413int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000415 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000417 case ST_RTP:
418 channel = transport_channel_;
419 break;
420 case ST_RTCP:
421 channel = rtcp_transport_channel_;
422 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000424 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425}
426
427void BaseChannel::OnWritableState(TransportChannel* channel) {
428 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
429 if (transport_channel_->writable()
430 && (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
431 ChannelWritable_w();
432 } else {
433 ChannelNotWritable_w();
434 }
435}
436
437void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000438 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000439 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000440 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000442 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443
444 // When using RTCP multiplexing we might get RTCP packets on the RTP
445 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
446 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000447 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000448 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449}
450
451void BaseChannel::OnReadyToSend(TransportChannel* channel) {
452 SetReadyToSend(channel, true);
453}
454
455void BaseChannel::SetReadyToSend(TransportChannel* channel, bool ready) {
456 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
457 if (channel == transport_channel_) {
458 rtp_ready_to_send_ = ready;
459 }
460 if (channel == rtcp_transport_channel_) {
461 rtcp_ready_to_send_ = ready;
462 }
463
464 if (!ready) {
465 // Notify the MediaChannel when either rtp or rtcp channel can't send.
466 media_channel_->OnReadyToSend(false);
467 } else if (rtp_ready_to_send_ &&
468 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
469 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
470 // Notify the MediaChannel when both rtp and rtcp channel can send.
471 media_channel_->OnReadyToSend(true);
472 }
473}
474
475bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
476 const char* data, size_t len) {
477 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000478 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479}
480
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000481bool BaseChannel::SendPacket(bool rtcp, rtc::Buffer* packet,
482 rtc::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483 // SendPacket gets called from MediaEngine, typically on an encoder thread.
484 // If the thread is not our worker thread, we will post to our worker
485 // so that the real work happens on our worker. This avoids us having to
486 // synchronize access to all the pieces of the send path, including
487 // SRTP and the inner workings of the transport channels.
488 // The only downside is that we can't return a proper failure code if
489 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000490 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491 // Avoid a copy by transferring the ownership of the packet data.
492 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
493 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200494 data->packet = packet->Pass();
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000495 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496 worker_thread_->Post(this, message_id, data);
497 return true;
498 }
499
500 // Now that we are on the correct thread, ensure we have a place to send this
501 // packet before doing anything. (We might get RTCP packets that we don't
502 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
503 // transport.
504 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
505 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000506 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507 return false;
508 }
509
510 // Protect ourselves against crazy data.
511 if (!ValidPacket(rtcp, packet)) {
512 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000513 << PacketType(rtcp)
514 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 return false;
516 }
517
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000518 rtc::PacketOptions options(dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 // Protect if needed.
520 if (srtp_filter_.IsActive()) {
521 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200522 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000523 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000525 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
526 // inside libsrtp for a RTP packet. A external HMAC module will be writing
527 // a fake HMAC value. This is ONLY done for a RTP packet.
528 // Socket layer will update rtp sendtime extension header if present in
529 // packet with current time before updating the HMAC.
530#if !defined(ENABLE_EXTERNAL_AUTH)
531 res = srtp_filter_.ProtectRtp(
532 data, len, static_cast<int>(packet->capacity()), &len);
533#else
henrike@webrtc.org05376342014-03-10 15:53:12 +0000534 options.packet_time_params.rtp_sendtime_extension_id =
535 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000536 res = srtp_filter_.ProtectRtp(
537 data, len, static_cast<int>(packet->capacity()), &len,
538 &options.packet_time_params.srtp_packet_index);
539 // If protection succeeds, let's get auth params from srtp.
540 if (res) {
541 uint8* auth_key = NULL;
542 int key_len;
543 res = srtp_filter_.GetRtpAuthParams(
544 &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
545 if (res) {
546 options.packet_time_params.srtp_auth_key.resize(key_len);
547 options.packet_time_params.srtp_auth_key.assign(auth_key,
548 auth_key + key_len);
549 }
550 }
551#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 if (!res) {
553 int seq_num = -1;
554 uint32 ssrc = 0;
555 GetRtpSeqNum(data, len, &seq_num);
556 GetRtpSsrc(data, len, &ssrc);
557 LOG(LS_ERROR) << "Failed to protect " << content_name_
558 << " RTP packet: size=" << len
559 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
560 return false;
561 }
562 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000563 res = srtp_filter_.ProtectRtcp(data, len,
564 static_cast<int>(packet->capacity()),
565 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000566 if (!res) {
567 int type = -1;
568 GetRtcpType(data, len, &type);
569 LOG(LS_ERROR) << "Failed to protect " << content_name_
570 << " RTCP packet: size=" << len << ", type=" << type;
571 return false;
572 }
573 }
574
575 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000576 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 } else if (secure_required_) {
578 // This is a double check for something that supposedly can't happen.
579 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
580 << " packet when SRTP is inactive and crypto is required";
581
582 ASSERT(false);
583 return false;
584 }
585
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000586 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000587 int ret =
Karl Wiberg94784372015-04-20 14:03:07 +0200588 channel->SendPacket(packet->data<char>(), packet->size(), options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000589 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
590 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591 if (channel->GetError() == EWOULDBLOCK) {
592 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
593 SetReadyToSend(channel, false);
594 }
595 return false;
596 }
597 return true;
598}
599
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000600bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000601 // Protect ourselves against crazy data.
602 if (!ValidPacket(rtcp, packet)) {
603 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000604 << PacketType(rtcp)
605 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 return false;
607 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000608
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000609 // Bundle filter handles both rtp and rtcp packets.
Karl Wiberg94784372015-04-20 14:03:07 +0200610 return bundle_filter_.DemuxPacket(packet->data<char>(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000611}
612
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000613void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
614 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615 if (!WantsPacket(rtcp, packet)) {
616 return;
617 }
618
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000619 // We are only interested in the first rtp packet because that
620 // indicates the media has started flowing.
621 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 has_received_packet_ = true;
623 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
624 }
625
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626 // Unprotect the packet, if needed.
627 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200628 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000629 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000630 bool res;
631 if (!rtcp) {
632 res = srtp_filter_.UnprotectRtp(data, len, &len);
633 if (!res) {
634 int seq_num = -1;
635 uint32 ssrc = 0;
636 GetRtpSeqNum(data, len, &seq_num);
637 GetRtpSsrc(data, len, &ssrc);
638 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
639 << " RTP packet: size=" << len
640 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
641 return;
642 }
643 } else {
644 res = srtp_filter_.UnprotectRtcp(data, len, &len);
645 if (!res) {
646 int type = -1;
647 GetRtcpType(data, len, &type);
648 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
649 << " RTCP packet: size=" << len << ", type=" << type;
650 return;
651 }
652 }
653
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000654 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655 } else if (secure_required_) {
656 // Our session description indicates that SRTP is required, but we got a
657 // packet before our SRTP filter is active. This means either that
658 // a) we got SRTP packets before we received the SDES keys, in which case
659 // we can't decrypt it anyway, or
660 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
661 // channels, so we haven't yet extracted keys, even if DTLS did complete
662 // on the channel that the packets are being sent on. It's really good
663 // practice to wait for both RTP and RTCP to be good to go before sending
664 // media, to prevent weird failure modes, so it's fine for us to just eat
665 // packets here. This is all sidestepped if RTCP mux is used anyway.
666 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
667 << " packet when SRTP is inactive and crypto is required";
668 return;
669 }
670
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671 // Push it down to the media channel.
672 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000673 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000675 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 }
677}
678
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000679bool BaseChannel::PushdownLocalDescription(
680 const SessionDescription* local_desc, ContentAction action,
681 std::string* error_desc) {
682 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 const MediaContentDescription* content_desc =
684 GetContentDescription(content_info);
685 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000686 !SetLocalContent(content_desc, action, error_desc)) {
687 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
688 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000690 return true;
691}
692
693bool BaseChannel::PushdownRemoteDescription(
694 const SessionDescription* remote_desc, ContentAction action,
695 std::string* error_desc) {
696 const ContentInfo* content_info = GetFirstContent(remote_desc);
697 const MediaContentDescription* content_desc =
698 GetContentDescription(content_info);
699 if (content_desc && content_info && !content_info->rejected &&
700 !SetRemoteContent(content_desc, action, error_desc)) {
701 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
702 return false;
703 }
704 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705}
706
707void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000708 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 if (enabled_)
710 return;
711
712 LOG(LS_INFO) << "Channel enabled";
713 enabled_ = true;
714 ChangeState();
715}
716
717void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000718 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 if (!enabled_)
720 return;
721
722 LOG(LS_INFO) << "Channel disabled";
723 enabled_ = false;
724 ChangeState();
725}
726
727bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000728 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000729 bool ret = media_channel()->MuteStream(ssrc, mute);
730 if (ret) {
731 if (mute)
732 muted_streams_.insert(ssrc);
733 else
734 muted_streams_.erase(ssrc);
735 }
736 return ret;
737}
738
739bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000740 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 return muted_streams_.find(ssrc) != muted_streams_.end();
742}
743
744void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000745 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 if (writable_)
747 return;
748
749 LOG(LS_INFO) << "Channel socket writable ("
750 << transport_channel_->content_name() << ", "
751 << transport_channel_->component() << ")"
752 << (was_ever_writable_ ? "" : " for the first time");
753
754 std::vector<ConnectionInfo> infos;
755 transport_channel_->GetStats(&infos);
756 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
757 it != infos.end(); ++it) {
758 if (it->best_connection) {
759 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
760 << "->" << it->remote_candidate.ToSensitiveString();
761 break;
762 }
763 }
764
765 // If we're doing DTLS-SRTP, now is the time.
766 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
767 if (!SetupDtlsSrtp(false)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000768 SignalDtlsSetupFailure(this, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000769 return;
770 }
771
772 if (rtcp_transport_channel_) {
773 if (!SetupDtlsSrtp(true)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000774 SignalDtlsSetupFailure(this, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 return;
776 }
777 }
778 }
779
780 was_ever_writable_ = true;
781 writable_ = true;
782 ChangeState();
783}
784
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000785void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
786 ASSERT(worker_thread() == rtc::Thread::Current());
787 signaling_thread()->Invoke<void>(Bind(
788 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
789}
790
791void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
792 ASSERT(signaling_thread() == rtc::Thread::Current());
793 SignalDtlsSetupFailure(this, rtcp);
794}
795
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000796bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
797 std::vector<std::string> ciphers;
798 // We always use the default SRTP ciphers for RTCP, but we may use different
799 // ciphers for RTP depending on the media type.
800 if (!rtcp) {
801 GetSrtpCiphers(&ciphers);
802 } else {
803 GetSupportedDefaultCryptoSuites(&ciphers);
804 }
805 return tc->SetSrtpCiphers(ciphers);
806}
807
808bool BaseChannel::ShouldSetupDtlsSrtp() const {
809 return true;
810}
811
812// This function returns true if either DTLS-SRTP is not in use
813// *or* DTLS-SRTP is successfully set up.
814bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
815 bool ret = false;
816
817 TransportChannel *channel = rtcp_channel ?
818 rtcp_transport_channel_ : transport_channel_;
819
820 // No DTLS
821 if (!channel->IsDtlsActive())
822 return true;
823
824 std::string selected_cipher;
825
826 if (!channel->GetSrtpCipher(&selected_cipher)) {
827 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
828 return false;
829 }
830
831 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
832 << content_name() << " "
833 << PacketType(rtcp_channel);
834
835 // OK, we're now doing DTLS (RFC 5764)
836 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
837 SRTP_MASTER_KEY_SALT_LEN * 2);
838
839 // RFC 5705 exporter using the RFC 5764 parameters
840 if (!channel->ExportKeyingMaterial(
841 kDtlsSrtpExporterLabel,
842 NULL, 0, false,
843 &dtls_buffer[0], dtls_buffer.size())) {
844 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
845 ASSERT(false); // This should never happen
846 return false;
847 }
848
849 // Sync up the keys with the DTLS-SRTP interface
850 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
851 SRTP_MASTER_KEY_SALT_LEN);
852 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
853 SRTP_MASTER_KEY_SALT_LEN);
854 size_t offset = 0;
855 memcpy(&client_write_key[0], &dtls_buffer[offset],
856 SRTP_MASTER_KEY_KEY_LEN);
857 offset += SRTP_MASTER_KEY_KEY_LEN;
858 memcpy(&server_write_key[0], &dtls_buffer[offset],
859 SRTP_MASTER_KEY_KEY_LEN);
860 offset += SRTP_MASTER_KEY_KEY_LEN;
861 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
862 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
863 offset += SRTP_MASTER_KEY_SALT_LEN;
864 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
865 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
866
867 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000868 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000869 if (!channel->GetSslRole(&role)) {
870 LOG(LS_WARNING) << "GetSslRole failed";
871 return false;
872 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000873
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000874 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000875 send_key = &server_write_key;
876 recv_key = &client_write_key;
877 } else {
878 send_key = &client_write_key;
879 recv_key = &server_write_key;
880 }
881
882 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000883 ret = srtp_filter_.SetRtcpParams(
884 selected_cipher,
885 &(*send_key)[0],
886 static_cast<int>(send_key->size()),
887 selected_cipher,
888 &(*recv_key)[0],
889 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000891 ret = srtp_filter_.SetRtpParams(
892 selected_cipher,
893 &(*send_key)[0],
894 static_cast<int>(send_key->size()),
895 selected_cipher,
896 &(*recv_key)[0],
897 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 }
899
900 if (!ret)
901 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
902 else
903 dtls_keyed_ = true;
904
905 return ret;
906}
907
908void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000909 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910 if (!writable_)
911 return;
912
913 LOG(LS_INFO) << "Channel socket not writable ("
914 << transport_channel_->content_name() << ", "
915 << transport_channel_->component() << ")";
916 writable_ = false;
917 ChangeState();
918}
919
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700920bool BaseChannel::SetRtpTransportParameters_w(
921 const MediaContentDescription* content,
922 ContentAction action,
923 ContentSource src,
924 std::string* error_desc) {
925 if (action == CA_UPDATE) {
926 // These parameters never get changed by a CA_UDPATE.
927 return true;
928 }
929
930 // Cache secure_required_ for belt and suspenders check on SendPacket
931 if (src == CS_LOCAL) {
932 set_secure_required(content->crypto_required() != CT_NONE);
933 }
934
935 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
936 return false;
937 }
938
939 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
940 return false;
941 }
942
943 return true;
944}
945
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000946// |dtls| will be set to true if DTLS is active for transport channel and
947// crypto is empty.
948bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000949 bool* dtls,
950 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000951 *dtls = transport_channel_->IsDtlsActive();
952 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000953 SafeSetError("Cryptos must be empty when DTLS is active.",
954 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000955 return false;
956 }
957 return true;
958}
959
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000961 ContentAction action,
962 ContentSource src,
963 std::string* error_desc) {
964 if (action == CA_UPDATE) {
965 // no crypto params.
966 return true;
967 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000969 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000970 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
971 if (!ret) {
972 return false;
973 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 switch (action) {
975 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000976 // If DTLS is already active on the channel, we could be renegotiating
977 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000978 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000979 ret = srtp_filter_.SetOffer(cryptos, src);
980 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 break;
982 case CA_PRANSWER:
983 // If we're doing DTLS-SRTP, we don't want to update the filter
984 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000985 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
987 }
988 break;
989 case CA_ANSWER:
990 // If we're doing DTLS-SRTP, we don't want to update the filter
991 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000992 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993 ret = srtp_filter_.SetAnswer(cryptos, src);
994 }
995 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 default:
997 break;
998 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000999 if (!ret) {
1000 SafeSetError("Failed to setup SRTP filter.", error_desc);
1001 return false;
1002 }
1003 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004}
1005
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001006void BaseChannel::ActivateRtcpMux() {
1007 worker_thread_->Invoke<void>(Bind(
1008 &BaseChannel::ActivateRtcpMux_w, this));
1009}
1010
1011void BaseChannel::ActivateRtcpMux_w() {
1012 if (!rtcp_mux_filter_.IsActive()) {
1013 rtcp_mux_filter_.SetActive();
1014 set_rtcp_transport_channel(NULL);
1015 }
1016}
1017
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001018bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001019 ContentSource src,
1020 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 bool ret = false;
1022 switch (action) {
1023 case CA_OFFER:
1024 ret = rtcp_mux_filter_.SetOffer(enable, src);
1025 break;
1026 case CA_PRANSWER:
1027 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1028 break;
1029 case CA_ANSWER:
1030 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1031 if (ret && rtcp_mux_filter_.IsActive()) {
1032 // We activated RTCP mux, close down the RTCP transport.
1033 set_rtcp_transport_channel(NULL);
1034 }
1035 break;
1036 case CA_UPDATE:
1037 // No RTCP mux info.
1038 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001039 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 default:
1041 break;
1042 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001043 if (!ret) {
1044 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1045 return false;
1046 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1048 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1049 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001050 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 // If the RTP transport is already writable, then so are we.
1052 if (transport_channel_->writable()) {
1053 ChannelWritable_w();
1054 }
1055 }
1056
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001057 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058}
1059
1060bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001061 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 if (!media_channel()->AddRecvStream(sp))
1063 return false;
1064
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001065 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066}
1067
1068bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001069 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001070 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 return media_channel()->RemoveRecvStream(ssrc);
1072}
1073
1074bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001075 ContentAction action,
1076 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1078 action == CA_PRANSWER || action == CA_UPDATE))
1079 return false;
1080
1081 // If this is an update, streams only contain streams that have changed.
1082 if (action == CA_UPDATE) {
1083 for (StreamParamsVec::const_iterator it = streams.begin();
1084 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001085 const StreamParams* existing_stream =
1086 GetStreamByIds(local_streams_, it->groupid, it->id);
1087 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088 if (media_channel()->AddSendStream(*it)) {
1089 local_streams_.push_back(*it);
1090 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1091 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001092 std::ostringstream desc;
1093 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1094 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 return false;
1096 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001097 } else if (existing_stream && !it->has_ssrcs()) {
1098 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001099 std::ostringstream desc;
1100 desc << "Failed to remove send stream with ssrc "
1101 << it->first_ssrc() << ".";
1102 SafeSetError(desc.str(), error_desc);
1103 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001105 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 } else {
1107 LOG(LS_WARNING) << "Ignore unsupported stream update";
1108 }
1109 }
1110 return true;
1111 }
1112 // Else streams are all the streams we want to send.
1113
1114 // Check for streams that have been removed.
1115 bool ret = true;
1116 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1117 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001118 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001120 std::ostringstream desc;
1121 desc << "Failed to remove send stream with ssrc "
1122 << it->first_ssrc() << ".";
1123 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 ret = false;
1125 }
1126 }
1127 }
1128 // Check for new streams.
1129 for (StreamParamsVec::const_iterator it = streams.begin();
1130 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001131 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132 if (media_channel()->AddSendStream(*it)) {
1133 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1134 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001135 std::ostringstream desc;
1136 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1137 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 ret = false;
1139 }
1140 }
1141 }
1142 local_streams_ = streams;
1143 return ret;
1144}
1145
1146bool BaseChannel::UpdateRemoteStreams_w(
1147 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001148 ContentAction action,
1149 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1151 action == CA_PRANSWER || action == CA_UPDATE))
1152 return false;
1153
1154 // If this is an update, streams only contain streams that have changed.
1155 if (action == CA_UPDATE) {
1156 for (StreamParamsVec::const_iterator it = streams.begin();
1157 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001158 const StreamParams* existing_stream =
1159 GetStreamByIds(remote_streams_, it->groupid, it->id);
1160 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 if (AddRecvStream_w(*it)) {
1162 remote_streams_.push_back(*it);
1163 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1164 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001165 std::ostringstream desc;
1166 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1167 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 return false;
1169 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001170 } else if (existing_stream && !it->has_ssrcs()) {
1171 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001172 std::ostringstream desc;
1173 desc << "Failed to remove remote stream with ssrc "
1174 << it->first_ssrc() << ".";
1175 SafeSetError(desc.str(), error_desc);
1176 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001178 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 } else {
1180 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001181 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 << " new stream = " << it->ToString();
1183 }
1184 }
1185 return true;
1186 }
1187 // Else streams are all the streams we want to receive.
1188
1189 // Check for streams that have been removed.
1190 bool ret = true;
1191 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1192 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001193 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001194 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001195 std::ostringstream desc;
1196 desc << "Failed to remove remote stream with ssrc "
1197 << it->first_ssrc() << ".";
1198 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 ret = false;
1200 }
1201 }
1202 }
1203 // Check for new streams.
1204 for (StreamParamsVec::const_iterator it = streams.begin();
1205 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001206 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001207 if (AddRecvStream_w(*it)) {
1208 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1209 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001210 std::ostringstream desc;
1211 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1212 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001213 ret = false;
1214 }
1215 }
1216 }
1217 remote_streams_ = streams;
1218 return ret;
1219}
1220
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001221void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1222 const std::vector<RtpHeaderExtension>& extensions) {
1223 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001224 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001225 rtp_abs_sendtime_extn_id_ =
1226 send_time_extension ? send_time_extension->id : -1;
1227}
1228
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001229void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 case MSG_RTPPACKET:
1232 case MSG_RTCPPACKET: {
1233 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001234 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 delete data; // because it is Posted
1236 break;
1237 }
1238 case MSG_FIRSTPACKETRECEIVED: {
1239 SignalFirstPacketReceived(this);
1240 break;
1241 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 }
1243}
1244
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245void BaseChannel::FlushRtcpMessages() {
1246 // Flush all remaining RTCP messages. This should only be called in
1247 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001248 ASSERT(rtc::Thread::Current() == worker_thread_);
1249 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001250 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001251 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001253 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 }
1255}
1256
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001257VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 MediaEngineInterface* media_engine,
1259 VoiceMediaChannel* media_channel,
1260 BaseSession* session,
1261 const std::string& content_name,
1262 bool rtcp)
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001263 : BaseChannel(thread, media_channel, session, content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001265 media_engine_(media_engine),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001266 received_media_(false) {
1267}
1268
1269VoiceChannel::~VoiceChannel() {
1270 StopAudioMonitor();
1271 StopMediaMonitor();
1272 // this can't be done in the base class, since it calls a virtual
1273 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001274 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275}
1276
1277bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001278 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 return false;
1280 }
1281 media_channel()->SignalMediaError.connect(
1282 this, &VoiceChannel::OnVoiceChannelError);
1283 srtp_filter()->SignalSrtpError.connect(
1284 this, &VoiceChannel::OnSrtpError);
1285 return true;
1286}
1287
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001288bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001289 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1290 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001291}
1292
1293bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001294 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1295 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296}
1297
1298bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001299 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300}
1301
1302// TODO(juberti): Handle early media the right way. We should get an explicit
1303// ringing message telling us to start playing local ringback, which we cancel
1304// if any early media actually arrives. For now, we do the opposite, which is
1305// to wait 1 second for early media, and start playing local ringback if none
1306// arrives.
1307void VoiceChannel::SetEarlyMedia(bool enable) {
1308 if (enable) {
1309 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001310 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1311 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 } else {
1313 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001314 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 }
1316}
1317
1318bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001319 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1320 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321}
1322
1323bool VoiceChannel::PressDTMF(int digit, bool playout) {
1324 int flags = DF_SEND;
1325 if (playout) {
1326 flags |= DF_PLAY;
1327 }
1328 int duration_ms = 160;
1329 return InsertDtmf(0, digit, duration_ms, flags);
1330}
1331
1332bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001333 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1334 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335}
1336
1337bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1338 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001339 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1340 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341}
1342
1343bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001344 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1345 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001347
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001349 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1350 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351}
1352
1353void VoiceChannel::StartMediaMonitor(int cms) {
1354 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001355 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356 media_monitor_->SignalUpdate.connect(
1357 this, &VoiceChannel::OnMediaMonitorUpdate);
1358 media_monitor_->Start(cms);
1359}
1360
1361void VoiceChannel::StopMediaMonitor() {
1362 if (media_monitor_) {
1363 media_monitor_->Stop();
1364 media_monitor_->SignalUpdate.disconnect(this);
1365 media_monitor_.reset();
1366 }
1367}
1368
1369void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001370 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371 audio_monitor_
1372 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1373 audio_monitor_->Start(cms);
1374}
1375
1376void VoiceChannel::StopAudioMonitor() {
1377 if (audio_monitor_) {
1378 audio_monitor_->Stop();
1379 audio_monitor_.reset();
1380 }
1381}
1382
1383bool VoiceChannel::IsAudioMonitorRunning() const {
1384 return (audio_monitor_.get() != NULL);
1385}
1386
1387void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1388 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1389 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1390}
1391
1392void VoiceChannel::StopTypingMonitor() {
1393 typing_monitor_.reset();
1394}
1395
1396bool VoiceChannel::IsTypingMonitorRunning() const {
1397 return typing_monitor_;
1398}
1399
1400bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1401 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1402 if (typing_monitor_ && mute)
1403 typing_monitor_->OnChannelMuted();
1404 return ret;
1405}
1406
1407int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001408 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409}
1410
1411int VoiceChannel::GetOutputLevel_w() {
1412 return media_channel()->GetOutputLevel();
1413}
1414
1415void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1416 media_channel()->GetActiveStreams(actives);
1417}
1418
1419void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001420 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001421 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001422 int flags) {
1423 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424
1425 // Set a flag when we've received an RTP packet. If we're waiting for early
1426 // media, this will disable the timeout.
1427 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1428 received_media_ = true;
1429 }
1430}
1431
1432void VoiceChannel::ChangeState() {
1433 // Render incoming data if we're the active call, and we have the local
1434 // content. We receive data on the default channel and multiplexed streams.
1435 bool recv = IsReadyToReceive();
1436 if (!media_channel()->SetPlayout(recv)) {
1437 SendLastMediaError();
1438 }
1439
1440 // Send outgoing data if we're the active call, we have the remote content,
1441 // and we have had some form of connectivity.
1442 bool send = IsReadyToSend();
1443 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1444 if (!media_channel()->SetSend(send_flag)) {
1445 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1446 SendLastMediaError();
1447 }
1448
1449 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1450}
1451
1452const ContentInfo* VoiceChannel::GetFirstContent(
1453 const SessionDescription* sdesc) {
1454 return GetFirstAudioContent(sdesc);
1455}
1456
1457bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001458 ContentAction action,
1459 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001460 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 LOG(LS_INFO) << "Setting local voice description";
1462
1463 const AudioContentDescription* audio =
1464 static_cast<const AudioContentDescription*>(content);
1465 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001466 if (!audio) {
1467 SafeSetError("Can't find audio content in local description.", error_desc);
1468 return false;
1469 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001471 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1472 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 }
1474
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001475 AudioRecvParameters recv_params = last_recv_params_;
1476 RtpParametersFromMediaDescription(audio, &recv_params);
1477 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001478 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001479 error_desc);
1480 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001482 for (const AudioCodec& codec : audio->codecs()) {
1483 bundle_filter()->AddPayloadType(codec.id);
1484 }
1485 last_recv_params_ = recv_params;
1486
1487 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1488 // only give it to the media channel once we have a remote
1489 // description too (without a remote description, we won't be able
1490 // to send them anyway).
1491 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1492 SafeSetError("Failed to set local audio description streams.", error_desc);
1493 return false;
1494 }
1495
1496 set_local_content_direction(content->direction());
1497 ChangeState();
1498 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499}
1500
1501bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001502 ContentAction action,
1503 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001504 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505 LOG(LS_INFO) << "Setting remote voice description";
1506
1507 const AudioContentDescription* audio =
1508 static_cast<const AudioContentDescription*>(content);
1509 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001510 if (!audio) {
1511 SafeSetError("Can't find audio content in remote description.", error_desc);
1512 return false;
1513 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001515 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1516 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001517 }
1518
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001519 AudioSendParameters send_params = last_send_params_;
1520 RtpSendParametersFromMediaDescription(audio, &send_params);
1521 if (audio->conference_mode()) {
1522 send_params.options.conference_mode.Set(true);
1523 }
1524 if (audio->agc_minus_10db()) {
1525 send_params.options.adjust_agc_delta.Set(kAgcMinus10db);
1526 }
1527 if (!media_channel()->SetSendParameters(send_params)) {
1528 SafeSetError("Failed to set remote audio description send parameters.",
1529 error_desc);
1530 return false;
1531 }
1532 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001534 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1535 // and only give it to the media channel once we have a local
1536 // description too (without a local description, we won't be able to
1537 // recv them anyway).
1538 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1539 SafeSetError("Failed to set remote audio description streams.", error_desc);
1540 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 }
1542
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001543 if (audio->rtp_header_extensions_set()) {
1544 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1545 }
1546
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001547 set_remote_content_direction(content->direction());
1548 ChangeState();
1549 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550}
1551
1552bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001553 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1555}
1556
1557bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001558 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559 if (play) {
1560 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1561 } else {
1562 LOG(LS_INFO) << "Stopping ringback tone";
1563 }
1564 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1565}
1566
1567void VoiceChannel::HandleEarlyMediaTimeout() {
1568 // This occurs on the main thread, not the worker thread.
1569 if (!received_media_) {
1570 LOG(LS_INFO) << "No early media received before timeout";
1571 SignalEarlyMediaTimeout(this);
1572 }
1573}
1574
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1576 int flags) {
1577 if (!enabled()) {
1578 return false;
1579 }
1580
1581 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1582}
1583
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001585 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1586 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587}
1588
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001589void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 case MSG_EARLYMEDIATIMEOUT:
1592 HandleEarlyMediaTimeout();
1593 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 case MSG_CHANNEL_ERROR: {
1595 VoiceChannelErrorMessageData* data =
1596 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1597 SignalMediaError(this, data->ssrc, data->error);
1598 delete data;
1599 break;
1600 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601 default:
1602 BaseChannel::OnMessage(pmsg);
1603 break;
1604 }
1605}
1606
1607void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001608 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 SignalConnectionMonitor(this, infos);
1610}
1611
1612void VoiceChannel::OnMediaMonitorUpdate(
1613 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1614 ASSERT(media_channel == this->media_channel());
1615 SignalMediaMonitor(this, info);
1616}
1617
1618void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1619 const AudioInfo& info) {
1620 SignalAudioMonitor(this, info);
1621}
1622
1623void VoiceChannel::OnVoiceChannelError(
1624 uint32 ssrc, VoiceMediaChannel::Error err) {
1625 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1626 ssrc, err);
1627 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1628}
1629
1630void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1631 SrtpFilter::Error error) {
1632 switch (error) {
1633 case SrtpFilter::ERROR_FAIL:
1634 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1635 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1636 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1637 break;
1638 case SrtpFilter::ERROR_AUTH:
1639 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1640 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1641 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1642 break;
1643 case SrtpFilter::ERROR_REPLAY:
1644 // Only receving channel should have this error.
1645 ASSERT(mode == SrtpFilter::UNPROTECT);
1646 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1647 break;
1648 default:
1649 break;
1650 }
1651}
1652
1653void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1654 GetSupportedAudioCryptoSuites(ciphers);
1655}
1656
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001657VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 VideoMediaChannel* media_channel,
1659 BaseSession* session,
1660 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001661 bool rtcp)
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001662 : BaseChannel(thread, media_channel, session, content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001663 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001665 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666}
1667
1668bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001669 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670 return false;
1671 }
1672 media_channel()->SignalMediaError.connect(
1673 this, &VideoChannel::OnVideoChannelError);
1674 srtp_filter()->SignalSrtpError.connect(
1675 this, &VideoChannel::OnSrtpError);
1676 return true;
1677}
1678
1679void VoiceChannel::SendLastMediaError() {
1680 uint32 ssrc;
1681 VoiceMediaChannel::Error error;
1682 media_channel()->GetLastMediaError(&ssrc, &error);
1683 SignalMediaError(this, ssrc, error);
1684}
1685
1686VideoChannel::~VideoChannel() {
1687 std::vector<uint32> screencast_ssrcs;
1688 ScreencastMap::iterator iter;
1689 while (!screencast_capturers_.empty()) {
1690 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1691 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1692 << screencast_capturers_.begin()->first;
1693 ASSERT(false);
1694 break;
1695 }
1696 }
1697
1698 StopMediaMonitor();
1699 // this can't be done in the base class, since it calls a virtual
1700 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001701
1702 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703}
1704
1705bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001706 worker_thread()->Invoke<void>(Bind(
1707 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 return true;
1709}
1710
1711bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001712 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713}
1714
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001715bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1716 return worker_thread()->Invoke<bool>(Bind(
1717 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718}
1719
1720bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001721 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1722 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723}
1724
1725bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001726 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727}
1728
1729bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001730 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731}
1732
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001733int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001734 ScreencastDetailsData data(ssrc);
1735 worker_thread()->Invoke<void>(Bind(
1736 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001737 return data.fps;
1738}
1739
1740int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001741 ScreencastDetailsData data(ssrc);
1742 worker_thread()->Invoke<void>(Bind(
1743 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001744 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745}
1746
1747bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001748 worker_thread()->Invoke<void>(Bind(
1749 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 return true;
1751}
1752
1753bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001754 worker_thread()->Invoke<void>(Bind(
1755 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 return true;
1757}
1758
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759void VideoChannel::ChangeState() {
1760 // Render incoming data if we're the active call, and we have the local
1761 // content. We receive data on the default channel and multiplexed streams.
1762 bool recv = IsReadyToReceive();
1763 if (!media_channel()->SetRender(recv)) {
1764 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1765 // TODO(gangji): Report error back to server.
1766 }
1767
1768 // Send outgoing data if we're the active call, we have the remote content,
1769 // and we have had some form of connectivity.
1770 bool send = IsReadyToSend();
1771 if (!media_channel()->SetSend(send)) {
1772 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1773 // TODO(gangji): Report error back to server.
1774 }
1775
1776 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1777}
1778
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001779bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1780 return InvokeOnWorker(
1781 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782}
1783
1784void VideoChannel::StartMediaMonitor(int cms) {
1785 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001786 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 media_monitor_->SignalUpdate.connect(
1788 this, &VideoChannel::OnMediaMonitorUpdate);
1789 media_monitor_->Start(cms);
1790}
1791
1792void VideoChannel::StopMediaMonitor() {
1793 if (media_monitor_) {
1794 media_monitor_->Stop();
1795 media_monitor_.reset();
1796 }
1797}
1798
1799const ContentInfo* VideoChannel::GetFirstContent(
1800 const SessionDescription* sdesc) {
1801 return GetFirstVideoContent(sdesc);
1802}
1803
1804bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001805 ContentAction action,
1806 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001807 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 LOG(LS_INFO) << "Setting local video description";
1809
1810 const VideoContentDescription* video =
1811 static_cast<const VideoContentDescription*>(content);
1812 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001813 if (!video) {
1814 SafeSetError("Can't find video content in local description.", error_desc);
1815 return false;
1816 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001818 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1819 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 }
1821
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001822 VideoRecvParameters recv_params = last_recv_params_;
1823 RtpParametersFromMediaDescription(video, &recv_params);
1824 if (!media_channel()->SetRecvParameters(recv_params)) {
1825 SafeSetError("Failed to set local video description recv parameters.",
1826 error_desc);
1827 return false;
1828 }
1829 for (const VideoCodec& codec : video->codecs()) {
1830 bundle_filter()->AddPayloadType(codec.id);
1831 }
1832 last_recv_params_ = recv_params;
1833
1834 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1835 // only give it to the media channel once we have a remote
1836 // description too (without a remote description, we won't be able
1837 // to send them anyway).
1838 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1839 SafeSetError("Failed to set local video description streams.", error_desc);
1840 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 }
1842
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001843 set_local_content_direction(content->direction());
1844 ChangeState();
1845 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846}
1847
1848bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001849 ContentAction action,
1850 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001851 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 LOG(LS_INFO) << "Setting remote video description";
1853
1854 const VideoContentDescription* video =
1855 static_cast<const VideoContentDescription*>(content);
1856 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001857 if (!video) {
1858 SafeSetError("Can't find video content in remote description.", error_desc);
1859 return false;
1860 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001862
1863 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1864 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 }
1866
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001867 VideoSendParameters send_params = last_send_params_;
1868 RtpSendParametersFromMediaDescription(video, &send_params);
1869 if (video->conference_mode()) {
1870 send_params.options.conference_mode.Set(true);
1871 }
1872 if (!media_channel()->SetSendParameters(send_params)) {
1873 SafeSetError("Failed to set remote video description send parameters.",
1874 error_desc);
1875 return false;
1876 }
1877 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001879 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1880 // and only give it to the media channel once we have a local
1881 // description too (without a local description, we won't be able to
1882 // recv them anyway).
1883 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1884 SafeSetError("Failed to set remote video description streams.", error_desc);
1885 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 }
1887
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001888 if (video->rtp_header_extensions_set()) {
1889 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001891
1892 set_remote_content_direction(content->direction());
1893 ChangeState();
1894 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895}
1896
1897bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1898 bool ret = true;
1899 // Set the send format for each of the local streams. If the view request
1900 // does not contain a local stream, set its send format to 0x0, which will
1901 // drop all frames.
1902 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1903 it != local_streams().end(); ++it) {
1904 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1905 StaticVideoViews::const_iterator view;
1906 for (view = request.static_video_views.begin();
1907 view != request.static_video_views.end(); ++view) {
1908 if (view->selector.Matches(*it)) {
1909 format.width = view->width;
1910 format.height = view->height;
1911 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1912 break;
1913 }
1914 }
1915
1916 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1917 }
1918
1919 // Check if the view request has invalid streams.
1920 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1921 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001922 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923 LOG(LS_WARNING) << "View request for ("
1924 << it->selector.ssrc << ", '"
1925 << it->selector.groupid << "', '"
1926 << it->selector.streamid << "'"
1927 << ") is not in the local streams.";
1928 }
1929 }
1930
1931 return ret;
1932}
1933
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001934bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001936 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001938 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1939 screencast_capturers_[ssrc] = capturer;
1940 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941}
1942
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1944 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1945 if (iter == screencast_capturers_.end()) {
1946 return false;
1947 }
1948 // Clean up VideoCapturer.
1949 delete iter->second;
1950 screencast_capturers_.erase(iter);
1951 return true;
1952}
1953
1954bool VideoChannel::IsScreencasting_w() const {
1955 return !screencast_capturers_.empty();
1956}
1957
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001958void VideoChannel::GetScreencastDetails_w(
1959 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001960 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001962 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 }
1964 VideoCapturer* capturer = iter->second;
1965 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001966 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1967 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968}
1969
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001971 rtc::WindowEvent we) {
1972 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973 SignalScreencastWindowEvent(ssrc, we);
1974}
1975
1976bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001977 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
1978 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979}
1980
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001981void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 case MSG_SCREENCASTWINDOWEVENT: {
1984 const ScreencastEventMessageData* data =
1985 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1986 OnScreencastWindowEvent_s(data->ssrc, data->event);
1987 delete data;
1988 break;
1989 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 case MSG_CHANNEL_ERROR: {
1991 const VideoChannelErrorMessageData* data =
1992 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
1993 SignalMediaError(this, data->ssrc, data->error);
1994 delete data;
1995 break;
1996 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997 default:
1998 BaseChannel::OnMessage(pmsg);
1999 break;
2000 }
2001}
2002
2003void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002004 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 SignalConnectionMonitor(this, infos);
2006}
2007
2008// TODO(pthatcher): Look into removing duplicate code between
2009// audio, video, and data, perhaps by using templates.
2010void VideoChannel::OnMediaMonitorUpdate(
2011 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2012 ASSERT(media_channel == this->media_channel());
2013 SignalMediaMonitor(this, info);
2014}
2015
2016void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002017 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018 ScreencastEventMessageData* pdata =
2019 new ScreencastEventMessageData(ssrc, event);
2020 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2021}
2022
2023void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2024 // Map capturer events to window events. In the future we may want to simply
2025 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002026 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002028 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002030 we = rtc::WE_MINIMIZE;
2031 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2032 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 } else {
2034 return;
2035 }
2036 previous_we_ = we;
2037
2038 uint32 ssrc = 0;
2039 if (!GetLocalSsrc(capturer, &ssrc)) {
2040 return;
2041 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002042
2043 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044}
2045
2046bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2047 *ssrc = 0;
2048 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2049 iter != screencast_capturers_.end(); ++iter) {
2050 if (iter->second == capturer) {
2051 *ssrc = iter->first;
2052 return true;
2053 }
2054 }
2055 return false;
2056}
2057
2058void VideoChannel::OnVideoChannelError(uint32 ssrc,
2059 VideoMediaChannel::Error error) {
2060 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2061 ssrc, error);
2062 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2063}
2064
2065void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2066 SrtpFilter::Error error) {
2067 switch (error) {
2068 case SrtpFilter::ERROR_FAIL:
2069 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2070 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2071 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2072 break;
2073 case SrtpFilter::ERROR_AUTH:
2074 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2075 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2076 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2077 break;
2078 case SrtpFilter::ERROR_REPLAY:
2079 // Only receving channel should have this error.
2080 ASSERT(mode == SrtpFilter::UNPROTECT);
2081 // TODO(gangji): Turn on the signaling of replay error once we have
2082 // switched to the new mechanism for doing video retransmissions.
2083 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2084 break;
2085 default:
2086 break;
2087 }
2088}
2089
2090
2091void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2092 GetSupportedVideoCryptoSuites(ciphers);
2093}
2094
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002095DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 DataMediaChannel* media_channel,
2097 BaseSession* session,
2098 const std::string& content_name,
2099 bool rtcp)
Fredrik Solenberg0c022642015-08-05 12:25:22 +02002100 : BaseChannel(thread, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002101 data_channel_type_(cricket::DCT_NONE),
2102 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002103}
2104
2105DataChannel::~DataChannel() {
2106 StopMediaMonitor();
2107 // this can't be done in the base class, since it calls a virtual
2108 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002109
2110 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111}
2112
2113bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002114 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115 return false;
2116 }
2117 media_channel()->SignalDataReceived.connect(
2118 this, &DataChannel::OnDataReceived);
2119 media_channel()->SignalMediaError.connect(
2120 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002121 media_channel()->SignalReadyToSend.connect(
2122 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002123 media_channel()->SignalStreamClosedRemotely.connect(
2124 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 srtp_filter()->SignalSrtpError.connect(
2126 this, &DataChannel::OnSrtpError);
2127 return true;
2128}
2129
2130bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002131 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002133 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2134 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135}
2136
2137const ContentInfo* DataChannel::GetFirstContent(
2138 const SessionDescription* sdesc) {
2139 return GetFirstDataContent(sdesc);
2140}
2141
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002142bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143 if (data_channel_type_ == DCT_SCTP) {
2144 // TODO(pthatcher): Do this in a more robust way by checking for
2145 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002146 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147 } else if (data_channel_type_ == DCT_RTP) {
2148 return BaseChannel::WantsPacket(rtcp, packet);
2149 }
2150 return false;
2151}
2152
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002153bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2154 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002155 // It hasn't been set before, so set it now.
2156 if (data_channel_type_ == DCT_NONE) {
2157 data_channel_type_ = new_data_channel_type;
2158 return true;
2159 }
2160
2161 // It's been set before, but doesn't match. That's bad.
2162 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002163 std::ostringstream desc;
2164 desc << "Data channel type mismatch."
2165 << " Expected " << data_channel_type_
2166 << " Got " << new_data_channel_type;
2167 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168 return false;
2169 }
2170
2171 // It's hasn't changed. Nothing to do.
2172 return true;
2173}
2174
2175bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002176 const DataContentDescription* content,
2177 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2179 (content->protocol() == kMediaProtocolDtlsSctp));
2180 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002181 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182}
2183
2184bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002185 ContentAction action,
2186 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002187 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 LOG(LS_INFO) << "Setting local data description";
2189
2190 const DataContentDescription* data =
2191 static_cast<const DataContentDescription*>(content);
2192 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002193 if (!data) {
2194 SafeSetError("Can't find data content in local description.", error_desc);
2195 return false;
2196 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002198 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199 return false;
2200 }
2201
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002202 if (data_channel_type_ == DCT_RTP) {
2203 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2204 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205 }
2206 }
2207
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002208 // FYI: We send the SCTP port number (not to be confused with the
2209 // underlying UDP port number) as a codec parameter. So even SCTP
2210 // data channels need codecs.
2211 DataRecvParameters recv_params = last_recv_params_;
2212 RtpParametersFromMediaDescription(data, &recv_params);
2213 if (!media_channel()->SetRecvParameters(recv_params)) {
2214 SafeSetError("Failed to set remote data description recv parameters.",
2215 error_desc);
2216 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002218 if (data_channel_type_ == DCT_RTP) {
2219 for (const DataCodec& codec : data->codecs()) {
2220 bundle_filter()->AddPayloadType(codec.id);
2221 }
2222 }
2223 last_recv_params_ = recv_params;
2224
2225 // TODO(pthatcher): Move local streams into DataSendParameters, and
2226 // only give it to the media channel once we have a remote
2227 // description too (without a remote description, we won't be able
2228 // to send them anyway).
2229 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2230 SafeSetError("Failed to set local data description streams.", error_desc);
2231 return false;
2232 }
2233
2234 set_local_content_direction(content->direction());
2235 ChangeState();
2236 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237}
2238
2239bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002240 ContentAction action,
2241 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002242 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002243
2244 const DataContentDescription* data =
2245 static_cast<const DataContentDescription*>(content);
2246 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002247 if (!data) {
2248 SafeSetError("Can't find data content in remote description.", error_desc);
2249 return false;
2250 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002252 // If the remote data doesn't have codecs and isn't an update, it
2253 // must be empty, so ignore it.
2254 if (!data->has_codecs() && action != CA_UPDATE) {
2255 return true;
2256 }
2257
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002258 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259 return false;
2260 }
2261
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002262 LOG(LS_INFO) << "Setting remote data description";
2263 if (data_channel_type_ == DCT_RTP &&
2264 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2265 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002266 }
2267
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002268
2269 DataSendParameters send_params = last_send_params_;
2270 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2271 if (!media_channel()->SetSendParameters(send_params)) {
2272 SafeSetError("Failed to set remote data description send parameters.",
2273 error_desc);
2274 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002276 last_send_params_ = send_params;
2277
2278 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2279 // and only give it to the media channel once we have a local
2280 // description too (without a local description, we won't be able to
2281 // recv them anyway).
2282 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2283 SafeSetError("Failed to set remote data description streams.",
2284 error_desc);
2285 return false;
2286 }
2287
2288 set_remote_content_direction(content->direction());
2289 ChangeState();
2290 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002291}
2292
2293void DataChannel::ChangeState() {
2294 // Render incoming data if we're the active call, and we have the local
2295 // content. We receive data on the default channel and multiplexed streams.
2296 bool recv = IsReadyToReceive();
2297 if (!media_channel()->SetReceive(recv)) {
2298 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2299 }
2300
2301 // Send outgoing data if we're the active call, we have the remote content,
2302 // and we have had some form of connectivity.
2303 bool send = IsReadyToSend();
2304 if (!media_channel()->SetSend(send)) {
2305 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2306 }
2307
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002308 // Trigger SignalReadyToSendData asynchronously.
2309 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310
2311 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2312}
2313
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002314void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315 switch (pmsg->message_id) {
2316 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002317 DataChannelReadyToSendMessageData* data =
2318 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002319 ready_to_send_data_ = data->data();
2320 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002321 delete data;
2322 break;
2323 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 case MSG_DATARECEIVED: {
2325 DataReceivedMessageData* data =
2326 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2327 SignalDataReceived(this, data->params, data->payload);
2328 delete data;
2329 break;
2330 }
2331 case MSG_CHANNEL_ERROR: {
2332 const DataChannelErrorMessageData* data =
2333 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2334 SignalMediaError(this, data->ssrc, data->error);
2335 delete data;
2336 break;
2337 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002338 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002339 rtc::TypedMessageData<uint32>* data =
2340 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002341 SignalStreamClosedRemotely(data->data());
2342 delete data;
2343 break;
2344 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002345 default:
2346 BaseChannel::OnMessage(pmsg);
2347 break;
2348 }
2349}
2350
2351void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002352 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353 SignalConnectionMonitor(this, infos);
2354}
2355
2356void DataChannel::StartMediaMonitor(int cms) {
2357 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002358 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002359 media_monitor_->SignalUpdate.connect(
2360 this, &DataChannel::OnMediaMonitorUpdate);
2361 media_monitor_->Start(cms);
2362}
2363
2364void DataChannel::StopMediaMonitor() {
2365 if (media_monitor_) {
2366 media_monitor_->Stop();
2367 media_monitor_->SignalUpdate.disconnect(this);
2368 media_monitor_.reset();
2369 }
2370}
2371
2372void DataChannel::OnMediaMonitorUpdate(
2373 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2374 ASSERT(media_channel == this->media_channel());
2375 SignalMediaMonitor(this, info);
2376}
2377
2378void DataChannel::OnDataReceived(
2379 const ReceiveDataParams& params, const char* data, size_t len) {
2380 DataReceivedMessageData* msg = new DataReceivedMessageData(
2381 params, data, len);
2382 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2383}
2384
2385void DataChannel::OnDataChannelError(
2386 uint32 ssrc, DataMediaChannel::Error err) {
2387 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2388 ssrc, err);
2389 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2390}
2391
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002392void DataChannel::OnDataChannelReadyToSend(bool writable) {
2393 // This is usded for congestion control to indicate that the stream is ready
2394 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2395 // that the transport channel is ready.
2396 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2397 new DataChannelReadyToSendMessageData(writable));
2398}
2399
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002400void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2401 SrtpFilter::Error error) {
2402 switch (error) {
2403 case SrtpFilter::ERROR_FAIL:
2404 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2405 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2406 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2407 break;
2408 case SrtpFilter::ERROR_AUTH:
2409 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2410 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2411 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2412 break;
2413 case SrtpFilter::ERROR_REPLAY:
2414 // Only receving channel should have this error.
2415 ASSERT(mode == SrtpFilter::UNPROTECT);
2416 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2417 break;
2418 default:
2419 break;
2420 }
2421}
2422
2423void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2424 GetSupportedDataCryptoSuites(ciphers);
2425}
2426
2427bool DataChannel::ShouldSetupDtlsSrtp() const {
2428 return (data_channel_type_ == DCT_RTP);
2429}
2430
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002431void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002432 rtc::TypedMessageData<uint32>* message =
2433 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002434 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2435}
2436
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002437} // namespace cricket