blob: f83afa1ea46434352abc18abe22d9ba70cd83318 [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
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800221 if (!SetDtlsSrtpCryptoSuites(transport_channel(), false)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000222 return false;
223 }
deadbeefcbecd352015-09-23 11:50:27 -0700224 if (rtcp_transport_enabled() &&
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800225 !SetDtlsSrtpCryptoSuites(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 }
pbos482b12e2015-11-16 10:19:58 -0800631 if (rtcp) {
632 // Permit all (seemingly valid) RTCP packets.
633 return true;
634 }
635 // Check whether we handle this payload.
636 return bundle_filter_.DemuxPacket(packet->data<uint8_t>(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637}
638
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000639void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
640 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 if (!WantsPacket(rtcp, packet)) {
642 return;
643 }
644
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000645 // We are only interested in the first rtp packet because that
646 // indicates the media has started flowing.
647 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 has_received_packet_ = true;
649 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
650 }
651
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 // Unprotect the packet, if needed.
653 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200654 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000655 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 bool res;
657 if (!rtcp) {
658 res = srtp_filter_.UnprotectRtp(data, len, &len);
659 if (!res) {
660 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200661 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 GetRtpSeqNum(data, len, &seq_num);
663 GetRtpSsrc(data, len, &ssrc);
664 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
665 << " RTP packet: size=" << len
666 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
667 return;
668 }
669 } else {
670 res = srtp_filter_.UnprotectRtcp(data, len, &len);
671 if (!res) {
672 int type = -1;
673 GetRtcpType(data, len, &type);
674 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
675 << " RTCP packet: size=" << len << ", type=" << type;
676 return;
677 }
678 }
679
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000680 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 } else if (secure_required_) {
682 // Our session description indicates that SRTP is required, but we got a
683 // packet before our SRTP filter is active. This means either that
684 // a) we got SRTP packets before we received the SDES keys, in which case
685 // we can't decrypt it anyway, or
686 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
687 // channels, so we haven't yet extracted keys, even if DTLS did complete
688 // on the channel that the packets are being sent on. It's really good
689 // practice to wait for both RTP and RTCP to be good to go before sending
690 // media, to prevent weird failure modes, so it's fine for us to just eat
691 // packets here. This is all sidestepped if RTCP mux is used anyway.
692 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
693 << " packet when SRTP is inactive and crypto is required";
694 return;
695 }
696
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000697 // Push it down to the media channel.
698 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000699 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000701 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 }
703}
704
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000705bool BaseChannel::PushdownLocalDescription(
706 const SessionDescription* local_desc, ContentAction action,
707 std::string* error_desc) {
708 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000709 const MediaContentDescription* content_desc =
710 GetContentDescription(content_info);
711 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000712 !SetLocalContent(content_desc, action, error_desc)) {
713 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
714 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000716 return true;
717}
718
719bool BaseChannel::PushdownRemoteDescription(
720 const SessionDescription* remote_desc, ContentAction action,
721 std::string* error_desc) {
722 const ContentInfo* content_info = GetFirstContent(remote_desc);
723 const MediaContentDescription* content_desc =
724 GetContentDescription(content_info);
725 if (content_desc && content_info && !content_info->rejected &&
726 !SetRemoteContent(content_desc, action, error_desc)) {
727 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
728 return false;
729 }
730 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731}
732
733void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000734 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 if (enabled_)
736 return;
737
738 LOG(LS_INFO) << "Channel enabled";
739 enabled_ = true;
740 ChangeState();
741}
742
743void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000744 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000745 if (!enabled_)
746 return;
747
748 LOG(LS_INFO) << "Channel disabled";
749 enabled_ = false;
750 ChangeState();
751}
752
deadbeefcbecd352015-09-23 11:50:27 -0700753void BaseChannel::UpdateWritableState_w() {
754 if (transport_channel_ && transport_channel_->writable() &&
755 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
756 ChannelWritable_w();
757 } else {
758 ChannelNotWritable_w();
759 }
760}
761
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000762void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000763 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764 if (writable_)
765 return;
766
deadbeefcbecd352015-09-23 11:50:27 -0700767 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000768 << (was_ever_writable_ ? "" : " for the first time");
769
770 std::vector<ConnectionInfo> infos;
771 transport_channel_->GetStats(&infos);
772 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
773 it != infos.end(); ++it) {
774 if (it->best_connection) {
775 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
776 << "->" << it->remote_candidate.ToSensitiveString();
777 break;
778 }
779 }
780
781 // If we're doing DTLS-SRTP, now is the time.
782 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
783 if (!SetupDtlsSrtp(false)) {
deadbeefcbecd352015-09-23 11:50:27 -0700784 SignalDtlsSetupFailure_w(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 return;
786 }
787
788 if (rtcp_transport_channel_) {
789 if (!SetupDtlsSrtp(true)) {
deadbeefcbecd352015-09-23 11:50:27 -0700790 SignalDtlsSetupFailure_w(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000791 return;
792 }
793 }
794 }
795
796 was_ever_writable_ = true;
797 writable_ = true;
798 ChangeState();
799}
800
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000801void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
802 ASSERT(worker_thread() == rtc::Thread::Current());
803 signaling_thread()->Invoke<void>(Bind(
804 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
805}
806
807void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
808 ASSERT(signaling_thread() == rtc::Thread::Current());
809 SignalDtlsSetupFailure(this, rtcp);
810}
811
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800812bool BaseChannel::SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp) {
813 std::vector<int> crypto_suites;
814 // We always use the default SRTP crypto suites for RTCP, but we may use
815 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000816 if (!rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800817 GetSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000818 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800819 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800821 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822}
823
824bool BaseChannel::ShouldSetupDtlsSrtp() const {
825 return true;
826}
827
828// This function returns true if either DTLS-SRTP is not in use
829// *or* DTLS-SRTP is successfully set up.
830bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
831 bool ret = false;
832
deadbeefcbecd352015-09-23 11:50:27 -0700833 TransportChannel* channel =
834 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835
836 // No DTLS
837 if (!channel->IsDtlsActive())
838 return true;
839
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800840 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800842 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
843 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000844 return false;
845 }
846
847 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
848 << content_name() << " "
849 << PacketType(rtcp_channel);
850
851 // OK, we're now doing DTLS (RFC 5764)
852 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
853 SRTP_MASTER_KEY_SALT_LEN * 2);
854
855 // RFC 5705 exporter using the RFC 5764 parameters
856 if (!channel->ExportKeyingMaterial(
857 kDtlsSrtpExporterLabel,
858 NULL, 0, false,
859 &dtls_buffer[0], dtls_buffer.size())) {
860 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
861 ASSERT(false); // This should never happen
862 return false;
863 }
864
865 // Sync up the keys with the DTLS-SRTP interface
866 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
867 SRTP_MASTER_KEY_SALT_LEN);
868 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
869 SRTP_MASTER_KEY_SALT_LEN);
870 size_t offset = 0;
871 memcpy(&client_write_key[0], &dtls_buffer[offset],
872 SRTP_MASTER_KEY_KEY_LEN);
873 offset += SRTP_MASTER_KEY_KEY_LEN;
874 memcpy(&server_write_key[0], &dtls_buffer[offset],
875 SRTP_MASTER_KEY_KEY_LEN);
876 offset += SRTP_MASTER_KEY_KEY_LEN;
877 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
878 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
879 offset += SRTP_MASTER_KEY_SALT_LEN;
880 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
881 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
882
883 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000884 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000885 if (!channel->GetSslRole(&role)) {
886 LOG(LS_WARNING) << "GetSslRole failed";
887 return false;
888 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000889
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000890 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 send_key = &server_write_key;
892 recv_key = &client_write_key;
893 } else {
894 send_key = &client_write_key;
895 recv_key = &server_write_key;
896 }
897
898 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800899 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
900 static_cast<int>(send_key->size()),
901 selected_crypto_suite, &(*recv_key)[0],
902 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800904 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
905 static_cast<int>(send_key->size()),
906 selected_crypto_suite, &(*recv_key)[0],
907 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 }
909
910 if (!ret)
911 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
912 else
913 dtls_keyed_ = true;
914
915 return ret;
916}
917
918void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000919 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 if (!writable_)
921 return;
922
deadbeefcbecd352015-09-23 11:50:27 -0700923 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 writable_ = false;
925 ChangeState();
926}
927
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700928bool BaseChannel::SetRtpTransportParameters_w(
929 const MediaContentDescription* content,
930 ContentAction action,
931 ContentSource src,
932 std::string* error_desc) {
933 if (action == CA_UPDATE) {
934 // These parameters never get changed by a CA_UDPATE.
935 return true;
936 }
937
938 // Cache secure_required_ for belt and suspenders check on SendPacket
939 if (src == CS_LOCAL) {
940 set_secure_required(content->crypto_required() != CT_NONE);
941 }
942
943 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
944 return false;
945 }
946
947 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
948 return false;
949 }
950
951 return true;
952}
953
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000954// |dtls| will be set to true if DTLS is active for transport channel and
955// crypto is empty.
956bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000957 bool* dtls,
958 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000959 *dtls = transport_channel_->IsDtlsActive();
960 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000961 SafeSetError("Cryptos must be empty when DTLS is active.",
962 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000963 return false;
964 }
965 return true;
966}
967
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000969 ContentAction action,
970 ContentSource src,
971 std::string* error_desc) {
972 if (action == CA_UPDATE) {
973 // no crypto params.
974 return true;
975 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000977 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000978 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
979 if (!ret) {
980 return false;
981 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 switch (action) {
983 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000984 // If DTLS is already active on the channel, we could be renegotiating
985 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000986 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000987 ret = srtp_filter_.SetOffer(cryptos, src);
988 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 break;
990 case CA_PRANSWER:
991 // If we're doing DTLS-SRTP, we don't want to update the filter
992 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000993 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
995 }
996 break;
997 case CA_ANSWER:
998 // If we're doing DTLS-SRTP, we don't want to update the filter
999 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001000 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 ret = srtp_filter_.SetAnswer(cryptos, src);
1002 }
1003 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 default:
1005 break;
1006 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001007 if (!ret) {
1008 SafeSetError("Failed to setup SRTP filter.", error_desc);
1009 return false;
1010 }
1011 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012}
1013
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001014void BaseChannel::ActivateRtcpMux() {
1015 worker_thread_->Invoke<void>(Bind(
1016 &BaseChannel::ActivateRtcpMux_w, this));
1017}
1018
1019void BaseChannel::ActivateRtcpMux_w() {
1020 if (!rtcp_mux_filter_.IsActive()) {
1021 rtcp_mux_filter_.SetActive();
deadbeefcbecd352015-09-23 11:50:27 -07001022 set_rtcp_transport_channel(nullptr);
1023 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001024 }
1025}
1026
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001028 ContentSource src,
1029 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030 bool ret = false;
1031 switch (action) {
1032 case CA_OFFER:
1033 ret = rtcp_mux_filter_.SetOffer(enable, src);
1034 break;
1035 case CA_PRANSWER:
1036 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1037 break;
1038 case CA_ANSWER:
1039 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1040 if (ret && rtcp_mux_filter_.IsActive()) {
1041 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001042 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1043 << " by destroying RTCP transport channel for "
1044 << transport_name();
1045 set_rtcp_transport_channel(nullptr);
1046 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 }
1048 break;
1049 case CA_UPDATE:
1050 // No RTCP mux info.
1051 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001052 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 default:
1054 break;
1055 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001056 if (!ret) {
1057 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1058 return false;
1059 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1061 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1062 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001063 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 // If the RTP transport is already writable, then so are we.
1065 if (transport_channel_->writable()) {
1066 ChannelWritable_w();
1067 }
1068 }
1069
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001070 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071}
1072
1073bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001074 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001075 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076}
1077
Peter Boström0c4e06b2015-10-07 12:23:21 +02001078bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001079 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 return media_channel()->RemoveRecvStream(ssrc);
1081}
1082
1083bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001084 ContentAction action,
1085 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1087 action == CA_PRANSWER || action == CA_UPDATE))
1088 return false;
1089
1090 // If this is an update, streams only contain streams that have changed.
1091 if (action == CA_UPDATE) {
1092 for (StreamParamsVec::const_iterator it = streams.begin();
1093 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001094 const StreamParams* existing_stream =
1095 GetStreamByIds(local_streams_, it->groupid, it->id);
1096 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 if (media_channel()->AddSendStream(*it)) {
1098 local_streams_.push_back(*it);
1099 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1100 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001101 std::ostringstream desc;
1102 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1103 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 return false;
1105 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001106 } else if (existing_stream && !it->has_ssrcs()) {
1107 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001108 std::ostringstream desc;
1109 desc << "Failed to remove send stream with ssrc "
1110 << it->first_ssrc() << ".";
1111 SafeSetError(desc.str(), error_desc);
1112 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001114 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 } else {
1116 LOG(LS_WARNING) << "Ignore unsupported stream update";
1117 }
1118 }
1119 return true;
1120 }
1121 // Else streams are all the streams we want to send.
1122
1123 // Check for streams that have been removed.
1124 bool ret = true;
1125 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1126 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001127 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001129 std::ostringstream desc;
1130 desc << "Failed to remove send stream with ssrc "
1131 << it->first_ssrc() << ".";
1132 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 ret = false;
1134 }
1135 }
1136 }
1137 // Check for new streams.
1138 for (StreamParamsVec::const_iterator it = streams.begin();
1139 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001140 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001141 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001142 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001144 std::ostringstream desc;
1145 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1146 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 ret = false;
1148 }
1149 }
1150 }
1151 local_streams_ = streams;
1152 return ret;
1153}
1154
1155bool BaseChannel::UpdateRemoteStreams_w(
1156 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001157 ContentAction action,
1158 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1160 action == CA_PRANSWER || action == CA_UPDATE))
1161 return false;
1162
1163 // If this is an update, streams only contain streams that have changed.
1164 if (action == CA_UPDATE) {
1165 for (StreamParamsVec::const_iterator it = streams.begin();
1166 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001167 const StreamParams* existing_stream =
1168 GetStreamByIds(remote_streams_, it->groupid, it->id);
1169 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 if (AddRecvStream_w(*it)) {
1171 remote_streams_.push_back(*it);
1172 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1173 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001174 std::ostringstream desc;
1175 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1176 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 return false;
1178 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001179 } else if (existing_stream && !it->has_ssrcs()) {
1180 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001181 std::ostringstream desc;
1182 desc << "Failed to remove remote stream with ssrc "
1183 << it->first_ssrc() << ".";
1184 SafeSetError(desc.str(), error_desc);
1185 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001187 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 } else {
1189 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001190 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 << " new stream = " << it->ToString();
1192 }
1193 }
1194 return true;
1195 }
1196 // Else streams are all the streams we want to receive.
1197
1198 // Check for streams that have been removed.
1199 bool ret = true;
1200 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1201 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001202 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001204 std::ostringstream desc;
1205 desc << "Failed to remove remote stream with ssrc "
1206 << it->first_ssrc() << ".";
1207 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 ret = false;
1209 }
1210 }
1211 }
1212 // Check for new streams.
1213 for (StreamParamsVec::const_iterator it = streams.begin();
1214 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001215 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 if (AddRecvStream_w(*it)) {
1217 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1218 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001219 std::ostringstream desc;
1220 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1221 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 ret = false;
1223 }
1224 }
1225 }
1226 remote_streams_ = streams;
1227 return ret;
1228}
1229
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001230void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1231 const std::vector<RtpHeaderExtension>& extensions) {
1232 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001233 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001234 rtp_abs_sendtime_extn_id_ =
1235 send_time_extension ? send_time_extension->id : -1;
1236}
1237
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001238void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 case MSG_RTPPACKET:
1241 case MSG_RTCPPACKET: {
1242 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
stefanc1aeaf02015-10-15 07:26:07 -07001243 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet,
1244 data->options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 delete data; // because it is Posted
1246 break;
1247 }
1248 case MSG_FIRSTPACKETRECEIVED: {
1249 SignalFirstPacketReceived(this);
1250 break;
1251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 }
1253}
1254
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255void BaseChannel::FlushRtcpMessages() {
1256 // Flush all remaining RTCP messages. This should only be called in
1257 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001258 ASSERT(rtc::Thread::Current() == worker_thread_);
1259 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001260 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001261 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001263 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 }
1265}
1266
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001267VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 MediaEngineInterface* media_engine,
1269 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001270 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271 const std::string& content_name,
1272 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001273 : BaseChannel(thread,
1274 media_channel,
1275 transport_controller,
1276 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001278 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001279 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001280
1281VoiceChannel::~VoiceChannel() {
1282 StopAudioMonitor();
1283 StopMediaMonitor();
1284 // this can't be done in the base class, since it calls a virtual
1285 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001286 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287}
1288
1289bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001290 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 return false;
1292 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 return true;
1294}
1295
Peter Boström0c4e06b2015-10-07 12:23:21 +02001296bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001297 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001298 const AudioOptions* options,
1299 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001300 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001301 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302}
1303
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304// TODO(juberti): Handle early media the right way. We should get an explicit
1305// ringing message telling us to start playing local ringback, which we cancel
1306// if any early media actually arrives. For now, we do the opposite, which is
1307// to wait 1 second for early media, and start playing local ringback if none
1308// arrives.
1309void VoiceChannel::SetEarlyMedia(bool enable) {
1310 if (enable) {
1311 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001312 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1313 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 } else {
1315 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001316 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 }
1318}
1319
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001321 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1322 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323}
1324
Peter Boström0c4e06b2015-10-07 12:23:21 +02001325bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1326 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001327 int duration) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001328 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
solenberg1d63dd02015-12-02 12:35:09 -08001329 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330}
1331
solenberg4bac9c52015-10-09 02:32:53 -07001332bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1333 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1334 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001336
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001338 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1339 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340}
1341
1342void VoiceChannel::StartMediaMonitor(int cms) {
1343 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001344 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345 media_monitor_->SignalUpdate.connect(
1346 this, &VoiceChannel::OnMediaMonitorUpdate);
1347 media_monitor_->Start(cms);
1348}
1349
1350void VoiceChannel::StopMediaMonitor() {
1351 if (media_monitor_) {
1352 media_monitor_->Stop();
1353 media_monitor_->SignalUpdate.disconnect(this);
1354 media_monitor_.reset();
1355 }
1356}
1357
1358void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001359 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001360 audio_monitor_
1361 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1362 audio_monitor_->Start(cms);
1363}
1364
1365void VoiceChannel::StopAudioMonitor() {
1366 if (audio_monitor_) {
1367 audio_monitor_->Stop();
1368 audio_monitor_.reset();
1369 }
1370}
1371
1372bool VoiceChannel::IsAudioMonitorRunning() const {
1373 return (audio_monitor_.get() != NULL);
1374}
1375
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001377 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378}
1379
1380int VoiceChannel::GetOutputLevel_w() {
1381 return media_channel()->GetOutputLevel();
1382}
1383
1384void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1385 media_channel()->GetActiveStreams(actives);
1386}
1387
1388void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001389 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001390 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001391 int flags) {
1392 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001393
1394 // Set a flag when we've received an RTP packet. If we're waiting for early
1395 // media, this will disable the timeout.
1396 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1397 received_media_ = true;
1398 }
1399}
1400
1401void VoiceChannel::ChangeState() {
1402 // Render incoming data if we're the active call, and we have the local
1403 // content. We receive data on the default channel and multiplexed streams.
1404 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001405 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001406
1407 // Send outgoing data if we're the active call, we have the remote content,
1408 // and we have had some form of connectivity.
1409 bool send = IsReadyToSend();
1410 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1411 if (!media_channel()->SetSend(send_flag)) {
1412 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413 }
1414
1415 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1416}
1417
1418const ContentInfo* VoiceChannel::GetFirstContent(
1419 const SessionDescription* sdesc) {
1420 return GetFirstAudioContent(sdesc);
1421}
1422
1423bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001424 ContentAction action,
1425 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001426 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 LOG(LS_INFO) << "Setting local voice description";
1428
1429 const AudioContentDescription* audio =
1430 static_cast<const AudioContentDescription*>(content);
1431 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001432 if (!audio) {
1433 SafeSetError("Can't find audio content in local description.", error_desc);
1434 return false;
1435 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001437 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1438 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 }
1440
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001441 AudioRecvParameters recv_params = last_recv_params_;
1442 RtpParametersFromMediaDescription(audio, &recv_params);
1443 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001444 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001445 error_desc);
1446 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001447 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001448 for (const AudioCodec& codec : audio->codecs()) {
1449 bundle_filter()->AddPayloadType(codec.id);
1450 }
1451 last_recv_params_ = recv_params;
1452
1453 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1454 // only give it to the media channel once we have a remote
1455 // description too (without a remote description, we won't be able
1456 // to send them anyway).
1457 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1458 SafeSetError("Failed to set local audio description streams.", error_desc);
1459 return false;
1460 }
1461
1462 set_local_content_direction(content->direction());
1463 ChangeState();
1464 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465}
1466
1467bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001468 ContentAction action,
1469 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001470 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 LOG(LS_INFO) << "Setting remote voice description";
1472
1473 const AudioContentDescription* audio =
1474 static_cast<const AudioContentDescription*>(content);
1475 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001476 if (!audio) {
1477 SafeSetError("Can't find audio content in remote description.", error_desc);
1478 return false;
1479 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001481 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1482 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 }
1484
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001485 AudioSendParameters send_params = last_send_params_;
1486 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001487 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001488 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001489 }
1490 if (!media_channel()->SetSendParameters(send_params)) {
1491 SafeSetError("Failed to set remote audio description send parameters.",
1492 error_desc);
1493 return false;
1494 }
1495 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001497 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1498 // and only give it to the media channel once we have a local
1499 // description too (without a local description, we won't be able to
1500 // recv them anyway).
1501 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1502 SafeSetError("Failed to set remote audio description streams.", error_desc);
1503 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504 }
1505
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001506 if (audio->rtp_header_extensions_set()) {
1507 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1508 }
1509
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001510 set_remote_content_direction(content->direction());
1511 ChangeState();
1512 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513}
1514
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515void VoiceChannel::HandleEarlyMediaTimeout() {
1516 // This occurs on the main thread, not the worker thread.
1517 if (!received_media_) {
1518 LOG(LS_INFO) << "No early media received before timeout";
1519 SignalEarlyMediaTimeout(this);
1520 }
1521}
1522
Peter Boström0c4e06b2015-10-07 12:23:21 +02001523bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1524 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001525 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 if (!enabled()) {
1527 return false;
1528 }
solenberg1d63dd02015-12-02 12:35:09 -08001529 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530}
1531
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001532void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534 case MSG_EARLYMEDIATIMEOUT:
1535 HandleEarlyMediaTimeout();
1536 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537 case MSG_CHANNEL_ERROR: {
1538 VoiceChannelErrorMessageData* data =
1539 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 delete data;
1541 break;
1542 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 default:
1544 BaseChannel::OnMessage(pmsg);
1545 break;
1546 }
1547}
1548
1549void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001550 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 SignalConnectionMonitor(this, infos);
1552}
1553
1554void VoiceChannel::OnMediaMonitorUpdate(
1555 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1556 ASSERT(media_channel == this->media_channel());
1557 SignalMediaMonitor(this, info);
1558}
1559
1560void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1561 const AudioInfo& info) {
1562 SignalAudioMonitor(this, info);
1563}
1564
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001565void VoiceChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1566 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567}
1568
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001569VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001571 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001573 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001574 : BaseChannel(thread,
1575 media_channel,
1576 transport_controller,
1577 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001579 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001580 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581
1582bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001583 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 return false;
1585 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 return true;
1587}
1588
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589VideoChannel::~VideoChannel() {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001590 std::vector<uint32_t> screencast_ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 ScreencastMap::iterator iter;
1592 while (!screencast_capturers_.empty()) {
1593 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1594 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1595 << screencast_capturers_.begin()->first;
1596 ASSERT(false);
1597 break;
1598 }
1599 }
1600
1601 StopMediaMonitor();
1602 // this can't be done in the base class, since it calls a virtual
1603 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001604
1605 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606}
1607
Peter Boström0c4e06b2015-10-07 12:23:21 +02001608bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001609 worker_thread()->Invoke<void>(Bind(
1610 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611 return true;
1612}
1613
1614bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001615 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616}
1617
Peter Boström0c4e06b2015-10-07 12:23:21 +02001618bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001619 return worker_thread()->Invoke<bool>(Bind(
1620 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621}
1622
Peter Boström0c4e06b2015-10-07 12:23:21 +02001623bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001624 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1625 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626}
1627
Peter Boström0c4e06b2015-10-07 12:23:21 +02001628bool VideoChannel::RemoveScreencast(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001629 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630}
1631
1632bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001633 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634}
1635
Peter Boström0c4e06b2015-10-07 12:23:21 +02001636int VideoChannel::GetScreencastFps(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001637 ScreencastDetailsData data(ssrc);
1638 worker_thread()->Invoke<void>(Bind(
1639 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001640 return data.fps;
1641}
1642
Peter Boström0c4e06b2015-10-07 12:23:21 +02001643int VideoChannel::GetScreencastMaxPixels(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001644 ScreencastDetailsData data(ssrc);
1645 worker_thread()->Invoke<void>(Bind(
1646 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001647 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648}
1649
1650bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001651 worker_thread()->Invoke<void>(Bind(
1652 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 return true;
1654}
1655
1656bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001657 worker_thread()->Invoke<void>(Bind(
1658 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 return true;
1660}
1661
Peter Boström0c4e06b2015-10-07 12:23:21 +02001662bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001663 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001664 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001665 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1666 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001667}
1668
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001670 // Send outgoing data if we're the active call, we have the remote content,
1671 // and we have had some form of connectivity.
1672 bool send = IsReadyToSend();
1673 if (!media_channel()->SetSend(send)) {
1674 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1675 // TODO(gangji): Report error back to server.
1676 }
1677
Peter Boström34fbfff2015-09-24 19:20:30 +02001678 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679}
1680
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001681bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1682 return InvokeOnWorker(
1683 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001684}
1685
1686void VideoChannel::StartMediaMonitor(int cms) {
1687 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001688 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 media_monitor_->SignalUpdate.connect(
1690 this, &VideoChannel::OnMediaMonitorUpdate);
1691 media_monitor_->Start(cms);
1692}
1693
1694void VideoChannel::StopMediaMonitor() {
1695 if (media_monitor_) {
1696 media_monitor_->Stop();
1697 media_monitor_.reset();
1698 }
1699}
1700
1701const ContentInfo* VideoChannel::GetFirstContent(
1702 const SessionDescription* sdesc) {
1703 return GetFirstVideoContent(sdesc);
1704}
1705
1706bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001707 ContentAction action,
1708 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001709 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710 LOG(LS_INFO) << "Setting local video description";
1711
1712 const VideoContentDescription* video =
1713 static_cast<const VideoContentDescription*>(content);
1714 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001715 if (!video) {
1716 SafeSetError("Can't find video content in local description.", error_desc);
1717 return false;
1718 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001720 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1721 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 }
1723
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001724 VideoRecvParameters recv_params = last_recv_params_;
1725 RtpParametersFromMediaDescription(video, &recv_params);
1726 if (!media_channel()->SetRecvParameters(recv_params)) {
1727 SafeSetError("Failed to set local video description recv parameters.",
1728 error_desc);
1729 return false;
1730 }
1731 for (const VideoCodec& codec : video->codecs()) {
1732 bundle_filter()->AddPayloadType(codec.id);
1733 }
1734 last_recv_params_ = recv_params;
1735
1736 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1737 // only give it to the media channel once we have a remote
1738 // description too (without a remote description, we won't be able
1739 // to send them anyway).
1740 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1741 SafeSetError("Failed to set local video description streams.", error_desc);
1742 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 }
1744
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001745 set_local_content_direction(content->direction());
1746 ChangeState();
1747 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748}
1749
1750bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001751 ContentAction action,
1752 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001753 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001754 LOG(LS_INFO) << "Setting remote video description";
1755
1756 const VideoContentDescription* video =
1757 static_cast<const VideoContentDescription*>(content);
1758 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001759 if (!video) {
1760 SafeSetError("Can't find video content in remote description.", error_desc);
1761 return false;
1762 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001764
1765 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1766 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 }
1768
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001769 VideoSendParameters send_params = last_send_params_;
1770 RtpSendParametersFromMediaDescription(video, &send_params);
1771 if (video->conference_mode()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001772 send_params.options.conference_mode = rtc::Optional<bool>(true);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001773 }
1774 if (!media_channel()->SetSendParameters(send_params)) {
1775 SafeSetError("Failed to set remote video description send parameters.",
1776 error_desc);
1777 return false;
1778 }
1779 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001781 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1782 // and only give it to the media channel once we have a local
1783 // description too (without a local description, we won't be able to
1784 // recv them anyway).
1785 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1786 SafeSetError("Failed to set remote video description streams.", error_desc);
1787 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 }
1789
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001790 if (video->rtp_header_extensions_set()) {
1791 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001793
1794 set_remote_content_direction(content->direction());
1795 ChangeState();
1796 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797}
1798
1799bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1800 bool ret = true;
1801 // Set the send format for each of the local streams. If the view request
1802 // does not contain a local stream, set its send format to 0x0, which will
1803 // drop all frames.
1804 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1805 it != local_streams().end(); ++it) {
1806 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1807 StaticVideoViews::const_iterator view;
1808 for (view = request.static_video_views.begin();
1809 view != request.static_video_views.end(); ++view) {
1810 if (view->selector.Matches(*it)) {
1811 format.width = view->width;
1812 format.height = view->height;
1813 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1814 break;
1815 }
1816 }
1817
1818 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1819 }
1820
1821 // Check if the view request has invalid streams.
1822 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1823 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001824 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 LOG(LS_WARNING) << "View request for ("
1826 << it->selector.ssrc << ", '"
1827 << it->selector.groupid << "', '"
1828 << it->selector.streamid << "'"
1829 << ") is not in the local streams.";
1830 }
1831 }
1832
1833 return ret;
1834}
1835
Peter Boström0c4e06b2015-10-07 12:23:21 +02001836bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001838 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001840 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1841 screencast_capturers_[ssrc] = capturer;
1842 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843}
1844
Peter Boström0c4e06b2015-10-07 12:23:21 +02001845bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001846 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1847 if (iter == screencast_capturers_.end()) {
1848 return false;
1849 }
1850 // Clean up VideoCapturer.
1851 delete iter->second;
1852 screencast_capturers_.erase(iter);
1853 return true;
1854}
1855
1856bool VideoChannel::IsScreencasting_w() const {
1857 return !screencast_capturers_.empty();
1858}
1859
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001860void VideoChannel::GetScreencastDetails_w(
1861 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001862 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001864 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 }
1866 VideoCapturer* capturer = iter->second;
1867 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001868 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1869 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870}
1871
Peter Boström0c4e06b2015-10-07 12:23:21 +02001872void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001873 rtc::WindowEvent we) {
1874 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 SignalScreencastWindowEvent(ssrc, we);
1876}
1877
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001878void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880 case MSG_SCREENCASTWINDOWEVENT: {
1881 const ScreencastEventMessageData* data =
1882 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1883 OnScreencastWindowEvent_s(data->ssrc, data->event);
1884 delete data;
1885 break;
1886 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 case MSG_CHANNEL_ERROR: {
1888 const VideoChannelErrorMessageData* data =
1889 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 delete data;
1891 break;
1892 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893 default:
1894 BaseChannel::OnMessage(pmsg);
1895 break;
1896 }
1897}
1898
1899void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001900 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901 SignalConnectionMonitor(this, infos);
1902}
1903
1904// TODO(pthatcher): Look into removing duplicate code between
1905// audio, video, and data, perhaps by using templates.
1906void VideoChannel::OnMediaMonitorUpdate(
1907 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1908 ASSERT(media_channel == this->media_channel());
1909 SignalMediaMonitor(this, info);
1910}
1911
Peter Boström0c4e06b2015-10-07 12:23:21 +02001912void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001913 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914 ScreencastEventMessageData* pdata =
1915 new ScreencastEventMessageData(ssrc, event);
1916 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1917}
1918
1919void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1920 // Map capturer events to window events. In the future we may want to simply
1921 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001922 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001924 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001925 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001926 we = rtc::WE_MINIMIZE;
1927 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1928 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929 } else {
1930 return;
1931 }
1932 previous_we_ = we;
1933
Peter Boström0c4e06b2015-10-07 12:23:21 +02001934 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935 if (!GetLocalSsrc(capturer, &ssrc)) {
1936 return;
1937 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001938
1939 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940}
1941
Peter Boström0c4e06b2015-10-07 12:23:21 +02001942bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 *ssrc = 0;
1944 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
1945 iter != screencast_capturers_.end(); ++iter) {
1946 if (iter->second == capturer) {
1947 *ssrc = iter->first;
1948 return true;
1949 }
1950 }
1951 return false;
1952}
1953
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001954void VideoChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1955 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956}
1957
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001958DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001960 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 const std::string& content_name,
1962 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001963 : BaseChannel(thread,
1964 media_channel,
1965 transport_controller,
1966 content_name,
1967 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001968 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07001969 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970
1971DataChannel::~DataChannel() {
1972 StopMediaMonitor();
1973 // this can't be done in the base class, since it calls a virtual
1974 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001975
1976 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977}
1978
1979bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001980 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 return false;
1982 }
1983 media_channel()->SignalDataReceived.connect(
1984 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00001985 media_channel()->SignalReadyToSend.connect(
1986 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00001987 media_channel()->SignalStreamClosedRemotely.connect(
1988 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989 return true;
1990}
1991
1992bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001993 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001995 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
1996 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997}
1998
1999const ContentInfo* DataChannel::GetFirstContent(
2000 const SessionDescription* sdesc) {
2001 return GetFirstDataContent(sdesc);
2002}
2003
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002004bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 if (data_channel_type_ == DCT_SCTP) {
2006 // TODO(pthatcher): Do this in a more robust way by checking for
2007 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002008 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009 } else if (data_channel_type_ == DCT_RTP) {
2010 return BaseChannel::WantsPacket(rtcp, packet);
2011 }
2012 return false;
2013}
2014
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002015bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2016 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 // It hasn't been set before, so set it now.
2018 if (data_channel_type_ == DCT_NONE) {
2019 data_channel_type_ = new_data_channel_type;
2020 return true;
2021 }
2022
2023 // It's been set before, but doesn't match. That's bad.
2024 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002025 std::ostringstream desc;
2026 desc << "Data channel type mismatch."
2027 << " Expected " << data_channel_type_
2028 << " Got " << new_data_channel_type;
2029 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030 return false;
2031 }
2032
2033 // It's hasn't changed. Nothing to do.
2034 return true;
2035}
2036
2037bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002038 const DataContentDescription* content,
2039 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2041 (content->protocol() == kMediaProtocolDtlsSctp));
2042 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002043 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044}
2045
2046bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002047 ContentAction action,
2048 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002049 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050 LOG(LS_INFO) << "Setting local data description";
2051
2052 const DataContentDescription* data =
2053 static_cast<const DataContentDescription*>(content);
2054 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002055 if (!data) {
2056 SafeSetError("Can't find data content in local description.", error_desc);
2057 return false;
2058 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002060 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 return false;
2062 }
2063
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002064 if (data_channel_type_ == DCT_RTP) {
2065 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2066 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067 }
2068 }
2069
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002070 // FYI: We send the SCTP port number (not to be confused with the
2071 // underlying UDP port number) as a codec parameter. So even SCTP
2072 // data channels need codecs.
2073 DataRecvParameters recv_params = last_recv_params_;
2074 RtpParametersFromMediaDescription(data, &recv_params);
2075 if (!media_channel()->SetRecvParameters(recv_params)) {
2076 SafeSetError("Failed to set remote data description recv parameters.",
2077 error_desc);
2078 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002080 if (data_channel_type_ == DCT_RTP) {
2081 for (const DataCodec& codec : data->codecs()) {
2082 bundle_filter()->AddPayloadType(codec.id);
2083 }
2084 }
2085 last_recv_params_ = recv_params;
2086
2087 // TODO(pthatcher): Move local streams into DataSendParameters, and
2088 // only give it to the media channel once we have a remote
2089 // description too (without a remote description, we won't be able
2090 // to send them anyway).
2091 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2092 SafeSetError("Failed to set local data description streams.", error_desc);
2093 return false;
2094 }
2095
2096 set_local_content_direction(content->direction());
2097 ChangeState();
2098 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099}
2100
2101bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002102 ContentAction action,
2103 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002104 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105
2106 const DataContentDescription* data =
2107 static_cast<const DataContentDescription*>(content);
2108 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002109 if (!data) {
2110 SafeSetError("Can't find data content in remote description.", error_desc);
2111 return false;
2112 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002114 // If the remote data doesn't have codecs and isn't an update, it
2115 // must be empty, so ignore it.
2116 if (!data->has_codecs() && action != CA_UPDATE) {
2117 return true;
2118 }
2119
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002120 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121 return false;
2122 }
2123
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002124 LOG(LS_INFO) << "Setting remote data description";
2125 if (data_channel_type_ == DCT_RTP &&
2126 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2127 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 }
2129
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002130
2131 DataSendParameters send_params = last_send_params_;
2132 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2133 if (!media_channel()->SetSendParameters(send_params)) {
2134 SafeSetError("Failed to set remote data description send parameters.",
2135 error_desc);
2136 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002138 last_send_params_ = send_params;
2139
2140 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2141 // and only give it to the media channel once we have a local
2142 // description too (without a local description, we won't be able to
2143 // recv them anyway).
2144 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2145 SafeSetError("Failed to set remote data description streams.",
2146 error_desc);
2147 return false;
2148 }
2149
2150 set_remote_content_direction(content->direction());
2151 ChangeState();
2152 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153}
2154
2155void DataChannel::ChangeState() {
2156 // Render incoming data if we're the active call, and we have the local
2157 // content. We receive data on the default channel and multiplexed streams.
2158 bool recv = IsReadyToReceive();
2159 if (!media_channel()->SetReceive(recv)) {
2160 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2161 }
2162
2163 // Send outgoing data if we're the active call, we have the remote content,
2164 // and we have had some form of connectivity.
2165 bool send = IsReadyToSend();
2166 if (!media_channel()->SetSend(send)) {
2167 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2168 }
2169
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002170 // Trigger SignalReadyToSendData asynchronously.
2171 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172
2173 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2174}
2175
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002176void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177 switch (pmsg->message_id) {
2178 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002179 DataChannelReadyToSendMessageData* data =
2180 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002181 ready_to_send_data_ = data->data();
2182 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183 delete data;
2184 break;
2185 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002186 case MSG_DATARECEIVED: {
2187 DataReceivedMessageData* data =
2188 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2189 SignalDataReceived(this, data->params, data->payload);
2190 delete data;
2191 break;
2192 }
2193 case MSG_CHANNEL_ERROR: {
2194 const DataChannelErrorMessageData* data =
2195 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196 delete data;
2197 break;
2198 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002199 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002200 rtc::TypedMessageData<uint32_t>* data =
2201 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002202 SignalStreamClosedRemotely(data->data());
2203 delete data;
2204 break;
2205 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 default:
2207 BaseChannel::OnMessage(pmsg);
2208 break;
2209 }
2210}
2211
2212void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002213 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 SignalConnectionMonitor(this, infos);
2215}
2216
2217void DataChannel::StartMediaMonitor(int cms) {
2218 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002219 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220 media_monitor_->SignalUpdate.connect(
2221 this, &DataChannel::OnMediaMonitorUpdate);
2222 media_monitor_->Start(cms);
2223}
2224
2225void DataChannel::StopMediaMonitor() {
2226 if (media_monitor_) {
2227 media_monitor_->Stop();
2228 media_monitor_->SignalUpdate.disconnect(this);
2229 media_monitor_.reset();
2230 }
2231}
2232
2233void DataChannel::OnMediaMonitorUpdate(
2234 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2235 ASSERT(media_channel == this->media_channel());
2236 SignalMediaMonitor(this, info);
2237}
2238
2239void DataChannel::OnDataReceived(
2240 const ReceiveDataParams& params, const char* data, size_t len) {
2241 DataReceivedMessageData* msg = new DataReceivedMessageData(
2242 params, data, len);
2243 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2244}
2245
Peter Boström0c4e06b2015-10-07 12:23:21 +02002246void DataChannel::OnDataChannelError(uint32_t ssrc,
2247 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2249 ssrc, err);
2250 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2251}
2252
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002253void DataChannel::OnDataChannelReadyToSend(bool writable) {
2254 // This is usded for congestion control to indicate that the stream is ready
2255 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2256 // that the transport channel is ready.
2257 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2258 new DataChannelReadyToSendMessageData(writable));
2259}
2260
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002261void DataChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2262 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263}
2264
2265bool DataChannel::ShouldSetupDtlsSrtp() const {
2266 return (data_channel_type_ == DCT_RTP);
2267}
2268
Peter Boström0c4e06b2015-10-07 12:23:21 +02002269void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2270 rtc::TypedMessageData<uint32_t>* message =
2271 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002272 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2273}
2274
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275} // namespace cricket