blob: 91a6d8cb5abbd9c1ed462fe34ae45140f191ab61 [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.org65b98d12014-08-07 22:09:08 +000034#include "webrtc/base/bind.h"
35#include "webrtc/base/buffer.h"
36#include "webrtc/base/byteorder.h"
37#include "webrtc/base/common.h"
38#include "webrtc/base/dscp.h"
39#include "webrtc/base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
41namespace cricket {
42
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000043using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000044
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000046 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047 MSG_SCREENCASTWINDOWEVENT,
48 MSG_RTPPACKET,
49 MSG_RTCPPACKET,
50 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000054 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055};
56
57// Value specified in RFC 5764.
58static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
59
60static const int kAgcMinus10db = -10;
61
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000062static void SafeSetError(const std::string& message, std::string* error_desc) {
63 if (error_desc) {
64 *error_desc = message;
65 }
66}
67
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000068struct PacketMessageData : public rtc::MessageData {
69 rtc::Buffer packet;
stefanc1aeaf02015-10-15 07:26:07 -070070 rtc::PacketOptions options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071};
72
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073struct ScreencastEventMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020074 ScreencastEventMessageData(uint32_t s, rtc::WindowEvent we)
75 : ssrc(s), event(we) {}
76 uint32_t ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000077 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078};
79
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020081 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020083 : ssrc(in_ssrc), error(in_error) {}
84 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 VoiceMediaChannel::Error error;
86};
87
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020091 : ssrc(in_ssrc), error(in_error) {}
92 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 VideoMediaChannel::Error error;
94};
95
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000096struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020097 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020099 : ssrc(in_ssrc), error(in_error) {}
100 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 DataMediaChannel::Error error;
102};
103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000105struct VideoChannel::ScreencastDetailsData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200106 explicit ScreencastDetailsData(uint32_t s)
107 : ssrc(s), fps(0), screencast_max_pixels(0) {}
108 uint32_t ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000109 int fps;
110 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111};
112
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113static const char* PacketType(bool rtcp) {
114 return (!rtcp) ? "RTP" : "RTCP";
115}
116
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000117static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 // Check the packet size. We could check the header too if needed.
119 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000120 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
121 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000122}
123
124static bool IsReceiveContentDirection(MediaContentDirection direction) {
125 return direction == MD_SENDRECV || direction == MD_RECVONLY;
126}
127
128static bool IsSendContentDirection(MediaContentDirection direction) {
129 return direction == MD_SENDRECV || direction == MD_SENDONLY;
130}
131
132static const MediaContentDescription* GetContentDescription(
133 const ContentInfo* cinfo) {
134 if (cinfo == NULL)
135 return NULL;
136 return static_cast<const MediaContentDescription*>(cinfo->description);
137}
138
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700139template <class Codec>
140void RtpParametersFromMediaDescription(
141 const MediaContentDescriptionImpl<Codec>* desc,
142 RtpParameters<Codec>* params) {
143 // TODO(pthatcher): Remove this once we're sure no one will give us
144 // a description without codecs (currently a CA_UPDATE with just
145 // streams can).
146 if (desc->has_codecs()) {
147 params->codecs = desc->codecs();
148 }
149 // TODO(pthatcher): See if we really need
150 // rtp_header_extensions_set() and remove it if we don't.
151 if (desc->rtp_header_extensions_set()) {
152 params->extensions = desc->rtp_header_extensions();
153 }
154}
155
156template <class Codec, class Options>
157void RtpSendParametersFromMediaDescription(
158 const MediaContentDescriptionImpl<Codec>* desc,
159 RtpSendParameters<Codec, Options>* send_params) {
160 RtpParametersFromMediaDescription(desc, send_params);
161 send_params->max_bandwidth_bps = desc->bandwidth();
162}
163
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000164BaseChannel::BaseChannel(rtc::Thread* thread,
deadbeefcbecd352015-09-23 11:50:27 -0700165 MediaChannel* media_channel,
166 TransportController* transport_controller,
167 const std::string& content_name,
168 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 : worker_thread_(thread),
deadbeefcbecd352015-09-23 11:50:27 -0700170 transport_controller_(transport_controller),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 media_channel_(media_channel),
172 content_name_(content_name),
deadbeefcbecd352015-09-23 11:50:27 -0700173 rtcp_transport_enabled_(rtcp),
174 transport_channel_(nullptr),
175 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 enabled_(false),
177 writable_(false),
178 rtp_ready_to_send_(false),
179 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 was_ever_writable_(false),
181 local_content_direction_(MD_INACTIVE),
182 remote_content_direction_(MD_INACTIVE),
183 has_received_packet_(false),
184 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000185 secure_required_(false),
186 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000187 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 LOG(LS_INFO) << "Created channel for " << content_name;
189}
190
191BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000192 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000193 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 StopConnectionMonitor();
195 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000196 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 // We must destroy the media channel before the transport channel, otherwise
198 // the media channel may try to send on the dead transport channel. NULLing
199 // is not an effective strategy since the sends will come on another thread.
200 delete media_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700201 // Note that we don't just call set_transport_channel(nullptr) because that
202 // would call a pure virtual method which we can't do from a destructor.
203 if (transport_channel_) {
204 DisconnectFromTransportChannel(transport_channel_);
205 transport_controller_->DestroyTransportChannel_w(
206 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
207 }
208 if (rtcp_transport_channel_) {
209 DisconnectFromTransportChannel(rtcp_transport_channel_);
210 transport_controller_->DestroyTransportChannel_w(
211 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
212 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 LOG(LS_INFO) << "Destroyed channel";
214}
215
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000216bool BaseChannel::Init() {
deadbeefcbecd352015-09-23 11:50:27 -0700217 if (!SetTransport(content_name())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 return false;
219 }
220
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000221 if (!SetDtlsSrtpCiphers(transport_channel(), false)) {
222 return false;
223 }
deadbeefcbecd352015-09-23 11:50:27 -0700224 if (rtcp_transport_enabled() &&
225 !SetDtlsSrtpCiphers(rtcp_transport_channel(), true)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000226 return false;
227 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000228
wu@webrtc.orgde305012013-10-31 15:40:38 +0000229 // Both RTP and RTCP channels are set, we can call SetInterface on
230 // media channel and it can set network options.
231 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 return true;
233}
234
wu@webrtc.org78187522013-10-07 23:32:02 +0000235void BaseChannel::Deinit() {
236 media_channel_->SetInterface(NULL);
237}
238
deadbeefcbecd352015-09-23 11:50:27 -0700239bool BaseChannel::SetTransport(const std::string& transport_name) {
240 return worker_thread_->Invoke<bool>(
241 Bind(&BaseChannel::SetTransport_w, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000242}
243
deadbeefcbecd352015-09-23 11:50:27 -0700244bool BaseChannel::SetTransport_w(const std::string& transport_name) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000245 ASSERT(worker_thread_ == rtc::Thread::Current());
246
deadbeefcbecd352015-09-23 11:50:27 -0700247 if (transport_name == transport_name_) {
248 // Nothing to do if transport name isn't changing
249 return true;
250 }
251
252 set_transport_channel(transport_controller_->CreateTransportChannel_w(
253 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000254 if (!transport_channel()) {
255 return false;
256 }
deadbeefcbecd352015-09-23 11:50:27 -0700257 if (rtcp_transport_enabled()) {
258 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
259 << " on " << transport_name << " transport ";
260 set_rtcp_transport_channel(transport_controller_->CreateTransportChannel_w(
261 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000262 if (!rtcp_transport_channel()) {
263 return false;
264 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000265 }
266
deadbeefcbecd352015-09-23 11:50:27 -0700267 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000268 return true;
269}
270
271void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
272 ASSERT(worker_thread_ == rtc::Thread::Current());
273
274 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700275 if (!old_tc && !new_tc) {
276 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000277 return;
278 }
deadbeefcbecd352015-09-23 11:50:27 -0700279 ASSERT(old_tc != new_tc);
280
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000281 if (old_tc) {
282 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700283 transport_controller_->DestroyTransportChannel_w(
284 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000285 }
286
287 transport_channel_ = new_tc;
288
289 if (new_tc) {
290 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700291 for (const auto& pair : socket_options_) {
292 new_tc->SetOption(pair.first, pair.second);
293 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000294 }
deadbeefcbecd352015-09-23 11:50:27 -0700295
296 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
297 // setting new channel
298 UpdateWritableState_w();
299 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000300}
301
302void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
303 ASSERT(worker_thread_ == rtc::Thread::Current());
304
305 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700306 if (!old_tc && !new_tc) {
307 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000308 return;
309 }
deadbeefcbecd352015-09-23 11:50:27 -0700310 ASSERT(old_tc != new_tc);
311
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000312 if (old_tc) {
313 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700314 transport_controller_->DestroyTransportChannel_w(
315 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000316 }
317
318 rtcp_transport_channel_ = new_tc;
319
320 if (new_tc) {
321 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700322 for (const auto& pair : rtcp_socket_options_) {
323 new_tc->SetOption(pair.first, pair.second);
324 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000325 }
deadbeefcbecd352015-09-23 11:50:27 -0700326
327 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
328 // setting new channel
329 UpdateWritableState_w();
330 SetReadyToSend(true, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000331}
332
333void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
334 ASSERT(worker_thread_ == rtc::Thread::Current());
335
336 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
337 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
338 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
339}
340
341void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
342 ASSERT(worker_thread_ == rtc::Thread::Current());
343
344 tc->SignalWritableState.disconnect(this);
345 tc->SignalReadPacket.disconnect(this);
346 tc->SignalReadyToSend.disconnect(this);
347}
348
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000350 worker_thread_->Invoke<void>(Bind(
351 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
352 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353 return true;
354}
355
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000356bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000357 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358}
359
Peter Boström0c4e06b2015-10-07 12:23:21 +0200360bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000361 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362}
363
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000364bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000365 return InvokeOnWorker(
366 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000367}
368
Peter Boström0c4e06b2015-10-07 12:23:21 +0200369bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000370 return InvokeOnWorker(
371 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000372}
373
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000375 ContentAction action,
376 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000377 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
378 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379}
380
381bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000382 ContentAction action,
383 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000384 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
385 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386}
387
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000389 // We pass in the BaseChannel instead of the transport_channel_
390 // because if the transport_channel_ changes, the ConnectionMonitor
391 // would be pointing to the wrong TransportChannel.
392 connection_monitor_.reset(new ConnectionMonitor(
393 this, worker_thread(), rtc::Thread::Current()));
394 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000396 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397}
398
399void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000400 if (connection_monitor_) {
401 connection_monitor_->Stop();
402 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 }
404}
405
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000406bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
407 ASSERT(worker_thread_ == rtc::Thread::Current());
408 return transport_channel_->GetStats(infos);
409}
410
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411bool BaseChannel::IsReadyToReceive() const {
412 // Receive data if we are enabled and have local content,
413 return enabled() && IsReceiveContentDirection(local_content_direction_);
414}
415
416bool BaseChannel::IsReadyToSend() const {
417 // Send outgoing data if we are enabled, have local and remote content,
418 // and we have had some form of connectivity.
419 return enabled() &&
420 IsReceiveContentDirection(remote_content_direction_) &&
421 IsSendContentDirection(local_content_direction_) &&
422 was_ever_writable();
423}
424
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000425bool BaseChannel::SendPacket(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700426 const rtc::PacketOptions& options) {
427 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428}
429
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000430bool BaseChannel::SendRtcp(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700431 const rtc::PacketOptions& options) {
432 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433}
434
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000435int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000437 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000439 case ST_RTP:
440 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700441 socket_options_.push_back(
442 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000443 break;
444 case ST_RTCP:
445 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700446 rtcp_socket_options_.push_back(
447 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000448 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000450 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451}
452
453void BaseChannel::OnWritableState(TransportChannel* channel) {
454 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
deadbeefcbecd352015-09-23 11:50:27 -0700455 UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456}
457
458void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000459 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000460 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000461 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000462 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000463 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464
465 // When using RTCP multiplexing we might get RTCP packets on the RTP
466 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
467 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000468 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000469 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470}
471
472void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700473 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
474 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475}
476
deadbeefcbecd352015-09-23 11:50:27 -0700477void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
478 if (rtcp) {
479 rtcp_ready_to_send_ = ready;
480 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000481 rtp_ready_to_send_ = ready;
482 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483
deadbeefcbecd352015-09-23 11:50:27 -0700484 if (rtp_ready_to_send_ &&
485 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
486 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
torbjornga81a42f2015-09-23 02:16:58 -0700487 // Notify the MediaChannel when both rtp and rtcp channel can send.
488 media_channel_->OnReadyToSend(true);
deadbeefcbecd352015-09-23 11:50:27 -0700489 } else {
490 // Notify the MediaChannel when either rtp or rtcp channel can't send.
491 media_channel_->OnReadyToSend(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 }
493}
494
495bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
496 const char* data, size_t len) {
497 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000498 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499}
500
stefanc1aeaf02015-10-15 07:26:07 -0700501bool BaseChannel::SendPacket(bool rtcp,
502 rtc::Buffer* packet,
503 const rtc::PacketOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000504 // SendPacket gets called from MediaEngine, typically on an encoder thread.
505 // If the thread is not our worker thread, we will post to our worker
506 // so that the real work happens on our worker. This avoids us having to
507 // synchronize access to all the pieces of the send path, including
508 // SRTP and the inner workings of the transport channels.
509 // The only downside is that we can't return a proper failure code if
510 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000511 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512 // Avoid a copy by transferring the ownership of the packet data.
513 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
514 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200515 data->packet = packet->Pass();
stefanc1aeaf02015-10-15 07:26:07 -0700516 data->options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 worker_thread_->Post(this, message_id, data);
518 return true;
519 }
520
521 // Now that we are on the correct thread, ensure we have a place to send this
522 // packet before doing anything. (We might get RTCP packets that we don't
523 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
524 // transport.
525 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
526 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000527 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528 return false;
529 }
530
531 // Protect ourselves against crazy data.
532 if (!ValidPacket(rtcp, packet)) {
533 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000534 << PacketType(rtcp)
535 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 return false;
537 }
538
stefanc1aeaf02015-10-15 07:26:07 -0700539 rtc::PacketOptions updated_options;
540 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000541 // Protect if needed.
542 if (srtp_filter_.IsActive()) {
543 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200544 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000545 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000547 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
548 // inside libsrtp for a RTP packet. A external HMAC module will be writing
549 // a fake HMAC value. This is ONLY done for a RTP packet.
550 // Socket layer will update rtp sendtime extension header if present in
551 // packet with current time before updating the HMAC.
552#if !defined(ENABLE_EXTERNAL_AUTH)
553 res = srtp_filter_.ProtectRtp(
554 data, len, static_cast<int>(packet->capacity()), &len);
555#else
stefanc1aeaf02015-10-15 07:26:07 -0700556 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000557 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000558 res = srtp_filter_.ProtectRtp(
559 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700560 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000561 // If protection succeeds, let's get auth params from srtp.
562 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200563 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000564 int key_len;
565 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700566 &auth_key, &key_len,
567 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000568 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700569 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
570 updated_options.packet_time_params.srtp_auth_key.assign(
571 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000572 }
573 }
574#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 if (!res) {
576 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200577 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000578 GetRtpSeqNum(data, len, &seq_num);
579 GetRtpSsrc(data, len, &ssrc);
580 LOG(LS_ERROR) << "Failed to protect " << content_name_
581 << " RTP packet: size=" << len
582 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
583 return false;
584 }
585 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000586 res = srtp_filter_.ProtectRtcp(data, len,
587 static_cast<int>(packet->capacity()),
588 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000589 if (!res) {
590 int type = -1;
591 GetRtcpType(data, len, &type);
592 LOG(LS_ERROR) << "Failed to protect " << content_name_
593 << " RTCP packet: size=" << len << ", type=" << type;
594 return false;
595 }
596 }
597
598 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000599 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600 } else if (secure_required_) {
601 // This is a double check for something that supposedly can't happen.
602 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
603 << " packet when SRTP is inactive and crypto is required";
604
605 ASSERT(false);
606 return false;
607 }
608
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000610 int ret =
stefanc1aeaf02015-10-15 07:26:07 -0700611 channel->SendPacket(packet->data<char>(), packet->size(), updated_options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000612 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
613 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 if (channel->GetError() == EWOULDBLOCK) {
615 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700616 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 }
618 return false;
619 }
620 return true;
621}
622
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000623bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 // Protect ourselves against crazy data.
625 if (!ValidPacket(rtcp, packet)) {
626 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000627 << PacketType(rtcp)
628 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 return false;
630 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000632 // Bundle filter handles both rtp and rtcp packets.
Karl Wiberg94784372015-04-20 14:03:07 +0200633 return bundle_filter_.DemuxPacket(packet->data<char>(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634}
635
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000636void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
637 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 if (!WantsPacket(rtcp, packet)) {
639 return;
640 }
641
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000642 // We are only interested in the first rtp packet because that
643 // indicates the media has started flowing.
644 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000645 has_received_packet_ = true;
646 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
647 }
648
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 // Unprotect the packet, if needed.
650 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200651 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000652 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 bool res;
654 if (!rtcp) {
655 res = srtp_filter_.UnprotectRtp(data, len, &len);
656 if (!res) {
657 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200658 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 GetRtpSeqNum(data, len, &seq_num);
660 GetRtpSsrc(data, len, &ssrc);
661 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
662 << " RTP packet: size=" << len
663 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
664 return;
665 }
666 } else {
667 res = srtp_filter_.UnprotectRtcp(data, len, &len);
668 if (!res) {
669 int type = -1;
670 GetRtcpType(data, len, &type);
671 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
672 << " RTCP packet: size=" << len << ", type=" << type;
673 return;
674 }
675 }
676
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000677 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678 } else if (secure_required_) {
679 // Our session description indicates that SRTP is required, but we got a
680 // packet before our SRTP filter is active. This means either that
681 // a) we got SRTP packets before we received the SDES keys, in which case
682 // we can't decrypt it anyway, or
683 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
684 // channels, so we haven't yet extracted keys, even if DTLS did complete
685 // on the channel that the packets are being sent on. It's really good
686 // practice to wait for both RTP and RTCP to be good to go before sending
687 // media, to prevent weird failure modes, so it's fine for us to just eat
688 // packets here. This is all sidestepped if RTCP mux is used anyway.
689 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
690 << " packet when SRTP is inactive and crypto is required";
691 return;
692 }
693
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 // Push it down to the media channel.
695 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000696 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000698 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 }
700}
701
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000702bool BaseChannel::PushdownLocalDescription(
703 const SessionDescription* local_desc, ContentAction action,
704 std::string* error_desc) {
705 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 const MediaContentDescription* content_desc =
707 GetContentDescription(content_info);
708 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000709 !SetLocalContent(content_desc, action, error_desc)) {
710 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
711 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000712 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000713 return true;
714}
715
716bool BaseChannel::PushdownRemoteDescription(
717 const SessionDescription* remote_desc, ContentAction action,
718 std::string* error_desc) {
719 const ContentInfo* content_info = GetFirstContent(remote_desc);
720 const MediaContentDescription* content_desc =
721 GetContentDescription(content_info);
722 if (content_desc && content_info && !content_info->rejected &&
723 !SetRemoteContent(content_desc, action, error_desc)) {
724 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
725 return false;
726 }
727 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000728}
729
730void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000731 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 if (enabled_)
733 return;
734
735 LOG(LS_INFO) << "Channel enabled";
736 enabled_ = true;
737 ChangeState();
738}
739
740void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000741 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000742 if (!enabled_)
743 return;
744
745 LOG(LS_INFO) << "Channel disabled";
746 enabled_ = false;
747 ChangeState();
748}
749
deadbeefcbecd352015-09-23 11:50:27 -0700750void BaseChannel::UpdateWritableState_w() {
751 if (transport_channel_ && transport_channel_->writable() &&
752 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
753 ChannelWritable_w();
754 } else {
755 ChannelNotWritable_w();
756 }
757}
758
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000760 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000761 if (writable_)
762 return;
763
deadbeefcbecd352015-09-23 11:50:27 -0700764 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000765 << (was_ever_writable_ ? "" : " for the first time");
766
767 std::vector<ConnectionInfo> infos;
768 transport_channel_->GetStats(&infos);
769 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
770 it != infos.end(); ++it) {
771 if (it->best_connection) {
772 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
773 << "->" << it->remote_candidate.ToSensitiveString();
774 break;
775 }
776 }
777
778 // If we're doing DTLS-SRTP, now is the time.
779 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
780 if (!SetupDtlsSrtp(false)) {
deadbeefcbecd352015-09-23 11:50:27 -0700781 SignalDtlsSetupFailure_w(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000782 return;
783 }
784
785 if (rtcp_transport_channel_) {
786 if (!SetupDtlsSrtp(true)) {
deadbeefcbecd352015-09-23 11:50:27 -0700787 SignalDtlsSetupFailure_w(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000788 return;
789 }
790 }
791 }
792
793 was_ever_writable_ = true;
794 writable_ = true;
795 ChangeState();
796}
797
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000798void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
799 ASSERT(worker_thread() == rtc::Thread::Current());
800 signaling_thread()->Invoke<void>(Bind(
801 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
802}
803
804void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
805 ASSERT(signaling_thread() == rtc::Thread::Current());
806 SignalDtlsSetupFailure(this, rtcp);
807}
808
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
810 std::vector<std::string> ciphers;
811 // We always use the default SRTP ciphers for RTCP, but we may use different
812 // ciphers for RTP depending on the media type.
813 if (!rtcp) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700814 GetSrtpCryptoSuiteNames(&ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 } else {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700816 GetDefaultSrtpCryptoSuiteNames(&ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 }
818 return tc->SetSrtpCiphers(ciphers);
819}
820
821bool BaseChannel::ShouldSetupDtlsSrtp() const {
822 return true;
823}
824
825// This function returns true if either DTLS-SRTP is not in use
826// *or* DTLS-SRTP is successfully set up.
827bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
828 bool ret = false;
829
deadbeefcbecd352015-09-23 11:50:27 -0700830 TransportChannel* channel =
831 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832
833 // No DTLS
834 if (!channel->IsDtlsActive())
835 return true;
836
837 std::string selected_cipher;
838
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700839 if (!channel->GetSrtpCryptoSuite(&selected_cipher)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
841 return false;
842 }
843
844 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
845 << content_name() << " "
846 << PacketType(rtcp_channel);
847
848 // OK, we're now doing DTLS (RFC 5764)
849 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
850 SRTP_MASTER_KEY_SALT_LEN * 2);
851
852 // RFC 5705 exporter using the RFC 5764 parameters
853 if (!channel->ExportKeyingMaterial(
854 kDtlsSrtpExporterLabel,
855 NULL, 0, false,
856 &dtls_buffer[0], dtls_buffer.size())) {
857 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
858 ASSERT(false); // This should never happen
859 return false;
860 }
861
862 // Sync up the keys with the DTLS-SRTP interface
863 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
864 SRTP_MASTER_KEY_SALT_LEN);
865 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
866 SRTP_MASTER_KEY_SALT_LEN);
867 size_t offset = 0;
868 memcpy(&client_write_key[0], &dtls_buffer[offset],
869 SRTP_MASTER_KEY_KEY_LEN);
870 offset += SRTP_MASTER_KEY_KEY_LEN;
871 memcpy(&server_write_key[0], &dtls_buffer[offset],
872 SRTP_MASTER_KEY_KEY_LEN);
873 offset += SRTP_MASTER_KEY_KEY_LEN;
874 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
875 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
876 offset += SRTP_MASTER_KEY_SALT_LEN;
877 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
878 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
879
880 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000881 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000882 if (!channel->GetSslRole(&role)) {
883 LOG(LS_WARNING) << "GetSslRole failed";
884 return false;
885 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000887 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 send_key = &server_write_key;
889 recv_key = &client_write_key;
890 } else {
891 send_key = &client_write_key;
892 recv_key = &server_write_key;
893 }
894
895 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000896 ret = srtp_filter_.SetRtcpParams(
897 selected_cipher,
898 &(*send_key)[0],
899 static_cast<int>(send_key->size()),
900 selected_cipher,
901 &(*recv_key)[0],
902 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000904 ret = srtp_filter_.SetRtpParams(
905 selected_cipher,
906 &(*send_key)[0],
907 static_cast<int>(send_key->size()),
908 selected_cipher,
909 &(*recv_key)[0],
910 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 }
912
913 if (!ret)
914 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
915 else
916 dtls_keyed_ = true;
917
918 return ret;
919}
920
921void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000922 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 if (!writable_)
924 return;
925
deadbeefcbecd352015-09-23 11:50:27 -0700926 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 writable_ = false;
928 ChangeState();
929}
930
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700931bool BaseChannel::SetRtpTransportParameters_w(
932 const MediaContentDescription* content,
933 ContentAction action,
934 ContentSource src,
935 std::string* error_desc) {
936 if (action == CA_UPDATE) {
937 // These parameters never get changed by a CA_UDPATE.
938 return true;
939 }
940
941 // Cache secure_required_ for belt and suspenders check on SendPacket
942 if (src == CS_LOCAL) {
943 set_secure_required(content->crypto_required() != CT_NONE);
944 }
945
946 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
947 return false;
948 }
949
950 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
951 return false;
952 }
953
954 return true;
955}
956
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000957// |dtls| will be set to true if DTLS is active for transport channel and
958// crypto is empty.
959bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000960 bool* dtls,
961 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000962 *dtls = transport_channel_->IsDtlsActive();
963 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000964 SafeSetError("Cryptos must be empty when DTLS is active.",
965 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000966 return false;
967 }
968 return true;
969}
970
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000972 ContentAction action,
973 ContentSource src,
974 std::string* error_desc) {
975 if (action == CA_UPDATE) {
976 // no crypto params.
977 return true;
978 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000980 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000981 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
982 if (!ret) {
983 return false;
984 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 switch (action) {
986 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000987 // If DTLS is already active on the channel, we could be renegotiating
988 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000989 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000990 ret = srtp_filter_.SetOffer(cryptos, src);
991 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992 break;
993 case CA_PRANSWER:
994 // If we're doing DTLS-SRTP, we don't want to update the filter
995 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000996 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
998 }
999 break;
1000 case CA_ANSWER:
1001 // If we're doing DTLS-SRTP, we don't want to update the filter
1002 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001003 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 ret = srtp_filter_.SetAnswer(cryptos, src);
1005 }
1006 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007 default:
1008 break;
1009 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001010 if (!ret) {
1011 SafeSetError("Failed to setup SRTP filter.", error_desc);
1012 return false;
1013 }
1014 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015}
1016
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001017void BaseChannel::ActivateRtcpMux() {
1018 worker_thread_->Invoke<void>(Bind(
1019 &BaseChannel::ActivateRtcpMux_w, this));
1020}
1021
1022void BaseChannel::ActivateRtcpMux_w() {
1023 if (!rtcp_mux_filter_.IsActive()) {
1024 rtcp_mux_filter_.SetActive();
deadbeefcbecd352015-09-23 11:50:27 -07001025 set_rtcp_transport_channel(nullptr);
1026 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001027 }
1028}
1029
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001031 ContentSource src,
1032 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 bool ret = false;
1034 switch (action) {
1035 case CA_OFFER:
1036 ret = rtcp_mux_filter_.SetOffer(enable, src);
1037 break;
1038 case CA_PRANSWER:
1039 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1040 break;
1041 case CA_ANSWER:
1042 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1043 if (ret && rtcp_mux_filter_.IsActive()) {
1044 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001045 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1046 << " by destroying RTCP transport channel for "
1047 << transport_name();
1048 set_rtcp_transport_channel(nullptr);
1049 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001050 }
1051 break;
1052 case CA_UPDATE:
1053 // No RTCP mux info.
1054 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001055 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 default:
1057 break;
1058 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001059 if (!ret) {
1060 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1061 return false;
1062 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1064 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1065 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001066 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 // If the RTP transport is already writable, then so are we.
1068 if (transport_channel_->writable()) {
1069 ChannelWritable_w();
1070 }
1071 }
1072
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001073 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074}
1075
1076bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001077 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 if (!media_channel()->AddRecvStream(sp))
1079 return false;
1080
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001081 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082}
1083
Peter Boström0c4e06b2015-10-07 12:23:21 +02001084bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001085 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001086 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087 return media_channel()->RemoveRecvStream(ssrc);
1088}
1089
1090bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001091 ContentAction action,
1092 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1094 action == CA_PRANSWER || action == CA_UPDATE))
1095 return false;
1096
1097 // If this is an update, streams only contain streams that have changed.
1098 if (action == CA_UPDATE) {
1099 for (StreamParamsVec::const_iterator it = streams.begin();
1100 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001101 const StreamParams* existing_stream =
1102 GetStreamByIds(local_streams_, it->groupid, it->id);
1103 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 if (media_channel()->AddSendStream(*it)) {
1105 local_streams_.push_back(*it);
1106 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1107 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001108 std::ostringstream desc;
1109 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1110 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 return false;
1112 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001113 } else if (existing_stream && !it->has_ssrcs()) {
1114 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001115 std::ostringstream desc;
1116 desc << "Failed to remove send stream with ssrc "
1117 << it->first_ssrc() << ".";
1118 SafeSetError(desc.str(), error_desc);
1119 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001120 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001121 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 } else {
1123 LOG(LS_WARNING) << "Ignore unsupported stream update";
1124 }
1125 }
1126 return true;
1127 }
1128 // Else streams are all the streams we want to send.
1129
1130 // Check for streams that have been removed.
1131 bool ret = true;
1132 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1133 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001134 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001136 std::ostringstream desc;
1137 desc << "Failed to remove send stream with ssrc "
1138 << it->first_ssrc() << ".";
1139 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 ret = false;
1141 }
1142 }
1143 }
1144 // Check for new streams.
1145 for (StreamParamsVec::const_iterator it = streams.begin();
1146 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001147 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001149 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001151 std::ostringstream desc;
1152 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1153 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154 ret = false;
1155 }
1156 }
1157 }
1158 local_streams_ = streams;
1159 return ret;
1160}
1161
1162bool BaseChannel::UpdateRemoteStreams_w(
1163 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001164 ContentAction action,
1165 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1167 action == CA_PRANSWER || action == CA_UPDATE))
1168 return false;
1169
1170 // If this is an update, streams only contain streams that have changed.
1171 if (action == CA_UPDATE) {
1172 for (StreamParamsVec::const_iterator it = streams.begin();
1173 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001174 const StreamParams* existing_stream =
1175 GetStreamByIds(remote_streams_, it->groupid, it->id);
1176 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 if (AddRecvStream_w(*it)) {
1178 remote_streams_.push_back(*it);
1179 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1180 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001181 std::ostringstream desc;
1182 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1183 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 return false;
1185 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001186 } else if (existing_stream && !it->has_ssrcs()) {
1187 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001188 std::ostringstream desc;
1189 desc << "Failed to remove remote stream with ssrc "
1190 << it->first_ssrc() << ".";
1191 SafeSetError(desc.str(), error_desc);
1192 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001194 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 } else {
1196 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001197 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 << " new stream = " << it->ToString();
1199 }
1200 }
1201 return true;
1202 }
1203 // Else streams are all the streams we want to receive.
1204
1205 // Check for streams that have been removed.
1206 bool ret = true;
1207 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1208 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001209 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001210 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001211 std::ostringstream desc;
1212 desc << "Failed to remove remote stream with ssrc "
1213 << it->first_ssrc() << ".";
1214 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 ret = false;
1216 }
1217 }
1218 }
1219 // Check for new streams.
1220 for (StreamParamsVec::const_iterator it = streams.begin();
1221 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001222 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 if (AddRecvStream_w(*it)) {
1224 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1225 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001226 std::ostringstream desc;
1227 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1228 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229 ret = false;
1230 }
1231 }
1232 }
1233 remote_streams_ = streams;
1234 return ret;
1235}
1236
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001237void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1238 const std::vector<RtpHeaderExtension>& extensions) {
1239 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001240 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001241 rtp_abs_sendtime_extn_id_ =
1242 send_time_extension ? send_time_extension->id : -1;
1243}
1244
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001245void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 case MSG_RTPPACKET:
1248 case MSG_RTCPPACKET: {
1249 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
stefanc1aeaf02015-10-15 07:26:07 -07001250 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet,
1251 data->options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 delete data; // because it is Posted
1253 break;
1254 }
1255 case MSG_FIRSTPACKETRECEIVED: {
1256 SignalFirstPacketReceived(this);
1257 break;
1258 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 }
1260}
1261
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262void BaseChannel::FlushRtcpMessages() {
1263 // Flush all remaining RTCP messages. This should only be called in
1264 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001265 ASSERT(rtc::Thread::Current() == worker_thread_);
1266 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001267 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001268 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001270 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 }
1272}
1273
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001274VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 MediaEngineInterface* media_engine,
1276 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001277 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001278 const std::string& content_name,
1279 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001280 : BaseChannel(thread,
1281 media_channel,
1282 transport_controller,
1283 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001285 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001286 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287
1288VoiceChannel::~VoiceChannel() {
1289 StopAudioMonitor();
1290 StopMediaMonitor();
1291 // this can't be done in the base class, since it calls a virtual
1292 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001293 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294}
1295
1296bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001297 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 return false;
1299 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 return true;
1301}
1302
Peter Boström0c4e06b2015-10-07 12:23:21 +02001303bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001304 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001305 const AudioOptions* options,
1306 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001307 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001308 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309}
1310
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311// TODO(juberti): Handle early media the right way. We should get an explicit
1312// ringing message telling us to start playing local ringback, which we cancel
1313// if any early media actually arrives. For now, we do the opposite, which is
1314// to wait 1 second for early media, and start playing local ringback if none
1315// arrives.
1316void VoiceChannel::SetEarlyMedia(bool enable) {
1317 if (enable) {
1318 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001319 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1320 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 } else {
1322 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001323 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 }
1325}
1326
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327bool VoiceChannel::PressDTMF(int digit, bool playout) {
1328 int flags = DF_SEND;
1329 if (playout) {
1330 flags |= DF_PLAY;
1331 }
1332 int duration_ms = 160;
1333 return InsertDtmf(0, digit, duration_ms, flags);
1334}
1335
1336bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001337 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1338 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339}
1340
Peter Boström0c4e06b2015-10-07 12:23:21 +02001341bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1342 int event_code,
1343 int duration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001345 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1346 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347}
1348
solenberg4bac9c52015-10-09 02:32:53 -07001349bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1350 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1351 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001353
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001355 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1356 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357}
1358
1359void VoiceChannel::StartMediaMonitor(int cms) {
1360 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001361 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001362 media_monitor_->SignalUpdate.connect(
1363 this, &VoiceChannel::OnMediaMonitorUpdate);
1364 media_monitor_->Start(cms);
1365}
1366
1367void VoiceChannel::StopMediaMonitor() {
1368 if (media_monitor_) {
1369 media_monitor_->Stop();
1370 media_monitor_->SignalUpdate.disconnect(this);
1371 media_monitor_.reset();
1372 }
1373}
1374
1375void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001376 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 audio_monitor_
1378 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1379 audio_monitor_->Start(cms);
1380}
1381
1382void VoiceChannel::StopAudioMonitor() {
1383 if (audio_monitor_) {
1384 audio_monitor_->Stop();
1385 audio_monitor_.reset();
1386 }
1387}
1388
1389bool VoiceChannel::IsAudioMonitorRunning() const {
1390 return (audio_monitor_.get() != NULL);
1391}
1392
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001394 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395}
1396
1397int VoiceChannel::GetOutputLevel_w() {
1398 return media_channel()->GetOutputLevel();
1399}
1400
1401void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1402 media_channel()->GetActiveStreams(actives);
1403}
1404
1405void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001406 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001407 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001408 int flags) {
1409 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410
1411 // Set a flag when we've received an RTP packet. If we're waiting for early
1412 // media, this will disable the timeout.
1413 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1414 received_media_ = true;
1415 }
1416}
1417
1418void VoiceChannel::ChangeState() {
1419 // Render incoming data if we're the active call, and we have the local
1420 // content. We receive data on the default channel and multiplexed streams.
1421 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001422 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423
1424 // Send outgoing data if we're the active call, we have the remote content,
1425 // and we have had some form of connectivity.
1426 bool send = IsReadyToSend();
1427 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1428 if (!media_channel()->SetSend(send_flag)) {
1429 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430 }
1431
1432 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1433}
1434
1435const ContentInfo* VoiceChannel::GetFirstContent(
1436 const SessionDescription* sdesc) {
1437 return GetFirstAudioContent(sdesc);
1438}
1439
1440bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001441 ContentAction action,
1442 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001443 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001444 LOG(LS_INFO) << "Setting local voice description";
1445
1446 const AudioContentDescription* audio =
1447 static_cast<const AudioContentDescription*>(content);
1448 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001449 if (!audio) {
1450 SafeSetError("Can't find audio content in local description.", error_desc);
1451 return false;
1452 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001454 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1455 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456 }
1457
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001458 AudioRecvParameters recv_params = last_recv_params_;
1459 RtpParametersFromMediaDescription(audio, &recv_params);
1460 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001461 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001462 error_desc);
1463 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001465 for (const AudioCodec& codec : audio->codecs()) {
1466 bundle_filter()->AddPayloadType(codec.id);
1467 }
1468 last_recv_params_ = recv_params;
1469
1470 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1471 // only give it to the media channel once we have a remote
1472 // description too (without a remote description, we won't be able
1473 // to send them anyway).
1474 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1475 SafeSetError("Failed to set local audio description streams.", error_desc);
1476 return false;
1477 }
1478
1479 set_local_content_direction(content->direction());
1480 ChangeState();
1481 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482}
1483
1484bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001485 ContentAction action,
1486 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001487 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 LOG(LS_INFO) << "Setting remote voice description";
1489
1490 const AudioContentDescription* audio =
1491 static_cast<const AudioContentDescription*>(content);
1492 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001493 if (!audio) {
1494 SafeSetError("Can't find audio content in remote description.", error_desc);
1495 return false;
1496 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001498 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1499 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 }
1501
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001502 AudioSendParameters send_params = last_send_params_;
1503 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001504 if (audio->agc_minus_10db()) {
1505 send_params.options.adjust_agc_delta.Set(kAgcMinus10db);
1506 }
1507 if (!media_channel()->SetSendParameters(send_params)) {
1508 SafeSetError("Failed to set remote audio description send parameters.",
1509 error_desc);
1510 return false;
1511 }
1512 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001514 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1515 // and only give it to the media channel once we have a local
1516 // description too (without a local description, we won't be able to
1517 // recv them anyway).
1518 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1519 SafeSetError("Failed to set remote audio description streams.", error_desc);
1520 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 }
1522
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001523 if (audio->rtp_header_extensions_set()) {
1524 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1525 }
1526
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001527 set_remote_content_direction(content->direction());
1528 ChangeState();
1529 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530}
1531
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532void VoiceChannel::HandleEarlyMediaTimeout() {
1533 // This occurs on the main thread, not the worker thread.
1534 if (!received_media_) {
1535 LOG(LS_INFO) << "No early media received before timeout";
1536 SignalEarlyMediaTimeout(this);
1537 }
1538}
1539
Peter Boström0c4e06b2015-10-07 12:23:21 +02001540bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1541 int event,
1542 int duration,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 int flags) {
1544 if (!enabled()) {
1545 return false;
1546 }
1547
1548 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1549}
1550
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001551void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553 case MSG_EARLYMEDIATIMEOUT:
1554 HandleEarlyMediaTimeout();
1555 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556 case MSG_CHANNEL_ERROR: {
1557 VoiceChannelErrorMessageData* data =
1558 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559 delete data;
1560 break;
1561 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 default:
1563 BaseChannel::OnMessage(pmsg);
1564 break;
1565 }
1566}
1567
1568void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001569 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 SignalConnectionMonitor(this, infos);
1571}
1572
1573void VoiceChannel::OnMediaMonitorUpdate(
1574 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1575 ASSERT(media_channel == this->media_channel());
1576 SignalMediaMonitor(this, info);
1577}
1578
1579void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1580 const AudioInfo& info) {
1581 SignalAudioMonitor(this, info);
1582}
1583
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001584void VoiceChannel::GetSrtpCryptoSuiteNames(
1585 std::vector<std::string>* ciphers) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 GetSupportedAudioCryptoSuites(ciphers);
1587}
1588
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001589VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001591 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001593 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001594 : BaseChannel(thread,
1595 media_channel,
1596 transport_controller,
1597 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001600 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601
1602bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001603 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604 return false;
1605 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 return true;
1607}
1608
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609VideoChannel::~VideoChannel() {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001610 std::vector<uint32_t> screencast_ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611 ScreencastMap::iterator iter;
1612 while (!screencast_capturers_.empty()) {
1613 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1614 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1615 << screencast_capturers_.begin()->first;
1616 ASSERT(false);
1617 break;
1618 }
1619 }
1620
1621 StopMediaMonitor();
1622 // this can't be done in the base class, since it calls a virtual
1623 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001624
1625 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626}
1627
Peter Boström0c4e06b2015-10-07 12:23:21 +02001628bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001629 worker_thread()->Invoke<void>(Bind(
1630 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001631 return true;
1632}
1633
1634bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001635 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636}
1637
Peter Boström0c4e06b2015-10-07 12:23:21 +02001638bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001639 return worker_thread()->Invoke<bool>(Bind(
1640 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641}
1642
Peter Boström0c4e06b2015-10-07 12:23:21 +02001643bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001644 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1645 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646}
1647
Peter Boström0c4e06b2015-10-07 12:23:21 +02001648bool VideoChannel::RemoveScreencast(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001649 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650}
1651
1652bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001653 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654}
1655
Peter Boström0c4e06b2015-10-07 12:23:21 +02001656int VideoChannel::GetScreencastFps(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001657 ScreencastDetailsData data(ssrc);
1658 worker_thread()->Invoke<void>(Bind(
1659 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001660 return data.fps;
1661}
1662
Peter Boström0c4e06b2015-10-07 12:23:21 +02001663int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001664 ScreencastDetailsData data(ssrc);
1665 worker_thread()->Invoke<void>(Bind(
1666 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001667 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668}
1669
1670bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001671 worker_thread()->Invoke<void>(Bind(
1672 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 return true;
1674}
1675
1676bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001677 worker_thread()->Invoke<void>(Bind(
1678 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679 return true;
1680}
1681
Peter Boström0c4e06b2015-10-07 12:23:21 +02001682bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001683 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001684 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001685 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1686 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001687}
1688
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 // Send outgoing data if we're the active call, we have the remote content,
1691 // and we have had some form of connectivity.
1692 bool send = IsReadyToSend();
1693 if (!media_channel()->SetSend(send)) {
1694 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1695 // TODO(gangji): Report error back to server.
1696 }
1697
Peter Boström34fbfff2015-09-24 19:20:30 +02001698 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699}
1700
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001701bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1702 return InvokeOnWorker(
1703 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704}
1705
1706void VideoChannel::StartMediaMonitor(int cms) {
1707 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001708 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 media_monitor_->SignalUpdate.connect(
1710 this, &VideoChannel::OnMediaMonitorUpdate);
1711 media_monitor_->Start(cms);
1712}
1713
1714void VideoChannel::StopMediaMonitor() {
1715 if (media_monitor_) {
1716 media_monitor_->Stop();
1717 media_monitor_.reset();
1718 }
1719}
1720
1721const ContentInfo* VideoChannel::GetFirstContent(
1722 const SessionDescription* sdesc) {
1723 return GetFirstVideoContent(sdesc);
1724}
1725
1726bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001727 ContentAction action,
1728 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001729 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 LOG(LS_INFO) << "Setting local video description";
1731
1732 const VideoContentDescription* video =
1733 static_cast<const VideoContentDescription*>(content);
1734 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001735 if (!video) {
1736 SafeSetError("Can't find video content in local description.", error_desc);
1737 return false;
1738 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001740 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1741 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742 }
1743
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001744 VideoRecvParameters recv_params = last_recv_params_;
1745 RtpParametersFromMediaDescription(video, &recv_params);
1746 if (!media_channel()->SetRecvParameters(recv_params)) {
1747 SafeSetError("Failed to set local video description recv parameters.",
1748 error_desc);
1749 return false;
1750 }
1751 for (const VideoCodec& codec : video->codecs()) {
1752 bundle_filter()->AddPayloadType(codec.id);
1753 }
1754 last_recv_params_ = recv_params;
1755
1756 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1757 // only give it to the media channel once we have a remote
1758 // description too (without a remote description, we won't be able
1759 // to send them anyway).
1760 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1761 SafeSetError("Failed to set local video description streams.", error_desc);
1762 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 }
1764
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001765 set_local_content_direction(content->direction());
1766 ChangeState();
1767 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768}
1769
1770bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001771 ContentAction action,
1772 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001773 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 LOG(LS_INFO) << "Setting remote video description";
1775
1776 const VideoContentDescription* video =
1777 static_cast<const VideoContentDescription*>(content);
1778 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001779 if (!video) {
1780 SafeSetError("Can't find video content in remote description.", error_desc);
1781 return false;
1782 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001784
1785 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1786 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787 }
1788
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001789 VideoSendParameters send_params = last_send_params_;
1790 RtpSendParametersFromMediaDescription(video, &send_params);
1791 if (video->conference_mode()) {
1792 send_params.options.conference_mode.Set(true);
1793 }
1794 if (!media_channel()->SetSendParameters(send_params)) {
1795 SafeSetError("Failed to set remote video description send parameters.",
1796 error_desc);
1797 return false;
1798 }
1799 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001801 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1802 // and only give it to the media channel once we have a local
1803 // description too (without a local description, we won't be able to
1804 // recv them anyway).
1805 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1806 SafeSetError("Failed to set remote video description streams.", error_desc);
1807 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 }
1809
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001810 if (video->rtp_header_extensions_set()) {
1811 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001813
1814 set_remote_content_direction(content->direction());
1815 ChangeState();
1816 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817}
1818
1819bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1820 bool ret = true;
1821 // Set the send format for each of the local streams. If the view request
1822 // does not contain a local stream, set its send format to 0x0, which will
1823 // drop all frames.
1824 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1825 it != local_streams().end(); ++it) {
1826 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1827 StaticVideoViews::const_iterator view;
1828 for (view = request.static_video_views.begin();
1829 view != request.static_video_views.end(); ++view) {
1830 if (view->selector.Matches(*it)) {
1831 format.width = view->width;
1832 format.height = view->height;
1833 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1834 break;
1835 }
1836 }
1837
1838 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1839 }
1840
1841 // Check if the view request has invalid streams.
1842 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1843 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001844 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 LOG(LS_WARNING) << "View request for ("
1846 << it->selector.ssrc << ", '"
1847 << it->selector.groupid << "', '"
1848 << it->selector.streamid << "'"
1849 << ") is not in the local streams.";
1850 }
1851 }
1852
1853 return ret;
1854}
1855
Peter Boström0c4e06b2015-10-07 12:23:21 +02001856bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001858 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001860 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1861 screencast_capturers_[ssrc] = capturer;
1862 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863}
1864
Peter Boström0c4e06b2015-10-07 12:23:21 +02001865bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1867 if (iter == screencast_capturers_.end()) {
1868 return false;
1869 }
1870 // Clean up VideoCapturer.
1871 delete iter->second;
1872 screencast_capturers_.erase(iter);
1873 return true;
1874}
1875
1876bool VideoChannel::IsScreencasting_w() const {
1877 return !screencast_capturers_.empty();
1878}
1879
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001880void VideoChannel::GetScreencastDetails_w(
1881 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001882 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001884 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 }
1886 VideoCapturer* capturer = iter->second;
1887 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001888 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1889 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890}
1891
Peter Boström0c4e06b2015-10-07 12:23:21 +02001892void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001893 rtc::WindowEvent we) {
1894 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 SignalScreencastWindowEvent(ssrc, we);
1896}
1897
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001898void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900 case MSG_SCREENCASTWINDOWEVENT: {
1901 const ScreencastEventMessageData* data =
1902 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1903 OnScreencastWindowEvent_s(data->ssrc, data->event);
1904 delete data;
1905 break;
1906 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907 case MSG_CHANNEL_ERROR: {
1908 const VideoChannelErrorMessageData* data =
1909 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 delete data;
1911 break;
1912 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 default:
1914 BaseChannel::OnMessage(pmsg);
1915 break;
1916 }
1917}
1918
1919void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001920 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 SignalConnectionMonitor(this, infos);
1922}
1923
1924// TODO(pthatcher): Look into removing duplicate code between
1925// audio, video, and data, perhaps by using templates.
1926void VideoChannel::OnMediaMonitorUpdate(
1927 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1928 ASSERT(media_channel == this->media_channel());
1929 SignalMediaMonitor(this, info);
1930}
1931
Peter Boström0c4e06b2015-10-07 12:23:21 +02001932void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001933 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 ScreencastEventMessageData* pdata =
1935 new ScreencastEventMessageData(ssrc, event);
1936 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1937}
1938
1939void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1940 // Map capturer events to window events. In the future we may want to simply
1941 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001942 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001944 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001946 we = rtc::WE_MINIMIZE;
1947 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1948 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949 } else {
1950 return;
1951 }
1952 previous_we_ = we;
1953
Peter Boström0c4e06b2015-10-07 12:23:21 +02001954 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 if (!GetLocalSsrc(capturer, &ssrc)) {
1956 return;
1957 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001958
1959 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960}
1961
Peter Boström0c4e06b2015-10-07 12:23:21 +02001962bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 *ssrc = 0;
1964 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
1965 iter != screencast_capturers_.end(); ++iter) {
1966 if (iter->second == capturer) {
1967 *ssrc = iter->first;
1968 return true;
1969 }
1970 }
1971 return false;
1972}
1973
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001974void VideoChannel::GetSrtpCryptoSuiteNames(
1975 std::vector<std::string>* ciphers) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 GetSupportedVideoCryptoSuites(ciphers);
1977}
1978
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001979DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001981 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 const std::string& content_name,
1983 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001984 : BaseChannel(thread,
1985 media_channel,
1986 transport_controller,
1987 content_name,
1988 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001989 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07001990 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991
1992DataChannel::~DataChannel() {
1993 StopMediaMonitor();
1994 // this can't be done in the base class, since it calls a virtual
1995 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001996
1997 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998}
1999
2000bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002001 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 return false;
2003 }
2004 media_channel()->SignalDataReceived.connect(
2005 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002006 media_channel()->SignalReadyToSend.connect(
2007 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002008 media_channel()->SignalStreamClosedRemotely.connect(
2009 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010 return true;
2011}
2012
2013bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002014 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002016 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2017 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018}
2019
2020const ContentInfo* DataChannel::GetFirstContent(
2021 const SessionDescription* sdesc) {
2022 return GetFirstDataContent(sdesc);
2023}
2024
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002025bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026 if (data_channel_type_ == DCT_SCTP) {
2027 // TODO(pthatcher): Do this in a more robust way by checking for
2028 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002029 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030 } else if (data_channel_type_ == DCT_RTP) {
2031 return BaseChannel::WantsPacket(rtcp, packet);
2032 }
2033 return false;
2034}
2035
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002036bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2037 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038 // It hasn't been set before, so set it now.
2039 if (data_channel_type_ == DCT_NONE) {
2040 data_channel_type_ = new_data_channel_type;
2041 return true;
2042 }
2043
2044 // It's been set before, but doesn't match. That's bad.
2045 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002046 std::ostringstream desc;
2047 desc << "Data channel type mismatch."
2048 << " Expected " << data_channel_type_
2049 << " Got " << new_data_channel_type;
2050 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002051 return false;
2052 }
2053
2054 // It's hasn't changed. Nothing to do.
2055 return true;
2056}
2057
2058bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002059 const DataContentDescription* content,
2060 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2062 (content->protocol() == kMediaProtocolDtlsSctp));
2063 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002064 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065}
2066
2067bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002068 ContentAction action,
2069 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002070 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002071 LOG(LS_INFO) << "Setting local data description";
2072
2073 const DataContentDescription* data =
2074 static_cast<const DataContentDescription*>(content);
2075 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002076 if (!data) {
2077 SafeSetError("Can't find data content in local description.", error_desc);
2078 return false;
2079 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002081 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082 return false;
2083 }
2084
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002085 if (data_channel_type_ == DCT_RTP) {
2086 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2087 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088 }
2089 }
2090
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002091 // FYI: We send the SCTP port number (not to be confused with the
2092 // underlying UDP port number) as a codec parameter. So even SCTP
2093 // data channels need codecs.
2094 DataRecvParameters recv_params = last_recv_params_;
2095 RtpParametersFromMediaDescription(data, &recv_params);
2096 if (!media_channel()->SetRecvParameters(recv_params)) {
2097 SafeSetError("Failed to set remote data description recv parameters.",
2098 error_desc);
2099 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002101 if (data_channel_type_ == DCT_RTP) {
2102 for (const DataCodec& codec : data->codecs()) {
2103 bundle_filter()->AddPayloadType(codec.id);
2104 }
2105 }
2106 last_recv_params_ = recv_params;
2107
2108 // TODO(pthatcher): Move local streams into DataSendParameters, and
2109 // only give it to the media channel once we have a remote
2110 // description too (without a remote description, we won't be able
2111 // to send them anyway).
2112 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2113 SafeSetError("Failed to set local data description streams.", error_desc);
2114 return false;
2115 }
2116
2117 set_local_content_direction(content->direction());
2118 ChangeState();
2119 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120}
2121
2122bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002123 ContentAction action,
2124 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002125 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126
2127 const DataContentDescription* data =
2128 static_cast<const DataContentDescription*>(content);
2129 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002130 if (!data) {
2131 SafeSetError("Can't find data content in remote description.", error_desc);
2132 return false;
2133 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002134
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002135 // If the remote data doesn't have codecs and isn't an update, it
2136 // must be empty, so ignore it.
2137 if (!data->has_codecs() && action != CA_UPDATE) {
2138 return true;
2139 }
2140
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002141 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142 return false;
2143 }
2144
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002145 LOG(LS_INFO) << "Setting remote data description";
2146 if (data_channel_type_ == DCT_RTP &&
2147 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2148 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 }
2150
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002151
2152 DataSendParameters send_params = last_send_params_;
2153 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2154 if (!media_channel()->SetSendParameters(send_params)) {
2155 SafeSetError("Failed to set remote data description send parameters.",
2156 error_desc);
2157 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002158 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002159 last_send_params_ = send_params;
2160
2161 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2162 // and only give it to the media channel once we have a local
2163 // description too (without a local description, we won't be able to
2164 // recv them anyway).
2165 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2166 SafeSetError("Failed to set remote data description streams.",
2167 error_desc);
2168 return false;
2169 }
2170
2171 set_remote_content_direction(content->direction());
2172 ChangeState();
2173 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174}
2175
2176void DataChannel::ChangeState() {
2177 // Render incoming data if we're the active call, and we have the local
2178 // content. We receive data on the default channel and multiplexed streams.
2179 bool recv = IsReadyToReceive();
2180 if (!media_channel()->SetReceive(recv)) {
2181 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2182 }
2183
2184 // Send outgoing data if we're the active call, we have the remote content,
2185 // and we have had some form of connectivity.
2186 bool send = IsReadyToSend();
2187 if (!media_channel()->SetSend(send)) {
2188 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2189 }
2190
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002191 // Trigger SignalReadyToSendData asynchronously.
2192 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193
2194 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2195}
2196
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002197void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198 switch (pmsg->message_id) {
2199 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002200 DataChannelReadyToSendMessageData* data =
2201 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002202 ready_to_send_data_ = data->data();
2203 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204 delete data;
2205 break;
2206 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207 case MSG_DATARECEIVED: {
2208 DataReceivedMessageData* data =
2209 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2210 SignalDataReceived(this, data->params, data->payload);
2211 delete data;
2212 break;
2213 }
2214 case MSG_CHANNEL_ERROR: {
2215 const DataChannelErrorMessageData* data =
2216 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 delete data;
2218 break;
2219 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002220 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002221 rtc::TypedMessageData<uint32_t>* data =
2222 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002223 SignalStreamClosedRemotely(data->data());
2224 delete data;
2225 break;
2226 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002227 default:
2228 BaseChannel::OnMessage(pmsg);
2229 break;
2230 }
2231}
2232
2233void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002234 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235 SignalConnectionMonitor(this, infos);
2236}
2237
2238void DataChannel::StartMediaMonitor(int cms) {
2239 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002240 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002241 media_monitor_->SignalUpdate.connect(
2242 this, &DataChannel::OnMediaMonitorUpdate);
2243 media_monitor_->Start(cms);
2244}
2245
2246void DataChannel::StopMediaMonitor() {
2247 if (media_monitor_) {
2248 media_monitor_->Stop();
2249 media_monitor_->SignalUpdate.disconnect(this);
2250 media_monitor_.reset();
2251 }
2252}
2253
2254void DataChannel::OnMediaMonitorUpdate(
2255 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2256 ASSERT(media_channel == this->media_channel());
2257 SignalMediaMonitor(this, info);
2258}
2259
2260void DataChannel::OnDataReceived(
2261 const ReceiveDataParams& params, const char* data, size_t len) {
2262 DataReceivedMessageData* msg = new DataReceivedMessageData(
2263 params, data, len);
2264 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2265}
2266
Peter Boström0c4e06b2015-10-07 12:23:21 +02002267void DataChannel::OnDataChannelError(uint32_t ssrc,
2268 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002269 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2270 ssrc, err);
2271 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2272}
2273
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002274void DataChannel::OnDataChannelReadyToSend(bool writable) {
2275 // This is usded for congestion control to indicate that the stream is ready
2276 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2277 // that the transport channel is ready.
2278 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2279 new DataChannelReadyToSendMessageData(writable));
2280}
2281
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002282void DataChannel::GetSrtpCryptoSuiteNames(
2283 std::vector<std::string>* ciphers) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284 GetSupportedDataCryptoSuites(ciphers);
2285}
2286
2287bool DataChannel::ShouldSetupDtlsSrtp() const {
2288 return (data_channel_type_ == DCT_RTP);
2289}
2290
Peter Boström0c4e06b2015-10-07 12:23:21 +02002291void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2292 rtc::TypedMessageData<uint32_t>* message =
2293 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002294 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2295}
2296
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297} // namespace cricket