blob: 294681ef1449aa94033112e8e5315cec5048a0f7 [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,
Guo-wei Shieh89024332015-09-18 13:50:22 -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),
Guo-wei Shieh89024332015-09-18 13:50:22 -0700176 session_(session),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 media_channel_(media_channel),
178 content_name_(content_name),
Guo-wei Shieh89024332015-09-18 13:50:22 -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_;
Guo-wei Shieh89024332015-09-18 13:50:22 -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() {
Guo-wei Shieh89024332015-09-18 13:50:22 -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 }
Guo-wei Shieh89024332015-09-18 13:50:22 -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
Guo-wei Shieh89024332015-09-18 13:50:22 -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
Guo-wei Shieh89024332015-09-18 13:50:22 -0700239bool BaseChannel::SetTransportChannels_w(BaseSession* session, bool rtcp) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000240 ASSERT(worker_thread_ == rtc::Thread::Current());
241
Guo-wei Shieh89024332015-09-18 13:50:22 -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 }
Guo-wei Shieh89024332015-09-18 13:50:22 -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 }
Guo-wei Shieh89024332015-09-18 13:50:22 -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_;
Guo-wei Shieh89024332015-09-18 13:50:22 -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);
Guo-wei Shieh89024332015-09-18 13:50:22 -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_;
Guo-wei Shieh89024332015-09-18 13:50:22 -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);
Guo-wei Shieh89024332015-09-18 13:50:22 -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_);
Guo-wei Shieh89024332015-09-18 13:50:22 -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) {
Guo-wei Shieh89024332015-09-18 13:50:22 -0700443 SetReadyToSend(channel, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000444}
445
Guo-wei Shieh89024332015-09-18 13:50:22 -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 }
Guo-wei Shieh89024332015-09-18 13:50:22 -0700451 if (channel == rtcp_transport_channel_) {
452 rtcp_ready_to_send_ = ready;
453 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454
Guo-wei Shieh89024332015-09-18 13:50:22 -0700455 if (!ready) {
deadbeef9af63f42015-09-18 12:55:56 -0700456 // Notify the MediaChannel when either rtp or rtcp channel can't send.
457 media_channel_->OnReadyToSend(false);
Guo-wei Shieh89024332015-09-18 13:50:22 -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.";
Guo-wei Shieh89024332015-09-18 13:50:22 -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
Guo-wei Shieh89024332015-09-18 13:50:22 -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)) {
Guo-wei Shieh89024332015-09-18 13:50:22 -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)) {
Guo-wei Shieh89024332015-09-18 13:50:22 -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
Guo-wei Shieh89024332015-09-18 13:50:22 -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
Guo-wei Shieh89024332015-09-18 13:50:22 -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();
Guo-wei Shieh89024332015-09-18 13:50:22 -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.
Guo-wei Shieh89024332015-09-18 13:50:22 -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,
Guo-wei Shieh89024332015-09-18 13:50:22 -07001234 BaseSession* session,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 const std::string& content_name,
1236 bool rtcp)
Guo-wei Shieh89024332015-09-18 13:50:22 -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),
Guo-wei Shieh89024332015-09-18 13:50:22 -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
Guo-wei Shieh89024332015-09-18 13:50:22 -07001267bool VoiceChannel::SetAudioSend(uint32 ssrc, bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001268 const AudioOptions* options,
1269 AudioRenderer* renderer) {
Guo-wei Shieh89024332015-09-18 13:50:22 -07001270 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend,
1271 media_channel(), ssrc, mute, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001272}
1273
1274bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001275 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001276}
1277
1278// TODO(juberti): Handle early media the right way. We should get an explicit
1279// ringing message telling us to start playing local ringback, which we cancel
1280// if any early media actually arrives. For now, we do the opposite, which is
1281// to wait 1 second for early media, and start playing local ringback if none
1282// arrives.
1283void VoiceChannel::SetEarlyMedia(bool enable) {
1284 if (enable) {
1285 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001286 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1287 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 } else {
1289 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001290 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001291 }
1292}
1293
1294bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001295 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1296 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297}
1298
1299bool VoiceChannel::PressDTMF(int digit, bool playout) {
1300 int flags = DF_SEND;
1301 if (playout) {
1302 flags |= DF_PLAY;
1303 }
1304 int duration_ms = 160;
1305 return InsertDtmf(0, digit, duration_ms, flags);
1306}
1307
1308bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001309 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1310 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311}
1312
1313bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1314 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001315 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1316 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317}
1318
1319bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001320 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1321 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001322}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001323
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001325 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1326 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001327}
1328
1329void VoiceChannel::StartMediaMonitor(int cms) {
1330 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001331 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332 media_monitor_->SignalUpdate.connect(
1333 this, &VoiceChannel::OnMediaMonitorUpdate);
1334 media_monitor_->Start(cms);
1335}
1336
1337void VoiceChannel::StopMediaMonitor() {
1338 if (media_monitor_) {
1339 media_monitor_->Stop();
1340 media_monitor_->SignalUpdate.disconnect(this);
1341 media_monitor_.reset();
1342 }
1343}
1344
1345void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001346 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001347 audio_monitor_
1348 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1349 audio_monitor_->Start(cms);
1350}
1351
1352void VoiceChannel::StopAudioMonitor() {
1353 if (audio_monitor_) {
1354 audio_monitor_->Stop();
1355 audio_monitor_.reset();
1356 }
1357}
1358
1359bool VoiceChannel::IsAudioMonitorRunning() const {
1360 return (audio_monitor_.get() != NULL);
1361}
1362
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001364 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365}
1366
1367int VoiceChannel::GetOutputLevel_w() {
1368 return media_channel()->GetOutputLevel();
1369}
1370
1371void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1372 media_channel()->GetActiveStreams(actives);
1373}
1374
1375void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001376 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001377 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001378 int flags) {
1379 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001380
1381 // Set a flag when we've received an RTP packet. If we're waiting for early
1382 // media, this will disable the timeout.
1383 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1384 received_media_ = true;
1385 }
1386}
1387
1388void VoiceChannel::ChangeState() {
1389 // Render incoming data if we're the active call, and we have the local
1390 // content. We receive data on the default channel and multiplexed streams.
1391 bool recv = IsReadyToReceive();
1392 if (!media_channel()->SetPlayout(recv)) {
1393 SendLastMediaError();
1394 }
1395
1396 // Send outgoing data if we're the active call, we have the remote content,
1397 // and we have had some form of connectivity.
1398 bool send = IsReadyToSend();
1399 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1400 if (!media_channel()->SetSend(send_flag)) {
1401 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1402 SendLastMediaError();
1403 }
1404
1405 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1406}
1407
1408const ContentInfo* VoiceChannel::GetFirstContent(
1409 const SessionDescription* sdesc) {
1410 return GetFirstAudioContent(sdesc);
1411}
1412
1413bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001414 ContentAction action,
1415 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001416 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417 LOG(LS_INFO) << "Setting local voice description";
1418
1419 const AudioContentDescription* audio =
1420 static_cast<const AudioContentDescription*>(content);
1421 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001422 if (!audio) {
1423 SafeSetError("Can't find audio content in local description.", error_desc);
1424 return false;
1425 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001427 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1428 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001429 }
1430
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001431 AudioRecvParameters recv_params = last_recv_params_;
1432 RtpParametersFromMediaDescription(audio, &recv_params);
1433 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001434 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001435 error_desc);
1436 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001438 for (const AudioCodec& codec : audio->codecs()) {
1439 bundle_filter()->AddPayloadType(codec.id);
1440 }
1441 last_recv_params_ = recv_params;
1442
1443 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1444 // only give it to the media channel once we have a remote
1445 // description too (without a remote description, we won't be able
1446 // to send them anyway).
1447 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1448 SafeSetError("Failed to set local audio description streams.", error_desc);
1449 return false;
1450 }
1451
1452 set_local_content_direction(content->direction());
1453 ChangeState();
1454 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455}
1456
1457bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001458 ContentAction action,
1459 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001460 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 LOG(LS_INFO) << "Setting remote voice description";
1462
1463 const AudioContentDescription* audio =
1464 static_cast<const AudioContentDescription*>(content);
1465 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001466 if (!audio) {
1467 SafeSetError("Can't find audio content in remote description.", error_desc);
1468 return false;
1469 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001471 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1472 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 }
1474
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001475 AudioSendParameters send_params = last_send_params_;
1476 RtpSendParametersFromMediaDescription(audio, &send_params);
1477 if (audio->conference_mode()) {
1478 send_params.options.conference_mode.Set(true);
1479 }
1480 if (audio->agc_minus_10db()) {
1481 send_params.options.adjust_agc_delta.Set(kAgcMinus10db);
1482 }
1483 if (!media_channel()->SetSendParameters(send_params)) {
1484 SafeSetError("Failed to set remote audio description send parameters.",
1485 error_desc);
1486 return false;
1487 }
1488 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001489
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001490 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1491 // and only give it to the media channel once we have a local
1492 // description too (without a local description, we won't be able to
1493 // recv them anyway).
1494 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1495 SafeSetError("Failed to set remote audio description streams.", error_desc);
1496 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497 }
1498
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001499 if (audio->rtp_header_extensions_set()) {
1500 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1501 }
1502
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001503 set_remote_content_direction(content->direction());
1504 ChangeState();
1505 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506}
1507
1508bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001509 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1511}
1512
1513bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001514 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 if (play) {
1516 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1517 } else {
1518 LOG(LS_INFO) << "Stopping ringback tone";
1519 }
1520 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1521}
1522
1523void VoiceChannel::HandleEarlyMediaTimeout() {
1524 // This occurs on the main thread, not the worker thread.
1525 if (!received_media_) {
1526 LOG(LS_INFO) << "No early media received before timeout";
1527 SignalEarlyMediaTimeout(this);
1528 }
1529}
1530
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1532 int flags) {
1533 if (!enabled()) {
1534 return false;
1535 }
1536
1537 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1538}
1539
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001540void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542 case MSG_EARLYMEDIATIMEOUT:
1543 HandleEarlyMediaTimeout();
1544 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 case MSG_CHANNEL_ERROR: {
1546 VoiceChannelErrorMessageData* data =
1547 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1548 SignalMediaError(this, data->ssrc, data->error);
1549 delete data;
1550 break;
1551 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 default:
1553 BaseChannel::OnMessage(pmsg);
1554 break;
1555 }
1556}
1557
1558void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001559 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 SignalConnectionMonitor(this, infos);
1561}
1562
1563void VoiceChannel::OnMediaMonitorUpdate(
1564 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1565 ASSERT(media_channel == this->media_channel());
1566 SignalMediaMonitor(this, info);
1567}
1568
1569void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1570 const AudioInfo& info) {
1571 SignalAudioMonitor(this, info);
1572}
1573
1574void VoiceChannel::OnVoiceChannelError(
1575 uint32 ssrc, VoiceMediaChannel::Error err) {
1576 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1577 ssrc, err);
1578 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1579}
1580
1581void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1582 SrtpFilter::Error error) {
1583 switch (error) {
1584 case SrtpFilter::ERROR_FAIL:
1585 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1586 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1587 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1588 break;
1589 case SrtpFilter::ERROR_AUTH:
1590 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1591 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1592 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1593 break;
1594 case SrtpFilter::ERROR_REPLAY:
1595 // Only receving channel should have this error.
1596 ASSERT(mode == SrtpFilter::UNPROTECT);
1597 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1598 break;
1599 default:
1600 break;
1601 }
1602}
1603
1604void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1605 GetSupportedAudioCryptoSuites(ciphers);
1606}
1607
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001608VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001609 VideoMediaChannel* media_channel,
Guo-wei Shieh89024332015-09-18 13:50:22 -07001610 BaseSession* session,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001612 bool rtcp)
Guo-wei Shieh89024332015-09-18 13:50:22 -07001613 : BaseChannel(thread, media_channel, session, content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001614 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615 renderer_(NULL),
Guo-wei Shieh89024332015-09-18 13:50:22 -07001616 previous_we_(rtc::WE_CLOSE) {
1617}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001618
1619bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001620 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 return false;
1622 }
1623 media_channel()->SignalMediaError.connect(
1624 this, &VideoChannel::OnVideoChannelError);
1625 srtp_filter()->SignalSrtpError.connect(
1626 this, &VideoChannel::OnSrtpError);
1627 return true;
1628}
1629
1630void VoiceChannel::SendLastMediaError() {
1631 uint32 ssrc;
1632 VoiceMediaChannel::Error error;
1633 media_channel()->GetLastMediaError(&ssrc, &error);
1634 SignalMediaError(this, ssrc, error);
1635}
1636
1637VideoChannel::~VideoChannel() {
1638 std::vector<uint32> screencast_ssrcs;
1639 ScreencastMap::iterator iter;
1640 while (!screencast_capturers_.empty()) {
1641 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1642 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1643 << screencast_capturers_.begin()->first;
1644 ASSERT(false);
1645 break;
1646 }
1647 }
1648
1649 StopMediaMonitor();
1650 // this can't be done in the base class, since it calls a virtual
1651 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001652
1653 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654}
1655
1656bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001657 worker_thread()->Invoke<void>(Bind(
1658 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 return true;
1660}
1661
1662bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001663 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664}
1665
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001666bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1667 return worker_thread()->Invoke<bool>(Bind(
1668 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669}
1670
1671bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001672 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1673 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001674}
1675
1676bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001677 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678}
1679
1680bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001681 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682}
1683
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001684int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001685 ScreencastDetailsData data(ssrc);
1686 worker_thread()->Invoke<void>(Bind(
1687 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001688 return data.fps;
1689}
1690
1691int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001692 ScreencastDetailsData data(ssrc);
1693 worker_thread()->Invoke<void>(Bind(
1694 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001695 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696}
1697
1698bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001699 worker_thread()->Invoke<void>(Bind(
1700 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701 return true;
1702}
1703
1704bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001705 worker_thread()->Invoke<void>(Bind(
1706 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 return true;
1708}
1709
Guo-wei Shieh89024332015-09-18 13:50:22 -07001710bool VideoChannel::SetVideoSend(uint32 ssrc, bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001711 const VideoOptions* options) {
Guo-wei Shieh89024332015-09-18 13:50:22 -07001712 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend,
1713 media_channel(), ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001714}
1715
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716void VideoChannel::ChangeState() {
1717 // Render incoming data if we're the active call, and we have the local
1718 // content. We receive data on the default channel and multiplexed streams.
1719 bool recv = IsReadyToReceive();
1720 if (!media_channel()->SetRender(recv)) {
1721 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1722 // TODO(gangji): Report error back to server.
1723 }
1724
1725 // Send outgoing data if we're the active call, we have the remote content,
1726 // and we have had some form of connectivity.
1727 bool send = IsReadyToSend();
1728 if (!media_channel()->SetSend(send)) {
1729 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1730 // TODO(gangji): Report error back to server.
1731 }
1732
1733 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1734}
1735
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001736bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1737 return InvokeOnWorker(
1738 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739}
1740
1741void VideoChannel::StartMediaMonitor(int cms) {
1742 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001743 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744 media_monitor_->SignalUpdate.connect(
1745 this, &VideoChannel::OnMediaMonitorUpdate);
1746 media_monitor_->Start(cms);
1747}
1748
1749void VideoChannel::StopMediaMonitor() {
1750 if (media_monitor_) {
1751 media_monitor_->Stop();
1752 media_monitor_.reset();
1753 }
1754}
1755
1756const ContentInfo* VideoChannel::GetFirstContent(
1757 const SessionDescription* sdesc) {
1758 return GetFirstVideoContent(sdesc);
1759}
1760
1761bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001762 ContentAction action,
1763 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001764 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 LOG(LS_INFO) << "Setting local video description";
1766
1767 const VideoContentDescription* video =
1768 static_cast<const VideoContentDescription*>(content);
1769 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001770 if (!video) {
1771 SafeSetError("Can't find video content in local description.", error_desc);
1772 return false;
1773 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001775 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1776 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 }
1778
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001779 VideoRecvParameters recv_params = last_recv_params_;
1780 RtpParametersFromMediaDescription(video, &recv_params);
1781 if (!media_channel()->SetRecvParameters(recv_params)) {
1782 SafeSetError("Failed to set local video description recv parameters.",
1783 error_desc);
1784 return false;
1785 }
1786 for (const VideoCodec& codec : video->codecs()) {
1787 bundle_filter()->AddPayloadType(codec.id);
1788 }
1789 last_recv_params_ = recv_params;
1790
1791 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1792 // only give it to the media channel once we have a remote
1793 // description too (without a remote description, we won't be able
1794 // to send them anyway).
1795 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1796 SafeSetError("Failed to set local video description streams.", error_desc);
1797 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 }
1799
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001800 set_local_content_direction(content->direction());
1801 ChangeState();
1802 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803}
1804
1805bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001806 ContentAction action,
1807 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001808 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 LOG(LS_INFO) << "Setting remote video description";
1810
1811 const VideoContentDescription* video =
1812 static_cast<const VideoContentDescription*>(content);
1813 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001814 if (!video) {
1815 SafeSetError("Can't find video content in remote description.", error_desc);
1816 return false;
1817 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001819
1820 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1821 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 }
1823
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001824 VideoSendParameters send_params = last_send_params_;
1825 RtpSendParametersFromMediaDescription(video, &send_params);
1826 if (video->conference_mode()) {
1827 send_params.options.conference_mode.Set(true);
1828 }
1829 if (!media_channel()->SetSendParameters(send_params)) {
1830 SafeSetError("Failed to set remote video description send parameters.",
1831 error_desc);
1832 return false;
1833 }
1834 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001836 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1837 // and only give it to the media channel once we have a local
1838 // description too (without a local description, we won't be able to
1839 // recv them anyway).
1840 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1841 SafeSetError("Failed to set remote video description streams.", error_desc);
1842 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 }
1844
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001845 if (video->rtp_header_extensions_set()) {
1846 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001848
1849 set_remote_content_direction(content->direction());
1850 ChangeState();
1851 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852}
1853
1854bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1855 bool ret = true;
1856 // Set the send format for each of the local streams. If the view request
1857 // does not contain a local stream, set its send format to 0x0, which will
1858 // drop all frames.
1859 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1860 it != local_streams().end(); ++it) {
1861 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1862 StaticVideoViews::const_iterator view;
1863 for (view = request.static_video_views.begin();
1864 view != request.static_video_views.end(); ++view) {
1865 if (view->selector.Matches(*it)) {
1866 format.width = view->width;
1867 format.height = view->height;
1868 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1869 break;
1870 }
1871 }
1872
1873 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1874 }
1875
1876 // Check if the view request has invalid streams.
1877 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1878 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001879 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001880 LOG(LS_WARNING) << "View request for ("
1881 << it->selector.ssrc << ", '"
1882 << it->selector.groupid << "', '"
1883 << it->selector.streamid << "'"
1884 << ") is not in the local streams.";
1885 }
1886 }
1887
1888 return ret;
1889}
1890
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001891bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001893 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001895 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1896 screencast_capturers_[ssrc] = capturer;
1897 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898}
1899
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1901 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1902 if (iter == screencast_capturers_.end()) {
1903 return false;
1904 }
1905 // Clean up VideoCapturer.
1906 delete iter->second;
1907 screencast_capturers_.erase(iter);
1908 return true;
1909}
1910
1911bool VideoChannel::IsScreencasting_w() const {
1912 return !screencast_capturers_.empty();
1913}
1914
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001915void VideoChannel::GetScreencastDetails_w(
1916 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001917 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001919 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 }
1921 VideoCapturer* capturer = iter->second;
1922 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001923 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1924 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001925}
1926
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001928 rtc::WindowEvent we) {
1929 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 SignalScreencastWindowEvent(ssrc, we);
1931}
1932
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001933void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935 case MSG_SCREENCASTWINDOWEVENT: {
1936 const ScreencastEventMessageData* data =
1937 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1938 OnScreencastWindowEvent_s(data->ssrc, data->event);
1939 delete data;
1940 break;
1941 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 case MSG_CHANNEL_ERROR: {
1943 const VideoChannelErrorMessageData* data =
1944 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
1945 SignalMediaError(this, data->ssrc, data->error);
1946 delete data;
1947 break;
1948 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949 default:
1950 BaseChannel::OnMessage(pmsg);
1951 break;
1952 }
1953}
1954
1955void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001956 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957 SignalConnectionMonitor(this, infos);
1958}
1959
1960// TODO(pthatcher): Look into removing duplicate code between
1961// audio, video, and data, perhaps by using templates.
1962void VideoChannel::OnMediaMonitorUpdate(
1963 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1964 ASSERT(media_channel == this->media_channel());
1965 SignalMediaMonitor(this, info);
1966}
1967
1968void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001969 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 ScreencastEventMessageData* pdata =
1971 new ScreencastEventMessageData(ssrc, event);
1972 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1973}
1974
1975void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1976 // Map capturer events to window events. In the future we may want to simply
1977 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001978 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001980 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001982 we = rtc::WE_MINIMIZE;
1983 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1984 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985 } else {
1986 return;
1987 }
1988 previous_we_ = we;
1989
1990 uint32 ssrc = 0;
1991 if (!GetLocalSsrc(capturer, &ssrc)) {
1992 return;
1993 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001994
1995 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996}
1997
1998bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
1999 *ssrc = 0;
2000 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2001 iter != screencast_capturers_.end(); ++iter) {
2002 if (iter->second == capturer) {
2003 *ssrc = iter->first;
2004 return true;
2005 }
2006 }
2007 return false;
2008}
2009
2010void VideoChannel::OnVideoChannelError(uint32 ssrc,
2011 VideoMediaChannel::Error error) {
2012 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2013 ssrc, error);
2014 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2015}
2016
2017void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2018 SrtpFilter::Error error) {
2019 switch (error) {
2020 case SrtpFilter::ERROR_FAIL:
2021 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2022 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2023 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2024 break;
2025 case SrtpFilter::ERROR_AUTH:
2026 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2027 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2028 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2029 break;
2030 case SrtpFilter::ERROR_REPLAY:
2031 // Only receving channel should have this error.
2032 ASSERT(mode == SrtpFilter::UNPROTECT);
2033 // TODO(gangji): Turn on the signaling of replay error once we have
2034 // switched to the new mechanism for doing video retransmissions.
2035 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2036 break;
2037 default:
2038 break;
2039 }
2040}
2041
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2043 GetSupportedVideoCryptoSuites(ciphers);
2044}
2045
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002046DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047 DataMediaChannel* media_channel,
Guo-wei Shieh89024332015-09-18 13:50:22 -07002048 BaseSession* session,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 const std::string& content_name,
2050 bool rtcp)
Guo-wei Shieh89024332015-09-18 13:50:22 -07002051 : BaseChannel(thread, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002052 data_channel_type_(cricket::DCT_NONE),
Guo-wei Shieh89024332015-09-18 13:50:22 -07002053 ready_to_send_data_(false) {
2054}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055
2056DataChannel::~DataChannel() {
2057 StopMediaMonitor();
2058 // this can't be done in the base class, since it calls a virtual
2059 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002060
2061 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062}
2063
2064bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002065 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066 return false;
2067 }
2068 media_channel()->SignalDataReceived.connect(
2069 this, &DataChannel::OnDataReceived);
2070 media_channel()->SignalMediaError.connect(
2071 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002072 media_channel()->SignalReadyToSend.connect(
2073 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002074 media_channel()->SignalStreamClosedRemotely.connect(
2075 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076 srtp_filter()->SignalSrtpError.connect(
2077 this, &DataChannel::OnSrtpError);
2078 return true;
2079}
2080
2081bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002082 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002084 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2085 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086}
2087
2088const ContentInfo* DataChannel::GetFirstContent(
2089 const SessionDescription* sdesc) {
2090 return GetFirstDataContent(sdesc);
2091}
2092
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002093bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002094 if (data_channel_type_ == DCT_SCTP) {
2095 // TODO(pthatcher): Do this in a more robust way by checking for
2096 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002097 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 } else if (data_channel_type_ == DCT_RTP) {
2099 return BaseChannel::WantsPacket(rtcp, packet);
2100 }
2101 return false;
2102}
2103
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002104bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2105 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 // It hasn't been set before, so set it now.
2107 if (data_channel_type_ == DCT_NONE) {
2108 data_channel_type_ = new_data_channel_type;
2109 return true;
2110 }
2111
2112 // It's been set before, but doesn't match. That's bad.
2113 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002114 std::ostringstream desc;
2115 desc << "Data channel type mismatch."
2116 << " Expected " << data_channel_type_
2117 << " Got " << new_data_channel_type;
2118 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119 return false;
2120 }
2121
2122 // It's hasn't changed. Nothing to do.
2123 return true;
2124}
2125
2126bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002127 const DataContentDescription* content,
2128 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2130 (content->protocol() == kMediaProtocolDtlsSctp));
2131 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002132 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133}
2134
2135bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002136 ContentAction action,
2137 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002138 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 LOG(LS_INFO) << "Setting local data description";
2140
2141 const DataContentDescription* data =
2142 static_cast<const DataContentDescription*>(content);
2143 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002144 if (!data) {
2145 SafeSetError("Can't find data content in local description.", error_desc);
2146 return false;
2147 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002149 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002150 return false;
2151 }
2152
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002153 if (data_channel_type_ == DCT_RTP) {
2154 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2155 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156 }
2157 }
2158
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002159 // FYI: We send the SCTP port number (not to be confused with the
2160 // underlying UDP port number) as a codec parameter. So even SCTP
2161 // data channels need codecs.
2162 DataRecvParameters recv_params = last_recv_params_;
2163 RtpParametersFromMediaDescription(data, &recv_params);
2164 if (!media_channel()->SetRecvParameters(recv_params)) {
2165 SafeSetError("Failed to set remote data description recv parameters.",
2166 error_desc);
2167 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002169 if (data_channel_type_ == DCT_RTP) {
2170 for (const DataCodec& codec : data->codecs()) {
2171 bundle_filter()->AddPayloadType(codec.id);
2172 }
2173 }
2174 last_recv_params_ = recv_params;
2175
2176 // TODO(pthatcher): Move local streams into DataSendParameters, and
2177 // only give it to the media channel once we have a remote
2178 // description too (without a remote description, we won't be able
2179 // to send them anyway).
2180 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2181 SafeSetError("Failed to set local data description streams.", error_desc);
2182 return false;
2183 }
2184
2185 set_local_content_direction(content->direction());
2186 ChangeState();
2187 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188}
2189
2190bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002191 ContentAction action,
2192 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002193 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194
2195 const DataContentDescription* data =
2196 static_cast<const DataContentDescription*>(content);
2197 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002198 if (!data) {
2199 SafeSetError("Can't find data content in remote description.", error_desc);
2200 return false;
2201 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002203 // If the remote data doesn't have codecs and isn't an update, it
2204 // must be empty, so ignore it.
2205 if (!data->has_codecs() && action != CA_UPDATE) {
2206 return true;
2207 }
2208
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002209 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210 return false;
2211 }
2212
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002213 LOG(LS_INFO) << "Setting remote data description";
2214 if (data_channel_type_ == DCT_RTP &&
2215 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2216 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 }
2218
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002219
2220 DataSendParameters send_params = last_send_params_;
2221 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2222 if (!media_channel()->SetSendParameters(send_params)) {
2223 SafeSetError("Failed to set remote data description send parameters.",
2224 error_desc);
2225 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002227 last_send_params_ = send_params;
2228
2229 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2230 // and only give it to the media channel once we have a local
2231 // description too (without a local description, we won't be able to
2232 // recv them anyway).
2233 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2234 SafeSetError("Failed to set remote data description streams.",
2235 error_desc);
2236 return false;
2237 }
2238
2239 set_remote_content_direction(content->direction());
2240 ChangeState();
2241 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242}
2243
2244void DataChannel::ChangeState() {
2245 // Render incoming data if we're the active call, and we have the local
2246 // content. We receive data on the default channel and multiplexed streams.
2247 bool recv = IsReadyToReceive();
2248 if (!media_channel()->SetReceive(recv)) {
2249 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2250 }
2251
2252 // Send outgoing data if we're the active call, we have the remote content,
2253 // and we have had some form of connectivity.
2254 bool send = IsReadyToSend();
2255 if (!media_channel()->SetSend(send)) {
2256 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2257 }
2258
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002259 // Trigger SignalReadyToSendData asynchronously.
2260 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002261
2262 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2263}
2264
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002265void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002266 switch (pmsg->message_id) {
2267 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002268 DataChannelReadyToSendMessageData* data =
2269 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002270 ready_to_send_data_ = data->data();
2271 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272 delete data;
2273 break;
2274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275 case MSG_DATARECEIVED: {
2276 DataReceivedMessageData* data =
2277 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2278 SignalDataReceived(this, data->params, data->payload);
2279 delete data;
2280 break;
2281 }
2282 case MSG_CHANNEL_ERROR: {
2283 const DataChannelErrorMessageData* data =
2284 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2285 SignalMediaError(this, data->ssrc, data->error);
2286 delete data;
2287 break;
2288 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002289 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002290 rtc::TypedMessageData<uint32>* data =
2291 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002292 SignalStreamClosedRemotely(data->data());
2293 delete data;
2294 break;
2295 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 default:
2297 BaseChannel::OnMessage(pmsg);
2298 break;
2299 }
2300}
2301
2302void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002303 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002304 SignalConnectionMonitor(this, infos);
2305}
2306
2307void DataChannel::StartMediaMonitor(int cms) {
2308 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002309 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310 media_monitor_->SignalUpdate.connect(
2311 this, &DataChannel::OnMediaMonitorUpdate);
2312 media_monitor_->Start(cms);
2313}
2314
2315void DataChannel::StopMediaMonitor() {
2316 if (media_monitor_) {
2317 media_monitor_->Stop();
2318 media_monitor_->SignalUpdate.disconnect(this);
2319 media_monitor_.reset();
2320 }
2321}
2322
2323void DataChannel::OnMediaMonitorUpdate(
2324 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2325 ASSERT(media_channel == this->media_channel());
2326 SignalMediaMonitor(this, info);
2327}
2328
2329void DataChannel::OnDataReceived(
2330 const ReceiveDataParams& params, const char* data, size_t len) {
2331 DataReceivedMessageData* msg = new DataReceivedMessageData(
2332 params, data, len);
2333 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2334}
2335
2336void DataChannel::OnDataChannelError(
2337 uint32 ssrc, DataMediaChannel::Error err) {
2338 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2339 ssrc, err);
2340 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2341}
2342
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002343void DataChannel::OnDataChannelReadyToSend(bool writable) {
2344 // This is usded for congestion control to indicate that the stream is ready
2345 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2346 // that the transport channel is ready.
2347 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2348 new DataChannelReadyToSendMessageData(writable));
2349}
2350
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002351void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2352 SrtpFilter::Error error) {
2353 switch (error) {
2354 case SrtpFilter::ERROR_FAIL:
2355 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2356 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2357 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2358 break;
2359 case SrtpFilter::ERROR_AUTH:
2360 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2361 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2362 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2363 break;
2364 case SrtpFilter::ERROR_REPLAY:
2365 // Only receving channel should have this error.
2366 ASSERT(mode == SrtpFilter::UNPROTECT);
2367 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2368 break;
2369 default:
2370 break;
2371 }
2372}
2373
2374void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2375 GetSupportedDataCryptoSuites(ciphers);
2376}
2377
2378bool DataChannel::ShouldSetupDtlsSrtp() const {
2379 return (data_channel_type_ == DCT_RTP);
2380}
2381
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002382void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002383 rtc::TypedMessageData<uint32>* message =
2384 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002385 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2386}
2387
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002388} // namespace cricket