blob: 5eec47cc4eae4a75ddc4803b607ea868b0600112 [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)) {
1478 SafeSetError("Failed to set local video description recv parameters.",
1479 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 Thatcherc2ee2c82015-08-07 16:05:34 -07001543 set_remote_content_direction(content->direction());
1544 ChangeState();
1545 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546}
1547
1548bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001549 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1551}
1552
1553bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001554 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 if (play) {
1556 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1557 } else {
1558 LOG(LS_INFO) << "Stopping ringback tone";
1559 }
1560 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1561}
1562
1563void VoiceChannel::HandleEarlyMediaTimeout() {
1564 // This occurs on the main thread, not the worker thread.
1565 if (!received_media_) {
1566 LOG(LS_INFO) << "No early media received before timeout";
1567 SignalEarlyMediaTimeout(this);
1568 }
1569}
1570
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1572 int flags) {
1573 if (!enabled()) {
1574 return false;
1575 }
1576
1577 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1578}
1579
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001581 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1582 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583}
1584
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001585void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 case MSG_EARLYMEDIATIMEOUT:
1588 HandleEarlyMediaTimeout();
1589 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 case MSG_CHANNEL_ERROR: {
1591 VoiceChannelErrorMessageData* data =
1592 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1593 SignalMediaError(this, data->ssrc, data->error);
1594 delete data;
1595 break;
1596 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 default:
1598 BaseChannel::OnMessage(pmsg);
1599 break;
1600 }
1601}
1602
1603void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001604 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 SignalConnectionMonitor(this, infos);
1606}
1607
1608void VoiceChannel::OnMediaMonitorUpdate(
1609 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1610 ASSERT(media_channel == this->media_channel());
1611 SignalMediaMonitor(this, info);
1612}
1613
1614void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1615 const AudioInfo& info) {
1616 SignalAudioMonitor(this, info);
1617}
1618
1619void VoiceChannel::OnVoiceChannelError(
1620 uint32 ssrc, VoiceMediaChannel::Error err) {
1621 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1622 ssrc, err);
1623 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1624}
1625
1626void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1627 SrtpFilter::Error error) {
1628 switch (error) {
1629 case SrtpFilter::ERROR_FAIL:
1630 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1631 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1632 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1633 break;
1634 case SrtpFilter::ERROR_AUTH:
1635 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1636 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1637 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1638 break;
1639 case SrtpFilter::ERROR_REPLAY:
1640 // Only receving channel should have this error.
1641 ASSERT(mode == SrtpFilter::UNPROTECT);
1642 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1643 break;
1644 default:
1645 break;
1646 }
1647}
1648
1649void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1650 GetSupportedAudioCryptoSuites(ciphers);
1651}
1652
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001653VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 VideoMediaChannel* media_channel,
1655 BaseSession* session,
1656 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001657 bool rtcp)
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001658 : BaseChannel(thread, media_channel, session, content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001661 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662}
1663
1664bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001665 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 return false;
1667 }
1668 media_channel()->SignalMediaError.connect(
1669 this, &VideoChannel::OnVideoChannelError);
1670 srtp_filter()->SignalSrtpError.connect(
1671 this, &VideoChannel::OnSrtpError);
1672 return true;
1673}
1674
1675void VoiceChannel::SendLastMediaError() {
1676 uint32 ssrc;
1677 VoiceMediaChannel::Error error;
1678 media_channel()->GetLastMediaError(&ssrc, &error);
1679 SignalMediaError(this, ssrc, error);
1680}
1681
1682VideoChannel::~VideoChannel() {
1683 std::vector<uint32> screencast_ssrcs;
1684 ScreencastMap::iterator iter;
1685 while (!screencast_capturers_.empty()) {
1686 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1687 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1688 << screencast_capturers_.begin()->first;
1689 ASSERT(false);
1690 break;
1691 }
1692 }
1693
1694 StopMediaMonitor();
1695 // this can't be done in the base class, since it calls a virtual
1696 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001697
1698 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699}
1700
1701bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001702 worker_thread()->Invoke<void>(Bind(
1703 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 return true;
1705}
1706
1707bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001708 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709}
1710
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001711bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1712 return worker_thread()->Invoke<bool>(Bind(
1713 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714}
1715
1716bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001717 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1718 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719}
1720
1721bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001722 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723}
1724
1725bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001726 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727}
1728
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001729int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001730 ScreencastDetailsData data(ssrc);
1731 worker_thread()->Invoke<void>(Bind(
1732 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001733 return data.fps;
1734}
1735
1736int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001737 ScreencastDetailsData data(ssrc);
1738 worker_thread()->Invoke<void>(Bind(
1739 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001740 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741}
1742
1743bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001744 worker_thread()->Invoke<void>(Bind(
1745 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 return true;
1747}
1748
1749bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001750 worker_thread()->Invoke<void>(Bind(
1751 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001752 return true;
1753}
1754
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755void VideoChannel::ChangeState() {
1756 // Render incoming data if we're the active call, and we have the local
1757 // content. We receive data on the default channel and multiplexed streams.
1758 bool recv = IsReadyToReceive();
1759 if (!media_channel()->SetRender(recv)) {
1760 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1761 // TODO(gangji): Report error back to server.
1762 }
1763
1764 // Send outgoing data if we're the active call, we have the remote content,
1765 // and we have had some form of connectivity.
1766 bool send = IsReadyToSend();
1767 if (!media_channel()->SetSend(send)) {
1768 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1769 // TODO(gangji): Report error back to server.
1770 }
1771
1772 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1773}
1774
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001775bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1776 return InvokeOnWorker(
1777 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778}
1779
1780void VideoChannel::StartMediaMonitor(int cms) {
1781 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001782 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783 media_monitor_->SignalUpdate.connect(
1784 this, &VideoChannel::OnMediaMonitorUpdate);
1785 media_monitor_->Start(cms);
1786}
1787
1788void VideoChannel::StopMediaMonitor() {
1789 if (media_monitor_) {
1790 media_monitor_->Stop();
1791 media_monitor_.reset();
1792 }
1793}
1794
1795const ContentInfo* VideoChannel::GetFirstContent(
1796 const SessionDescription* sdesc) {
1797 return GetFirstVideoContent(sdesc);
1798}
1799
1800bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001801 ContentAction action,
1802 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001803 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 LOG(LS_INFO) << "Setting local video description";
1805
1806 const VideoContentDescription* video =
1807 static_cast<const VideoContentDescription*>(content);
1808 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001809 if (!video) {
1810 SafeSetError("Can't find video content in local description.", error_desc);
1811 return false;
1812 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001814 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1815 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 }
1817
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001818 VideoRecvParameters recv_params = last_recv_params_;
1819 RtpParametersFromMediaDescription(video, &recv_params);
1820 if (!media_channel()->SetRecvParameters(recv_params)) {
1821 SafeSetError("Failed to set local video description recv parameters.",
1822 error_desc);
1823 return false;
1824 }
1825 for (const VideoCodec& codec : video->codecs()) {
1826 bundle_filter()->AddPayloadType(codec.id);
1827 }
1828 last_recv_params_ = recv_params;
1829
1830 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1831 // only give it to the media channel once we have a remote
1832 // description too (without a remote description, we won't be able
1833 // to send them anyway).
1834 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1835 SafeSetError("Failed to set local video description streams.", error_desc);
1836 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 }
1838
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001839 set_local_content_direction(content->direction());
1840 ChangeState();
1841 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842}
1843
1844bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001845 ContentAction action,
1846 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001847 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848 LOG(LS_INFO) << "Setting remote video description";
1849
1850 const VideoContentDescription* video =
1851 static_cast<const VideoContentDescription*>(content);
1852 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001853 if (!video) {
1854 SafeSetError("Can't find video content in remote description.", error_desc);
1855 return false;
1856 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001858
1859 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1860 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 }
1862
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001863 VideoSendParameters send_params = last_send_params_;
1864 RtpSendParametersFromMediaDescription(video, &send_params);
1865 if (video->conference_mode()) {
1866 send_params.options.conference_mode.Set(true);
1867 }
1868 if (!media_channel()->SetSendParameters(send_params)) {
1869 SafeSetError("Failed to set remote video description send parameters.",
1870 error_desc);
1871 return false;
1872 }
1873 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001875 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1876 // and only give it to the media channel once we have a local
1877 // description too (without a local description, we won't be able to
1878 // recv them anyway).
1879 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1880 SafeSetError("Failed to set remote video description streams.", error_desc);
1881 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882 }
1883
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001884 if (video->rtp_header_extensions_set()) {
1885 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001887
1888 set_remote_content_direction(content->direction());
1889 ChangeState();
1890 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891}
1892
1893bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1894 bool ret = true;
1895 // Set the send format for each of the local streams. If the view request
1896 // does not contain a local stream, set its send format to 0x0, which will
1897 // drop all frames.
1898 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1899 it != local_streams().end(); ++it) {
1900 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1901 StaticVideoViews::const_iterator view;
1902 for (view = request.static_video_views.begin();
1903 view != request.static_video_views.end(); ++view) {
1904 if (view->selector.Matches(*it)) {
1905 format.width = view->width;
1906 format.height = view->height;
1907 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1908 break;
1909 }
1910 }
1911
1912 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1913 }
1914
1915 // Check if the view request has invalid streams.
1916 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1917 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001918 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001919 LOG(LS_WARNING) << "View request for ("
1920 << it->selector.ssrc << ", '"
1921 << it->selector.groupid << "', '"
1922 << it->selector.streamid << "'"
1923 << ") is not in the local streams.";
1924 }
1925 }
1926
1927 return ret;
1928}
1929
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001930bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001932 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001934 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1935 screencast_capturers_[ssrc] = capturer;
1936 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937}
1938
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1940 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1941 if (iter == screencast_capturers_.end()) {
1942 return false;
1943 }
1944 // Clean up VideoCapturer.
1945 delete iter->second;
1946 screencast_capturers_.erase(iter);
1947 return true;
1948}
1949
1950bool VideoChannel::IsScreencasting_w() const {
1951 return !screencast_capturers_.empty();
1952}
1953
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001954void VideoChannel::GetScreencastDetails_w(
1955 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001956 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001958 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 }
1960 VideoCapturer* capturer = iter->second;
1961 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001962 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1963 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964}
1965
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001967 rtc::WindowEvent we) {
1968 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969 SignalScreencastWindowEvent(ssrc, we);
1970}
1971
1972bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001973 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
1974 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975}
1976
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001977void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 case MSG_SCREENCASTWINDOWEVENT: {
1980 const ScreencastEventMessageData* data =
1981 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1982 OnScreencastWindowEvent_s(data->ssrc, data->event);
1983 delete data;
1984 break;
1985 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 case MSG_CHANNEL_ERROR: {
1987 const VideoChannelErrorMessageData* data =
1988 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
1989 SignalMediaError(this, data->ssrc, data->error);
1990 delete data;
1991 break;
1992 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 default:
1994 BaseChannel::OnMessage(pmsg);
1995 break;
1996 }
1997}
1998
1999void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002000 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 SignalConnectionMonitor(this, infos);
2002}
2003
2004// TODO(pthatcher): Look into removing duplicate code between
2005// audio, video, and data, perhaps by using templates.
2006void VideoChannel::OnMediaMonitorUpdate(
2007 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2008 ASSERT(media_channel == this->media_channel());
2009 SignalMediaMonitor(this, info);
2010}
2011
2012void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002013 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 ScreencastEventMessageData* pdata =
2015 new ScreencastEventMessageData(ssrc, event);
2016 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2017}
2018
2019void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2020 // Map capturer events to window events. In the future we may want to simply
2021 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002022 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002024 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002026 we = rtc::WE_MINIMIZE;
2027 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2028 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 } else {
2030 return;
2031 }
2032 previous_we_ = we;
2033
2034 uint32 ssrc = 0;
2035 if (!GetLocalSsrc(capturer, &ssrc)) {
2036 return;
2037 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002038
2039 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040}
2041
2042bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2043 *ssrc = 0;
2044 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2045 iter != screencast_capturers_.end(); ++iter) {
2046 if (iter->second == capturer) {
2047 *ssrc = iter->first;
2048 return true;
2049 }
2050 }
2051 return false;
2052}
2053
2054void VideoChannel::OnVideoChannelError(uint32 ssrc,
2055 VideoMediaChannel::Error error) {
2056 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2057 ssrc, error);
2058 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2059}
2060
2061void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2062 SrtpFilter::Error error) {
2063 switch (error) {
2064 case SrtpFilter::ERROR_FAIL:
2065 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2066 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2067 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2068 break;
2069 case SrtpFilter::ERROR_AUTH:
2070 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2071 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2072 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2073 break;
2074 case SrtpFilter::ERROR_REPLAY:
2075 // Only receving channel should have this error.
2076 ASSERT(mode == SrtpFilter::UNPROTECT);
2077 // TODO(gangji): Turn on the signaling of replay error once we have
2078 // switched to the new mechanism for doing video retransmissions.
2079 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2080 break;
2081 default:
2082 break;
2083 }
2084}
2085
2086
2087void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2088 GetSupportedVideoCryptoSuites(ciphers);
2089}
2090
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002091DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002092 DataMediaChannel* media_channel,
2093 BaseSession* session,
2094 const std::string& content_name,
2095 bool rtcp)
Fredrik Solenberg0c022642015-08-05 12:25:22 +02002096 : BaseChannel(thread, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002097 data_channel_type_(cricket::DCT_NONE),
2098 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099}
2100
2101DataChannel::~DataChannel() {
2102 StopMediaMonitor();
2103 // this can't be done in the base class, since it calls a virtual
2104 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002105
2106 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107}
2108
2109bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002110 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 return false;
2112 }
2113 media_channel()->SignalDataReceived.connect(
2114 this, &DataChannel::OnDataReceived);
2115 media_channel()->SignalMediaError.connect(
2116 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002117 media_channel()->SignalReadyToSend.connect(
2118 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002119 media_channel()->SignalStreamClosedRemotely.connect(
2120 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121 srtp_filter()->SignalSrtpError.connect(
2122 this, &DataChannel::OnSrtpError);
2123 return true;
2124}
2125
2126bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002127 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002129 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2130 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002131}
2132
2133const ContentInfo* DataChannel::GetFirstContent(
2134 const SessionDescription* sdesc) {
2135 return GetFirstDataContent(sdesc);
2136}
2137
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002138bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 if (data_channel_type_ == DCT_SCTP) {
2140 // TODO(pthatcher): Do this in a more robust way by checking for
2141 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002142 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143 } else if (data_channel_type_ == DCT_RTP) {
2144 return BaseChannel::WantsPacket(rtcp, packet);
2145 }
2146 return false;
2147}
2148
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002149bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2150 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151 // It hasn't been set before, so set it now.
2152 if (data_channel_type_ == DCT_NONE) {
2153 data_channel_type_ = new_data_channel_type;
2154 return true;
2155 }
2156
2157 // It's been set before, but doesn't match. That's bad.
2158 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002159 std::ostringstream desc;
2160 desc << "Data channel type mismatch."
2161 << " Expected " << data_channel_type_
2162 << " Got " << new_data_channel_type;
2163 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 return false;
2165 }
2166
2167 // It's hasn't changed. Nothing to do.
2168 return true;
2169}
2170
2171bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002172 const DataContentDescription* content,
2173 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2175 (content->protocol() == kMediaProtocolDtlsSctp));
2176 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002177 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178}
2179
2180bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002181 ContentAction action,
2182 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002183 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002184 LOG(LS_INFO) << "Setting local data description";
2185
2186 const DataContentDescription* data =
2187 static_cast<const DataContentDescription*>(content);
2188 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002189 if (!data) {
2190 SafeSetError("Can't find data content in local description.", error_desc);
2191 return false;
2192 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002194 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 return false;
2196 }
2197
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002198 if (data_channel_type_ == DCT_RTP) {
2199 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2200 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201 }
2202 }
2203
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002204 // FYI: We send the SCTP port number (not to be confused with the
2205 // underlying UDP port number) as a codec parameter. So even SCTP
2206 // data channels need codecs.
2207 DataRecvParameters recv_params = last_recv_params_;
2208 RtpParametersFromMediaDescription(data, &recv_params);
2209 if (!media_channel()->SetRecvParameters(recv_params)) {
2210 SafeSetError("Failed to set remote data description recv parameters.",
2211 error_desc);
2212 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002214 if (data_channel_type_ == DCT_RTP) {
2215 for (const DataCodec& codec : data->codecs()) {
2216 bundle_filter()->AddPayloadType(codec.id);
2217 }
2218 }
2219 last_recv_params_ = recv_params;
2220
2221 // TODO(pthatcher): Move local streams into DataSendParameters, and
2222 // only give it to the media channel once we have a remote
2223 // description too (without a remote description, we won't be able
2224 // to send them anyway).
2225 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2226 SafeSetError("Failed to set local data description streams.", error_desc);
2227 return false;
2228 }
2229
2230 set_local_content_direction(content->direction());
2231 ChangeState();
2232 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233}
2234
2235bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002236 ContentAction action,
2237 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002238 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002239
2240 const DataContentDescription* data =
2241 static_cast<const DataContentDescription*>(content);
2242 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002243 if (!data) {
2244 SafeSetError("Can't find data content in remote description.", error_desc);
2245 return false;
2246 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002248 // If the remote data doesn't have codecs and isn't an update, it
2249 // must be empty, so ignore it.
2250 if (!data->has_codecs() && action != CA_UPDATE) {
2251 return true;
2252 }
2253
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002254 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002255 return false;
2256 }
2257
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002258 LOG(LS_INFO) << "Setting remote data description";
2259 if (data_channel_type_ == DCT_RTP &&
2260 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2261 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002262 }
2263
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002264
2265 DataSendParameters send_params = last_send_params_;
2266 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2267 if (!media_channel()->SetSendParameters(send_params)) {
2268 SafeSetError("Failed to set remote data description send parameters.",
2269 error_desc);
2270 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002272 last_send_params_ = send_params;
2273
2274 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2275 // and only give it to the media channel once we have a local
2276 // description too (without a local description, we won't be able to
2277 // recv them anyway).
2278 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2279 SafeSetError("Failed to set remote data description streams.",
2280 error_desc);
2281 return false;
2282 }
2283
2284 set_remote_content_direction(content->direction());
2285 ChangeState();
2286 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287}
2288
2289void DataChannel::ChangeState() {
2290 // Render incoming data if we're the active call, and we have the local
2291 // content. We receive data on the default channel and multiplexed streams.
2292 bool recv = IsReadyToReceive();
2293 if (!media_channel()->SetReceive(recv)) {
2294 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2295 }
2296
2297 // Send outgoing data if we're the active call, we have the remote content,
2298 // and we have had some form of connectivity.
2299 bool send = IsReadyToSend();
2300 if (!media_channel()->SetSend(send)) {
2301 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2302 }
2303
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002304 // Trigger SignalReadyToSendData asynchronously.
2305 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002306
2307 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2308}
2309
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002310void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311 switch (pmsg->message_id) {
2312 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002313 DataChannelReadyToSendMessageData* data =
2314 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002315 ready_to_send_data_ = data->data();
2316 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317 delete data;
2318 break;
2319 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320 case MSG_DATARECEIVED: {
2321 DataReceivedMessageData* data =
2322 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2323 SignalDataReceived(this, data->params, data->payload);
2324 delete data;
2325 break;
2326 }
2327 case MSG_CHANNEL_ERROR: {
2328 const DataChannelErrorMessageData* data =
2329 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2330 SignalMediaError(this, data->ssrc, data->error);
2331 delete data;
2332 break;
2333 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002334 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002335 rtc::TypedMessageData<uint32>* data =
2336 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002337 SignalStreamClosedRemotely(data->data());
2338 delete data;
2339 break;
2340 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002341 default:
2342 BaseChannel::OnMessage(pmsg);
2343 break;
2344 }
2345}
2346
2347void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002348 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002349 SignalConnectionMonitor(this, infos);
2350}
2351
2352void DataChannel::StartMediaMonitor(int cms) {
2353 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002354 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355 media_monitor_->SignalUpdate.connect(
2356 this, &DataChannel::OnMediaMonitorUpdate);
2357 media_monitor_->Start(cms);
2358}
2359
2360void DataChannel::StopMediaMonitor() {
2361 if (media_monitor_) {
2362 media_monitor_->Stop();
2363 media_monitor_->SignalUpdate.disconnect(this);
2364 media_monitor_.reset();
2365 }
2366}
2367
2368void DataChannel::OnMediaMonitorUpdate(
2369 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2370 ASSERT(media_channel == this->media_channel());
2371 SignalMediaMonitor(this, info);
2372}
2373
2374void DataChannel::OnDataReceived(
2375 const ReceiveDataParams& params, const char* data, size_t len) {
2376 DataReceivedMessageData* msg = new DataReceivedMessageData(
2377 params, data, len);
2378 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2379}
2380
2381void DataChannel::OnDataChannelError(
2382 uint32 ssrc, DataMediaChannel::Error err) {
2383 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2384 ssrc, err);
2385 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2386}
2387
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002388void DataChannel::OnDataChannelReadyToSend(bool writable) {
2389 // This is usded for congestion control to indicate that the stream is ready
2390 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2391 // that the transport channel is ready.
2392 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2393 new DataChannelReadyToSendMessageData(writable));
2394}
2395
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002396void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2397 SrtpFilter::Error error) {
2398 switch (error) {
2399 case SrtpFilter::ERROR_FAIL:
2400 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2401 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2402 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2403 break;
2404 case SrtpFilter::ERROR_AUTH:
2405 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2406 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2407 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2408 break;
2409 case SrtpFilter::ERROR_REPLAY:
2410 // Only receving channel should have this error.
2411 ASSERT(mode == SrtpFilter::UNPROTECT);
2412 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2413 break;
2414 default:
2415 break;
2416 }
2417}
2418
2419void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2420 GetSupportedDataCryptoSuites(ciphers);
2421}
2422
2423bool DataChannel::ShouldSetupDtlsSrtp() const {
2424 return (data_channel_type_ == DCT_RTP);
2425}
2426
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002427void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002428 rtc::TypedMessageData<uint32>* message =
2429 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002430 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2431}
2432
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002433} // namespace cricket