blob: 3c5b9ddc449ed250e37f177997235e16fa43dabb [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,
torbjornga81a42f2015-09-23 02:16:58 -0700173 MediaChannel* media_channel, BaseSession* session,
174 const std::string& content_name, bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 : worker_thread_(thread),
torbjornga81a42f2015-09-23 02:16:58 -0700176 session_(session),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 media_channel_(media_channel),
178 content_name_(content_name),
torbjornga81a42f2015-09-23 02:16:58 -0700179 rtcp_(rtcp),
180 transport_channel_(NULL),
181 rtcp_transport_channel_(NULL),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 enabled_(false),
183 writable_(false),
184 rtp_ready_to_send_(false),
185 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 was_ever_writable_(false),
187 local_content_direction_(MD_INACTIVE),
188 remote_content_direction_(MD_INACTIVE),
189 has_received_packet_(false),
190 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000191 secure_required_(false),
192 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000193 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194 LOG(LS_INFO) << "Created channel for " << content_name;
195}
196
197BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000198 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000199 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 StopConnectionMonitor();
201 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000202 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203 // We must destroy the media channel before the transport channel, otherwise
204 // the media channel may try to send on the dead transport channel. NULLing
205 // is not an effective strategy since the sends will come on another thread.
206 delete media_channel_;
torbjornga81a42f2015-09-23 02:16:58 -0700207 set_transport_channel(nullptr);
208 set_rtcp_transport_channel(nullptr);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 LOG(LS_INFO) << "Destroyed channel";
210}
211
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000212bool BaseChannel::Init() {
torbjornga81a42f2015-09-23 02:16:58 -0700213 if (!SetTransportChannels(session(), rtcp())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 return false;
215 }
216
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000217 if (!SetDtlsSrtpCiphers(transport_channel(), false)) {
218 return false;
219 }
torbjornga81a42f2015-09-23 02:16:58 -0700220 if (rtcp() && !SetDtlsSrtpCiphers(rtcp_transport_channel(), true)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000221 return false;
222 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000223
wu@webrtc.orgde305012013-10-31 15:40:38 +0000224 // Both RTP and RTCP channels are set, we can call SetInterface on
225 // media channel and it can set network options.
226 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 return true;
228}
229
wu@webrtc.org78187522013-10-07 23:32:02 +0000230void BaseChannel::Deinit() {
231 media_channel_->SetInterface(NULL);
232}
233
torbjornga81a42f2015-09-23 02:16:58 -0700234bool BaseChannel::SetTransportChannels(BaseSession* session, bool rtcp) {
235 return worker_thread_->Invoke<bool>(Bind(
236 &BaseChannel::SetTransportChannels_w, this, session, rtcp));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000237}
238
torbjornga81a42f2015-09-23 02:16:58 -0700239bool BaseChannel::SetTransportChannels_w(BaseSession* session, bool rtcp) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000240 ASSERT(worker_thread_ == rtc::Thread::Current());
241
torbjornga81a42f2015-09-23 02:16:58 -0700242 set_transport_channel(session->CreateChannel(
243 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000244 if (!transport_channel()) {
245 return false;
246 }
torbjornga81a42f2015-09-23 02:16:58 -0700247 if (rtcp) {
248 set_rtcp_transport_channel(session->CreateChannel(
249 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTCP));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000250 if (!rtcp_transport_channel()) {
251 return false;
252 }
torbjornga81a42f2015-09-23 02:16:58 -0700253 } else {
254 set_rtcp_transport_channel(nullptr);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000255 }
256
257 return true;
258}
259
260void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
261 ASSERT(worker_thread_ == rtc::Thread::Current());
262
263 TransportChannel* old_tc = transport_channel_;
torbjornga81a42f2015-09-23 02:16:58 -0700264
265 if (old_tc == new_tc) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000266 return;
267 }
268 if (old_tc) {
269 DisconnectFromTransportChannel(old_tc);
torbjornga81a42f2015-09-23 02:16:58 -0700270 session()->DestroyChannel(
271 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000272 }
273
274 transport_channel_ = new_tc;
275
276 if (new_tc) {
277 ConnectToTransportChannel(new_tc);
278 }
279}
280
281void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc) {
282 ASSERT(worker_thread_ == rtc::Thread::Current());
283
284 TransportChannel* old_tc = rtcp_transport_channel_;
torbjornga81a42f2015-09-23 02:16:58 -0700285
286 if (old_tc == new_tc) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000287 return;
288 }
289 if (old_tc) {
290 DisconnectFromTransportChannel(old_tc);
torbjornga81a42f2015-09-23 02:16:58 -0700291 session()->DestroyChannel(
292 content_name(), cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000293 }
294
295 rtcp_transport_channel_ = new_tc;
296
297 if (new_tc) {
298 ConnectToTransportChannel(new_tc);
299 }
300}
301
302void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
303 ASSERT(worker_thread_ == rtc::Thread::Current());
304
305 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
306 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
307 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
308}
309
310void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
311 ASSERT(worker_thread_ == rtc::Thread::Current());
312
313 tc->SignalWritableState.disconnect(this);
314 tc->SignalReadPacket.disconnect(this);
315 tc->SignalReadyToSend.disconnect(this);
316}
317
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000318bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000319 worker_thread_->Invoke<void>(Bind(
320 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
321 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322 return true;
323}
324
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000326 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000327}
328
329bool BaseChannel::RemoveRecvStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000330 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000331}
332
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000333bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000334 return InvokeOnWorker(
335 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000336}
337
338bool BaseChannel::RemoveSendStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000339 return InvokeOnWorker(
340 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000341}
342
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000344 ContentAction action,
345 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000346 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
347 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348}
349
350bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000351 ContentAction action,
352 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000353 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
354 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000355}
356
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000358 // We pass in the BaseChannel instead of the transport_channel_
359 // because if the transport_channel_ changes, the ConnectionMonitor
360 // would be pointing to the wrong TransportChannel.
361 connection_monitor_.reset(new ConnectionMonitor(
362 this, worker_thread(), rtc::Thread::Current()));
363 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000364 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000365 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366}
367
368void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000369 if (connection_monitor_) {
370 connection_monitor_->Stop();
371 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000372 }
373}
374
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000375bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
376 ASSERT(worker_thread_ == rtc::Thread::Current());
377 return transport_channel_->GetStats(infos);
378}
379
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000380bool BaseChannel::IsReadyToReceive() const {
381 // Receive data if we are enabled and have local content,
382 return enabled() && IsReceiveContentDirection(local_content_direction_);
383}
384
385bool BaseChannel::IsReadyToSend() const {
386 // Send outgoing data if we are enabled, have local and remote content,
387 // and we have had some form of connectivity.
388 return enabled() &&
389 IsReceiveContentDirection(remote_content_direction_) &&
390 IsSendContentDirection(local_content_direction_) &&
391 was_ever_writable();
392}
393
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000394bool BaseChannel::SendPacket(rtc::Buffer* packet,
395 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000396 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397}
398
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000399bool BaseChannel::SendRtcp(rtc::Buffer* packet,
400 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000401 return SendPacket(true, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402}
403
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000404int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000406 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000408 case ST_RTP:
409 channel = transport_channel_;
410 break;
411 case ST_RTCP:
412 channel = rtcp_transport_channel_;
413 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000415 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416}
417
418void BaseChannel::OnWritableState(TransportChannel* channel) {
419 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
torbjornga81a42f2015-09-23 02:16:58 -0700420 if (transport_channel_->writable()
421 && (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
422 ChannelWritable_w();
423 } else {
424 ChannelNotWritable_w();
425 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426}
427
428void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000429 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000430 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000431 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000432 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000433 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434
435 // When using RTCP multiplexing we might get RTCP packets on the RTP
436 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
437 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000438 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000439 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440}
441
442void BaseChannel::OnReadyToSend(TransportChannel* channel) {
torbjornga81a42f2015-09-23 02:16:58 -0700443 SetReadyToSend(channel, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444}
445
torbjornga81a42f2015-09-23 02:16:58 -0700446void BaseChannel::SetReadyToSend(TransportChannel* channel, bool ready) {
447 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
448 if (channel == transport_channel_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 rtp_ready_to_send_ = ready;
450 }
torbjornga81a42f2015-09-23 02:16:58 -0700451 if (channel == rtcp_transport_channel_) {
452 rtcp_ready_to_send_ = ready;
453 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454
torbjornga81a42f2015-09-23 02:16:58 -0700455 if (!ready) {
deadbeef47ee2f32015-09-22 15:08:23 -0700456 // Notify the MediaChannel when either rtp or rtcp channel can't send.
457 media_channel_->OnReadyToSend(false);
torbjornga81a42f2015-09-23 02:16:58 -0700458 } else if (rtp_ready_to_send_ &&
459 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
460 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
461 // Notify the MediaChannel when both rtp and rtcp channel can send.
462 media_channel_->OnReadyToSend(true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 }
464}
465
466bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
467 const char* data, size_t len) {
468 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000469 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470}
471
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000472bool BaseChannel::SendPacket(bool rtcp, rtc::Buffer* packet,
473 rtc::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000474 // SendPacket gets called from MediaEngine, typically on an encoder thread.
475 // If the thread is not our worker thread, we will post to our worker
476 // so that the real work happens on our worker. This avoids us having to
477 // synchronize access to all the pieces of the send path, including
478 // SRTP and the inner workings of the transport channels.
479 // The only downside is that we can't return a proper failure code if
480 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000481 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 // Avoid a copy by transferring the ownership of the packet data.
483 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
484 PacketMessageData* data = new PacketMessageData;
Karl Wiberg94784372015-04-20 14:03:07 +0200485 data->packet = packet->Pass();
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000486 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 worker_thread_->Post(this, message_id, data);
488 return true;
489 }
490
491 // Now that we are on the correct thread, ensure we have a place to send this
492 // packet before doing anything. (We might get RTCP packets that we don't
493 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
494 // transport.
495 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
496 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000497 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498 return false;
499 }
500
501 // Protect ourselves against crazy data.
502 if (!ValidPacket(rtcp, packet)) {
503 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000504 << PacketType(rtcp)
505 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 return false;
507 }
508
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000509 rtc::PacketOptions options(dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 // Protect if needed.
511 if (srtp_filter_.IsActive()) {
512 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200513 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000514 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000516 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
517 // inside libsrtp for a RTP packet. A external HMAC module will be writing
518 // a fake HMAC value. This is ONLY done for a RTP packet.
519 // Socket layer will update rtp sendtime extension header if present in
520 // packet with current time before updating the HMAC.
521#if !defined(ENABLE_EXTERNAL_AUTH)
522 res = srtp_filter_.ProtectRtp(
523 data, len, static_cast<int>(packet->capacity()), &len);
524#else
henrike@webrtc.org05376342014-03-10 15:53:12 +0000525 options.packet_time_params.rtp_sendtime_extension_id =
526 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000527 res = srtp_filter_.ProtectRtp(
528 data, len, static_cast<int>(packet->capacity()), &len,
529 &options.packet_time_params.srtp_packet_index);
530 // If protection succeeds, let's get auth params from srtp.
531 if (res) {
532 uint8* auth_key = NULL;
533 int key_len;
534 res = srtp_filter_.GetRtpAuthParams(
535 &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
536 if (res) {
537 options.packet_time_params.srtp_auth_key.resize(key_len);
538 options.packet_time_params.srtp_auth_key.assign(auth_key,
539 auth_key + key_len);
540 }
541 }
542#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 if (!res) {
544 int seq_num = -1;
545 uint32 ssrc = 0;
546 GetRtpSeqNum(data, len, &seq_num);
547 GetRtpSsrc(data, len, &ssrc);
548 LOG(LS_ERROR) << "Failed to protect " << content_name_
549 << " RTP packet: size=" << len
550 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
551 return false;
552 }
553 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000554 res = srtp_filter_.ProtectRtcp(data, len,
555 static_cast<int>(packet->capacity()),
556 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557 if (!res) {
558 int type = -1;
559 GetRtcpType(data, len, &type);
560 LOG(LS_ERROR) << "Failed to protect " << content_name_
561 << " RTCP packet: size=" << len << ", type=" << type;
562 return false;
563 }
564 }
565
566 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000567 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000568 } else if (secure_required_) {
569 // This is a double check for something that supposedly can't happen.
570 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
571 << " packet when SRTP is inactive and crypto is required";
572
573 ASSERT(false);
574 return false;
575 }
576
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000578 int ret =
Karl Wiberg94784372015-04-20 14:03:07 +0200579 channel->SendPacket(packet->data<char>(), packet->size(), options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000580 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
581 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000582 if (channel->GetError() == EWOULDBLOCK) {
583 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
torbjornga81a42f2015-09-23 02:16:58 -0700584 SetReadyToSend(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 }
586 return false;
587 }
588 return true;
589}
590
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000591bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000592 // Protect ourselves against crazy data.
593 if (!ValidPacket(rtcp, packet)) {
594 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000595 << PacketType(rtcp)
596 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597 return false;
598 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000599
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000600 // Bundle filter handles both rtp and rtcp packets.
Karl Wiberg94784372015-04-20 14:03:07 +0200601 return bundle_filter_.DemuxPacket(packet->data<char>(), packet->size(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000602}
603
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000604void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
605 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000606 if (!WantsPacket(rtcp, packet)) {
607 return;
608 }
609
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000610 // We are only interested in the first rtp packet because that
611 // indicates the media has started flowing.
612 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000613 has_received_packet_ = true;
614 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
615 }
616
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 // Unprotect the packet, if needed.
618 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200619 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000620 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000621 bool res;
622 if (!rtcp) {
623 res = srtp_filter_.UnprotectRtp(data, len, &len);
624 if (!res) {
625 int seq_num = -1;
626 uint32 ssrc = 0;
627 GetRtpSeqNum(data, len, &seq_num);
628 GetRtpSsrc(data, len, &ssrc);
629 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
630 << " RTP packet: size=" << len
631 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
632 return;
633 }
634 } else {
635 res = srtp_filter_.UnprotectRtcp(data, len, &len);
636 if (!res) {
637 int type = -1;
638 GetRtcpType(data, len, &type);
639 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
640 << " RTCP packet: size=" << len << ", type=" << type;
641 return;
642 }
643 }
644
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000645 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 } else if (secure_required_) {
647 // Our session description indicates that SRTP is required, but we got a
648 // packet before our SRTP filter is active. This means either that
649 // a) we got SRTP packets before we received the SDES keys, in which case
650 // we can't decrypt it anyway, or
651 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
652 // channels, so we haven't yet extracted keys, even if DTLS did complete
653 // on the channel that the packets are being sent on. It's really good
654 // practice to wait for both RTP and RTCP to be good to go before sending
655 // media, to prevent weird failure modes, so it's fine for us to just eat
656 // packets here. This is all sidestepped if RTCP mux is used anyway.
657 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
658 << " packet when SRTP is inactive and crypto is required";
659 return;
660 }
661
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662 // Push it down to the media channel.
663 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000664 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000666 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 }
668}
669
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000670bool BaseChannel::PushdownLocalDescription(
671 const SessionDescription* local_desc, ContentAction action,
672 std::string* error_desc) {
673 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 const MediaContentDescription* content_desc =
675 GetContentDescription(content_info);
676 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000677 !SetLocalContent(content_desc, action, error_desc)) {
678 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
679 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000681 return true;
682}
683
684bool BaseChannel::PushdownRemoteDescription(
685 const SessionDescription* remote_desc, ContentAction action,
686 std::string* error_desc) {
687 const ContentInfo* content_info = GetFirstContent(remote_desc);
688 const MediaContentDescription* content_desc =
689 GetContentDescription(content_info);
690 if (content_desc && content_info && !content_info->rejected &&
691 !SetRemoteContent(content_desc, action, error_desc)) {
692 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
693 return false;
694 }
695 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696}
697
698void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000699 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 if (enabled_)
701 return;
702
703 LOG(LS_INFO) << "Channel enabled";
704 enabled_ = true;
705 ChangeState();
706}
707
708void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000709 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 if (!enabled_)
711 return;
712
713 LOG(LS_INFO) << "Channel disabled";
714 enabled_ = false;
715 ChangeState();
716}
717
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000719 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720 if (writable_)
721 return;
722
torbjornga81a42f2015-09-23 02:16:58 -0700723 LOG(LS_INFO) << "Channel socket writable ("
724 << transport_channel_->content_name() << ", "
725 << transport_channel_->component() << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 << (was_ever_writable_ ? "" : " for the first time");
727
728 std::vector<ConnectionInfo> infos;
729 transport_channel_->GetStats(&infos);
730 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
731 it != infos.end(); ++it) {
732 if (it->best_connection) {
733 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
734 << "->" << it->remote_candidate.ToSensitiveString();
735 break;
736 }
737 }
738
739 // If we're doing DTLS-SRTP, now is the time.
740 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
741 if (!SetupDtlsSrtp(false)) {
torbjornga81a42f2015-09-23 02:16:58 -0700742 SignalDtlsSetupFailure(this, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000743 return;
744 }
745
746 if (rtcp_transport_channel_) {
747 if (!SetupDtlsSrtp(true)) {
torbjornga81a42f2015-09-23 02:16:58 -0700748 SignalDtlsSetupFailure(this, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000749 return;
750 }
751 }
752 }
753
754 was_ever_writable_ = true;
755 writable_ = true;
756 ChangeState();
757}
758
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000759void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
760 ASSERT(worker_thread() == rtc::Thread::Current());
761 signaling_thread()->Invoke<void>(Bind(
762 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
763}
764
765void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
766 ASSERT(signaling_thread() == rtc::Thread::Current());
767 SignalDtlsSetupFailure(this, rtcp);
768}
769
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
771 std::vector<std::string> ciphers;
772 // We always use the default SRTP ciphers for RTCP, but we may use different
773 // ciphers for RTP depending on the media type.
774 if (!rtcp) {
775 GetSrtpCiphers(&ciphers);
776 } else {
777 GetSupportedDefaultCryptoSuites(&ciphers);
778 }
779 return tc->SetSrtpCiphers(ciphers);
780}
781
782bool BaseChannel::ShouldSetupDtlsSrtp() const {
783 return true;
784}
785
786// This function returns true if either DTLS-SRTP is not in use
787// *or* DTLS-SRTP is successfully set up.
788bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
789 bool ret = false;
790
torbjornga81a42f2015-09-23 02:16:58 -0700791 TransportChannel *channel = rtcp_channel ?
792 rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793
794 // No DTLS
795 if (!channel->IsDtlsActive())
796 return true;
797
798 std::string selected_cipher;
799
800 if (!channel->GetSrtpCipher(&selected_cipher)) {
801 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
802 return false;
803 }
804
805 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
806 << content_name() << " "
807 << PacketType(rtcp_channel);
808
809 // OK, we're now doing DTLS (RFC 5764)
810 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
811 SRTP_MASTER_KEY_SALT_LEN * 2);
812
813 // RFC 5705 exporter using the RFC 5764 parameters
814 if (!channel->ExportKeyingMaterial(
815 kDtlsSrtpExporterLabel,
816 NULL, 0, false,
817 &dtls_buffer[0], dtls_buffer.size())) {
818 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
819 ASSERT(false); // This should never happen
820 return false;
821 }
822
823 // Sync up the keys with the DTLS-SRTP interface
824 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
825 SRTP_MASTER_KEY_SALT_LEN);
826 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
827 SRTP_MASTER_KEY_SALT_LEN);
828 size_t offset = 0;
829 memcpy(&client_write_key[0], &dtls_buffer[offset],
830 SRTP_MASTER_KEY_KEY_LEN);
831 offset += SRTP_MASTER_KEY_KEY_LEN;
832 memcpy(&server_write_key[0], &dtls_buffer[offset],
833 SRTP_MASTER_KEY_KEY_LEN);
834 offset += SRTP_MASTER_KEY_KEY_LEN;
835 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
836 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
837 offset += SRTP_MASTER_KEY_SALT_LEN;
838 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
839 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
840
841 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000842 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000843 if (!channel->GetSslRole(&role)) {
844 LOG(LS_WARNING) << "GetSslRole failed";
845 return false;
846 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000848 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 send_key = &server_write_key;
850 recv_key = &client_write_key;
851 } else {
852 send_key = &client_write_key;
853 recv_key = &server_write_key;
854 }
855
856 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000857 ret = srtp_filter_.SetRtcpParams(
858 selected_cipher,
859 &(*send_key)[0],
860 static_cast<int>(send_key->size()),
861 selected_cipher,
862 &(*recv_key)[0],
863 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000865 ret = srtp_filter_.SetRtpParams(
866 selected_cipher,
867 &(*send_key)[0],
868 static_cast<int>(send_key->size()),
869 selected_cipher,
870 &(*recv_key)[0],
871 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 }
873
874 if (!ret)
875 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
876 else
877 dtls_keyed_ = true;
878
879 return ret;
880}
881
882void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000883 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 if (!writable_)
885 return;
886
torbjornga81a42f2015-09-23 02:16:58 -0700887 LOG(LS_INFO) << "Channel socket not writable ("
888 << transport_channel_->content_name() << ", "
889 << transport_channel_->component() << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 writable_ = false;
891 ChangeState();
892}
893
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700894bool BaseChannel::SetRtpTransportParameters_w(
895 const MediaContentDescription* content,
896 ContentAction action,
897 ContentSource src,
898 std::string* error_desc) {
899 if (action == CA_UPDATE) {
900 // These parameters never get changed by a CA_UDPATE.
901 return true;
902 }
903
904 // Cache secure_required_ for belt and suspenders check on SendPacket
905 if (src == CS_LOCAL) {
906 set_secure_required(content->crypto_required() != CT_NONE);
907 }
908
909 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
910 return false;
911 }
912
913 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
914 return false;
915 }
916
917 return true;
918}
919
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000920// |dtls| will be set to true if DTLS is active for transport channel and
921// crypto is empty.
922bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000923 bool* dtls,
924 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000925 *dtls = transport_channel_->IsDtlsActive();
926 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000927 SafeSetError("Cryptos must be empty when DTLS is active.",
928 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000929 return false;
930 }
931 return true;
932}
933
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000935 ContentAction action,
936 ContentSource src,
937 std::string* error_desc) {
938 if (action == CA_UPDATE) {
939 // no crypto params.
940 return true;
941 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000943 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000944 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
945 if (!ret) {
946 return false;
947 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948 switch (action) {
949 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000950 // If DTLS is already active on the channel, we could be renegotiating
951 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000952 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000953 ret = srtp_filter_.SetOffer(cryptos, src);
954 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 break;
956 case CA_PRANSWER:
957 // If we're doing DTLS-SRTP, we don't want to update the filter
958 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000959 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
961 }
962 break;
963 case CA_ANSWER:
964 // If we're doing DTLS-SRTP, we don't want to update the filter
965 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000966 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 ret = srtp_filter_.SetAnswer(cryptos, src);
968 }
969 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 default:
971 break;
972 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000973 if (!ret) {
974 SafeSetError("Failed to setup SRTP filter.", error_desc);
975 return false;
976 }
977 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978}
979
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700980void BaseChannel::ActivateRtcpMux() {
981 worker_thread_->Invoke<void>(Bind(
982 &BaseChannel::ActivateRtcpMux_w, this));
983}
984
985void BaseChannel::ActivateRtcpMux_w() {
986 if (!rtcp_mux_filter_.IsActive()) {
987 rtcp_mux_filter_.SetActive();
torbjornga81a42f2015-09-23 02:16:58 -0700988 set_rtcp_transport_channel(NULL);
Peter Thatcheraf55ccc2015-05-21 07:48:41 -0700989 }
990}
991
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000993 ContentSource src,
994 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 bool ret = false;
996 switch (action) {
997 case CA_OFFER:
998 ret = rtcp_mux_filter_.SetOffer(enable, src);
999 break;
1000 case CA_PRANSWER:
1001 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1002 break;
1003 case CA_ANSWER:
1004 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1005 if (ret && rtcp_mux_filter_.IsActive()) {
1006 // We activated RTCP mux, close down the RTCP transport.
torbjornga81a42f2015-09-23 02:16:58 -07001007 set_rtcp_transport_channel(NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 }
1009 break;
1010 case CA_UPDATE:
1011 // No RTCP mux info.
1012 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001013 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 default:
1015 break;
1016 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001017 if (!ret) {
1018 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1019 return false;
1020 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1022 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1023 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001024 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 // If the RTP transport is already writable, then so are we.
1026 if (transport_channel_->writable()) {
1027 ChannelWritable_w();
1028 }
1029 }
1030
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001031 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001032}
1033
1034bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001035 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 if (!media_channel()->AddRecvStream(sp))
1037 return false;
1038
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001039 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040}
1041
1042bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001043 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001044 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 return media_channel()->RemoveRecvStream(ssrc);
1046}
1047
1048bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001049 ContentAction action,
1050 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1052 action == CA_PRANSWER || action == CA_UPDATE))
1053 return false;
1054
1055 // If this is an update, streams only contain streams that have changed.
1056 if (action == CA_UPDATE) {
1057 for (StreamParamsVec::const_iterator it = streams.begin();
1058 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001059 const StreamParams* existing_stream =
1060 GetStreamByIds(local_streams_, it->groupid, it->id);
1061 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001062 if (media_channel()->AddSendStream(*it)) {
1063 local_streams_.push_back(*it);
1064 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1065 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001066 std::ostringstream desc;
1067 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1068 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 return false;
1070 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001071 } else if (existing_stream && !it->has_ssrcs()) {
1072 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001073 std::ostringstream desc;
1074 desc << "Failed to remove send stream with ssrc "
1075 << it->first_ssrc() << ".";
1076 SafeSetError(desc.str(), error_desc);
1077 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001079 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 } else {
1081 LOG(LS_WARNING) << "Ignore unsupported stream update";
1082 }
1083 }
1084 return true;
1085 }
1086 // Else streams are all the streams we want to send.
1087
1088 // Check for streams that have been removed.
1089 bool ret = true;
1090 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1091 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001092 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001094 std::ostringstream desc;
1095 desc << "Failed to remove send stream with ssrc "
1096 << it->first_ssrc() << ".";
1097 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 ret = false;
1099 }
1100 }
1101 }
1102 // Check for new streams.
1103 for (StreamParamsVec::const_iterator it = streams.begin();
1104 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001105 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 if (media_channel()->AddSendStream(*it)) {
1107 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1108 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001109 std::ostringstream desc;
1110 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1111 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112 ret = false;
1113 }
1114 }
1115 }
1116 local_streams_ = streams;
1117 return ret;
1118}
1119
1120bool BaseChannel::UpdateRemoteStreams_w(
1121 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001122 ContentAction action,
1123 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1125 action == CA_PRANSWER || action == CA_UPDATE))
1126 return false;
1127
1128 // If this is an update, streams only contain streams that have changed.
1129 if (action == CA_UPDATE) {
1130 for (StreamParamsVec::const_iterator it = streams.begin();
1131 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001132 const StreamParams* existing_stream =
1133 GetStreamByIds(remote_streams_, it->groupid, it->id);
1134 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001135 if (AddRecvStream_w(*it)) {
1136 remote_streams_.push_back(*it);
1137 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1138 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001139 std::ostringstream desc;
1140 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1141 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 return false;
1143 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001144 } else if (existing_stream && !it->has_ssrcs()) {
1145 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001146 std::ostringstream desc;
1147 desc << "Failed to remove remote stream with ssrc "
1148 << it->first_ssrc() << ".";
1149 SafeSetError(desc.str(), error_desc);
1150 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001152 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153 } else {
1154 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001155 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 << " new stream = " << it->ToString();
1157 }
1158 }
1159 return true;
1160 }
1161 // Else streams are all the streams we want to receive.
1162
1163 // Check for streams that have been removed.
1164 bool ret = true;
1165 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1166 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001167 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001169 std::ostringstream desc;
1170 desc << "Failed to remove remote stream with ssrc "
1171 << it->first_ssrc() << ".";
1172 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001173 ret = false;
1174 }
1175 }
1176 }
1177 // Check for new streams.
1178 for (StreamParamsVec::const_iterator it = streams.begin();
1179 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001180 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 if (AddRecvStream_w(*it)) {
1182 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1183 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001184 std::ostringstream desc;
1185 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1186 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 ret = false;
1188 }
1189 }
1190 }
1191 remote_streams_ = streams;
1192 return ret;
1193}
1194
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001195void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1196 const std::vector<RtpHeaderExtension>& extensions) {
1197 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001198 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001199 rtp_abs_sendtime_extn_id_ =
1200 send_time_extension ? send_time_extension->id : -1;
1201}
1202
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001203void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 case MSG_RTPPACKET:
1206 case MSG_RTCPPACKET: {
1207 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001208 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 delete data; // because it is Posted
1210 break;
1211 }
1212 case MSG_FIRSTPACKETRECEIVED: {
1213 SignalFirstPacketReceived(this);
1214 break;
1215 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001216 }
1217}
1218
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219void BaseChannel::FlushRtcpMessages() {
1220 // Flush all remaining RTCP messages. This should only be called in
1221 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001222 ASSERT(rtc::Thread::Current() == worker_thread_);
1223 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001224 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001225 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001227 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 }
1229}
1230
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001231VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 MediaEngineInterface* media_engine,
1233 VoiceMediaChannel* media_channel,
torbjornga81a42f2015-09-23 02:16:58 -07001234 BaseSession* session,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 const std::string& content_name,
1236 bool rtcp)
torbjornga81a42f2015-09-23 02:16:58 -07001237 : BaseChannel(thread, media_channel, session, content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001239 media_engine_(media_engine),
torbjornga81a42f2015-09-23 02:16:58 -07001240 received_media_(false) {
1241}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242
1243VoiceChannel::~VoiceChannel() {
1244 StopAudioMonitor();
1245 StopMediaMonitor();
1246 // this can't be done in the base class, since it calls a virtual
1247 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001248 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249}
1250
1251bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001252 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001253 return false;
1254 }
1255 media_channel()->SignalMediaError.connect(
1256 this, &VoiceChannel::OnVoiceChannelError);
1257 srtp_filter()->SignalSrtpError.connect(
1258 this, &VoiceChannel::OnSrtpError);
1259 return true;
1260}
1261
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001262bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001263 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1264 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001265}
1266
torbjornga81a42f2015-09-23 02:16:58 -07001267bool VoiceChannel::SetAudioSend(uint32 ssrc, bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001268 const AudioOptions* options,
1269 AudioRenderer* renderer) {
torbjornga81a42f2015-09-23 02:16:58 -07001270 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend,
1271 media_channel(), ssrc, mute, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272}
1273
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274// TODO(juberti): Handle early media the right way. We should get an explicit
1275// ringing message telling us to start playing local ringback, which we cancel
1276// if any early media actually arrives. For now, we do the opposite, which is
1277// to wait 1 second for early media, and start playing local ringback if none
1278// arrives.
1279void VoiceChannel::SetEarlyMedia(bool enable) {
1280 if (enable) {
1281 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001282 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1283 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284 } else {
1285 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001286 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 }
1288}
1289
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290bool VoiceChannel::PressDTMF(int digit, bool playout) {
1291 int flags = DF_SEND;
1292 if (playout) {
1293 flags |= DF_PLAY;
1294 }
1295 int duration_ms = 160;
1296 return InsertDtmf(0, digit, duration_ms, flags);
1297}
1298
1299bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001300 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1301 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302}
1303
1304bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1305 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001306 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1307 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308}
1309
1310bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001311 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1312 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001314
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001316 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1317 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318}
1319
1320void VoiceChannel::StartMediaMonitor(int cms) {
1321 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001322 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323 media_monitor_->SignalUpdate.connect(
1324 this, &VoiceChannel::OnMediaMonitorUpdate);
1325 media_monitor_->Start(cms);
1326}
1327
1328void VoiceChannel::StopMediaMonitor() {
1329 if (media_monitor_) {
1330 media_monitor_->Stop();
1331 media_monitor_->SignalUpdate.disconnect(this);
1332 media_monitor_.reset();
1333 }
1334}
1335
1336void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001337 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338 audio_monitor_
1339 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1340 audio_monitor_->Start(cms);
1341}
1342
1343void VoiceChannel::StopAudioMonitor() {
1344 if (audio_monitor_) {
1345 audio_monitor_->Stop();
1346 audio_monitor_.reset();
1347 }
1348}
1349
1350bool VoiceChannel::IsAudioMonitorRunning() const {
1351 return (audio_monitor_.get() != NULL);
1352}
1353
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001355 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356}
1357
1358int VoiceChannel::GetOutputLevel_w() {
1359 return media_channel()->GetOutputLevel();
1360}
1361
1362void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1363 media_channel()->GetActiveStreams(actives);
1364}
1365
1366void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001367 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001368 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001369 int flags) {
1370 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371
1372 // Set a flag when we've received an RTP packet. If we're waiting for early
1373 // media, this will disable the timeout.
1374 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1375 received_media_ = true;
1376 }
1377}
1378
1379void VoiceChannel::ChangeState() {
1380 // Render incoming data if we're the active call, and we have the local
1381 // content. We receive data on the default channel and multiplexed streams.
1382 bool recv = IsReadyToReceive();
1383 if (!media_channel()->SetPlayout(recv)) {
1384 SendLastMediaError();
1385 }
1386
1387 // Send outgoing data if we're the active call, we have the remote content,
1388 // and we have had some form of connectivity.
1389 bool send = IsReadyToSend();
1390 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1391 if (!media_channel()->SetSend(send_flag)) {
1392 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1393 SendLastMediaError();
1394 }
1395
1396 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1397}
1398
1399const ContentInfo* VoiceChannel::GetFirstContent(
1400 const SessionDescription* sdesc) {
1401 return GetFirstAudioContent(sdesc);
1402}
1403
1404bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001405 ContentAction action,
1406 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001407 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001408 LOG(LS_INFO) << "Setting local voice description";
1409
1410 const AudioContentDescription* audio =
1411 static_cast<const AudioContentDescription*>(content);
1412 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001413 if (!audio) {
1414 SafeSetError("Can't find audio content in local description.", error_desc);
1415 return false;
1416 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001418 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1419 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001420 }
1421
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001422 AudioRecvParameters recv_params = last_recv_params_;
1423 RtpParametersFromMediaDescription(audio, &recv_params);
1424 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001425 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001426 error_desc);
1427 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001429 for (const AudioCodec& codec : audio->codecs()) {
1430 bundle_filter()->AddPayloadType(codec.id);
1431 }
1432 last_recv_params_ = recv_params;
1433
1434 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1435 // only give it to the media channel once we have a remote
1436 // description too (without a remote description, we won't be able
1437 // to send them anyway).
1438 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1439 SafeSetError("Failed to set local audio description streams.", error_desc);
1440 return false;
1441 }
1442
1443 set_local_content_direction(content->direction());
1444 ChangeState();
1445 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446}
1447
1448bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001449 ContentAction action,
1450 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001451 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001452 LOG(LS_INFO) << "Setting remote voice description";
1453
1454 const AudioContentDescription* audio =
1455 static_cast<const AudioContentDescription*>(content);
1456 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001457 if (!audio) {
1458 SafeSetError("Can't find audio content in remote description.", error_desc);
1459 return false;
1460 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001462 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1463 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464 }
1465
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001466 AudioSendParameters send_params = last_send_params_;
1467 RtpSendParametersFromMediaDescription(audio, &send_params);
1468 if (audio->conference_mode()) {
1469 send_params.options.conference_mode.Set(true);
1470 }
1471 if (audio->agc_minus_10db()) {
1472 send_params.options.adjust_agc_delta.Set(kAgcMinus10db);
1473 }
1474 if (!media_channel()->SetSendParameters(send_params)) {
1475 SafeSetError("Failed to set remote audio description send parameters.",
1476 error_desc);
1477 return false;
1478 }
1479 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001481 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1482 // and only give it to the media channel once we have a local
1483 // description too (without a local description, we won't be able to
1484 // recv them anyway).
1485 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1486 SafeSetError("Failed to set remote audio description streams.", error_desc);
1487 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 }
1489
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001490 if (audio->rtp_header_extensions_set()) {
1491 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1492 }
1493
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001494 set_remote_content_direction(content->direction());
1495 ChangeState();
1496 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497}
1498
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499void VoiceChannel::HandleEarlyMediaTimeout() {
1500 // This occurs on the main thread, not the worker thread.
1501 if (!received_media_) {
1502 LOG(LS_INFO) << "No early media received before timeout";
1503 SignalEarlyMediaTimeout(this);
1504 }
1505}
1506
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1508 int flags) {
1509 if (!enabled()) {
1510 return false;
1511 }
1512
1513 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1514}
1515
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001516void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001517 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518 case MSG_EARLYMEDIATIMEOUT:
1519 HandleEarlyMediaTimeout();
1520 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 case MSG_CHANNEL_ERROR: {
1522 VoiceChannelErrorMessageData* data =
1523 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1524 SignalMediaError(this, data->ssrc, data->error);
1525 delete data;
1526 break;
1527 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528 default:
1529 BaseChannel::OnMessage(pmsg);
1530 break;
1531 }
1532}
1533
1534void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001535 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 SignalConnectionMonitor(this, infos);
1537}
1538
1539void VoiceChannel::OnMediaMonitorUpdate(
1540 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1541 ASSERT(media_channel == this->media_channel());
1542 SignalMediaMonitor(this, info);
1543}
1544
1545void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1546 const AudioInfo& info) {
1547 SignalAudioMonitor(this, info);
1548}
1549
1550void VoiceChannel::OnVoiceChannelError(
1551 uint32 ssrc, VoiceMediaChannel::Error err) {
1552 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1553 ssrc, err);
1554 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1555}
1556
1557void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1558 SrtpFilter::Error error) {
1559 switch (error) {
1560 case SrtpFilter::ERROR_FAIL:
1561 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1562 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1563 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1564 break;
1565 case SrtpFilter::ERROR_AUTH:
1566 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1567 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1568 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1569 break;
1570 case SrtpFilter::ERROR_REPLAY:
1571 // Only receving channel should have this error.
1572 ASSERT(mode == SrtpFilter::UNPROTECT);
1573 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1574 break;
1575 default:
1576 break;
1577 }
1578}
1579
1580void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1581 GetSupportedAudioCryptoSuites(ciphers);
1582}
1583
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001584VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001585 VideoMediaChannel* media_channel,
torbjornga81a42f2015-09-23 02:16:58 -07001586 BaseSession* session,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001588 bool rtcp)
torbjornga81a42f2015-09-23 02:16:58 -07001589 : BaseChannel(thread, media_channel, session, content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 renderer_(NULL),
torbjornga81a42f2015-09-23 02:16:58 -07001592 previous_we_(rtc::WE_CLOSE) {
1593}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594
1595bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001596 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 return false;
1598 }
1599 media_channel()->SignalMediaError.connect(
1600 this, &VideoChannel::OnVideoChannelError);
1601 srtp_filter()->SignalSrtpError.connect(
1602 this, &VideoChannel::OnSrtpError);
1603 return true;
1604}
1605
1606void VoiceChannel::SendLastMediaError() {
1607 uint32 ssrc;
1608 VoiceMediaChannel::Error error;
1609 media_channel()->GetLastMediaError(&ssrc, &error);
1610 SignalMediaError(this, ssrc, error);
1611}
1612
1613VideoChannel::~VideoChannel() {
1614 std::vector<uint32> screencast_ssrcs;
1615 ScreencastMap::iterator iter;
1616 while (!screencast_capturers_.empty()) {
1617 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1618 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1619 << screencast_capturers_.begin()->first;
1620 ASSERT(false);
1621 break;
1622 }
1623 }
1624
1625 StopMediaMonitor();
1626 // this can't be done in the base class, since it calls a virtual
1627 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001628
1629 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630}
1631
1632bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001633 worker_thread()->Invoke<void>(Bind(
1634 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 return true;
1636}
1637
1638bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001639 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640}
1641
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001642bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1643 return worker_thread()->Invoke<bool>(Bind(
1644 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645}
1646
1647bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001648 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1649 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650}
1651
1652bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001653 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654}
1655
1656bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001657 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658}
1659
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001660int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001661 ScreencastDetailsData data(ssrc);
1662 worker_thread()->Invoke<void>(Bind(
1663 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001664 return data.fps;
1665}
1666
1667int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001668 ScreencastDetailsData data(ssrc);
1669 worker_thread()->Invoke<void>(Bind(
1670 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001671 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001672}
1673
1674bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001675 worker_thread()->Invoke<void>(Bind(
1676 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 return true;
1678}
1679
1680bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001681 worker_thread()->Invoke<void>(Bind(
1682 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683 return true;
1684}
1685
torbjornga81a42f2015-09-23 02:16:58 -07001686bool VideoChannel::SetVideoSend(uint32 ssrc, bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001687 const VideoOptions* options) {
torbjornga81a42f2015-09-23 02:16:58 -07001688 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend,
1689 media_channel(), ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001690}
1691
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692void VideoChannel::ChangeState() {
1693 // Render incoming data if we're the active call, and we have the local
1694 // content. We receive data on the default channel and multiplexed streams.
1695 bool recv = IsReadyToReceive();
1696 if (!media_channel()->SetRender(recv)) {
1697 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1698 // TODO(gangji): Report error back to server.
1699 }
1700
1701 // Send outgoing data if we're the active call, we have the remote content,
1702 // and we have had some form of connectivity.
1703 bool send = IsReadyToSend();
1704 if (!media_channel()->SetSend(send)) {
1705 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1706 // TODO(gangji): Report error back to server.
1707 }
1708
1709 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1710}
1711
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001712bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1713 return InvokeOnWorker(
1714 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715}
1716
1717void VideoChannel::StartMediaMonitor(int cms) {
1718 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001719 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 media_monitor_->SignalUpdate.connect(
1721 this, &VideoChannel::OnMediaMonitorUpdate);
1722 media_monitor_->Start(cms);
1723}
1724
1725void VideoChannel::StopMediaMonitor() {
1726 if (media_monitor_) {
1727 media_monitor_->Stop();
1728 media_monitor_.reset();
1729 }
1730}
1731
1732const ContentInfo* VideoChannel::GetFirstContent(
1733 const SessionDescription* sdesc) {
1734 return GetFirstVideoContent(sdesc);
1735}
1736
1737bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001738 ContentAction action,
1739 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001740 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 LOG(LS_INFO) << "Setting local video description";
1742
1743 const VideoContentDescription* video =
1744 static_cast<const VideoContentDescription*>(content);
1745 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001746 if (!video) {
1747 SafeSetError("Can't find video content in local description.", error_desc);
1748 return false;
1749 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001751 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1752 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 }
1754
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001755 VideoRecvParameters recv_params = last_recv_params_;
1756 RtpParametersFromMediaDescription(video, &recv_params);
1757 if (!media_channel()->SetRecvParameters(recv_params)) {
1758 SafeSetError("Failed to set local video description recv parameters.",
1759 error_desc);
1760 return false;
1761 }
1762 for (const VideoCodec& codec : video->codecs()) {
1763 bundle_filter()->AddPayloadType(codec.id);
1764 }
1765 last_recv_params_ = recv_params;
1766
1767 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1768 // only give it to the media channel once we have a remote
1769 // description too (without a remote description, we won't be able
1770 // to send them anyway).
1771 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1772 SafeSetError("Failed to set local video description streams.", error_desc);
1773 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 }
1775
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001776 set_local_content_direction(content->direction());
1777 ChangeState();
1778 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779}
1780
1781bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001782 ContentAction action,
1783 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001784 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 LOG(LS_INFO) << "Setting remote video description";
1786
1787 const VideoContentDescription* video =
1788 static_cast<const VideoContentDescription*>(content);
1789 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001790 if (!video) {
1791 SafeSetError("Can't find video content in remote description.", error_desc);
1792 return false;
1793 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001795
1796 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1797 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 }
1799
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001800 VideoSendParameters send_params = last_send_params_;
1801 RtpSendParametersFromMediaDescription(video, &send_params);
1802 if (video->conference_mode()) {
1803 send_params.options.conference_mode.Set(true);
1804 }
1805 if (!media_channel()->SetSendParameters(send_params)) {
1806 SafeSetError("Failed to set remote video description send parameters.",
1807 error_desc);
1808 return false;
1809 }
1810 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001812 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1813 // and only give it to the media channel once we have a local
1814 // description too (without a local description, we won't be able to
1815 // recv them anyway).
1816 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1817 SafeSetError("Failed to set remote 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 if (video->rtp_header_extensions_set()) {
1822 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001824
1825 set_remote_content_direction(content->direction());
1826 ChangeState();
1827 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828}
1829
1830bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1831 bool ret = true;
1832 // Set the send format for each of the local streams. If the view request
1833 // does not contain a local stream, set its send format to 0x0, which will
1834 // drop all frames.
1835 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1836 it != local_streams().end(); ++it) {
1837 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1838 StaticVideoViews::const_iterator view;
1839 for (view = request.static_video_views.begin();
1840 view != request.static_video_views.end(); ++view) {
1841 if (view->selector.Matches(*it)) {
1842 format.width = view->width;
1843 format.height = view->height;
1844 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1845 break;
1846 }
1847 }
1848
1849 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1850 }
1851
1852 // Check if the view request has invalid streams.
1853 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1854 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001855 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 LOG(LS_WARNING) << "View request for ("
1857 << it->selector.ssrc << ", '"
1858 << it->selector.groupid << "', '"
1859 << it->selector.streamid << "'"
1860 << ") is not in the local streams.";
1861 }
1862 }
1863
1864 return ret;
1865}
1866
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001867bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001869 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001871 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1872 screencast_capturers_[ssrc] = capturer;
1873 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874}
1875
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1877 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1878 if (iter == screencast_capturers_.end()) {
1879 return false;
1880 }
1881 // Clean up VideoCapturer.
1882 delete iter->second;
1883 screencast_capturers_.erase(iter);
1884 return true;
1885}
1886
1887bool VideoChannel::IsScreencasting_w() const {
1888 return !screencast_capturers_.empty();
1889}
1890
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001891void VideoChannel::GetScreencastDetails_w(
1892 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001893 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001895 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896 }
1897 VideoCapturer* capturer = iter->second;
1898 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001899 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1900 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901}
1902
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001904 rtc::WindowEvent we) {
1905 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 SignalScreencastWindowEvent(ssrc, we);
1907}
1908
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001909void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 case MSG_SCREENCASTWINDOWEVENT: {
1912 const ScreencastEventMessageData* data =
1913 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1914 OnScreencastWindowEvent_s(data->ssrc, data->event);
1915 delete data;
1916 break;
1917 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 case MSG_CHANNEL_ERROR: {
1919 const VideoChannelErrorMessageData* data =
1920 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
1921 SignalMediaError(this, data->ssrc, data->error);
1922 delete data;
1923 break;
1924 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001925 default:
1926 BaseChannel::OnMessage(pmsg);
1927 break;
1928 }
1929}
1930
1931void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001932 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 SignalConnectionMonitor(this, infos);
1934}
1935
1936// TODO(pthatcher): Look into removing duplicate code between
1937// audio, video, and data, perhaps by using templates.
1938void VideoChannel::OnMediaMonitorUpdate(
1939 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1940 ASSERT(media_channel == this->media_channel());
1941 SignalMediaMonitor(this, info);
1942}
1943
1944void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001945 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946 ScreencastEventMessageData* pdata =
1947 new ScreencastEventMessageData(ssrc, event);
1948 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1949}
1950
1951void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1952 // Map capturer events to window events. In the future we may want to simply
1953 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001954 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001956 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001958 we = rtc::WE_MINIMIZE;
1959 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1960 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 } else {
1962 return;
1963 }
1964 previous_we_ = we;
1965
1966 uint32 ssrc = 0;
1967 if (!GetLocalSsrc(capturer, &ssrc)) {
1968 return;
1969 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001970
1971 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972}
1973
1974bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
1975 *ssrc = 0;
1976 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
1977 iter != screencast_capturers_.end(); ++iter) {
1978 if (iter->second == capturer) {
1979 *ssrc = iter->first;
1980 return true;
1981 }
1982 }
1983 return false;
1984}
1985
1986void VideoChannel::OnVideoChannelError(uint32 ssrc,
1987 VideoMediaChannel::Error error) {
1988 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
1989 ssrc, error);
1990 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1991}
1992
1993void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1994 SrtpFilter::Error error) {
1995 switch (error) {
1996 case SrtpFilter::ERROR_FAIL:
1997 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1998 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
1999 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2000 break;
2001 case SrtpFilter::ERROR_AUTH:
2002 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2003 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2004 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2005 break;
2006 case SrtpFilter::ERROR_REPLAY:
2007 // Only receving channel should have this error.
2008 ASSERT(mode == SrtpFilter::UNPROTECT);
2009 // TODO(gangji): Turn on the signaling of replay error once we have
2010 // switched to the new mechanism for doing video retransmissions.
2011 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2012 break;
2013 default:
2014 break;
2015 }
2016}
2017
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2019 GetSupportedVideoCryptoSuites(ciphers);
2020}
2021
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002022DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 DataMediaChannel* media_channel,
torbjornga81a42f2015-09-23 02:16:58 -07002024 BaseSession* session,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025 const std::string& content_name,
2026 bool rtcp)
torbjornga81a42f2015-09-23 02:16:58 -07002027 : BaseChannel(thread, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002028 data_channel_type_(cricket::DCT_NONE),
torbjornga81a42f2015-09-23 02:16:58 -07002029 ready_to_send_data_(false) {
2030}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031
2032DataChannel::~DataChannel() {
2033 StopMediaMonitor();
2034 // this can't be done in the base class, since it calls a virtual
2035 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002036
2037 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038}
2039
2040bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002041 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 return false;
2043 }
2044 media_channel()->SignalDataReceived.connect(
2045 this, &DataChannel::OnDataReceived);
2046 media_channel()->SignalMediaError.connect(
2047 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002048 media_channel()->SignalReadyToSend.connect(
2049 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002050 media_channel()->SignalStreamClosedRemotely.connect(
2051 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 srtp_filter()->SignalSrtpError.connect(
2053 this, &DataChannel::OnSrtpError);
2054 return true;
2055}
2056
2057bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002058 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002060 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2061 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062}
2063
2064const ContentInfo* DataChannel::GetFirstContent(
2065 const SessionDescription* sdesc) {
2066 return GetFirstDataContent(sdesc);
2067}
2068
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002069bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070 if (data_channel_type_ == DCT_SCTP) {
2071 // TODO(pthatcher): Do this in a more robust way by checking for
2072 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002073 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 } else if (data_channel_type_ == DCT_RTP) {
2075 return BaseChannel::WantsPacket(rtcp, packet);
2076 }
2077 return false;
2078}
2079
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002080bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2081 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082 // It hasn't been set before, so set it now.
2083 if (data_channel_type_ == DCT_NONE) {
2084 data_channel_type_ = new_data_channel_type;
2085 return true;
2086 }
2087
2088 // It's been set before, but doesn't match. That's bad.
2089 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002090 std::ostringstream desc;
2091 desc << "Data channel type mismatch."
2092 << " Expected " << data_channel_type_
2093 << " Got " << new_data_channel_type;
2094 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095 return false;
2096 }
2097
2098 // It's hasn't changed. Nothing to do.
2099 return true;
2100}
2101
2102bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002103 const DataContentDescription* content,
2104 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2106 (content->protocol() == kMediaProtocolDtlsSctp));
2107 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002108 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109}
2110
2111bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002112 ContentAction action,
2113 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002114 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115 LOG(LS_INFO) << "Setting local data description";
2116
2117 const DataContentDescription* data =
2118 static_cast<const DataContentDescription*>(content);
2119 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002120 if (!data) {
2121 SafeSetError("Can't find data content in local description.", error_desc);
2122 return false;
2123 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002125 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 return false;
2127 }
2128
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002129 if (data_channel_type_ == DCT_RTP) {
2130 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2131 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 }
2133 }
2134
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002135 // FYI: We send the SCTP port number (not to be confused with the
2136 // underlying UDP port number) as a codec parameter. So even SCTP
2137 // data channels need codecs.
2138 DataRecvParameters recv_params = last_recv_params_;
2139 RtpParametersFromMediaDescription(data, &recv_params);
2140 if (!media_channel()->SetRecvParameters(recv_params)) {
2141 SafeSetError("Failed to set remote data description recv parameters.",
2142 error_desc);
2143 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002145 if (data_channel_type_ == DCT_RTP) {
2146 for (const DataCodec& codec : data->codecs()) {
2147 bundle_filter()->AddPayloadType(codec.id);
2148 }
2149 }
2150 last_recv_params_ = recv_params;
2151
2152 // TODO(pthatcher): Move local streams into DataSendParameters, and
2153 // only give it to the media channel once we have a remote
2154 // description too (without a remote description, we won't be able
2155 // to send them anyway).
2156 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2157 SafeSetError("Failed to set local data description streams.", error_desc);
2158 return false;
2159 }
2160
2161 set_local_content_direction(content->direction());
2162 ChangeState();
2163 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164}
2165
2166bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002167 ContentAction action,
2168 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002169 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170
2171 const DataContentDescription* data =
2172 static_cast<const DataContentDescription*>(content);
2173 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002174 if (!data) {
2175 SafeSetError("Can't find data content in remote description.", error_desc);
2176 return false;
2177 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002179 // If the remote data doesn't have codecs and isn't an update, it
2180 // must be empty, so ignore it.
2181 if (!data->has_codecs() && action != CA_UPDATE) {
2182 return true;
2183 }
2184
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002185 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002186 return false;
2187 }
2188
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002189 LOG(LS_INFO) << "Setting remote data description";
2190 if (data_channel_type_ == DCT_RTP &&
2191 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2192 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193 }
2194
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002195
2196 DataSendParameters send_params = last_send_params_;
2197 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2198 if (!media_channel()->SetSendParameters(send_params)) {
2199 SafeSetError("Failed to set remote data description send parameters.",
2200 error_desc);
2201 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002203 last_send_params_ = send_params;
2204
2205 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2206 // and only give it to the media channel once we have a local
2207 // description too (without a local description, we won't be able to
2208 // recv them anyway).
2209 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2210 SafeSetError("Failed to set remote data description streams.",
2211 error_desc);
2212 return false;
2213 }
2214
2215 set_remote_content_direction(content->direction());
2216 ChangeState();
2217 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218}
2219
2220void DataChannel::ChangeState() {
2221 // Render incoming data if we're the active call, and we have the local
2222 // content. We receive data on the default channel and multiplexed streams.
2223 bool recv = IsReadyToReceive();
2224 if (!media_channel()->SetReceive(recv)) {
2225 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2226 }
2227
2228 // Send outgoing data if we're the active call, we have the remote content,
2229 // and we have had some form of connectivity.
2230 bool send = IsReadyToSend();
2231 if (!media_channel()->SetSend(send)) {
2232 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2233 }
2234
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002235 // Trigger SignalReadyToSendData asynchronously.
2236 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237
2238 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2239}
2240
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002241void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242 switch (pmsg->message_id) {
2243 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002244 DataChannelReadyToSendMessageData* data =
2245 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002246 ready_to_send_data_ = data->data();
2247 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248 delete data;
2249 break;
2250 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251 case MSG_DATARECEIVED: {
2252 DataReceivedMessageData* data =
2253 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2254 SignalDataReceived(this, data->params, data->payload);
2255 delete data;
2256 break;
2257 }
2258 case MSG_CHANNEL_ERROR: {
2259 const DataChannelErrorMessageData* data =
2260 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2261 SignalMediaError(this, data->ssrc, data->error);
2262 delete data;
2263 break;
2264 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002265 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002266 rtc::TypedMessageData<uint32>* data =
2267 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002268 SignalStreamClosedRemotely(data->data());
2269 delete data;
2270 break;
2271 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272 default:
2273 BaseChannel::OnMessage(pmsg);
2274 break;
2275 }
2276}
2277
2278void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002279 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280 SignalConnectionMonitor(this, infos);
2281}
2282
2283void DataChannel::StartMediaMonitor(int cms) {
2284 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002285 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002286 media_monitor_->SignalUpdate.connect(
2287 this, &DataChannel::OnMediaMonitorUpdate);
2288 media_monitor_->Start(cms);
2289}
2290
2291void DataChannel::StopMediaMonitor() {
2292 if (media_monitor_) {
2293 media_monitor_->Stop();
2294 media_monitor_->SignalUpdate.disconnect(this);
2295 media_monitor_.reset();
2296 }
2297}
2298
2299void DataChannel::OnMediaMonitorUpdate(
2300 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2301 ASSERT(media_channel == this->media_channel());
2302 SignalMediaMonitor(this, info);
2303}
2304
2305void DataChannel::OnDataReceived(
2306 const ReceiveDataParams& params, const char* data, size_t len) {
2307 DataReceivedMessageData* msg = new DataReceivedMessageData(
2308 params, data, len);
2309 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2310}
2311
2312void DataChannel::OnDataChannelError(
2313 uint32 ssrc, DataMediaChannel::Error err) {
2314 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2315 ssrc, err);
2316 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2317}
2318
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002319void DataChannel::OnDataChannelReadyToSend(bool writable) {
2320 // This is usded for congestion control to indicate that the stream is ready
2321 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2322 // that the transport channel is ready.
2323 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2324 new DataChannelReadyToSendMessageData(writable));
2325}
2326
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002327void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2328 SrtpFilter::Error error) {
2329 switch (error) {
2330 case SrtpFilter::ERROR_FAIL:
2331 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2332 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2333 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2334 break;
2335 case SrtpFilter::ERROR_AUTH:
2336 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2337 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2338 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2339 break;
2340 case SrtpFilter::ERROR_REPLAY:
2341 // Only receving channel should have this error.
2342 ASSERT(mode == SrtpFilter::UNPROTECT);
2343 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2344 break;
2345 default:
2346 break;
2347 }
2348}
2349
2350void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2351 GetSupportedDataCryptoSuites(ciphers);
2352}
2353
2354bool DataChannel::ShouldSetupDtlsSrtp() const {
2355 return (data_channel_type_ == DCT_RTP);
2356}
2357
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002358void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002359 rtc::TypedMessageData<uint32>* message =
2360 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002361 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2362}
2363
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364} // namespace cricket