blob: 5a6b7e198f16cb8380790e82d4eda8bc958e4c2a [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) {
819 GetSrtpCiphers(&ciphers);
820 } else {
821 GetSupportedDefaultCryptoSuites(&ciphers);
822 }
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
844 if (!channel->GetSrtpCipher(&selected_cipher)) {
845 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 }
1304 media_channel()->SignalMediaError.connect(
1305 this, &VoiceChannel::OnVoiceChannelError);
1306 srtp_filter()->SignalSrtpError.connect(
1307 this, &VoiceChannel::OnSrtpError);
1308 return true;
1309}
1310
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001311bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001312 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1313 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001314}
1315
deadbeefcbecd352015-09-23 11:50:27 -07001316bool VoiceChannel::SetAudioSend(uint32 ssrc,
1317 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001318 const AudioOptions* options,
1319 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001320 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
1321 ssrc, mute, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322}
1323
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324// TODO(juberti): Handle early media the right way. We should get an explicit
1325// ringing message telling us to start playing local ringback, which we cancel
1326// if any early media actually arrives. For now, we do the opposite, which is
1327// to wait 1 second for early media, and start playing local ringback if none
1328// arrives.
1329void VoiceChannel::SetEarlyMedia(bool enable) {
1330 if (enable) {
1331 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001332 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1333 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334 } else {
1335 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001336 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001337 }
1338}
1339
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340bool VoiceChannel::PressDTMF(int digit, bool playout) {
1341 int flags = DF_SEND;
1342 if (playout) {
1343 flags |= DF_PLAY;
1344 }
1345 int duration_ms = 160;
1346 return InsertDtmf(0, digit, duration_ms, flags);
1347}
1348
1349bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001350 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1351 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001352}
1353
1354bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1355 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001356 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1357 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001358}
1359
1360bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001361 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1362 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001364
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001366 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1367 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368}
1369
1370void VoiceChannel::StartMediaMonitor(int cms) {
1371 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001372 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373 media_monitor_->SignalUpdate.connect(
1374 this, &VoiceChannel::OnMediaMonitorUpdate);
1375 media_monitor_->Start(cms);
1376}
1377
1378void VoiceChannel::StopMediaMonitor() {
1379 if (media_monitor_) {
1380 media_monitor_->Stop();
1381 media_monitor_->SignalUpdate.disconnect(this);
1382 media_monitor_.reset();
1383 }
1384}
1385
1386void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001387 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388 audio_monitor_
1389 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1390 audio_monitor_->Start(cms);
1391}
1392
1393void VoiceChannel::StopAudioMonitor() {
1394 if (audio_monitor_) {
1395 audio_monitor_->Stop();
1396 audio_monitor_.reset();
1397 }
1398}
1399
1400bool VoiceChannel::IsAudioMonitorRunning() const {
1401 return (audio_monitor_.get() != NULL);
1402}
1403
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001405 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001406}
1407
1408int VoiceChannel::GetOutputLevel_w() {
1409 return media_channel()->GetOutputLevel();
1410}
1411
1412void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1413 media_channel()->GetActiveStreams(actives);
1414}
1415
1416void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001417 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001418 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001419 int flags) {
1420 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421
1422 // Set a flag when we've received an RTP packet. If we're waiting for early
1423 // media, this will disable the timeout.
1424 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1425 received_media_ = true;
1426 }
1427}
1428
1429void VoiceChannel::ChangeState() {
1430 // Render incoming data if we're the active call, and we have the local
1431 // content. We receive data on the default channel and multiplexed streams.
1432 bool recv = IsReadyToReceive();
1433 if (!media_channel()->SetPlayout(recv)) {
1434 SendLastMediaError();
1435 }
1436
1437 // Send outgoing data if we're the active call, we have the remote content,
1438 // and we have had some form of connectivity.
1439 bool send = IsReadyToSend();
1440 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1441 if (!media_channel()->SetSend(send_flag)) {
1442 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1443 SendLastMediaError();
1444 }
1445
1446 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1447}
1448
1449const ContentInfo* VoiceChannel::GetFirstContent(
1450 const SessionDescription* sdesc) {
1451 return GetFirstAudioContent(sdesc);
1452}
1453
1454bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001455 ContentAction action,
1456 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001457 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 LOG(LS_INFO) << "Setting local voice description";
1459
1460 const AudioContentDescription* audio =
1461 static_cast<const AudioContentDescription*>(content);
1462 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001463 if (!audio) {
1464 SafeSetError("Can't find audio content in local description.", error_desc);
1465 return false;
1466 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001468 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1469 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470 }
1471
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001472 AudioRecvParameters recv_params = last_recv_params_;
1473 RtpParametersFromMediaDescription(audio, &recv_params);
1474 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001475 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001476 error_desc);
1477 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001479 for (const AudioCodec& codec : audio->codecs()) {
1480 bundle_filter()->AddPayloadType(codec.id);
1481 }
1482 last_recv_params_ = recv_params;
1483
1484 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1485 // only give it to the media channel once we have a remote
1486 // description too (without a remote description, we won't be able
1487 // to send them anyway).
1488 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1489 SafeSetError("Failed to set local audio description streams.", error_desc);
1490 return false;
1491 }
1492
1493 set_local_content_direction(content->direction());
1494 ChangeState();
1495 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496}
1497
1498bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001499 ContentAction action,
1500 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001501 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 LOG(LS_INFO) << "Setting remote voice description";
1503
1504 const AudioContentDescription* audio =
1505 static_cast<const AudioContentDescription*>(content);
1506 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001507 if (!audio) {
1508 SafeSetError("Can't find audio content in remote description.", error_desc);
1509 return false;
1510 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001512 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1513 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001514 }
1515
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001516 AudioSendParameters send_params = last_send_params_;
1517 RtpSendParametersFromMediaDescription(audio, &send_params);
1518 if (audio->conference_mode()) {
1519 send_params.options.conference_mode.Set(true);
1520 }
1521 if (audio->agc_minus_10db()) {
1522 send_params.options.adjust_agc_delta.Set(kAgcMinus10db);
1523 }
1524 if (!media_channel()->SetSendParameters(send_params)) {
1525 SafeSetError("Failed to set remote audio description send parameters.",
1526 error_desc);
1527 return false;
1528 }
1529 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001531 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1532 // and only give it to the media channel once we have a local
1533 // description too (without a local description, we won't be able to
1534 // recv them anyway).
1535 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1536 SafeSetError("Failed to set remote audio description streams.", error_desc);
1537 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538 }
1539
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001540 if (audio->rtp_header_extensions_set()) {
1541 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1542 }
1543
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001544 set_remote_content_direction(content->direction());
1545 ChangeState();
1546 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547}
1548
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549void VoiceChannel::HandleEarlyMediaTimeout() {
1550 // This occurs on the main thread, not the worker thread.
1551 if (!received_media_) {
1552 LOG(LS_INFO) << "No early media received before timeout";
1553 SignalEarlyMediaTimeout(this);
1554 }
1555}
1556
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1558 int flags) {
1559 if (!enabled()) {
1560 return false;
1561 }
1562
1563 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1564}
1565
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001566void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 case MSG_EARLYMEDIATIMEOUT:
1569 HandleEarlyMediaTimeout();
1570 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 case MSG_CHANNEL_ERROR: {
1572 VoiceChannelErrorMessageData* data =
1573 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1574 SignalMediaError(this, data->ssrc, data->error);
1575 delete data;
1576 break;
1577 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 default:
1579 BaseChannel::OnMessage(pmsg);
1580 break;
1581 }
1582}
1583
1584void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001585 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 SignalConnectionMonitor(this, infos);
1587}
1588
1589void VoiceChannel::OnMediaMonitorUpdate(
1590 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1591 ASSERT(media_channel == this->media_channel());
1592 SignalMediaMonitor(this, info);
1593}
1594
1595void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1596 const AudioInfo& info) {
1597 SignalAudioMonitor(this, info);
1598}
1599
1600void VoiceChannel::OnVoiceChannelError(
1601 uint32 ssrc, VoiceMediaChannel::Error err) {
1602 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1603 ssrc, err);
1604 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1605}
1606
1607void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1608 SrtpFilter::Error error) {
1609 switch (error) {
1610 case SrtpFilter::ERROR_FAIL:
1611 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1612 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1613 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1614 break;
1615 case SrtpFilter::ERROR_AUTH:
1616 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1617 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1618 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1619 break;
1620 case SrtpFilter::ERROR_REPLAY:
1621 // Only receving channel should have this error.
1622 ASSERT(mode == SrtpFilter::UNPROTECT);
1623 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1624 break;
1625 default:
1626 break;
1627 }
1628}
1629
1630void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1631 GetSupportedAudioCryptoSuites(ciphers);
1632}
1633
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001634VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001636 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001637 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001638 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001639 : BaseChannel(thread,
1640 media_channel,
1641 transport_controller,
1642 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001644 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001645 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646
1647bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001648 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 return false;
1650 }
1651 media_channel()->SignalMediaError.connect(
1652 this, &VideoChannel::OnVideoChannelError);
1653 srtp_filter()->SignalSrtpError.connect(
1654 this, &VideoChannel::OnSrtpError);
1655 return true;
1656}
1657
1658void VoiceChannel::SendLastMediaError() {
1659 uint32 ssrc;
1660 VoiceMediaChannel::Error error;
1661 media_channel()->GetLastMediaError(&ssrc, &error);
1662 SignalMediaError(this, ssrc, error);
1663}
1664
1665VideoChannel::~VideoChannel() {
1666 std::vector<uint32> screencast_ssrcs;
1667 ScreencastMap::iterator iter;
1668 while (!screencast_capturers_.empty()) {
1669 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1670 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1671 << screencast_capturers_.begin()->first;
1672 ASSERT(false);
1673 break;
1674 }
1675 }
1676
1677 StopMediaMonitor();
1678 // this can't be done in the base class, since it calls a virtual
1679 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001680
1681 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682}
1683
1684bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001685 worker_thread()->Invoke<void>(Bind(
1686 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687 return true;
1688}
1689
1690bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001691 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692}
1693
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001694bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1695 return worker_thread()->Invoke<bool>(Bind(
1696 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697}
1698
1699bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001700 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1701 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702}
1703
1704bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001705 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706}
1707
1708bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001709 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710}
1711
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001712int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001713 ScreencastDetailsData data(ssrc);
1714 worker_thread()->Invoke<void>(Bind(
1715 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001716 return data.fps;
1717}
1718
1719int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001720 ScreencastDetailsData data(ssrc);
1721 worker_thread()->Invoke<void>(Bind(
1722 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001723 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724}
1725
1726bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001727 worker_thread()->Invoke<void>(Bind(
1728 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 return true;
1730}
1731
1732bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001733 worker_thread()->Invoke<void>(Bind(
1734 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735 return true;
1736}
1737
deadbeefcbecd352015-09-23 11:50:27 -07001738bool VideoChannel::SetVideoSend(uint32 ssrc,
1739 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001740 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001741 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1742 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001743}
1744
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001746 // Send outgoing data if we're the active call, we have the remote content,
1747 // and we have had some form of connectivity.
1748 bool send = IsReadyToSend();
1749 if (!media_channel()->SetSend(send)) {
1750 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1751 // TODO(gangji): Report error back to server.
1752 }
1753
Peter Boström34fbfff2015-09-24 19:20:30 +02001754 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755}
1756
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001757bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1758 return InvokeOnWorker(
1759 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760}
1761
1762void VideoChannel::StartMediaMonitor(int cms) {
1763 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001764 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 media_monitor_->SignalUpdate.connect(
1766 this, &VideoChannel::OnMediaMonitorUpdate);
1767 media_monitor_->Start(cms);
1768}
1769
1770void VideoChannel::StopMediaMonitor() {
1771 if (media_monitor_) {
1772 media_monitor_->Stop();
1773 media_monitor_.reset();
1774 }
1775}
1776
1777const ContentInfo* VideoChannel::GetFirstContent(
1778 const SessionDescription* sdesc) {
1779 return GetFirstVideoContent(sdesc);
1780}
1781
1782bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001783 ContentAction action,
1784 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001785 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001786 LOG(LS_INFO) << "Setting local video description";
1787
1788 const VideoContentDescription* video =
1789 static_cast<const VideoContentDescription*>(content);
1790 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001791 if (!video) {
1792 SafeSetError("Can't find video content in local description.", error_desc);
1793 return false;
1794 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001796 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1797 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 }
1799
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001800 VideoRecvParameters recv_params = last_recv_params_;
1801 RtpParametersFromMediaDescription(video, &recv_params);
1802 if (!media_channel()->SetRecvParameters(recv_params)) {
1803 SafeSetError("Failed to set local video description recv parameters.",
1804 error_desc);
1805 return false;
1806 }
1807 for (const VideoCodec& codec : video->codecs()) {
1808 bundle_filter()->AddPayloadType(codec.id);
1809 }
1810 last_recv_params_ = recv_params;
1811
1812 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1813 // only give it to the media channel once we have a remote
1814 // description too (without a remote description, we won't be able
1815 // to send them anyway).
1816 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1817 SafeSetError("Failed to set local video description streams.", error_desc);
1818 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 }
1820
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001821 set_local_content_direction(content->direction());
1822 ChangeState();
1823 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824}
1825
1826bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001827 ContentAction action,
1828 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001829 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 LOG(LS_INFO) << "Setting remote video description";
1831
1832 const VideoContentDescription* video =
1833 static_cast<const VideoContentDescription*>(content);
1834 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001835 if (!video) {
1836 SafeSetError("Can't find video content in remote description.", error_desc);
1837 return false;
1838 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001840
1841 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1842 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 }
1844
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001845 VideoSendParameters send_params = last_send_params_;
1846 RtpSendParametersFromMediaDescription(video, &send_params);
1847 if (video->conference_mode()) {
1848 send_params.options.conference_mode.Set(true);
1849 }
1850 if (!media_channel()->SetSendParameters(send_params)) {
1851 SafeSetError("Failed to set remote video description send parameters.",
1852 error_desc);
1853 return false;
1854 }
1855 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001857 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1858 // and only give it to the media channel once we have a local
1859 // description too (without a local description, we won't be able to
1860 // recv them anyway).
1861 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1862 SafeSetError("Failed to set remote video description streams.", error_desc);
1863 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 }
1865
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001866 if (video->rtp_header_extensions_set()) {
1867 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001869
1870 set_remote_content_direction(content->direction());
1871 ChangeState();
1872 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873}
1874
1875bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1876 bool ret = true;
1877 // Set the send format for each of the local streams. If the view request
1878 // does not contain a local stream, set its send format to 0x0, which will
1879 // drop all frames.
1880 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1881 it != local_streams().end(); ++it) {
1882 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1883 StaticVideoViews::const_iterator view;
1884 for (view = request.static_video_views.begin();
1885 view != request.static_video_views.end(); ++view) {
1886 if (view->selector.Matches(*it)) {
1887 format.width = view->width;
1888 format.height = view->height;
1889 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1890 break;
1891 }
1892 }
1893
1894 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1895 }
1896
1897 // Check if the view request has invalid streams.
1898 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1899 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001900 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901 LOG(LS_WARNING) << "View request for ("
1902 << it->selector.ssrc << ", '"
1903 << it->selector.groupid << "', '"
1904 << it->selector.streamid << "'"
1905 << ") is not in the local streams.";
1906 }
1907 }
1908
1909 return ret;
1910}
1911
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001912bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001914 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001915 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001916 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1917 screencast_capturers_[ssrc] = capturer;
1918 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001919}
1920
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1922 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1923 if (iter == screencast_capturers_.end()) {
1924 return false;
1925 }
1926 // Clean up VideoCapturer.
1927 delete iter->second;
1928 screencast_capturers_.erase(iter);
1929 return true;
1930}
1931
1932bool VideoChannel::IsScreencasting_w() const {
1933 return !screencast_capturers_.empty();
1934}
1935
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001936void VideoChannel::GetScreencastDetails_w(
1937 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001938 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001940 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 }
1942 VideoCapturer* capturer = iter->second;
1943 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001944 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1945 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946}
1947
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001949 rtc::WindowEvent we) {
1950 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 SignalScreencastWindowEvent(ssrc, we);
1952}
1953
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001954void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 case MSG_SCREENCASTWINDOWEVENT: {
1957 const ScreencastEventMessageData* data =
1958 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1959 OnScreencastWindowEvent_s(data->ssrc, data->event);
1960 delete data;
1961 break;
1962 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 case MSG_CHANNEL_ERROR: {
1964 const VideoChannelErrorMessageData* data =
1965 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
1966 SignalMediaError(this, data->ssrc, data->error);
1967 delete data;
1968 break;
1969 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 default:
1971 BaseChannel::OnMessage(pmsg);
1972 break;
1973 }
1974}
1975
1976void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001977 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 SignalConnectionMonitor(this, infos);
1979}
1980
1981// TODO(pthatcher): Look into removing duplicate code between
1982// audio, video, and data, perhaps by using templates.
1983void VideoChannel::OnMediaMonitorUpdate(
1984 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1985 ASSERT(media_channel == this->media_channel());
1986 SignalMediaMonitor(this, info);
1987}
1988
1989void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001990 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991 ScreencastEventMessageData* pdata =
1992 new ScreencastEventMessageData(ssrc, event);
1993 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1994}
1995
1996void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1997 // Map capturer events to window events. In the future we may want to simply
1998 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001999 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002001 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002003 we = rtc::WE_MINIMIZE;
2004 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2005 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 } else {
2007 return;
2008 }
2009 previous_we_ = we;
2010
2011 uint32 ssrc = 0;
2012 if (!GetLocalSsrc(capturer, &ssrc)) {
2013 return;
2014 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002015
2016 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017}
2018
2019bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2020 *ssrc = 0;
2021 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2022 iter != screencast_capturers_.end(); ++iter) {
2023 if (iter->second == capturer) {
2024 *ssrc = iter->first;
2025 return true;
2026 }
2027 }
2028 return false;
2029}
2030
2031void VideoChannel::OnVideoChannelError(uint32 ssrc,
2032 VideoMediaChannel::Error error) {
2033 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2034 ssrc, error);
2035 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2036}
2037
2038void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2039 SrtpFilter::Error error) {
2040 switch (error) {
2041 case SrtpFilter::ERROR_FAIL:
2042 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2043 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2044 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2045 break;
2046 case SrtpFilter::ERROR_AUTH:
2047 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2048 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2049 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2050 break;
2051 case SrtpFilter::ERROR_REPLAY:
2052 // Only receving channel should have this error.
2053 ASSERT(mode == SrtpFilter::UNPROTECT);
2054 // TODO(gangji): Turn on the signaling of replay error once we have
2055 // switched to the new mechanism for doing video retransmissions.
2056 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2057 break;
2058 default:
2059 break;
2060 }
2061}
2062
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2064 GetSupportedVideoCryptoSuites(ciphers);
2065}
2066
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002067DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07002069 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070 const std::string& content_name,
2071 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07002072 : BaseChannel(thread,
2073 media_channel,
2074 transport_controller,
2075 content_name,
2076 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002077 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002078 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079
2080DataChannel::~DataChannel() {
2081 StopMediaMonitor();
2082 // this can't be done in the base class, since it calls a virtual
2083 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002084
2085 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086}
2087
2088bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002089 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002090 return false;
2091 }
2092 media_channel()->SignalDataReceived.connect(
2093 this, &DataChannel::OnDataReceived);
2094 media_channel()->SignalMediaError.connect(
2095 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002096 media_channel()->SignalReadyToSend.connect(
2097 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002098 media_channel()->SignalStreamClosedRemotely.connect(
2099 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 srtp_filter()->SignalSrtpError.connect(
2101 this, &DataChannel::OnSrtpError);
2102 return true;
2103}
2104
2105bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002106 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002107 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002108 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2109 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002110}
2111
2112const ContentInfo* DataChannel::GetFirstContent(
2113 const SessionDescription* sdesc) {
2114 return GetFirstDataContent(sdesc);
2115}
2116
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002117bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118 if (data_channel_type_ == DCT_SCTP) {
2119 // TODO(pthatcher): Do this in a more robust way by checking for
2120 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002121 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122 } else if (data_channel_type_ == DCT_RTP) {
2123 return BaseChannel::WantsPacket(rtcp, packet);
2124 }
2125 return false;
2126}
2127
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002128bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2129 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002130 // It hasn't been set before, so set it now.
2131 if (data_channel_type_ == DCT_NONE) {
2132 data_channel_type_ = new_data_channel_type;
2133 return true;
2134 }
2135
2136 // It's been set before, but doesn't match. That's bad.
2137 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002138 std::ostringstream desc;
2139 desc << "Data channel type mismatch."
2140 << " Expected " << data_channel_type_
2141 << " Got " << new_data_channel_type;
2142 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143 return false;
2144 }
2145
2146 // It's hasn't changed. Nothing to do.
2147 return true;
2148}
2149
2150bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002151 const DataContentDescription* content,
2152 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2154 (content->protocol() == kMediaProtocolDtlsSctp));
2155 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002156 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157}
2158
2159bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002160 ContentAction action,
2161 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002162 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163 LOG(LS_INFO) << "Setting local data description";
2164
2165 const DataContentDescription* data =
2166 static_cast<const DataContentDescription*>(content);
2167 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002168 if (!data) {
2169 SafeSetError("Can't find data content in local description.", error_desc);
2170 return false;
2171 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002173 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 return false;
2175 }
2176
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002177 if (data_channel_type_ == DCT_RTP) {
2178 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2179 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180 }
2181 }
2182
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002183 // FYI: We send the SCTP port number (not to be confused with the
2184 // underlying UDP port number) as a codec parameter. So even SCTP
2185 // data channels need codecs.
2186 DataRecvParameters recv_params = last_recv_params_;
2187 RtpParametersFromMediaDescription(data, &recv_params);
2188 if (!media_channel()->SetRecvParameters(recv_params)) {
2189 SafeSetError("Failed to set remote data description recv parameters.",
2190 error_desc);
2191 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002193 if (data_channel_type_ == DCT_RTP) {
2194 for (const DataCodec& codec : data->codecs()) {
2195 bundle_filter()->AddPayloadType(codec.id);
2196 }
2197 }
2198 last_recv_params_ = recv_params;
2199
2200 // TODO(pthatcher): Move local streams into DataSendParameters, and
2201 // only give it to the media channel once we have a remote
2202 // description too (without a remote description, we won't be able
2203 // to send them anyway).
2204 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2205 SafeSetError("Failed to set local data description streams.", error_desc);
2206 return false;
2207 }
2208
2209 set_local_content_direction(content->direction());
2210 ChangeState();
2211 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212}
2213
2214bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002215 ContentAction action,
2216 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002217 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218
2219 const DataContentDescription* data =
2220 static_cast<const DataContentDescription*>(content);
2221 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002222 if (!data) {
2223 SafeSetError("Can't find data content in remote description.", error_desc);
2224 return false;
2225 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002227 // If the remote data doesn't have codecs and isn't an update, it
2228 // must be empty, so ignore it.
2229 if (!data->has_codecs() && action != CA_UPDATE) {
2230 return true;
2231 }
2232
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002233 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 return false;
2235 }
2236
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002237 LOG(LS_INFO) << "Setting remote data description";
2238 if (data_channel_type_ == DCT_RTP &&
2239 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2240 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002241 }
2242
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002243
2244 DataSendParameters send_params = last_send_params_;
2245 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2246 if (!media_channel()->SetSendParameters(send_params)) {
2247 SafeSetError("Failed to set remote data description send parameters.",
2248 error_desc);
2249 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002251 last_send_params_ = send_params;
2252
2253 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2254 // and only give it to the media channel once we have a local
2255 // description too (without a local description, we won't be able to
2256 // recv them anyway).
2257 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2258 SafeSetError("Failed to set remote data description streams.",
2259 error_desc);
2260 return false;
2261 }
2262
2263 set_remote_content_direction(content->direction());
2264 ChangeState();
2265 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002266}
2267
2268void DataChannel::ChangeState() {
2269 // Render incoming data if we're the active call, and we have the local
2270 // content. We receive data on the default channel and multiplexed streams.
2271 bool recv = IsReadyToReceive();
2272 if (!media_channel()->SetReceive(recv)) {
2273 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2274 }
2275
2276 // Send outgoing data if we're the active call, we have the remote content,
2277 // and we have had some form of connectivity.
2278 bool send = IsReadyToSend();
2279 if (!media_channel()->SetSend(send)) {
2280 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2281 }
2282
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002283 // Trigger SignalReadyToSendData asynchronously.
2284 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285
2286 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2287}
2288
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002289void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 switch (pmsg->message_id) {
2291 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002292 DataChannelReadyToSendMessageData* data =
2293 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002294 ready_to_send_data_ = data->data();
2295 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 delete data;
2297 break;
2298 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299 case MSG_DATARECEIVED: {
2300 DataReceivedMessageData* data =
2301 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2302 SignalDataReceived(this, data->params, data->payload);
2303 delete data;
2304 break;
2305 }
2306 case MSG_CHANNEL_ERROR: {
2307 const DataChannelErrorMessageData* data =
2308 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2309 SignalMediaError(this, data->ssrc, data->error);
2310 delete data;
2311 break;
2312 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002313 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002314 rtc::TypedMessageData<uint32>* data =
2315 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002316 SignalStreamClosedRemotely(data->data());
2317 delete data;
2318 break;
2319 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320 default:
2321 BaseChannel::OnMessage(pmsg);
2322 break;
2323 }
2324}
2325
2326void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002327 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328 SignalConnectionMonitor(this, infos);
2329}
2330
2331void DataChannel::StartMediaMonitor(int cms) {
2332 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002333 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334 media_monitor_->SignalUpdate.connect(
2335 this, &DataChannel::OnMediaMonitorUpdate);
2336 media_monitor_->Start(cms);
2337}
2338
2339void DataChannel::StopMediaMonitor() {
2340 if (media_monitor_) {
2341 media_monitor_->Stop();
2342 media_monitor_->SignalUpdate.disconnect(this);
2343 media_monitor_.reset();
2344 }
2345}
2346
2347void DataChannel::OnMediaMonitorUpdate(
2348 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2349 ASSERT(media_channel == this->media_channel());
2350 SignalMediaMonitor(this, info);
2351}
2352
2353void DataChannel::OnDataReceived(
2354 const ReceiveDataParams& params, const char* data, size_t len) {
2355 DataReceivedMessageData* msg = new DataReceivedMessageData(
2356 params, data, len);
2357 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2358}
2359
2360void DataChannel::OnDataChannelError(
2361 uint32 ssrc, DataMediaChannel::Error err) {
2362 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2363 ssrc, err);
2364 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2365}
2366
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002367void DataChannel::OnDataChannelReadyToSend(bool writable) {
2368 // This is usded for congestion control to indicate that the stream is ready
2369 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2370 // that the transport channel is ready.
2371 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2372 new DataChannelReadyToSendMessageData(writable));
2373}
2374
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002375void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2376 SrtpFilter::Error error) {
2377 switch (error) {
2378 case SrtpFilter::ERROR_FAIL:
2379 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2380 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2381 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2382 break;
2383 case SrtpFilter::ERROR_AUTH:
2384 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2385 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2386 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2387 break;
2388 case SrtpFilter::ERROR_REPLAY:
2389 // Only receving channel should have this error.
2390 ASSERT(mode == SrtpFilter::UNPROTECT);
2391 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2392 break;
2393 default:
2394 break;
2395 }
2396}
2397
2398void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2399 GetSupportedDataCryptoSuites(ciphers);
2400}
2401
2402bool DataChannel::ShouldSetupDtlsSrtp() const {
2403 return (data_channel_type_ == DCT_RTP);
2404}
2405
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002406void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002407 rtc::TypedMessageData<uint32>* message =
2408 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002409 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2410}
2411
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002412} // namespace cricket