blob: fc998c2531e0d564447a359da9363d117ffe6d5c [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;
70 rtc::DiffServCodePoint dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071};
72
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000073struct ScreencastEventMessageData : public rtc::MessageData {
74 ScreencastEventMessageData(uint32 s, rtc::WindowEvent we)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 : ssrc(s),
76 event(we) {
77 }
78 uint32 ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080};
81
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000082struct VoiceChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 VoiceChannelErrorMessageData(uint32 in_ssrc,
84 VoiceMediaChannel::Error in_error)
85 : ssrc(in_ssrc),
86 error(in_error) {
87 }
88 uint32 ssrc;
89 VoiceMediaChannel::Error error;
90};
91
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000092struct VideoChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 VideoChannelErrorMessageData(uint32 in_ssrc,
94 VideoMediaChannel::Error in_error)
95 : ssrc(in_ssrc),
96 error(in_error) {
97 }
98 uint32 ssrc;
99 VideoMediaChannel::Error error;
100};
101
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000102struct DataChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103 DataChannelErrorMessageData(uint32 in_ssrc,
104 DataMediaChannel::Error in_error)
105 : ssrc(in_ssrc),
106 error(in_error) {}
107 uint32 ssrc;
108 DataMediaChannel::Error error;
109};
110
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000111
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000112struct VideoChannel::ScreencastDetailsData {
113 explicit ScreencastDetailsData(uint32 s)
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000114 : ssrc(s), fps(0), screencast_max_pixels(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 }
116 uint32 ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000117 int fps;
118 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000119};
120
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121static const char* PacketType(bool rtcp) {
122 return (!rtcp) ? "RTP" : "RTCP";
123}
124
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000125static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 // Check the packet size. We could check the header too if needed.
127 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000128 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
129 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130}
131
132static bool IsReceiveContentDirection(MediaContentDirection direction) {
133 return direction == MD_SENDRECV || direction == MD_RECVONLY;
134}
135
136static bool IsSendContentDirection(MediaContentDirection direction) {
137 return direction == MD_SENDRECV || direction == MD_SENDONLY;
138}
139
140static const MediaContentDescription* GetContentDescription(
141 const ContentInfo* cinfo) {
142 if (cinfo == NULL)
143 return NULL;
144 return static_cast<const MediaContentDescription*>(cinfo->description);
145}
146
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700147template <class Codec>
148void RtpParametersFromMediaDescription(
149 const MediaContentDescriptionImpl<Codec>* desc,
150 RtpParameters<Codec>* params) {
151 // TODO(pthatcher): Remove this once we're sure no one will give us
152 // a description without codecs (currently a CA_UPDATE with just
153 // streams can).
154 if (desc->has_codecs()) {
155 params->codecs = desc->codecs();
156 }
157 // TODO(pthatcher): See if we really need
158 // rtp_header_extensions_set() and remove it if we don't.
159 if (desc->rtp_header_extensions_set()) {
160 params->extensions = desc->rtp_header_extensions();
161 }
162}
163
164template <class Codec, class Options>
165void RtpSendParametersFromMediaDescription(
166 const MediaContentDescriptionImpl<Codec>* desc,
167 RtpSendParameters<Codec, Options>* send_params) {
168 RtpParametersFromMediaDescription(desc, send_params);
169 send_params->max_bandwidth_bps = desc->bandwidth();
170}
171
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000172BaseChannel::BaseChannel(rtc::Thread* thread,
deadbeefcbecd352015-09-23 11:50:27 -0700173 MediaChannel* media_channel,
174 TransportController* transport_controller,
175 const std::string& content_name,
176 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 : worker_thread_(thread),
deadbeefcbecd352015-09-23 11:50:27 -0700178 transport_controller_(transport_controller),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 media_channel_(media_channel),
180 content_name_(content_name),
deadbeefcbecd352015-09-23 11:50:27 -0700181 rtcp_transport_enabled_(rtcp),
182 transport_channel_(nullptr),
183 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184 enabled_(false),
185 writable_(false),
186 rtp_ready_to_send_(false),
187 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000188 was_ever_writable_(false),
189 local_content_direction_(MD_INACTIVE),
190 remote_content_direction_(MD_INACTIVE),
191 has_received_packet_(false),
192 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000193 secure_required_(false),
194 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000195 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 LOG(LS_INFO) << "Created channel for " << content_name;
197}
198
199BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000200 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000201 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 StopConnectionMonitor();
203 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000204 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 // We must destroy the media channel before the transport channel, otherwise
206 // the media channel may try to send on the dead transport channel. NULLing
207 // is not an effective strategy since the sends will come on another thread.
208 delete media_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700209 // Note that we don't just call set_transport_channel(nullptr) because that
210 // would call a pure virtual method which we can't do from a destructor.
211 if (transport_channel_) {
212 DisconnectFromTransportChannel(transport_channel_);
213 transport_controller_->DestroyTransportChannel_w(
214 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
215 }
216 if (rtcp_transport_channel_) {
217 DisconnectFromTransportChannel(rtcp_transport_channel_);
218 transport_controller_->DestroyTransportChannel_w(
219 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
220 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221 LOG(LS_INFO) << "Destroyed channel";
222}
223
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000224bool BaseChannel::Init() {
deadbeefcbecd352015-09-23 11:50:27 -0700225 if (!SetTransport(content_name())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 return false;
227 }
228
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000229 if (!SetDtlsSrtpCiphers(transport_channel(), false)) {
230 return false;
231 }
deadbeefcbecd352015-09-23 11:50:27 -0700232 if (rtcp_transport_enabled() &&
233 !SetDtlsSrtpCiphers(rtcp_transport_channel(), true)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000234 return false;
235 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236
wu@webrtc.orgde305012013-10-31 15:40:38 +0000237 // Both RTP and RTCP channels are set, we can call SetInterface on
238 // media channel and it can set network options.
239 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 return true;
241}
242
wu@webrtc.org78187522013-10-07 23:32:02 +0000243void BaseChannel::Deinit() {
244 media_channel_->SetInterface(NULL);
245}
246
deadbeefcbecd352015-09-23 11:50:27 -0700247bool BaseChannel::SetTransport(const std::string& transport_name) {
248 return worker_thread_->Invoke<bool>(
249 Bind(&BaseChannel::SetTransport_w, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000250}
251
deadbeefcbecd352015-09-23 11:50:27 -0700252bool BaseChannel::SetTransport_w(const std::string& transport_name) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000253 ASSERT(worker_thread_ == rtc::Thread::Current());
254
deadbeefcbecd352015-09-23 11:50:27 -0700255 if (transport_name == transport_name_) {
256 // Nothing to do if transport name isn't changing
257 return true;
258 }
259
260 set_transport_channel(transport_controller_->CreateTransportChannel_w(
261 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000262 if (!transport_channel()) {
263 return false;
264 }
deadbeefcbecd352015-09-23 11:50:27 -0700265 if (rtcp_transport_enabled()) {
266 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
267 << " on " << transport_name << " transport ";
268 set_rtcp_transport_channel(transport_controller_->CreateTransportChannel_w(
269 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000270 if (!rtcp_transport_channel()) {
271 return false;
272 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000273 }
274
deadbeefcbecd352015-09-23 11:50:27 -0700275 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000276 return true;
277}
278
279void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
280 ASSERT(worker_thread_ == rtc::Thread::Current());
281
282 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700283 if (!old_tc && !new_tc) {
284 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000285 return;
286 }
deadbeefcbecd352015-09-23 11:50:27 -0700287 ASSERT(old_tc != new_tc);
288
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000289 if (old_tc) {
290 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700291 transport_controller_->DestroyTransportChannel_w(
292 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000293 }
294
295 transport_channel_ = new_tc;
296
297 if (new_tc) {
298 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700299 for (const auto& pair : socket_options_) {
300 new_tc->SetOption(pair.first, pair.second);
301 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000302 }
deadbeefcbecd352015-09-23 11:50:27 -0700303
304 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
305 // setting new channel
306 UpdateWritableState_w();
307 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000308}
309
310void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
311 ASSERT(worker_thread_ == rtc::Thread::Current());
312
313 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700314 if (!old_tc && !new_tc) {
315 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000316 return;
317 }
deadbeefcbecd352015-09-23 11:50:27 -0700318 ASSERT(old_tc != new_tc);
319
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000320 if (old_tc) {
321 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700322 transport_controller_->DestroyTransportChannel_w(
323 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000324 }
325
326 rtcp_transport_channel_ = new_tc;
327
328 if (new_tc) {
329 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700330 for (const auto& pair : rtcp_socket_options_) {
331 new_tc->SetOption(pair.first, pair.second);
332 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000333 }
deadbeefcbecd352015-09-23 11:50:27 -0700334
335 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
336 // setting new channel
337 UpdateWritableState_w();
338 SetReadyToSend(true, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000339}
340
341void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
342 ASSERT(worker_thread_ == rtc::Thread::Current());
343
344 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
345 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
346 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
347}
348
349void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
350 ASSERT(worker_thread_ == rtc::Thread::Current());
351
352 tc->SignalWritableState.disconnect(this);
353 tc->SignalReadPacket.disconnect(this);
354 tc->SignalReadyToSend.disconnect(this);
355}
356
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000358 worker_thread_->Invoke<void>(Bind(
359 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
360 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000361 return true;
362}
363
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000365 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366}
367
368bool BaseChannel::RemoveRecvStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000369 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370}
371
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000372bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000373 return InvokeOnWorker(
374 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000375}
376
377bool BaseChannel::RemoveSendStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000378 return InvokeOnWorker(
379 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000380}
381
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000382bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000383 ContentAction action,
384 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000385 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
386 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387}
388
389bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000390 ContentAction action,
391 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000392 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
393 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000394}
395
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000396void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000397 // We pass in the BaseChannel instead of the transport_channel_
398 // because if the transport_channel_ changes, the ConnectionMonitor
399 // would be pointing to the wrong TransportChannel.
400 connection_monitor_.reset(new ConnectionMonitor(
401 this, worker_thread(), rtc::Thread::Current()));
402 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000404 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405}
406
407void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000408 if (connection_monitor_) {
409 connection_monitor_->Stop();
410 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000411 }
412}
413
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000414bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
415 ASSERT(worker_thread_ == rtc::Thread::Current());
416 return transport_channel_->GetStats(infos);
417}
418
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419bool BaseChannel::IsReadyToReceive() const {
420 // Receive data if we are enabled and have local content,
421 return enabled() && IsReceiveContentDirection(local_content_direction_);
422}
423
424bool BaseChannel::IsReadyToSend() const {
425 // Send outgoing data if we are enabled, have local and remote content,
426 // and we have had some form of connectivity.
427 return enabled() &&
428 IsReceiveContentDirection(remote_content_direction_) &&
429 IsSendContentDirection(local_content_direction_) &&
430 was_ever_writable();
431}
432
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000433bool BaseChannel::SendPacket(rtc::Buffer* packet,
434 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000435 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436}
437
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000438bool BaseChannel::SendRtcp(rtc::Buffer* packet,
439 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000440 return SendPacket(true, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441}
442
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000443int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000445 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000446 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000447 case ST_RTP:
448 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700449 socket_options_.push_back(
450 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000451 break;
452 case ST_RTCP:
453 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700454 rtcp_socket_options_.push_back(
455 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000456 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000458 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459}
460
461void BaseChannel::OnWritableState(TransportChannel* channel) {
462 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
deadbeefcbecd352015-09-23 11:50:27 -0700463 UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464}
465
466void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000467 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000468 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000469 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000471 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472
473 // When using RTCP multiplexing we might get RTCP packets on the RTP
474 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
475 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000476 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000477 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478}
479
480void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700481 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
482 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483}
484
deadbeefcbecd352015-09-23 11:50:27 -0700485void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
486 if (rtcp) {
487 rtcp_ready_to_send_ = ready;
488 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 rtp_ready_to_send_ = ready;
490 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000491
deadbeefcbecd352015-09-23 11:50:27 -0700492 if (rtp_ready_to_send_ &&
493 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
494 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
torbjornga81a42f2015-09-23 02:16:58 -0700495 // Notify the MediaChannel when both rtp and rtcp channel can send.
496 media_channel_->OnReadyToSend(true);
deadbeefcbecd352015-09-23 11:50:27 -0700497 } else {
498 // Notify the MediaChannel when either rtp or rtcp channel can't send.
499 media_channel_->OnReadyToSend(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500 }
501}
502
503bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
504 const char* data, size_t len) {
505 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000506 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000507}
508
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000509bool BaseChannel::SendPacket(bool rtcp, rtc::Buffer* packet,
510 rtc::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 // SendPacket gets called from MediaEngine, typically on an encoder thread.
512 // If the thread is not our worker thread, we will post to our worker
513 // so that the real work happens on our worker. This avoids us having to
514 // synchronize access to all the pieces of the send path, including
515 // SRTP and the inner workings of the transport channels.
516 // The only downside is that we can't return a proper failure code if
517 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000518 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000519 // Avoid a copy by transferring the ownership of the packet data.
520 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
521 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200522 data->packet = packet->Pass();
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000523 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 worker_thread_->Post(this, message_id, data);
525 return true;
526 }
527
528 // Now that we are on the correct thread, ensure we have a place to send this
529 // packet before doing anything. (We might get RTCP packets that we don't
530 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
531 // transport.
532 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
533 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000534 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535 return false;
536 }
537
538 // Protect ourselves against crazy data.
539 if (!ValidPacket(rtcp, packet)) {
540 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000541 << PacketType(rtcp)
542 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 return false;
544 }
545
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000546 rtc::PacketOptions options(dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000547 // Protect if needed.
548 if (srtp_filter_.IsActive()) {
549 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200550 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000551 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000553 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
554 // inside libsrtp for a RTP packet. A external HMAC module will be writing
555 // a fake HMAC value. This is ONLY done for a RTP packet.
556 // Socket layer will update rtp sendtime extension header if present in
557 // packet with current time before updating the HMAC.
558#if !defined(ENABLE_EXTERNAL_AUTH)
559 res = srtp_filter_.ProtectRtp(
560 data, len, static_cast<int>(packet->capacity()), &len);
561#else
henrike@webrtc.org05376342014-03-10 15:53:12 +0000562 options.packet_time_params.rtp_sendtime_extension_id =
563 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000564 res = srtp_filter_.ProtectRtp(
565 data, len, static_cast<int>(packet->capacity()), &len,
566 &options.packet_time_params.srtp_packet_index);
567 // If protection succeeds, let's get auth params from srtp.
568 if (res) {
569 uint8* auth_key = NULL;
570 int key_len;
571 res = srtp_filter_.GetRtpAuthParams(
572 &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
573 if (res) {
574 options.packet_time_params.srtp_auth_key.resize(key_len);
575 options.packet_time_params.srtp_auth_key.assign(auth_key,
576 auth_key + key_len);
577 }
578 }
579#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 if (!res) {
581 int seq_num = -1;
582 uint32 ssrc = 0;
583 GetRtpSeqNum(data, len, &seq_num);
584 GetRtpSsrc(data, len, &ssrc);
585 LOG(LS_ERROR) << "Failed to protect " << content_name_
586 << " RTP packet: size=" << len
587 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
588 return false;
589 }
590 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000591 res = srtp_filter_.ProtectRtcp(data, len,
592 static_cast<int>(packet->capacity()),
593 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 if (!res) {
595 int type = -1;
596 GetRtcpType(data, len, &type);
597 LOG(LS_ERROR) << "Failed to protect " << content_name_
598 << " RTCP packet: size=" << len << ", type=" << type;
599 return false;
600 }
601 }
602
603 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000604 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000605 } else if (secure_required_) {
606 // This is a double check for something that supposedly can't happen.
607 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
608 << " packet when SRTP is inactive and crypto is required";
609
610 ASSERT(false);
611 return false;
612 }
613
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000615 int ret =
Karl Wiberg94784372015-04-20 14:03:07 +0200616 channel->SendPacket(packet->data<char>(), packet->size(), options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000617 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
618 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 if (channel->GetError() == EWOULDBLOCK) {
620 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700621 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 }
623 return false;
624 }
625 return true;
626}
627
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000628bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 // Protect ourselves against crazy data.
630 if (!ValidPacket(rtcp, packet)) {
631 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000632 << PacketType(rtcp)
633 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 return false;
635 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000637 // Bundle filter handles both rtp and rtcp packets.
Karl Wiberg94784372015-04-20 14:03:07 +0200638 return bundle_filter_.DemuxPacket(packet->data<char>(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000639}
640
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000641void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
642 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 if (!WantsPacket(rtcp, packet)) {
644 return;
645 }
646
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000647 // We are only interested in the first rtp packet because that
648 // indicates the media has started flowing.
649 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 has_received_packet_ = true;
651 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
652 }
653
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 // Unprotect the packet, if needed.
655 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200656 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000657 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 bool res;
659 if (!rtcp) {
660 res = srtp_filter_.UnprotectRtp(data, len, &len);
661 if (!res) {
662 int seq_num = -1;
663 uint32 ssrc = 0;
664 GetRtpSeqNum(data, len, &seq_num);
665 GetRtpSsrc(data, len, &ssrc);
666 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
667 << " RTP packet: size=" << len
668 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
669 return;
670 }
671 } else {
672 res = srtp_filter_.UnprotectRtcp(data, len, &len);
673 if (!res) {
674 int type = -1;
675 GetRtcpType(data, len, &type);
676 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
677 << " RTCP packet: size=" << len << ", type=" << type;
678 return;
679 }
680 }
681
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000682 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 } else if (secure_required_) {
684 // Our session description indicates that SRTP is required, but we got a
685 // packet before our SRTP filter is active. This means either that
686 // a) we got SRTP packets before we received the SDES keys, in which case
687 // we can't decrypt it anyway, or
688 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
689 // channels, so we haven't yet extracted keys, even if DTLS did complete
690 // on the channel that the packets are being sent on. It's really good
691 // practice to wait for both RTP and RTCP to be good to go before sending
692 // media, to prevent weird failure modes, so it's fine for us to just eat
693 // packets here. This is all sidestepped if RTCP mux is used anyway.
694 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
695 << " packet when SRTP is inactive and crypto is required";
696 return;
697 }
698
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000699 // Push it down to the media channel.
700 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000701 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000702 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000703 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704 }
705}
706
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000707bool BaseChannel::PushdownLocalDescription(
708 const SessionDescription* local_desc, ContentAction action,
709 std::string* error_desc) {
710 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711 const MediaContentDescription* content_desc =
712 GetContentDescription(content_info);
713 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000714 !SetLocalContent(content_desc, action, error_desc)) {
715 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
716 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000718 return true;
719}
720
721bool BaseChannel::PushdownRemoteDescription(
722 const SessionDescription* remote_desc, ContentAction action,
723 std::string* error_desc) {
724 const ContentInfo* content_info = GetFirstContent(remote_desc);
725 const MediaContentDescription* content_desc =
726 GetContentDescription(content_info);
727 if (content_desc && content_info && !content_info->rejected &&
728 !SetRemoteContent(content_desc, action, error_desc)) {
729 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
730 return false;
731 }
732 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733}
734
735void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000736 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 if (enabled_)
738 return;
739
740 LOG(LS_INFO) << "Channel enabled";
741 enabled_ = true;
742 ChangeState();
743}
744
745void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000746 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 if (!enabled_)
748 return;
749
750 LOG(LS_INFO) << "Channel disabled";
751 enabled_ = false;
752 ChangeState();
753}
754
deadbeefcbecd352015-09-23 11:50:27 -0700755void BaseChannel::UpdateWritableState_w() {
756 if (transport_channel_ && transport_channel_->writable() &&
757 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
758 ChannelWritable_w();
759 } else {
760 ChannelNotWritable_w();
761 }
762}
763
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000764void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000765 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000766 if (writable_)
767 return;
768
deadbeefcbecd352015-09-23 11:50:27 -0700769 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 << (was_ever_writable_ ? "" : " for the first time");
771
772 std::vector<ConnectionInfo> infos;
773 transport_channel_->GetStats(&infos);
774 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
775 it != infos.end(); ++it) {
776 if (it->best_connection) {
777 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
778 << "->" << it->remote_candidate.ToSensitiveString();
779 break;
780 }
781 }
782
783 // If we're doing DTLS-SRTP, now is the time.
784 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
785 if (!SetupDtlsSrtp(false)) {
deadbeefcbecd352015-09-23 11:50:27 -0700786 SignalDtlsSetupFailure_w(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000787 return;
788 }
789
790 if (rtcp_transport_channel_) {
791 if (!SetupDtlsSrtp(true)) {
deadbeefcbecd352015-09-23 11:50:27 -0700792 SignalDtlsSetupFailure_w(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 return;
794 }
795 }
796 }
797
798 was_ever_writable_ = true;
799 writable_ = true;
800 ChangeState();
801}
802
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000803void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
804 ASSERT(worker_thread() == rtc::Thread::Current());
805 signaling_thread()->Invoke<void>(Bind(
806 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
807}
808
809void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
810 ASSERT(signaling_thread() == rtc::Thread::Current());
811 SignalDtlsSetupFailure(this, rtcp);
812}
813
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000814bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
815 std::vector<std::string> ciphers;
816 // We always use the default SRTP ciphers for RTCP, but we may use different
817 // ciphers for RTP depending on the media type.
818 if (!rtcp) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700819 GetSrtpCryptoSuiteNames(&ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820 } else {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700821 GetDefaultSrtpCryptoSuiteNames(&ciphers);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000822 }
823 return tc->SetSrtpCiphers(ciphers);
824}
825
826bool BaseChannel::ShouldSetupDtlsSrtp() const {
827 return true;
828}
829
830// This function returns true if either DTLS-SRTP is not in use
831// *or* DTLS-SRTP is successfully set up.
832bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
833 bool ret = false;
834
deadbeefcbecd352015-09-23 11:50:27 -0700835 TransportChannel* channel =
836 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000837
838 // No DTLS
839 if (!channel->IsDtlsActive())
840 return true;
841
842 std::string selected_cipher;
843
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700844 if (!channel->GetSrtpCryptoSuite(&selected_cipher)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000845 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
846 return false;
847 }
848
849 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
850 << content_name() << " "
851 << PacketType(rtcp_channel);
852
853 // OK, we're now doing DTLS (RFC 5764)
854 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
855 SRTP_MASTER_KEY_SALT_LEN * 2);
856
857 // RFC 5705 exporter using the RFC 5764 parameters
858 if (!channel->ExportKeyingMaterial(
859 kDtlsSrtpExporterLabel,
860 NULL, 0, false,
861 &dtls_buffer[0], dtls_buffer.size())) {
862 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
863 ASSERT(false); // This should never happen
864 return false;
865 }
866
867 // Sync up the keys with the DTLS-SRTP interface
868 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
869 SRTP_MASTER_KEY_SALT_LEN);
870 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
871 SRTP_MASTER_KEY_SALT_LEN);
872 size_t offset = 0;
873 memcpy(&client_write_key[0], &dtls_buffer[offset],
874 SRTP_MASTER_KEY_KEY_LEN);
875 offset += SRTP_MASTER_KEY_KEY_LEN;
876 memcpy(&server_write_key[0], &dtls_buffer[offset],
877 SRTP_MASTER_KEY_KEY_LEN);
878 offset += SRTP_MASTER_KEY_KEY_LEN;
879 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
880 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
881 offset += SRTP_MASTER_KEY_SALT_LEN;
882 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
883 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
884
885 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000886 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000887 if (!channel->GetSslRole(&role)) {
888 LOG(LS_WARNING) << "GetSslRole failed";
889 return false;
890 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000892 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000893 send_key = &server_write_key;
894 recv_key = &client_write_key;
895 } else {
896 send_key = &client_write_key;
897 recv_key = &server_write_key;
898 }
899
900 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000901 ret = srtp_filter_.SetRtcpParams(
902 selected_cipher,
903 &(*send_key)[0],
904 static_cast<int>(send_key->size()),
905 selected_cipher,
906 &(*recv_key)[0],
907 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000909 ret = srtp_filter_.SetRtpParams(
910 selected_cipher,
911 &(*send_key)[0],
912 static_cast<int>(send_key->size()),
913 selected_cipher,
914 &(*recv_key)[0],
915 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 }
917
918 if (!ret)
919 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
920 else
921 dtls_keyed_ = true;
922
923 return ret;
924}
925
926void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000927 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 if (!writable_)
929 return;
930
deadbeefcbecd352015-09-23 11:50:27 -0700931 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 writable_ = false;
933 ChangeState();
934}
935
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700936bool BaseChannel::SetRtpTransportParameters_w(
937 const MediaContentDescription* content,
938 ContentAction action,
939 ContentSource src,
940 std::string* error_desc) {
941 if (action == CA_UPDATE) {
942 // These parameters never get changed by a CA_UDPATE.
943 return true;
944 }
945
946 // Cache secure_required_ for belt and suspenders check on SendPacket
947 if (src == CS_LOCAL) {
948 set_secure_required(content->crypto_required() != CT_NONE);
949 }
950
951 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
952 return false;
953 }
954
955 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
956 return false;
957 }
958
959 return true;
960}
961
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000962// |dtls| will be set to true if DTLS is active for transport channel and
963// crypto is empty.
964bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000965 bool* dtls,
966 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000967 *dtls = transport_channel_->IsDtlsActive();
968 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000969 SafeSetError("Cryptos must be empty when DTLS is active.",
970 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000971 return false;
972 }
973 return true;
974}
975
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000977 ContentAction action,
978 ContentSource src,
979 std::string* error_desc) {
980 if (action == CA_UPDATE) {
981 // no crypto params.
982 return true;
983 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000985 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000986 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
987 if (!ret) {
988 return false;
989 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 switch (action) {
991 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000992 // If DTLS is already active on the channel, we could be renegotiating
993 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000994 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000995 ret = srtp_filter_.SetOffer(cryptos, src);
996 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 break;
998 case CA_PRANSWER:
999 // If we're doing DTLS-SRTP, we don't want to update the filter
1000 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001001 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1003 }
1004 break;
1005 case CA_ANSWER:
1006 // If we're doing DTLS-SRTP, we don't want to update the filter
1007 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001008 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 ret = srtp_filter_.SetAnswer(cryptos, src);
1010 }
1011 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 default:
1013 break;
1014 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001015 if (!ret) {
1016 SafeSetError("Failed to setup SRTP filter.", error_desc);
1017 return false;
1018 }
1019 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020}
1021
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001022void BaseChannel::ActivateRtcpMux() {
1023 worker_thread_->Invoke<void>(Bind(
1024 &BaseChannel::ActivateRtcpMux_w, this));
1025}
1026
1027void BaseChannel::ActivateRtcpMux_w() {
1028 if (!rtcp_mux_filter_.IsActive()) {
1029 rtcp_mux_filter_.SetActive();
deadbeefcbecd352015-09-23 11:50:27 -07001030 set_rtcp_transport_channel(nullptr);
1031 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001032 }
1033}
1034
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001036 ContentSource src,
1037 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 bool ret = false;
1039 switch (action) {
1040 case CA_OFFER:
1041 ret = rtcp_mux_filter_.SetOffer(enable, src);
1042 break;
1043 case CA_PRANSWER:
1044 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1045 break;
1046 case CA_ANSWER:
1047 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1048 if (ret && rtcp_mux_filter_.IsActive()) {
1049 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001050 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1051 << " by destroying RTCP transport channel for "
1052 << transport_name();
1053 set_rtcp_transport_channel(nullptr);
1054 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 }
1056 break;
1057 case CA_UPDATE:
1058 // No RTCP mux info.
1059 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001060 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 default:
1062 break;
1063 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001064 if (!ret) {
1065 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1066 return false;
1067 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1069 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1070 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001071 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 // If the RTP transport is already writable, then so are we.
1073 if (transport_channel_->writable()) {
1074 ChannelWritable_w();
1075 }
1076 }
1077
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001078 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079}
1080
1081bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001082 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083 if (!media_channel()->AddRecvStream(sp))
1084 return false;
1085
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001086 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087}
1088
1089bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001090 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001091 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 return media_channel()->RemoveRecvStream(ssrc);
1093}
1094
1095bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001096 ContentAction action,
1097 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1099 action == CA_PRANSWER || action == CA_UPDATE))
1100 return false;
1101
1102 // If this is an update, streams only contain streams that have changed.
1103 if (action == CA_UPDATE) {
1104 for (StreamParamsVec::const_iterator it = streams.begin();
1105 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001106 const StreamParams* existing_stream =
1107 GetStreamByIds(local_streams_, it->groupid, it->id);
1108 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 if (media_channel()->AddSendStream(*it)) {
1110 local_streams_.push_back(*it);
1111 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1112 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001113 std::ostringstream desc;
1114 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1115 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001116 return false;
1117 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001118 } else if (existing_stream && !it->has_ssrcs()) {
1119 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001120 std::ostringstream desc;
1121 desc << "Failed to remove send stream with ssrc "
1122 << it->first_ssrc() << ".";
1123 SafeSetError(desc.str(), error_desc);
1124 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001126 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 } else {
1128 LOG(LS_WARNING) << "Ignore unsupported stream update";
1129 }
1130 }
1131 return true;
1132 }
1133 // Else streams are all the streams we want to send.
1134
1135 // Check for streams that have been removed.
1136 bool ret = true;
1137 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1138 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001139 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001141 std::ostringstream desc;
1142 desc << "Failed to remove send stream with ssrc "
1143 << it->first_ssrc() << ".";
1144 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 ret = false;
1146 }
1147 }
1148 }
1149 // Check for new streams.
1150 for (StreamParamsVec::const_iterator it = streams.begin();
1151 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001152 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153 if (media_channel()->AddSendStream(*it)) {
1154 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1155 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001156 std::ostringstream desc;
1157 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1158 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 ret = false;
1160 }
1161 }
1162 }
1163 local_streams_ = streams;
1164 return ret;
1165}
1166
1167bool BaseChannel::UpdateRemoteStreams_w(
1168 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001169 ContentAction action,
1170 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001171 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1172 action == CA_PRANSWER || action == CA_UPDATE))
1173 return false;
1174
1175 // If this is an update, streams only contain streams that have changed.
1176 if (action == CA_UPDATE) {
1177 for (StreamParamsVec::const_iterator it = streams.begin();
1178 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001179 const StreamParams* existing_stream =
1180 GetStreamByIds(remote_streams_, it->groupid, it->id);
1181 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 if (AddRecvStream_w(*it)) {
1183 remote_streams_.push_back(*it);
1184 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1185 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001186 std::ostringstream desc;
1187 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1188 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 return false;
1190 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001191 } else if (existing_stream && !it->has_ssrcs()) {
1192 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001193 std::ostringstream desc;
1194 desc << "Failed to remove remote stream with ssrc "
1195 << it->first_ssrc() << ".";
1196 SafeSetError(desc.str(), error_desc);
1197 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001199 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001200 } else {
1201 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001202 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 << " new stream = " << it->ToString();
1204 }
1205 }
1206 return true;
1207 }
1208 // Else streams are all the streams we want to receive.
1209
1210 // Check for streams that have been removed.
1211 bool ret = true;
1212 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1213 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001214 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001215 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001216 std::ostringstream desc;
1217 desc << "Failed to remove remote stream with ssrc "
1218 << it->first_ssrc() << ".";
1219 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001220 ret = false;
1221 }
1222 }
1223 }
1224 // Check for new streams.
1225 for (StreamParamsVec::const_iterator it = streams.begin();
1226 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001227 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 if (AddRecvStream_w(*it)) {
1229 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1230 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001231 std::ostringstream desc;
1232 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1233 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001234 ret = false;
1235 }
1236 }
1237 }
1238 remote_streams_ = streams;
1239 return ret;
1240}
1241
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001242void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1243 const std::vector<RtpHeaderExtension>& extensions) {
1244 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001245 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001246 rtp_abs_sendtime_extn_id_ =
1247 send_time_extension ? send_time_extension->id : -1;
1248}
1249
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001250void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 case MSG_RTPPACKET:
1253 case MSG_RTCPPACKET: {
1254 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001255 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001256 delete data; // because it is Posted
1257 break;
1258 }
1259 case MSG_FIRSTPACKETRECEIVED: {
1260 SignalFirstPacketReceived(this);
1261 break;
1262 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001263 }
1264}
1265
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001266void BaseChannel::FlushRtcpMessages() {
1267 // Flush all remaining RTCP messages. This should only be called in
1268 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001269 ASSERT(rtc::Thread::Current() == worker_thread_);
1270 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001271 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001272 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001273 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001274 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 }
1276}
1277
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001278VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279 MediaEngineInterface* media_engine,
1280 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001281 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001282 const std::string& content_name,
1283 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001284 : BaseChannel(thread,
1285 media_channel,
1286 transport_controller,
1287 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001289 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001290 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291
1292VoiceChannel::~VoiceChannel() {
1293 StopAudioMonitor();
1294 StopMediaMonitor();
1295 // this can't be done in the base class, since it calls a virtual
1296 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001297 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298}
1299
1300bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001301 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302 return false;
1303 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001304 return true;
1305}
1306
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001307bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001308 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1309 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001310}
1311
deadbeefcbecd352015-09-23 11:50:27 -07001312bool VoiceChannel::SetAudioSend(uint32 ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001313 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001314 const AudioOptions* options,
1315 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001316 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001317 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318}
1319
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001320// TODO(juberti): Handle early media the right way. We should get an explicit
1321// ringing message telling us to start playing local ringback, which we cancel
1322// if any early media actually arrives. For now, we do the opposite, which is
1323// to wait 1 second for early media, and start playing local ringback if none
1324// arrives.
1325void VoiceChannel::SetEarlyMedia(bool enable) {
1326 if (enable) {
1327 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001328 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1329 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 } else {
1331 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001332 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333 }
1334}
1335
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001336bool VoiceChannel::PressDTMF(int digit, bool playout) {
1337 int flags = DF_SEND;
1338 if (playout) {
1339 flags |= DF_PLAY;
1340 }
1341 int duration_ms = 160;
1342 return InsertDtmf(0, digit, duration_ms, flags);
1343}
1344
1345bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001346 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1347 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348}
1349
1350bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1351 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001352 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1353 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354}
1355
1356bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001357 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1358 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001360
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001362 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1363 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364}
1365
1366void VoiceChannel::StartMediaMonitor(int cms) {
1367 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001368 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 media_monitor_->SignalUpdate.connect(
1370 this, &VoiceChannel::OnMediaMonitorUpdate);
1371 media_monitor_->Start(cms);
1372}
1373
1374void VoiceChannel::StopMediaMonitor() {
1375 if (media_monitor_) {
1376 media_monitor_->Stop();
1377 media_monitor_->SignalUpdate.disconnect(this);
1378 media_monitor_.reset();
1379 }
1380}
1381
1382void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001383 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 audio_monitor_
1385 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1386 audio_monitor_->Start(cms);
1387}
1388
1389void VoiceChannel::StopAudioMonitor() {
1390 if (audio_monitor_) {
1391 audio_monitor_->Stop();
1392 audio_monitor_.reset();
1393 }
1394}
1395
1396bool VoiceChannel::IsAudioMonitorRunning() const {
1397 return (audio_monitor_.get() != NULL);
1398}
1399
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001400int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001401 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001402}
1403
1404int VoiceChannel::GetOutputLevel_w() {
1405 return media_channel()->GetOutputLevel();
1406}
1407
1408void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1409 media_channel()->GetActiveStreams(actives);
1410}
1411
1412void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001413 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001414 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001415 int flags) {
1416 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417
1418 // Set a flag when we've received an RTP packet. If we're waiting for early
1419 // media, this will disable the timeout.
1420 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1421 received_media_ = true;
1422 }
1423}
1424
1425void VoiceChannel::ChangeState() {
1426 // Render incoming data if we're the active call, and we have the local
1427 // content. We receive data on the default channel and multiplexed streams.
1428 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001429 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430
1431 // Send outgoing data if we're the active call, we have the remote content,
1432 // and we have had some form of connectivity.
1433 bool send = IsReadyToSend();
1434 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1435 if (!media_channel()->SetSend(send_flag)) {
1436 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 }
1438
1439 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1440}
1441
1442const ContentInfo* VoiceChannel::GetFirstContent(
1443 const SessionDescription* sdesc) {
1444 return GetFirstAudioContent(sdesc);
1445}
1446
1447bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001448 ContentAction action,
1449 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001450 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451 LOG(LS_INFO) << "Setting local voice description";
1452
1453 const AudioContentDescription* audio =
1454 static_cast<const AudioContentDescription*>(content);
1455 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001456 if (!audio) {
1457 SafeSetError("Can't find audio content in local description.", error_desc);
1458 return false;
1459 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001461 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1462 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463 }
1464
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001465 AudioRecvParameters recv_params = last_recv_params_;
1466 RtpParametersFromMediaDescription(audio, &recv_params);
1467 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001468 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001469 error_desc);
1470 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001472 for (const AudioCodec& codec : audio->codecs()) {
1473 bundle_filter()->AddPayloadType(codec.id);
1474 }
1475 last_recv_params_ = recv_params;
1476
1477 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1478 // only give it to the media channel once we have a remote
1479 // description too (without a remote description, we won't be able
1480 // to send them anyway).
1481 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1482 SafeSetError("Failed to set local audio description streams.", error_desc);
1483 return false;
1484 }
1485
1486 set_local_content_direction(content->direction());
1487 ChangeState();
1488 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489}
1490
1491bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001492 ContentAction action,
1493 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001494 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495 LOG(LS_INFO) << "Setting remote voice description";
1496
1497 const AudioContentDescription* audio =
1498 static_cast<const AudioContentDescription*>(content);
1499 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001500 if (!audio) {
1501 SafeSetError("Can't find audio content in remote description.", error_desc);
1502 return false;
1503 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001505 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1506 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507 }
1508
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001509 AudioSendParameters send_params = last_send_params_;
1510 RtpSendParametersFromMediaDescription(audio, &send_params);
1511 if (audio->conference_mode()) {
1512 send_params.options.conference_mode.Set(true);
1513 }
1514 if (audio->agc_minus_10db()) {
1515 send_params.options.adjust_agc_delta.Set(kAgcMinus10db);
1516 }
1517 if (!media_channel()->SetSendParameters(send_params)) {
1518 SafeSetError("Failed to set remote audio description send parameters.",
1519 error_desc);
1520 return false;
1521 }
1522 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001524 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1525 // and only give it to the media channel once we have a local
1526 // description too (without a local description, we won't be able to
1527 // recv them anyway).
1528 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1529 SafeSetError("Failed to set remote audio description streams.", error_desc);
1530 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531 }
1532
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001533 if (audio->rtp_header_extensions_set()) {
1534 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1535 }
1536
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001537 set_remote_content_direction(content->direction());
1538 ChangeState();
1539 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540}
1541
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542void VoiceChannel::HandleEarlyMediaTimeout() {
1543 // This occurs on the main thread, not the worker thread.
1544 if (!received_media_) {
1545 LOG(LS_INFO) << "No early media received before timeout";
1546 SignalEarlyMediaTimeout(this);
1547 }
1548}
1549
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1551 int flags) {
1552 if (!enabled()) {
1553 return false;
1554 }
1555
1556 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1557}
1558
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001559void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 case MSG_EARLYMEDIATIMEOUT:
1562 HandleEarlyMediaTimeout();
1563 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564 case MSG_CHANNEL_ERROR: {
1565 VoiceChannelErrorMessageData* data =
1566 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 delete data;
1568 break;
1569 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 default:
1571 BaseChannel::OnMessage(pmsg);
1572 break;
1573 }
1574}
1575
1576void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001577 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 SignalConnectionMonitor(this, infos);
1579}
1580
1581void VoiceChannel::OnMediaMonitorUpdate(
1582 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1583 ASSERT(media_channel == this->media_channel());
1584 SignalMediaMonitor(this, info);
1585}
1586
1587void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1588 const AudioInfo& info) {
1589 SignalAudioMonitor(this, info);
1590}
1591
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001592void VoiceChannel::GetSrtpCryptoSuiteNames(
1593 std::vector<std::string>* ciphers) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 GetSupportedAudioCryptoSuites(ciphers);
1595}
1596
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001597VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001599 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001601 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001602 : BaseChannel(thread,
1603 media_channel,
1604 transport_controller,
1605 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001607 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001608 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609
1610bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001611 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612 return false;
1613 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 return true;
1615}
1616
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617VideoChannel::~VideoChannel() {
1618 std::vector<uint32> screencast_ssrcs;
1619 ScreencastMap::iterator iter;
1620 while (!screencast_capturers_.empty()) {
1621 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1622 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1623 << screencast_capturers_.begin()->first;
1624 ASSERT(false);
1625 break;
1626 }
1627 }
1628
1629 StopMediaMonitor();
1630 // this can't be done in the base class, since it calls a virtual
1631 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001632
1633 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634}
1635
1636bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001637 worker_thread()->Invoke<void>(Bind(
1638 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 return true;
1640}
1641
1642bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001643 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644}
1645
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001646bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1647 return worker_thread()->Invoke<bool>(Bind(
1648 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649}
1650
1651bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001652 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1653 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654}
1655
1656bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001657 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658}
1659
1660bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001661 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662}
1663
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001664int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001665 ScreencastDetailsData data(ssrc);
1666 worker_thread()->Invoke<void>(Bind(
1667 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001668 return data.fps;
1669}
1670
1671int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001672 ScreencastDetailsData data(ssrc);
1673 worker_thread()->Invoke<void>(Bind(
1674 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001675 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676}
1677
1678bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001679 worker_thread()->Invoke<void>(Bind(
1680 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001681 return true;
1682}
1683
1684bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001685 worker_thread()->Invoke<void>(Bind(
1686 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687 return true;
1688}
1689
deadbeefcbecd352015-09-23 11:50:27 -07001690bool VideoChannel::SetVideoSend(uint32 ssrc,
1691 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001692 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001693 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1694 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001695}
1696
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698 // Send outgoing data if we're the active call, we have the remote content,
1699 // and we have had some form of connectivity.
1700 bool send = IsReadyToSend();
1701 if (!media_channel()->SetSend(send)) {
1702 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1703 // TODO(gangji): Report error back to server.
1704 }
1705
Peter Boström34fbfff2015-09-24 19:20:30 +02001706 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707}
1708
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001709bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1710 return InvokeOnWorker(
1711 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712}
1713
1714void VideoChannel::StartMediaMonitor(int cms) {
1715 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001716 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 media_monitor_->SignalUpdate.connect(
1718 this, &VideoChannel::OnMediaMonitorUpdate);
1719 media_monitor_->Start(cms);
1720}
1721
1722void VideoChannel::StopMediaMonitor() {
1723 if (media_monitor_) {
1724 media_monitor_->Stop();
1725 media_monitor_.reset();
1726 }
1727}
1728
1729const ContentInfo* VideoChannel::GetFirstContent(
1730 const SessionDescription* sdesc) {
1731 return GetFirstVideoContent(sdesc);
1732}
1733
1734bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001735 ContentAction action,
1736 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001737 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 LOG(LS_INFO) << "Setting local video description";
1739
1740 const VideoContentDescription* video =
1741 static_cast<const VideoContentDescription*>(content);
1742 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001743 if (!video) {
1744 SafeSetError("Can't find video content in local description.", error_desc);
1745 return false;
1746 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001748 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1749 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 }
1751
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001752 VideoRecvParameters recv_params = last_recv_params_;
1753 RtpParametersFromMediaDescription(video, &recv_params);
1754 if (!media_channel()->SetRecvParameters(recv_params)) {
1755 SafeSetError("Failed to set local video description recv parameters.",
1756 error_desc);
1757 return false;
1758 }
1759 for (const VideoCodec& codec : video->codecs()) {
1760 bundle_filter()->AddPayloadType(codec.id);
1761 }
1762 last_recv_params_ = recv_params;
1763
1764 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1765 // only give it to the media channel once we have a remote
1766 // description too (without a remote description, we won't be able
1767 // to send them anyway).
1768 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1769 SafeSetError("Failed to set local video description streams.", error_desc);
1770 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 }
1772
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001773 set_local_content_direction(content->direction());
1774 ChangeState();
1775 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776}
1777
1778bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001779 ContentAction action,
1780 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001781 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 LOG(LS_INFO) << "Setting remote video description";
1783
1784 const VideoContentDescription* video =
1785 static_cast<const VideoContentDescription*>(content);
1786 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001787 if (!video) {
1788 SafeSetError("Can't find video content in remote description.", error_desc);
1789 return false;
1790 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001792
1793 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1794 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 }
1796
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001797 VideoSendParameters send_params = last_send_params_;
1798 RtpSendParametersFromMediaDescription(video, &send_params);
1799 if (video->conference_mode()) {
1800 send_params.options.conference_mode.Set(true);
1801 }
1802 if (!media_channel()->SetSendParameters(send_params)) {
1803 SafeSetError("Failed to set remote video description send parameters.",
1804 error_desc);
1805 return false;
1806 }
1807 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001809 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1810 // and only give it to the media channel once we have a local
1811 // description too (without a local description, we won't be able to
1812 // recv them anyway).
1813 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1814 SafeSetError("Failed to set remote video description streams.", error_desc);
1815 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 }
1817
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001818 if (video->rtp_header_extensions_set()) {
1819 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001821
1822 set_remote_content_direction(content->direction());
1823 ChangeState();
1824 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825}
1826
1827bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1828 bool ret = true;
1829 // Set the send format for each of the local streams. If the view request
1830 // does not contain a local stream, set its send format to 0x0, which will
1831 // drop all frames.
1832 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1833 it != local_streams().end(); ++it) {
1834 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1835 StaticVideoViews::const_iterator view;
1836 for (view = request.static_video_views.begin();
1837 view != request.static_video_views.end(); ++view) {
1838 if (view->selector.Matches(*it)) {
1839 format.width = view->width;
1840 format.height = view->height;
1841 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1842 break;
1843 }
1844 }
1845
1846 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1847 }
1848
1849 // Check if the view request has invalid streams.
1850 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1851 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001852 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 LOG(LS_WARNING) << "View request for ("
1854 << it->selector.ssrc << ", '"
1855 << it->selector.groupid << "', '"
1856 << it->selector.streamid << "'"
1857 << ") is not in the local streams.";
1858 }
1859 }
1860
1861 return ret;
1862}
1863
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001864bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001866 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001868 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1869 screencast_capturers_[ssrc] = capturer;
1870 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871}
1872
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1874 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1875 if (iter == screencast_capturers_.end()) {
1876 return false;
1877 }
1878 // Clean up VideoCapturer.
1879 delete iter->second;
1880 screencast_capturers_.erase(iter);
1881 return true;
1882}
1883
1884bool VideoChannel::IsScreencasting_w() const {
1885 return !screencast_capturers_.empty();
1886}
1887
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001888void VideoChannel::GetScreencastDetails_w(
1889 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001890 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001892 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893 }
1894 VideoCapturer* capturer = iter->second;
1895 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001896 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1897 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898}
1899
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001901 rtc::WindowEvent we) {
1902 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903 SignalScreencastWindowEvent(ssrc, we);
1904}
1905
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001906void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908 case MSG_SCREENCASTWINDOWEVENT: {
1909 const ScreencastEventMessageData* data =
1910 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1911 OnScreencastWindowEvent_s(data->ssrc, data->event);
1912 delete data;
1913 break;
1914 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001915 case MSG_CHANNEL_ERROR: {
1916 const VideoChannelErrorMessageData* data =
1917 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 delete data;
1919 break;
1920 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 default:
1922 BaseChannel::OnMessage(pmsg);
1923 break;
1924 }
1925}
1926
1927void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001928 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929 SignalConnectionMonitor(this, infos);
1930}
1931
1932// TODO(pthatcher): Look into removing duplicate code between
1933// audio, video, and data, perhaps by using templates.
1934void VideoChannel::OnMediaMonitorUpdate(
1935 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1936 ASSERT(media_channel == this->media_channel());
1937 SignalMediaMonitor(this, info);
1938}
1939
1940void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001941 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 ScreencastEventMessageData* pdata =
1943 new ScreencastEventMessageData(ssrc, event);
1944 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1945}
1946
1947void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1948 // Map capturer events to window events. In the future we may want to simply
1949 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001950 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001952 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001954 we = rtc::WE_MINIMIZE;
1955 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1956 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957 } else {
1958 return;
1959 }
1960 previous_we_ = we;
1961
1962 uint32 ssrc = 0;
1963 if (!GetLocalSsrc(capturer, &ssrc)) {
1964 return;
1965 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001966
1967 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968}
1969
1970bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
1971 *ssrc = 0;
1972 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
1973 iter != screencast_capturers_.end(); ++iter) {
1974 if (iter->second == capturer) {
1975 *ssrc = iter->first;
1976 return true;
1977 }
1978 }
1979 return false;
1980}
1981
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001982void VideoChannel::GetSrtpCryptoSuiteNames(
1983 std::vector<std::string>* ciphers) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 GetSupportedVideoCryptoSuites(ciphers);
1985}
1986
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001987DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001989 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990 const std::string& content_name,
1991 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001992 : BaseChannel(thread,
1993 media_channel,
1994 transport_controller,
1995 content_name,
1996 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00001997 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07001998 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999
2000DataChannel::~DataChannel() {
2001 StopMediaMonitor();
2002 // this can't be done in the base class, since it calls a virtual
2003 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002004
2005 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006}
2007
2008bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002009 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010 return false;
2011 }
2012 media_channel()->SignalDataReceived.connect(
2013 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002014 media_channel()->SignalReadyToSend.connect(
2015 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002016 media_channel()->SignalStreamClosedRemotely.connect(
2017 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018 return true;
2019}
2020
2021bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002022 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002024 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2025 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026}
2027
2028const ContentInfo* DataChannel::GetFirstContent(
2029 const SessionDescription* sdesc) {
2030 return GetFirstDataContent(sdesc);
2031}
2032
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002033bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 if (data_channel_type_ == DCT_SCTP) {
2035 // TODO(pthatcher): Do this in a more robust way by checking for
2036 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002037 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038 } else if (data_channel_type_ == DCT_RTP) {
2039 return BaseChannel::WantsPacket(rtcp, packet);
2040 }
2041 return false;
2042}
2043
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002044bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2045 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046 // It hasn't been set before, so set it now.
2047 if (data_channel_type_ == DCT_NONE) {
2048 data_channel_type_ = new_data_channel_type;
2049 return true;
2050 }
2051
2052 // It's been set before, but doesn't match. That's bad.
2053 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002054 std::ostringstream desc;
2055 desc << "Data channel type mismatch."
2056 << " Expected " << data_channel_type_
2057 << " Got " << new_data_channel_type;
2058 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 return false;
2060 }
2061
2062 // It's hasn't changed. Nothing to do.
2063 return true;
2064}
2065
2066bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002067 const DataContentDescription* content,
2068 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2070 (content->protocol() == kMediaProtocolDtlsSctp));
2071 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002072 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073}
2074
2075bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002076 ContentAction action,
2077 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002078 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079 LOG(LS_INFO) << "Setting local data description";
2080
2081 const DataContentDescription* data =
2082 static_cast<const DataContentDescription*>(content);
2083 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002084 if (!data) {
2085 SafeSetError("Can't find data content in local description.", error_desc);
2086 return false;
2087 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002089 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090 return false;
2091 }
2092
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002093 if (data_channel_type_ == DCT_RTP) {
2094 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2095 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096 }
2097 }
2098
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002099 // FYI: We send the SCTP port number (not to be confused with the
2100 // underlying UDP port number) as a codec parameter. So even SCTP
2101 // data channels need codecs.
2102 DataRecvParameters recv_params = last_recv_params_;
2103 RtpParametersFromMediaDescription(data, &recv_params);
2104 if (!media_channel()->SetRecvParameters(recv_params)) {
2105 SafeSetError("Failed to set remote data description recv parameters.",
2106 error_desc);
2107 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002109 if (data_channel_type_ == DCT_RTP) {
2110 for (const DataCodec& codec : data->codecs()) {
2111 bundle_filter()->AddPayloadType(codec.id);
2112 }
2113 }
2114 last_recv_params_ = recv_params;
2115
2116 // TODO(pthatcher): Move local streams into DataSendParameters, and
2117 // only give it to the media channel once we have a remote
2118 // description too (without a remote description, we won't be able
2119 // to send them anyway).
2120 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2121 SafeSetError("Failed to set local data description streams.", error_desc);
2122 return false;
2123 }
2124
2125 set_local_content_direction(content->direction());
2126 ChangeState();
2127 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128}
2129
2130bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002131 ContentAction action,
2132 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002133 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002134
2135 const DataContentDescription* data =
2136 static_cast<const DataContentDescription*>(content);
2137 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002138 if (!data) {
2139 SafeSetError("Can't find data content in remote description.", error_desc);
2140 return false;
2141 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002143 // If the remote data doesn't have codecs and isn't an update, it
2144 // must be empty, so ignore it.
2145 if (!data->has_codecs() && action != CA_UPDATE) {
2146 return true;
2147 }
2148
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002149 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002150 return false;
2151 }
2152
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002153 LOG(LS_INFO) << "Setting remote data description";
2154 if (data_channel_type_ == DCT_RTP &&
2155 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2156 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 }
2158
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002159
2160 DataSendParameters send_params = last_send_params_;
2161 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2162 if (!media_channel()->SetSendParameters(send_params)) {
2163 SafeSetError("Failed to set remote data description send parameters.",
2164 error_desc);
2165 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002167 last_send_params_ = send_params;
2168
2169 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2170 // and only give it to the media channel once we have a local
2171 // description too (without a local description, we won't be able to
2172 // recv them anyway).
2173 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2174 SafeSetError("Failed to set remote data description streams.",
2175 error_desc);
2176 return false;
2177 }
2178
2179 set_remote_content_direction(content->direction());
2180 ChangeState();
2181 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182}
2183
2184void DataChannel::ChangeState() {
2185 // Render incoming data if we're the active call, and we have the local
2186 // content. We receive data on the default channel and multiplexed streams.
2187 bool recv = IsReadyToReceive();
2188 if (!media_channel()->SetReceive(recv)) {
2189 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2190 }
2191
2192 // Send outgoing data if we're the active call, we have the remote content,
2193 // and we have had some form of connectivity.
2194 bool send = IsReadyToSend();
2195 if (!media_channel()->SetSend(send)) {
2196 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2197 }
2198
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002199 // Trigger SignalReadyToSendData asynchronously.
2200 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201
2202 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2203}
2204
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002205void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 switch (pmsg->message_id) {
2207 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002208 DataChannelReadyToSendMessageData* data =
2209 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002210 ready_to_send_data_ = data->data();
2211 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212 delete data;
2213 break;
2214 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215 case MSG_DATARECEIVED: {
2216 DataReceivedMessageData* data =
2217 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2218 SignalDataReceived(this, data->params, data->payload);
2219 delete data;
2220 break;
2221 }
2222 case MSG_CHANNEL_ERROR: {
2223 const DataChannelErrorMessageData* data =
2224 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 delete data;
2226 break;
2227 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002228 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002229 rtc::TypedMessageData<uint32>* data =
2230 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002231 SignalStreamClosedRemotely(data->data());
2232 delete data;
2233 break;
2234 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235 default:
2236 BaseChannel::OnMessage(pmsg);
2237 break;
2238 }
2239}
2240
2241void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002242 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002243 SignalConnectionMonitor(this, infos);
2244}
2245
2246void DataChannel::StartMediaMonitor(int cms) {
2247 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002248 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002249 media_monitor_->SignalUpdate.connect(
2250 this, &DataChannel::OnMediaMonitorUpdate);
2251 media_monitor_->Start(cms);
2252}
2253
2254void DataChannel::StopMediaMonitor() {
2255 if (media_monitor_) {
2256 media_monitor_->Stop();
2257 media_monitor_->SignalUpdate.disconnect(this);
2258 media_monitor_.reset();
2259 }
2260}
2261
2262void DataChannel::OnMediaMonitorUpdate(
2263 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2264 ASSERT(media_channel == this->media_channel());
2265 SignalMediaMonitor(this, info);
2266}
2267
2268void DataChannel::OnDataReceived(
2269 const ReceiveDataParams& params, const char* data, size_t len) {
2270 DataReceivedMessageData* msg = new DataReceivedMessageData(
2271 params, data, len);
2272 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2273}
2274
2275void DataChannel::OnDataChannelError(
2276 uint32 ssrc, DataMediaChannel::Error err) {
2277 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2278 ssrc, err);
2279 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2280}
2281
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002282void DataChannel::OnDataChannelReadyToSend(bool writable) {
2283 // This is usded for congestion control to indicate that the stream is ready
2284 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2285 // that the transport channel is ready.
2286 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2287 new DataChannelReadyToSendMessageData(writable));
2288}
2289
Guo-wei Shieh456696a2015-09-30 21:48:54 -07002290void DataChannel::GetSrtpCryptoSuiteNames(
2291 std::vector<std::string>* ciphers) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 GetSupportedDataCryptoSuites(ciphers);
2293}
2294
2295bool DataChannel::ShouldSetupDtlsSrtp() const {
2296 return (data_channel_type_ == DCT_RTP);
2297}
2298
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002299void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002300 rtc::TypedMessageData<uint32>* message =
2301 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002302 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2303}
2304
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002305} // namespace cricket