blob: fa1165af87c796a49691f3cb308f0ce35ed67af5 [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
kwiberg0eb15ed2015-12-17 03:04:15 -080028#include <utility>
29
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030#include "talk/session/media/channel.h"
31
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000032#include "talk/media/base/constants.h"
33#include "talk/media/base/rtputils.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000034#include "talk/session/media/channelmanager.h"
Tommif888bb52015-12-12 01:37:01 +010035#include "webrtc/audio/audio_sink.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000036#include "webrtc/base/bind.h"
37#include "webrtc/base/buffer.h"
38#include "webrtc/base/byteorder.h"
39#include "webrtc/base/common.h"
40#include "webrtc/base/dscp.h"
41#include "webrtc/base/logging.h"
Peter Boström6f28cf02015-12-07 23:17:15 +010042#include "webrtc/base/trace_event.h"
43#include "webrtc/p2p/base/transportchannel.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
45namespace cricket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000046using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000047
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000049 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 MSG_SCREENCASTWINDOWEVENT,
51 MSG_RTPPACKET,
52 MSG_RTCPPACKET,
53 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000057 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058};
59
60// Value specified in RFC 5764.
61static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
62
63static const int kAgcMinus10db = -10;
64
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000065static void SafeSetError(const std::string& message, std::string* error_desc) {
66 if (error_desc) {
67 *error_desc = message;
68 }
69}
70
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000071struct PacketMessageData : public rtc::MessageData {
72 rtc::Buffer packet;
stefanc1aeaf02015-10-15 07:26:07 -070073 rtc::PacketOptions options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074};
75
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000076struct ScreencastEventMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020077 ScreencastEventMessageData(uint32_t s, rtc::WindowEvent we)
78 : ssrc(s), event(we) {}
79 uint32_t ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081};
82
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000083struct VoiceChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020084 VoiceChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085 VoiceMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020086 : ssrc(in_ssrc), error(in_error) {}
87 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 VoiceMediaChannel::Error error;
89};
90
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091struct VideoChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +020092 VideoChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093 VideoMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +020094 : ssrc(in_ssrc), error(in_error) {}
95 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096 VideoMediaChannel::Error error;
97};
98
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000099struct DataChannelErrorMessageData : public rtc::MessageData {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200100 DataChannelErrorMessageData(uint32_t in_ssrc,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 DataMediaChannel::Error in_error)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200102 : ssrc(in_ssrc), error(in_error) {}
103 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 DataMediaChannel::Error error;
105};
106
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107static const char* PacketType(bool rtcp) {
108 return (!rtcp) ? "RTP" : "RTCP";
109}
110
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000111static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 // Check the packet size. We could check the header too if needed.
113 return (packet &&
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000114 packet->size() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
115 packet->size() <= kMaxRtpPacketLen);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116}
117
118static bool IsReceiveContentDirection(MediaContentDirection direction) {
119 return direction == MD_SENDRECV || direction == MD_RECVONLY;
120}
121
122static bool IsSendContentDirection(MediaContentDirection direction) {
123 return direction == MD_SENDRECV || direction == MD_SENDONLY;
124}
125
126static const MediaContentDescription* GetContentDescription(
127 const ContentInfo* cinfo) {
128 if (cinfo == NULL)
129 return NULL;
130 return static_cast<const MediaContentDescription*>(cinfo->description);
131}
132
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700133template <class Codec>
134void RtpParametersFromMediaDescription(
135 const MediaContentDescriptionImpl<Codec>* desc,
136 RtpParameters<Codec>* params) {
137 // TODO(pthatcher): Remove this once we're sure no one will give us
138 // a description without codecs (currently a CA_UPDATE with just
139 // streams can).
140 if (desc->has_codecs()) {
141 params->codecs = desc->codecs();
142 }
143 // TODO(pthatcher): See if we really need
144 // rtp_header_extensions_set() and remove it if we don't.
145 if (desc->rtp_header_extensions_set()) {
146 params->extensions = desc->rtp_header_extensions();
147 }
deadbeef13871492015-12-09 12:37:51 -0800148 params->rtcp.reduced_size = desc->rtcp_reduced_size();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700149}
150
151template <class Codec, class Options>
152void RtpSendParametersFromMediaDescription(
153 const MediaContentDescriptionImpl<Codec>* desc,
154 RtpSendParameters<Codec, Options>* send_params) {
155 RtpParametersFromMediaDescription(desc, send_params);
156 send_params->max_bandwidth_bps = desc->bandwidth();
157}
158
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000159BaseChannel::BaseChannel(rtc::Thread* thread,
deadbeefcbecd352015-09-23 11:50:27 -0700160 MediaChannel* media_channel,
161 TransportController* transport_controller,
162 const std::string& content_name,
163 bool rtcp)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 : worker_thread_(thread),
deadbeefcbecd352015-09-23 11:50:27 -0700165 transport_controller_(transport_controller),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 media_channel_(media_channel),
167 content_name_(content_name),
deadbeefcbecd352015-09-23 11:50:27 -0700168 rtcp_transport_enabled_(rtcp),
169 transport_channel_(nullptr),
170 rtcp_transport_channel_(nullptr),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000171 enabled_(false),
172 writable_(false),
173 rtp_ready_to_send_(false),
174 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 was_ever_writable_(false),
176 local_content_direction_(MD_INACTIVE),
177 remote_content_direction_(MD_INACTIVE),
178 has_received_packet_(false),
179 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000180 secure_required_(false),
181 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000182 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 LOG(LS_INFO) << "Created channel for " << content_name;
184}
185
186BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000187 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000188 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 StopConnectionMonitor();
190 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000191 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 // We must destroy the media channel before the transport channel, otherwise
193 // the media channel may try to send on the dead transport channel. NULLing
194 // is not an effective strategy since the sends will come on another thread.
195 delete media_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700196 // Note that we don't just call set_transport_channel(nullptr) because that
197 // would call a pure virtual method which we can't do from a destructor.
198 if (transport_channel_) {
199 DisconnectFromTransportChannel(transport_channel_);
200 transport_controller_->DestroyTransportChannel_w(
201 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
202 }
203 if (rtcp_transport_channel_) {
204 DisconnectFromTransportChannel(rtcp_transport_channel_);
205 transport_controller_->DestroyTransportChannel_w(
206 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
207 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 LOG(LS_INFO) << "Destroyed channel";
209}
210
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000211bool BaseChannel::Init() {
deadbeefcbecd352015-09-23 11:50:27 -0700212 if (!SetTransport(content_name())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 return false;
214 }
215
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800216 if (!SetDtlsSrtpCryptoSuites(transport_channel(), false)) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000217 return false;
218 }
deadbeefcbecd352015-09-23 11:50:27 -0700219 if (rtcp_transport_enabled() &&
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800220 !SetDtlsSrtpCryptoSuites(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
deadbeefcbecd352015-09-23 11:50:27 -0700234bool BaseChannel::SetTransport(const std::string& transport_name) {
235 return worker_thread_->Invoke<bool>(
236 Bind(&BaseChannel::SetTransport_w, this, transport_name));
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000237}
238
deadbeefcbecd352015-09-23 11:50:27 -0700239bool BaseChannel::SetTransport_w(const std::string& transport_name) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000240 ASSERT(worker_thread_ == rtc::Thread::Current());
241
deadbeefcbecd352015-09-23 11:50:27 -0700242 if (transport_name == transport_name_) {
243 // Nothing to do if transport name isn't changing
244 return true;
245 }
246
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800247 // When using DTLS-SRTP, we must reset the SrtpFilter every time the transport
248 // changes and wait until the DTLS handshake is complete to set the newly
249 // negotiated parameters.
250 if (ShouldSetupDtlsSrtp()) {
guoweis46383312015-12-17 16:45:59 -0800251 // Set |writable_| to false such that UpdateWritableState_w can set up
252 // DTLS-SRTP when the writable_ becomes true again.
253 writable_ = false;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800254 srtp_filter_.ResetParams();
255 }
256
guoweis46383312015-12-17 16:45:59 -0800257 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
deadbeefcbecd352015-09-23 11:50:27 -0700258 if (rtcp_transport_enabled()) {
259 LOG(LS_INFO) << "Create RTCP TransportChannel for " << content_name()
260 << " on " << transport_name << " transport ";
guoweis46383312015-12-17 16:45:59 -0800261 set_rtcp_transport_channel(
262 transport_controller_->CreateTransportChannel_w(
263 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTCP),
264 false /* update_writablity */);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000265 if (!rtcp_transport_channel()) {
266 return false;
267 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000268 }
269
guoweis46383312015-12-17 16:45:59 -0800270 // We're not updating the writablity during the transition state.
271 set_transport_channel(transport_controller_->CreateTransportChannel_w(
272 transport_name, cricket::ICE_CANDIDATE_COMPONENT_RTP));
273 if (!transport_channel()) {
274 return false;
275 }
276
277 // TODO(guoweis): Remove this grossness when we remove non-muxed RTCP.
278 if (rtcp_transport_enabled()) {
279 // We can only update the RTCP ready to send after set_transport_channel has
280 // handled channel writability.
281 SetReadyToSend(
282 true, rtcp_transport_channel() && rtcp_transport_channel()->writable());
283 }
deadbeefcbecd352015-09-23 11:50:27 -0700284 transport_name_ = transport_name;
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000285 return true;
286}
287
288void BaseChannel::set_transport_channel(TransportChannel* new_tc) {
289 ASSERT(worker_thread_ == rtc::Thread::Current());
290
291 TransportChannel* old_tc = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700292 if (!old_tc && !new_tc) {
293 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000294 return;
295 }
deadbeefcbecd352015-09-23 11:50:27 -0700296 ASSERT(old_tc != new_tc);
297
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000298 if (old_tc) {
299 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700300 transport_controller_->DestroyTransportChannel_w(
301 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000302 }
303
304 transport_channel_ = new_tc;
305
306 if (new_tc) {
307 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700308 for (const auto& pair : socket_options_) {
309 new_tc->SetOption(pair.first, pair.second);
310 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000311 }
deadbeefcbecd352015-09-23 11:50:27 -0700312
313 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
314 // setting new channel
315 UpdateWritableState_w();
316 SetReadyToSend(false, new_tc && new_tc->writable());
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000317}
318
guoweis46383312015-12-17 16:45:59 -0800319void BaseChannel::set_rtcp_transport_channel(TransportChannel* new_tc,
320 bool update_writablity) {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000321 ASSERT(worker_thread_ == rtc::Thread::Current());
322
323 TransportChannel* old_tc = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700324 if (!old_tc && !new_tc) {
325 // Nothing to do
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000326 return;
327 }
deadbeefcbecd352015-09-23 11:50:27 -0700328 ASSERT(old_tc != new_tc);
329
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000330 if (old_tc) {
331 DisconnectFromTransportChannel(old_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700332 transport_controller_->DestroyTransportChannel_w(
333 transport_name_, cricket::ICE_CANDIDATE_COMPONENT_RTCP);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000334 }
335
336 rtcp_transport_channel_ = new_tc;
337
338 if (new_tc) {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800339 RTC_CHECK(!(ShouldSetupDtlsSrtp() && srtp_filter_.IsActive()))
340 << "Setting RTCP for DTLS/SRTP after SrtpFilter is active "
341 << "should never happen.";
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000342 ConnectToTransportChannel(new_tc);
deadbeefcbecd352015-09-23 11:50:27 -0700343 for (const auto& pair : rtcp_socket_options_) {
344 new_tc->SetOption(pair.first, pair.second);
345 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000346 }
deadbeefcbecd352015-09-23 11:50:27 -0700347
guoweis46383312015-12-17 16:45:59 -0800348 if (update_writablity) {
349 // Update aggregate writable/ready-to-send state between RTP and RTCP upon
350 // setting new channel
351 UpdateWritableState_w();
352 SetReadyToSend(true, new_tc && new_tc->writable());
353 }
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000354}
355
356void BaseChannel::ConnectToTransportChannel(TransportChannel* tc) {
357 ASSERT(worker_thread_ == rtc::Thread::Current());
358
359 tc->SignalWritableState.connect(this, &BaseChannel::OnWritableState);
360 tc->SignalReadPacket.connect(this, &BaseChannel::OnChannelRead);
361 tc->SignalReadyToSend.connect(this, &BaseChannel::OnReadyToSend);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800362 tc->SignalDtlsState.connect(this, &BaseChannel::OnDtlsState);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000363}
364
365void BaseChannel::DisconnectFromTransportChannel(TransportChannel* tc) {
366 ASSERT(worker_thread_ == rtc::Thread::Current());
367
368 tc->SignalWritableState.disconnect(this);
369 tc->SignalReadPacket.disconnect(this);
370 tc->SignalReadyToSend.disconnect(this);
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800371 tc->SignalDtlsState.disconnect(this);
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +0000372}
373
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000374bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000375 worker_thread_->Invoke<void>(Bind(
376 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
377 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000378 return true;
379}
380
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000381bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000382 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383}
384
Peter Boström0c4e06b2015-10-07 12:23:21 +0200385bool BaseChannel::RemoveRecvStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000386 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387}
388
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000389bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000390 return InvokeOnWorker(
391 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000392}
393
Peter Boström0c4e06b2015-10-07 12:23:21 +0200394bool BaseChannel::RemoveSendStream(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000395 return InvokeOnWorker(
396 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000397}
398
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000400 ContentAction action,
401 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100402 TRACE_EVENT0("webrtc", "BaseChannel::SetLocalContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000403 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
404 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405}
406
407bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000408 ContentAction action,
409 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +0100410 TRACE_EVENT0("webrtc", "BaseChannel::SetRemoteContent");
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000411 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
412 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413}
414
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000416 // We pass in the BaseChannel instead of the transport_channel_
417 // because if the transport_channel_ changes, the ConnectionMonitor
418 // would be pointing to the wrong TransportChannel.
419 connection_monitor_.reset(new ConnectionMonitor(
420 this, worker_thread(), rtc::Thread::Current()));
421 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000423 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424}
425
426void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000427 if (connection_monitor_) {
428 connection_monitor_->Stop();
429 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 }
431}
432
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000433bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
434 ASSERT(worker_thread_ == rtc::Thread::Current());
435 return transport_channel_->GetStats(infos);
436}
437
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438bool BaseChannel::IsReadyToReceive() const {
439 // Receive data if we are enabled and have local content,
440 return enabled() && IsReceiveContentDirection(local_content_direction_);
441}
442
443bool BaseChannel::IsReadyToSend() const {
444 // Send outgoing data if we are enabled, have local and remote content,
445 // and we have had some form of connectivity.
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800446 return enabled() && IsReceiveContentDirection(remote_content_direction_) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000447 IsSendContentDirection(local_content_direction_) &&
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800448 was_ever_writable() &&
449 (srtp_filter_.IsActive() || !ShouldSetupDtlsSrtp());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450}
451
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000452bool BaseChannel::SendPacket(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700453 const rtc::PacketOptions& options) {
454 return SendPacket(false, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455}
456
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000457bool BaseChannel::SendRtcp(rtc::Buffer* packet,
stefanc1aeaf02015-10-15 07:26:07 -0700458 const rtc::PacketOptions& options) {
459 return SendPacket(true, packet, options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460}
461
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000462int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000464 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000465 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000466 case ST_RTP:
467 channel = transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700468 socket_options_.push_back(
469 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000470 break;
471 case ST_RTCP:
472 channel = rtcp_transport_channel_;
deadbeefcbecd352015-09-23 11:50:27 -0700473 rtcp_socket_options_.push_back(
474 std::pair<rtc::Socket::Option, int>(opt, value));
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000475 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000476 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000477 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478}
479
480void BaseChannel::OnWritableState(TransportChannel* channel) {
481 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
deadbeefcbecd352015-09-23 11:50:27 -0700482 UpdateWritableState_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000483}
484
485void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000486 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000487 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000488 int flags) {
Peter Boström6f28cf02015-12-07 23:17:15 +0100489 TRACE_EVENT0("webrtc", "BaseChannel::OnChannelRead");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000490 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000491 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492
493 // When using RTCP multiplexing we might get RTCP packets on the RTP
494 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
495 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000496 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000497 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000498}
499
500void BaseChannel::OnReadyToSend(TransportChannel* channel) {
deadbeefcbecd352015-09-23 11:50:27 -0700501 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
502 SetReadyToSend(channel == rtcp_transport_channel_, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000503}
504
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800505void BaseChannel::OnDtlsState(TransportChannel* channel,
506 DtlsTransportState state) {
507 if (!ShouldSetupDtlsSrtp()) {
508 return;
509 }
510
511 // Reset the srtp filter if it's not the CONNECTED state. For the CONNECTED
512 // state, setting up DTLS-SRTP context is deferred to ChannelWritable_w to
513 // cover other scenarios like the whole channel is writable (not just this
514 // TransportChannel) or when TransportChannel is attached after DTLS is
515 // negotiated.
516 if (state != DTLS_TRANSPORT_CONNECTED) {
517 srtp_filter_.ResetParams();
518 }
519}
520
deadbeefcbecd352015-09-23 11:50:27 -0700521void BaseChannel::SetReadyToSend(bool rtcp, bool ready) {
522 if (rtcp) {
523 rtcp_ready_to_send_ = ready;
524 } else {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000525 rtp_ready_to_send_ = ready;
526 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527
deadbeefcbecd352015-09-23 11:50:27 -0700528 if (rtp_ready_to_send_ &&
529 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
530 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
torbjornga81a42f2015-09-23 02:16:58 -0700531 // Notify the MediaChannel when both rtp and rtcp channel can send.
532 media_channel_->OnReadyToSend(true);
deadbeefcbecd352015-09-23 11:50:27 -0700533 } else {
534 // Notify the MediaChannel when either rtp or rtcp channel can't send.
535 media_channel_->OnReadyToSend(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 }
537}
538
539bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
540 const char* data, size_t len) {
541 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000542 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543}
544
stefanc1aeaf02015-10-15 07:26:07 -0700545bool BaseChannel::SendPacket(bool rtcp,
546 rtc::Buffer* packet,
547 const rtc::PacketOptions& options) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000548 // SendPacket gets called from MediaEngine, typically on an encoder thread.
549 // If the thread is not our worker thread, we will post to our worker
550 // so that the real work happens on our worker. This avoids us having to
551 // synchronize access to all the pieces of the send path, including
552 // SRTP and the inner workings of the transport channels.
553 // The only downside is that we can't return a proper failure code if
554 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000555 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556 // Avoid a copy by transferring the ownership of the packet data.
557 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
558 PacketMessageData* data = new PacketMessageData;
kwiberg0eb15ed2015-12-17 03:04:15 -0800559 data->packet = std::move(*packet);
stefanc1aeaf02015-10-15 07:26:07 -0700560 data->options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561 worker_thread_->Post(this, message_id, data);
562 return true;
563 }
564
565 // Now that we are on the correct thread, ensure we have a place to send this
566 // packet before doing anything. (We might get RTCP packets that we don't
567 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
568 // transport.
569 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
570 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000571 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 return false;
573 }
574
575 // Protect ourselves against crazy data.
576 if (!ValidPacket(rtcp, packet)) {
577 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000578 << PacketType(rtcp)
579 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580 return false;
581 }
582
stefanc1aeaf02015-10-15 07:26:07 -0700583 rtc::PacketOptions updated_options;
584 updated_options = options;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 // Protect if needed.
586 if (srtp_filter_.IsActive()) {
587 bool res;
Karl Wibergc56ac1e2015-05-04 14:54:55 +0200588 uint8_t* data = packet->data();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000589 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000590 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000591 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
592 // inside libsrtp for a RTP packet. A external HMAC module will be writing
593 // a fake HMAC value. This is ONLY done for a RTP packet.
594 // Socket layer will update rtp sendtime extension header if present in
595 // packet with current time before updating the HMAC.
596#if !defined(ENABLE_EXTERNAL_AUTH)
597 res = srtp_filter_.ProtectRtp(
598 data, len, static_cast<int>(packet->capacity()), &len);
599#else
stefanc1aeaf02015-10-15 07:26:07 -0700600 updated_options.packet_time_params.rtp_sendtime_extension_id =
henrike@webrtc.org05376342014-03-10 15:53:12 +0000601 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000602 res = srtp_filter_.ProtectRtp(
603 data, len, static_cast<int>(packet->capacity()), &len,
stefanc1aeaf02015-10-15 07:26:07 -0700604 &updated_options.packet_time_params.srtp_packet_index);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000605 // If protection succeeds, let's get auth params from srtp.
606 if (res) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200607 uint8_t* auth_key = NULL;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000608 int key_len;
609 res = srtp_filter_.GetRtpAuthParams(
stefanc1aeaf02015-10-15 07:26:07 -0700610 &auth_key, &key_len,
611 &updated_options.packet_time_params.srtp_auth_tag_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000612 if (res) {
stefanc1aeaf02015-10-15 07:26:07 -0700613 updated_options.packet_time_params.srtp_auth_key.resize(key_len);
614 updated_options.packet_time_params.srtp_auth_key.assign(
615 auth_key, auth_key + key_len);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000616 }
617 }
618#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000619 if (!res) {
620 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200621 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 GetRtpSeqNum(data, len, &seq_num);
623 GetRtpSsrc(data, len, &ssrc);
624 LOG(LS_ERROR) << "Failed to protect " << content_name_
625 << " RTP packet: size=" << len
626 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
627 return false;
628 }
629 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000630 res = srtp_filter_.ProtectRtcp(data, len,
631 static_cast<int>(packet->capacity()),
632 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 if (!res) {
634 int type = -1;
635 GetRtcpType(data, len, &type);
636 LOG(LS_ERROR) << "Failed to protect " << content_name_
637 << " RTCP packet: size=" << len << ", type=" << type;
638 return false;
639 }
640 }
641
642 // Update the length of the packet now that we've added the auth tag.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000643 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 } else if (secure_required_) {
645 // This is a double check for something that supposedly can't happen.
646 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
647 << " packet when SRTP is inactive and crypto is required";
648
649 ASSERT(false);
650 return false;
651 }
652
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 // Bon voyage.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000654 int ret =
stefanc1aeaf02015-10-15 07:26:07 -0700655 channel->SendPacket(packet->data<char>(), packet->size(), updated_options,
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000656 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
657 if (ret != static_cast<int>(packet->size())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 if (channel->GetError() == EWOULDBLOCK) {
659 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
deadbeefcbecd352015-09-23 11:50:27 -0700660 SetReadyToSend(rtcp, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 }
662 return false;
663 }
664 return true;
665}
666
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000667bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 // Protect ourselves against crazy data.
669 if (!ValidPacket(rtcp, packet)) {
670 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000671 << PacketType(rtcp)
672 << " packet: wrong size=" << packet->size();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 return false;
674 }
pbos482b12e2015-11-16 10:19:58 -0800675 if (rtcp) {
676 // Permit all (seemingly valid) RTCP packets.
677 return true;
678 }
679 // Check whether we handle this payload.
680 return bundle_filter_.DemuxPacket(packet->data<uint8_t>(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681}
682
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000683void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
684 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000685 if (!WantsPacket(rtcp, packet)) {
686 return;
687 }
688
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000689 // We are only interested in the first rtp packet because that
690 // indicates the media has started flowing.
691 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692 has_received_packet_ = true;
693 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
694 }
695
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696 // Unprotect the packet, if needed.
697 if (srtp_filter_.IsActive()) {
Karl Wiberg94784372015-04-20 14:03:07 +0200698 char* data = packet->data<char>();
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000699 int len = static_cast<int>(packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000700 bool res;
701 if (!rtcp) {
702 res = srtp_filter_.UnprotectRtp(data, len, &len);
703 if (!res) {
704 int seq_num = -1;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200705 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706 GetRtpSeqNum(data, len, &seq_num);
707 GetRtpSsrc(data, len, &ssrc);
708 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
709 << " RTP packet: size=" << len
710 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
711 return;
712 }
713 } else {
714 res = srtp_filter_.UnprotectRtcp(data, len, &len);
715 if (!res) {
716 int type = -1;
717 GetRtcpType(data, len, &type);
718 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
719 << " RTCP packet: size=" << len << ", type=" << type;
720 return;
721 }
722 }
723
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000724 packet->SetSize(len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000725 } else if (secure_required_) {
726 // Our session description indicates that SRTP is required, but we got a
727 // packet before our SRTP filter is active. This means either that
728 // a) we got SRTP packets before we received the SDES keys, in which case
729 // we can't decrypt it anyway, or
730 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
731 // channels, so we haven't yet extracted keys, even if DTLS did complete
732 // on the channel that the packets are being sent on. It's really good
733 // practice to wait for both RTP and RTCP to be good to go before sending
734 // media, to prevent weird failure modes, so it's fine for us to just eat
735 // packets here. This is all sidestepped if RTCP mux is used anyway.
736 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
737 << " packet when SRTP is inactive and crypto is required";
738 return;
739 }
740
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000741 // Push it down to the media channel.
742 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000743 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000745 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 }
747}
748
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000749bool BaseChannel::PushdownLocalDescription(
750 const SessionDescription* local_desc, ContentAction action,
751 std::string* error_desc) {
752 const ContentInfo* content_info = GetFirstContent(local_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 const MediaContentDescription* content_desc =
754 GetContentDescription(content_info);
755 if (content_desc && content_info && !content_info->rejected &&
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000756 !SetLocalContent(content_desc, action, error_desc)) {
757 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
758 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 }
pthatcher@webrtc.org592470b2015-03-16 21:15:37 +0000760 return true;
761}
762
763bool BaseChannel::PushdownRemoteDescription(
764 const SessionDescription* remote_desc, ContentAction action,
765 std::string* error_desc) {
766 const ContentInfo* content_info = GetFirstContent(remote_desc);
767 const MediaContentDescription* content_desc =
768 GetContentDescription(content_info);
769 if (content_desc && content_info && !content_info->rejected &&
770 !SetRemoteContent(content_desc, action, error_desc)) {
771 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
772 return false;
773 }
774 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775}
776
777void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000778 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000779 if (enabled_)
780 return;
781
782 LOG(LS_INFO) << "Channel enabled";
783 enabled_ = true;
784 ChangeState();
785}
786
787void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000788 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000789 if (!enabled_)
790 return;
791
792 LOG(LS_INFO) << "Channel disabled";
793 enabled_ = false;
794 ChangeState();
795}
796
deadbeefcbecd352015-09-23 11:50:27 -0700797void BaseChannel::UpdateWritableState_w() {
798 if (transport_channel_ && transport_channel_->writable() &&
799 (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
800 ChannelWritable_w();
801 } else {
802 ChannelNotWritable_w();
803 }
804}
805
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000807 ASSERT(worker_thread_ == rtc::Thread::Current());
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800808 if (writable_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000809 return;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800810 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000811
deadbeefcbecd352015-09-23 11:50:27 -0700812 LOG(LS_INFO) << "Channel writable (" << content_name_ << ")"
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 << (was_ever_writable_ ? "" : " for the first time");
814
815 std::vector<ConnectionInfo> infos;
816 transport_channel_->GetStats(&infos);
817 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
818 it != infos.end(); ++it) {
819 if (it->best_connection) {
820 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
821 << "->" << it->remote_candidate.ToSensitiveString();
822 break;
823 }
824 }
825
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 was_ever_writable_ = true;
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800827 MaybeSetupDtlsSrtp_w();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 writable_ = true;
829 ChangeState();
830}
831
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000832void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
833 ASSERT(worker_thread() == rtc::Thread::Current());
834 signaling_thread()->Invoke<void>(Bind(
835 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
836}
837
838void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
839 ASSERT(signaling_thread() == rtc::Thread::Current());
840 SignalDtlsSetupFailure(this, rtcp);
841}
842
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800843bool BaseChannel::SetDtlsSrtpCryptoSuites(TransportChannel* tc, bool rtcp) {
844 std::vector<int> crypto_suites;
845 // We always use the default SRTP crypto suites for RTCP, but we may use
846 // different crypto suites for RTP depending on the media type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 if (!rtcp) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800848 GetSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800850 GetDefaultSrtpCryptoSuites(&crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800852 return tc->SetSrtpCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000853}
854
855bool BaseChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800856 // Since DTLS is applied to all channels, checking RTP should be enough.
857 return transport_channel_ && transport_channel_->IsDtlsActive();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858}
859
860// This function returns true if either DTLS-SRTP is not in use
861// *or* DTLS-SRTP is successfully set up.
862bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
863 bool ret = false;
864
deadbeefcbecd352015-09-23 11:50:27 -0700865 TransportChannel* channel =
866 rtcp_channel ? rtcp_transport_channel_ : transport_channel_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800868 RTC_DCHECK(channel->IsDtlsActive());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800870 int selected_crypto_suite;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800872 if (!channel->GetSrtpCryptoSuite(&selected_crypto_suite)) {
873 LOG(LS_ERROR) << "No DTLS-SRTP selected crypto suite";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 return false;
875 }
876
877 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
878 << content_name() << " "
879 << PacketType(rtcp_channel);
880
881 // OK, we're now doing DTLS (RFC 5764)
882 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
883 SRTP_MASTER_KEY_SALT_LEN * 2);
884
885 // RFC 5705 exporter using the RFC 5764 parameters
886 if (!channel->ExportKeyingMaterial(
887 kDtlsSrtpExporterLabel,
888 NULL, 0, false,
889 &dtls_buffer[0], dtls_buffer.size())) {
890 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
891 ASSERT(false); // This should never happen
892 return false;
893 }
894
895 // Sync up the keys with the DTLS-SRTP interface
896 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
897 SRTP_MASTER_KEY_SALT_LEN);
898 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
899 SRTP_MASTER_KEY_SALT_LEN);
900 size_t offset = 0;
901 memcpy(&client_write_key[0], &dtls_buffer[offset],
902 SRTP_MASTER_KEY_KEY_LEN);
903 offset += SRTP_MASTER_KEY_KEY_LEN;
904 memcpy(&server_write_key[0], &dtls_buffer[offset],
905 SRTP_MASTER_KEY_KEY_LEN);
906 offset += SRTP_MASTER_KEY_KEY_LEN;
907 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
908 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
909 offset += SRTP_MASTER_KEY_SALT_LEN;
910 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
911 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
912
913 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000914 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000915 if (!channel->GetSslRole(&role)) {
916 LOG(LS_WARNING) << "GetSslRole failed";
917 return false;
918 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000920 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 send_key = &server_write_key;
922 recv_key = &client_write_key;
923 } else {
924 send_key = &client_write_key;
925 recv_key = &server_write_key;
926 }
927
928 if (rtcp_channel) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800929 ret = srtp_filter_.SetRtcpParams(selected_crypto_suite, &(*send_key)[0],
930 static_cast<int>(send_key->size()),
931 selected_crypto_suite, &(*recv_key)[0],
932 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 } else {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800934 ret = srtp_filter_.SetRtpParams(selected_crypto_suite, &(*send_key)[0],
935 static_cast<int>(send_key->size()),
936 selected_crypto_suite, &(*recv_key)[0],
937 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 }
939
940 if (!ret)
941 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
942 else
943 dtls_keyed_ = true;
944
945 return ret;
946}
947
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -0800948void BaseChannel::MaybeSetupDtlsSrtp_w() {
949 if (srtp_filter_.IsActive()) {
950 return;
951 }
952
953 if (!ShouldSetupDtlsSrtp()) {
954 return;
955 }
956
957 if (!SetupDtlsSrtp(false)) {
958 SignalDtlsSetupFailure_w(false);
959 return;
960 }
961
962 if (rtcp_transport_channel_) {
963 if (!SetupDtlsSrtp(true)) {
964 SignalDtlsSetupFailure_w(true);
965 return;
966 }
967 }
968}
969
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000971 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 if (!writable_)
973 return;
974
deadbeefcbecd352015-09-23 11:50:27 -0700975 LOG(LS_INFO) << "Channel not writable (" << content_name_ << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 writable_ = false;
977 ChangeState();
978}
979
Peter Thatcherc2ee2c82015-08-07 16:05:34 -0700980bool BaseChannel::SetRtpTransportParameters_w(
981 const MediaContentDescription* content,
982 ContentAction action,
983 ContentSource src,
984 std::string* error_desc) {
985 if (action == CA_UPDATE) {
986 // These parameters never get changed by a CA_UDPATE.
987 return true;
988 }
989
990 // Cache secure_required_ for belt and suspenders check on SendPacket
991 if (src == CS_LOCAL) {
992 set_secure_required(content->crypto_required() != CT_NONE);
993 }
994
995 if (!SetSrtp_w(content->cryptos(), action, src, error_desc)) {
996 return false;
997 }
998
999 if (!SetRtcpMux_w(content->rtcp_mux(), action, src, error_desc)) {
1000 return false;
1001 }
1002
1003 return true;
1004}
1005
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001006// |dtls| will be set to true if DTLS is active for transport channel and
1007// crypto is empty.
1008bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001009 bool* dtls,
1010 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001011 *dtls = transport_channel_->IsDtlsActive();
1012 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001013 SafeSetError("Cryptos must be empty when DTLS is active.",
1014 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001015 return false;
1016 }
1017 return true;
1018}
1019
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001021 ContentAction action,
1022 ContentSource src,
1023 std::string* error_desc) {
1024 if (action == CA_UPDATE) {
1025 // no crypto params.
1026 return true;
1027 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001029 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001030 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
1031 if (!ret) {
1032 return false;
1033 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 switch (action) {
1035 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001036 // If DTLS is already active on the channel, we could be renegotiating
1037 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001038 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00001039 ret = srtp_filter_.SetOffer(cryptos, src);
1040 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 break;
1042 case CA_PRANSWER:
1043 // If we're doing DTLS-SRTP, we don't want to update the filter
1044 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001045 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
1047 }
1048 break;
1049 case CA_ANSWER:
1050 // If we're doing DTLS-SRTP, we don't want to update the filter
1051 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001052 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 ret = srtp_filter_.SetAnswer(cryptos, src);
1054 }
1055 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 default:
1057 break;
1058 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001059 if (!ret) {
1060 SafeSetError("Failed to setup SRTP filter.", error_desc);
1061 return false;
1062 }
1063 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064}
1065
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001066void BaseChannel::ActivateRtcpMux() {
1067 worker_thread_->Invoke<void>(Bind(
1068 &BaseChannel::ActivateRtcpMux_w, this));
1069}
1070
1071void BaseChannel::ActivateRtcpMux_w() {
1072 if (!rtcp_mux_filter_.IsActive()) {
1073 rtcp_mux_filter_.SetActive();
guoweis46383312015-12-17 16:45:59 -08001074 set_rtcp_transport_channel(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001075 rtcp_transport_enabled_ = false;
Peter Thatcheraf55ccc2015-05-21 07:48:41 -07001076 }
1077}
1078
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001079bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001080 ContentSource src,
1081 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082 bool ret = false;
1083 switch (action) {
1084 case CA_OFFER:
1085 ret = rtcp_mux_filter_.SetOffer(enable, src);
1086 break;
1087 case CA_PRANSWER:
1088 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
1089 break;
1090 case CA_ANSWER:
1091 ret = rtcp_mux_filter_.SetAnswer(enable, src);
1092 if (ret && rtcp_mux_filter_.IsActive()) {
1093 // We activated RTCP mux, close down the RTCP transport.
deadbeefcbecd352015-09-23 11:50:27 -07001094 LOG(LS_INFO) << "Enabling rtcp-mux for " << content_name()
1095 << " by destroying RTCP transport channel for "
1096 << transport_name();
guoweis46383312015-12-17 16:45:59 -08001097 set_rtcp_transport_channel(nullptr, true);
deadbeefcbecd352015-09-23 11:50:27 -07001098 rtcp_transport_enabled_ = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 }
1100 break;
1101 case CA_UPDATE:
1102 // No RTCP mux info.
1103 ret = true;
Henrik Kjellander7c027b62015-04-22 13:21:30 +02001104 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001105 default:
1106 break;
1107 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001108 if (!ret) {
1109 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1110 return false;
1111 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001112 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1113 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1114 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001115 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001116 // If the RTP transport is already writable, then so are we.
1117 if (transport_channel_->writable()) {
1118 ChannelWritable_w();
1119 }
1120 }
1121
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001122 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123}
1124
1125bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001126 ASSERT(worker_thread() == rtc::Thread::Current());
pbos482b12e2015-11-16 10:19:58 -08001127 return media_channel()->AddRecvStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128}
1129
Peter Boström0c4e06b2015-10-07 12:23:21 +02001130bool BaseChannel::RemoveRecvStream_w(uint32_t ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001131 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132 return media_channel()->RemoveRecvStream(ssrc);
1133}
1134
1135bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001136 ContentAction action,
1137 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1139 action == CA_PRANSWER || action == CA_UPDATE))
1140 return false;
1141
1142 // If this is an update, streams only contain streams that have changed.
1143 if (action == CA_UPDATE) {
1144 for (StreamParamsVec::const_iterator it = streams.begin();
1145 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001146 const StreamParams* existing_stream =
1147 GetStreamByIds(local_streams_, it->groupid, it->id);
1148 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001149 if (media_channel()->AddSendStream(*it)) {
1150 local_streams_.push_back(*it);
1151 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1152 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001153 std::ostringstream desc;
1154 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1155 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 return false;
1157 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001158 } else if (existing_stream && !it->has_ssrcs()) {
1159 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001160 std::ostringstream desc;
1161 desc << "Failed to remove send stream with ssrc "
1162 << it->first_ssrc() << ".";
1163 SafeSetError(desc.str(), error_desc);
1164 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001165 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001166 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 } else {
1168 LOG(LS_WARNING) << "Ignore unsupported stream update";
1169 }
1170 }
1171 return true;
1172 }
1173 // Else streams are all the streams we want to send.
1174
1175 // Check for streams that have been removed.
1176 bool ret = true;
1177 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1178 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001179 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001181 std::ostringstream desc;
1182 desc << "Failed to remove send stream with ssrc "
1183 << it->first_ssrc() << ".";
1184 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001185 ret = false;
1186 }
1187 }
1188 }
1189 // Check for new streams.
1190 for (StreamParamsVec::const_iterator it = streams.begin();
1191 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001192 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 if (media_channel()->AddSendStream(*it)) {
stefanc1aeaf02015-10-15 07:26:07 -07001194 LOG(LS_INFO) << "Add send stream ssrc: " << it->ssrcs[0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001196 std::ostringstream desc;
1197 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1198 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 ret = false;
1200 }
1201 }
1202 }
1203 local_streams_ = streams;
1204 return ret;
1205}
1206
1207bool BaseChannel::UpdateRemoteStreams_w(
1208 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001209 ContentAction action,
1210 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1212 action == CA_PRANSWER || action == CA_UPDATE))
1213 return false;
1214
1215 // If this is an update, streams only contain streams that have changed.
1216 if (action == CA_UPDATE) {
1217 for (StreamParamsVec::const_iterator it = streams.begin();
1218 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001219 const StreamParams* existing_stream =
1220 GetStreamByIds(remote_streams_, it->groupid, it->id);
1221 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 if (AddRecvStream_w(*it)) {
1223 remote_streams_.push_back(*it);
1224 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1225 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001226 std::ostringstream desc;
1227 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1228 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229 return false;
1230 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001231 } else if (existing_stream && !it->has_ssrcs()) {
1232 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001233 std::ostringstream desc;
1234 desc << "Failed to remove remote stream with ssrc "
1235 << it->first_ssrc() << ".";
1236 SafeSetError(desc.str(), error_desc);
1237 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001239 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 } else {
1241 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001242 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 << " new stream = " << it->ToString();
1244 }
1245 }
1246 return true;
1247 }
1248 // Else streams are all the streams we want to receive.
1249
1250 // Check for streams that have been removed.
1251 bool ret = true;
1252 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1253 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001254 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001256 std::ostringstream desc;
1257 desc << "Failed to remove remote stream with ssrc "
1258 << it->first_ssrc() << ".";
1259 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001260 ret = false;
1261 }
1262 }
1263 }
1264 // Check for new streams.
1265 for (StreamParamsVec::const_iterator it = streams.begin();
1266 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001267 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 if (AddRecvStream_w(*it)) {
1269 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1270 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001271 std::ostringstream desc;
1272 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1273 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274 ret = false;
1275 }
1276 }
1277 }
1278 remote_streams_ = streams;
1279 return ret;
1280}
1281
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001282void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1283 const std::vector<RtpHeaderExtension>& extensions) {
1284 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001285 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001286 rtp_abs_sendtime_extn_id_ =
1287 send_time_extension ? send_time_extension->id : -1;
1288}
1289
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001290void BaseChannel::OnMessage(rtc::Message *pmsg) {
Peter Boström6f28cf02015-12-07 23:17:15 +01001291 TRACE_EVENT0("webrtc", "BaseChannel::OnMessage");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001292 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293 case MSG_RTPPACKET:
1294 case MSG_RTCPPACKET: {
1295 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
stefanc1aeaf02015-10-15 07:26:07 -07001296 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet,
1297 data->options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 delete data; // because it is Posted
1299 break;
1300 }
1301 case MSG_FIRSTPACKETRECEIVED: {
1302 SignalFirstPacketReceived(this);
1303 break;
1304 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305 }
1306}
1307
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308void BaseChannel::FlushRtcpMessages() {
1309 // Flush all remaining RTCP messages. This should only be called in
1310 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001311 ASSERT(rtc::Thread::Current() == worker_thread_);
1312 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001313 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001314 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001316 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 }
1318}
1319
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001320VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 MediaEngineInterface* media_engine,
1322 VoiceMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001323 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324 const std::string& content_name,
1325 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001326 : BaseChannel(thread,
1327 media_channel,
1328 transport_controller,
1329 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330 rtcp),
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001331 media_engine_(media_engine),
deadbeefcbecd352015-09-23 11:50:27 -07001332 received_media_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333
1334VoiceChannel::~VoiceChannel() {
1335 StopAudioMonitor();
1336 StopMediaMonitor();
1337 // this can't be done in the base class, since it calls a virtual
1338 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001339 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340}
1341
1342bool VoiceChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001343 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344 return false;
1345 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346 return true;
1347}
1348
Peter Boström0c4e06b2015-10-07 12:23:21 +02001349bool VoiceChannel::SetAudioSend(uint32_t ssrc,
solenbergdfc8f4f2015-10-01 02:31:10 -07001350 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001351 const AudioOptions* options,
1352 AudioRenderer* renderer) {
deadbeefcbecd352015-09-23 11:50:27 -07001353 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetAudioSend, media_channel(),
solenbergdfc8f4f2015-10-01 02:31:10 -07001354 ssrc, enable, options, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001355}
1356
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001357// TODO(juberti): Handle early media the right way. We should get an explicit
1358// ringing message telling us to start playing local ringback, which we cancel
1359// if any early media actually arrives. For now, we do the opposite, which is
1360// to wait 1 second for early media, and start playing local ringback if none
1361// arrives.
1362void VoiceChannel::SetEarlyMedia(bool enable) {
1363 if (enable) {
1364 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001365 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1366 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001367 } else {
1368 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001369 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001370 }
1371}
1372
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001373bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001374 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1375 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376}
1377
Peter Boström0c4e06b2015-10-07 12:23:21 +02001378bool VoiceChannel::InsertDtmf(uint32_t ssrc,
1379 int event_code,
solenberg1d63dd02015-12-02 12:35:09 -08001380 int duration) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001381 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
solenberg1d63dd02015-12-02 12:35:09 -08001382 ssrc, event_code, duration));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383}
1384
solenberg4bac9c52015-10-09 02:32:53 -07001385bool VoiceChannel::SetOutputVolume(uint32_t ssrc, double volume) {
1386 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputVolume,
1387 media_channel(), ssrc, volume));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001389
Tommif888bb52015-12-12 01:37:01 +01001390void VoiceChannel::SetRawAudioSink(
1391 uint32_t ssrc,
deadbeefe591f932016-01-12 16:44:59 -08001392 const rtc::scoped_refptr<webrtc::AudioSinkInterface>& sink) {
1393 worker_thread()->Invoke<void>(
1394 Bind(&VoiceMediaChannel::SetRawAudioSink, media_channel(), ssrc, sink));
Tommif888bb52015-12-12 01:37:01 +01001395}
1396
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001398 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1399 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001400}
1401
1402void VoiceChannel::StartMediaMonitor(int cms) {
1403 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001404 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001405 media_monitor_->SignalUpdate.connect(
1406 this, &VoiceChannel::OnMediaMonitorUpdate);
1407 media_monitor_->Start(cms);
1408}
1409
1410void VoiceChannel::StopMediaMonitor() {
1411 if (media_monitor_) {
1412 media_monitor_->Stop();
1413 media_monitor_->SignalUpdate.disconnect(this);
1414 media_monitor_.reset();
1415 }
1416}
1417
1418void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001419 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001420 audio_monitor_
1421 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1422 audio_monitor_->Start(cms);
1423}
1424
1425void VoiceChannel::StopAudioMonitor() {
1426 if (audio_monitor_) {
1427 audio_monitor_->Stop();
1428 audio_monitor_.reset();
1429 }
1430}
1431
1432bool VoiceChannel::IsAudioMonitorRunning() const {
1433 return (audio_monitor_.get() != NULL);
1434}
1435
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436int VoiceChannel::GetInputLevel_w() {
Fredrik Solenberg0c022642015-08-05 12:25:22 +02001437 return media_engine_->GetInputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001438}
1439
1440int VoiceChannel::GetOutputLevel_w() {
1441 return media_channel()->GetOutputLevel();
1442}
1443
1444void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1445 media_channel()->GetActiveStreams(actives);
1446}
1447
1448void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001449 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001450 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001451 int flags) {
1452 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001453
1454 // Set a flag when we've received an RTP packet. If we're waiting for early
1455 // media, this will disable the timeout.
1456 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1457 received_media_ = true;
1458 }
1459}
1460
1461void VoiceChannel::ChangeState() {
1462 // Render incoming data if we're the active call, and we have the local
1463 // content. We receive data on the default channel and multiplexed streams.
1464 bool recv = IsReadyToReceive();
solenberg5b14b422015-10-01 04:10:31 -07001465 media_channel()->SetPlayout(recv);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466
1467 // Send outgoing data if we're the active call, we have the remote content,
1468 // and we have had some form of connectivity.
1469 bool send = IsReadyToSend();
1470 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1471 if (!media_channel()->SetSend(send_flag)) {
1472 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473 }
1474
1475 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1476}
1477
1478const ContentInfo* VoiceChannel::GetFirstContent(
1479 const SessionDescription* sdesc) {
1480 return GetFirstAudioContent(sdesc);
1481}
1482
1483bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001484 ContentAction action,
1485 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001486 TRACE_EVENT0("webrtc", "VoiceChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001487 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 LOG(LS_INFO) << "Setting local voice description";
1489
1490 const AudioContentDescription* audio =
1491 static_cast<const AudioContentDescription*>(content);
1492 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001493 if (!audio) {
1494 SafeSetError("Can't find audio content in local description.", error_desc);
1495 return false;
1496 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001498 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1499 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 }
1501
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001502 AudioRecvParameters recv_params = last_recv_params_;
1503 RtpParametersFromMediaDescription(audio, &recv_params);
1504 if (!media_channel()->SetRecvParameters(recv_params)) {
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001505 SafeSetError("Failed to set local audio description recv parameters.",
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001506 error_desc);
1507 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001509 for (const AudioCodec& codec : audio->codecs()) {
1510 bundle_filter()->AddPayloadType(codec.id);
1511 }
1512 last_recv_params_ = recv_params;
1513
1514 // TODO(pthatcher): Move local streams into AudioSendParameters, and
1515 // only give it to the media channel once we have a remote
1516 // description too (without a remote description, we won't be able
1517 // to send them anyway).
1518 if (!UpdateLocalStreams_w(audio->streams(), action, error_desc)) {
1519 SafeSetError("Failed to set local audio description streams.", error_desc);
1520 return false;
1521 }
1522
1523 set_local_content_direction(content->direction());
1524 ChangeState();
1525 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526}
1527
1528bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001529 ContentAction action,
1530 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001531 TRACE_EVENT0("webrtc", "VoiceChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001532 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533 LOG(LS_INFO) << "Setting remote voice description";
1534
1535 const AudioContentDescription* audio =
1536 static_cast<const AudioContentDescription*>(content);
1537 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001538 if (!audio) {
1539 SafeSetError("Can't find audio content in remote description.", error_desc);
1540 return false;
1541 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001543 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1544 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 }
1546
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001547 AudioSendParameters send_params = last_send_params_;
1548 RtpSendParametersFromMediaDescription(audio, &send_params);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001549 if (audio->agc_minus_10db()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001550 send_params.options.adjust_agc_delta = rtc::Optional<int>(kAgcMinus10db);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001551 }
1552 if (!media_channel()->SetSendParameters(send_params)) {
1553 SafeSetError("Failed to set remote audio description send parameters.",
1554 error_desc);
1555 return false;
1556 }
1557 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001559 // TODO(pthatcher): Move remote streams into AudioRecvParameters,
1560 // and only give it to the media channel once we have a local
1561 // description too (without a local description, we won't be able to
1562 // recv them anyway).
1563 if (!UpdateRemoteStreams_w(audio->streams(), action, error_desc)) {
1564 SafeSetError("Failed to set remote audio description streams.", error_desc);
1565 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 }
1567
Peter Thatcherbfab5cb2015-08-20 17:40:24 -07001568 if (audio->rtp_header_extensions_set()) {
1569 MaybeCacheRtpAbsSendTimeHeaderExtension(audio->rtp_header_extensions());
1570 }
1571
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001572 set_remote_content_direction(content->direction());
1573 ChangeState();
1574 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001575}
1576
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577void VoiceChannel::HandleEarlyMediaTimeout() {
1578 // This occurs on the main thread, not the worker thread.
1579 if (!received_media_) {
1580 LOG(LS_INFO) << "No early media received before timeout";
1581 SignalEarlyMediaTimeout(this);
1582 }
1583}
1584
Peter Boström0c4e06b2015-10-07 12:23:21 +02001585bool VoiceChannel::InsertDtmf_w(uint32_t ssrc,
1586 int event,
solenberg1d63dd02015-12-02 12:35:09 -08001587 int duration) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588 if (!enabled()) {
1589 return false;
1590 }
solenberg1d63dd02015-12-02 12:35:09 -08001591 return media_channel()->InsertDtmf(ssrc, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592}
1593
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001594void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001595 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 case MSG_EARLYMEDIATIMEOUT:
1597 HandleEarlyMediaTimeout();
1598 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 case MSG_CHANNEL_ERROR: {
1600 VoiceChannelErrorMessageData* data =
1601 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001602 delete data;
1603 break;
1604 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 default:
1606 BaseChannel::OnMessage(pmsg);
1607 break;
1608 }
1609}
1610
1611void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001612 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613 SignalConnectionMonitor(this, infos);
1614}
1615
1616void VoiceChannel::OnMediaMonitorUpdate(
1617 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1618 ASSERT(media_channel == this->media_channel());
1619 SignalMediaMonitor(this, info);
1620}
1621
1622void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1623 const AudioInfo& info) {
1624 SignalAudioMonitor(this, info);
1625}
1626
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001627void VoiceChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1628 GetSupportedAudioCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001629}
1630
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001631VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 VideoMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001633 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 const std::string& content_name,
Fredrik Solenberg7fb711f2015-04-22 15:30:51 +02001635 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07001636 : BaseChannel(thread,
1637 media_channel,
1638 transport_controller,
1639 content_name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 rtcp),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001641 renderer_(NULL),
deadbeefcbecd352015-09-23 11:50:27 -07001642 previous_we_(rtc::WE_CLOSE) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643
1644bool VideoChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00001645 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646 return false;
1647 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 return true;
1649}
1650
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651VideoChannel::~VideoChannel() {
Peter Boström0c4e06b2015-10-07 12:23:21 +02001652 std::vector<uint32_t> screencast_ssrcs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 ScreencastMap::iterator iter;
1654 while (!screencast_capturers_.empty()) {
1655 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1656 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1657 << screencast_capturers_.begin()->first;
1658 ASSERT(false);
1659 break;
1660 }
1661 }
1662
1663 StopMediaMonitor();
1664 // this can't be done in the base class, since it calls a virtual
1665 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001666
1667 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668}
1669
Peter Boström0c4e06b2015-10-07 12:23:21 +02001670bool VideoChannel::SetRenderer(uint32_t ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001671 worker_thread()->Invoke<void>(Bind(
1672 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673 return true;
1674}
1675
1676bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001677 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678}
1679
Peter Boström0c4e06b2015-10-07 12:23:21 +02001680bool VideoChannel::AddScreencast(uint32_t ssrc, VideoCapturer* capturer) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001681 return worker_thread()->Invoke<bool>(Bind(
1682 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683}
1684
Peter Boström0c4e06b2015-10-07 12:23:21 +02001685bool VideoChannel::SetCapturer(uint32_t ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001686 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1687 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688}
1689
Peter Boström0c4e06b2015-10-07 12:23:21 +02001690bool VideoChannel::RemoveScreencast(uint32_t ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001691 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001692}
1693
1694bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001695 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001696}
1697
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001698bool 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
Peter Boström0c4e06b2015-10-07 12:23:21 +02001710bool VideoChannel::SetVideoSend(uint32_t ssrc,
deadbeefcbecd352015-09-23 11:50:27 -07001711 bool mute,
solenberg1dd98f32015-09-10 01:57:14 -07001712 const VideoOptions* options) {
deadbeefcbecd352015-09-23 11:50:27 -07001713 return InvokeOnWorker(Bind(&VideoMediaChannel::SetVideoSend, media_channel(),
1714 ssrc, mute, options));
solenberg1dd98f32015-09-10 01:57:14 -07001715}
1716
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717void VideoChannel::ChangeState() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 // Send outgoing data if we're the active call, we have the remote content,
1719 // and we have had some form of connectivity.
1720 bool send = IsReadyToSend();
1721 if (!media_channel()->SetSend(send)) {
1722 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1723 // TODO(gangji): Report error back to server.
1724 }
1725
Peter Boström34fbfff2015-09-24 19:20:30 +02001726 LOG(LS_INFO) << "Changing video state, send=" << send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727}
1728
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001729bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1730 return InvokeOnWorker(
1731 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732}
1733
1734void VideoChannel::StartMediaMonitor(int cms) {
1735 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001736 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 media_monitor_->SignalUpdate.connect(
1738 this, &VideoChannel::OnMediaMonitorUpdate);
1739 media_monitor_->Start(cms);
1740}
1741
1742void VideoChannel::StopMediaMonitor() {
1743 if (media_monitor_) {
1744 media_monitor_->Stop();
1745 media_monitor_.reset();
1746 }
1747}
1748
1749const ContentInfo* VideoChannel::GetFirstContent(
1750 const SessionDescription* sdesc) {
1751 return GetFirstVideoContent(sdesc);
1752}
1753
1754bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001755 ContentAction action,
1756 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001757 TRACE_EVENT0("webrtc", "VideoChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001758 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 LOG(LS_INFO) << "Setting local video description";
1760
1761 const VideoContentDescription* video =
1762 static_cast<const VideoContentDescription*>(content);
1763 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001764 if (!video) {
1765 SafeSetError("Can't find video content in local description.", error_desc);
1766 return false;
1767 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001769 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
1770 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 }
1772
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001773 VideoRecvParameters recv_params = last_recv_params_;
1774 RtpParametersFromMediaDescription(video, &recv_params);
1775 if (!media_channel()->SetRecvParameters(recv_params)) {
1776 SafeSetError("Failed to set local video description recv parameters.",
1777 error_desc);
1778 return false;
1779 }
1780 for (const VideoCodec& codec : video->codecs()) {
1781 bundle_filter()->AddPayloadType(codec.id);
1782 }
1783 last_recv_params_ = recv_params;
1784
1785 // TODO(pthatcher): Move local streams into VideoSendParameters, and
1786 // only give it to the media channel once we have a remote
1787 // description too (without a remote description, we won't be able
1788 // to send them anyway).
1789 if (!UpdateLocalStreams_w(video->streams(), action, error_desc)) {
1790 SafeSetError("Failed to set local video description streams.", error_desc);
1791 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792 }
1793
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001794 set_local_content_direction(content->direction());
1795 ChangeState();
1796 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797}
1798
1799bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001800 ContentAction action,
1801 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01001802 TRACE_EVENT0("webrtc", "VideoChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001803 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 LOG(LS_INFO) << "Setting remote video description";
1805
1806 const VideoContentDescription* video =
1807 static_cast<const VideoContentDescription*>(content);
1808 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001809 if (!video) {
1810 SafeSetError("Can't find video content in remote description.", error_desc);
1811 return false;
1812 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001814
1815 if (!SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
1816 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 }
1818
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001819 VideoSendParameters send_params = last_send_params_;
1820 RtpSendParametersFromMediaDescription(video, &send_params);
1821 if (video->conference_mode()) {
Karl Wibergbe579832015-11-10 22:34:18 +01001822 send_params.options.conference_mode = rtc::Optional<bool>(true);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001823 }
1824 if (!media_channel()->SetSendParameters(send_params)) {
1825 SafeSetError("Failed to set remote video description send parameters.",
1826 error_desc);
1827 return false;
1828 }
1829 last_send_params_ = send_params;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001831 // TODO(pthatcher): Move remote streams into VideoRecvParameters,
1832 // and only give it to the media channel once we have a local
1833 // description too (without a local description, we won't be able to
1834 // recv them anyway).
1835 if (!UpdateRemoteStreams_w(video->streams(), action, error_desc)) {
1836 SafeSetError("Failed to set remote video description streams.", error_desc);
1837 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001838 }
1839
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001840 if (video->rtp_header_extensions_set()) {
1841 MaybeCacheRtpAbsSendTimeHeaderExtension(video->rtp_header_extensions());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001843
1844 set_remote_content_direction(content->direction());
1845 ChangeState();
1846 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847}
1848
1849bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1850 bool ret = true;
1851 // Set the send format for each of the local streams. If the view request
1852 // does not contain a local stream, set its send format to 0x0, which will
1853 // drop all frames.
1854 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1855 it != local_streams().end(); ++it) {
1856 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1857 StaticVideoViews::const_iterator view;
1858 for (view = request.static_video_views.begin();
1859 view != request.static_video_views.end(); ++view) {
1860 if (view->selector.Matches(*it)) {
1861 format.width = view->width;
1862 format.height = view->height;
1863 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1864 break;
1865 }
1866 }
1867
1868 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1869 }
1870
1871 // Check if the view request has invalid streams.
1872 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1873 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001874 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 LOG(LS_WARNING) << "View request for ("
1876 << it->selector.ssrc << ", '"
1877 << it->selector.groupid << "', '"
1878 << it->selector.streamid << "'"
1879 << ") is not in the local streams.";
1880 }
1881 }
1882
1883 return ret;
1884}
1885
Peter Boström0c4e06b2015-10-07 12:23:21 +02001886bool VideoChannel::AddScreencast_w(uint32_t ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001887 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001888 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001890 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1891 screencast_capturers_[ssrc] = capturer;
1892 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893}
1894
Peter Boström0c4e06b2015-10-07 12:23:21 +02001895bool VideoChannel::RemoveScreencast_w(uint32_t ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1897 if (iter == screencast_capturers_.end()) {
1898 return false;
1899 }
1900 // Clean up VideoCapturer.
1901 delete iter->second;
1902 screencast_capturers_.erase(iter);
1903 return true;
1904}
1905
1906bool VideoChannel::IsScreencasting_w() const {
1907 return !screencast_capturers_.empty();
1908}
1909
Peter Boström0c4e06b2015-10-07 12:23:21 +02001910void VideoChannel::OnScreencastWindowEvent_s(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001911 rtc::WindowEvent we) {
1912 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 SignalScreencastWindowEvent(ssrc, we);
1914}
1915
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001916void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 case MSG_SCREENCASTWINDOWEVENT: {
1919 const ScreencastEventMessageData* data =
1920 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1921 OnScreencastWindowEvent_s(data->ssrc, data->event);
1922 delete data;
1923 break;
1924 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001925 case MSG_CHANNEL_ERROR: {
1926 const VideoChannelErrorMessageData* data =
1927 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928 delete data;
1929 break;
1930 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931 default:
1932 BaseChannel::OnMessage(pmsg);
1933 break;
1934 }
1935}
1936
1937void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001938 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 SignalConnectionMonitor(this, infos);
1940}
1941
1942// TODO(pthatcher): Look into removing duplicate code between
1943// audio, video, and data, perhaps by using templates.
1944void VideoChannel::OnMediaMonitorUpdate(
1945 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
1946 ASSERT(media_channel == this->media_channel());
1947 SignalMediaMonitor(this, info);
1948}
1949
Peter Boström0c4e06b2015-10-07 12:23:21 +02001950void VideoChannel::OnScreencastWindowEvent(uint32_t ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001951 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 ScreencastEventMessageData* pdata =
1953 new ScreencastEventMessageData(ssrc, event);
1954 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
1955}
1956
1957void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
1958 // Map capturer events to window events. In the future we may want to simply
1959 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001960 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001962 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001964 we = rtc::WE_MINIMIZE;
1965 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
1966 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 } else {
1968 return;
1969 }
1970 previous_we_ = we;
1971
Peter Boström0c4e06b2015-10-07 12:23:21 +02001972 uint32_t ssrc = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973 if (!GetLocalSsrc(capturer, &ssrc)) {
1974 return;
1975 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001976
1977 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978}
1979
Peter Boström0c4e06b2015-10-07 12:23:21 +02001980bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32_t* ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 *ssrc = 0;
1982 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
1983 iter != screencast_capturers_.end(); ++iter) {
1984 if (iter->second == capturer) {
1985 *ssrc = iter->first;
1986 return true;
1987 }
1988 }
1989 return false;
1990}
1991
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001992void VideoChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
1993 GetSupportedVideoCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994}
1995
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001996DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997 DataMediaChannel* media_channel,
deadbeefcbecd352015-09-23 11:50:27 -07001998 TransportController* transport_controller,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 const std::string& content_name,
2000 bool rtcp)
deadbeefcbecd352015-09-23 11:50:27 -07002001 : BaseChannel(thread,
2002 media_channel,
2003 transport_controller,
2004 content_name,
2005 rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002006 data_channel_type_(cricket::DCT_NONE),
deadbeefcbecd352015-09-23 11:50:27 -07002007 ready_to_send_data_(false) {}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008
2009DataChannel::~DataChannel() {
2010 StopMediaMonitor();
2011 // this can't be done in the base class, since it calls a virtual
2012 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002013
2014 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015}
2016
2017bool DataChannel::Init() {
pthatcher@webrtc.org6ad507a2015-03-16 20:19:12 +00002018 if (!BaseChannel::Init()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019 return false;
2020 }
2021 media_channel()->SignalDataReceived.connect(
2022 this, &DataChannel::OnDataReceived);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002023 media_channel()->SignalReadyToSend.connect(
2024 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002025 media_channel()->SignalStreamClosedRemotely.connect(
2026 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027 return true;
2028}
2029
2030bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002031 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002033 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2034 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035}
2036
2037const ContentInfo* DataChannel::GetFirstContent(
2038 const SessionDescription* sdesc) {
2039 return GetFirstDataContent(sdesc);
2040}
2041
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002042bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002043 if (data_channel_type_ == DCT_SCTP) {
2044 // TODO(pthatcher): Do this in a more robust way by checking for
2045 // SCTP or DTLS.
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002046 return !IsRtpPacket(packet->data(), packet->size());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047 } else if (data_channel_type_ == DCT_RTP) {
2048 return BaseChannel::WantsPacket(rtcp, packet);
2049 }
2050 return false;
2051}
2052
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002053bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2054 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055 // It hasn't been set before, so set it now.
2056 if (data_channel_type_ == DCT_NONE) {
2057 data_channel_type_ = new_data_channel_type;
2058 return true;
2059 }
2060
2061 // It's been set before, but doesn't match. That's bad.
2062 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002063 std::ostringstream desc;
2064 desc << "Data channel type mismatch."
2065 << " Expected " << data_channel_type_
2066 << " Got " << new_data_channel_type;
2067 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 return false;
2069 }
2070
2071 // It's hasn't changed. Nothing to do.
2072 return true;
2073}
2074
2075bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002076 const DataContentDescription* content,
2077 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2079 (content->protocol() == kMediaProtocolDtlsSctp));
2080 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002081 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082}
2083
2084bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002085 ContentAction action,
2086 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002087 TRACE_EVENT0("webrtc", "DataChannel::SetLocalContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002088 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089 LOG(LS_INFO) << "Setting local data description";
2090
2091 const DataContentDescription* data =
2092 static_cast<const DataContentDescription*>(content);
2093 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002094 if (!data) {
2095 SafeSetError("Can't find data content in local description.", error_desc);
2096 return false;
2097 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002099 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 return false;
2101 }
2102
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002103 if (data_channel_type_ == DCT_RTP) {
2104 if (!SetRtpTransportParameters_w(content, action, CS_LOCAL, error_desc)) {
2105 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 }
2107 }
2108
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002109 // FYI: We send the SCTP port number (not to be confused with the
2110 // underlying UDP port number) as a codec parameter. So even SCTP
2111 // data channels need codecs.
2112 DataRecvParameters recv_params = last_recv_params_;
2113 RtpParametersFromMediaDescription(data, &recv_params);
2114 if (!media_channel()->SetRecvParameters(recv_params)) {
2115 SafeSetError("Failed to set remote data description recv parameters.",
2116 error_desc);
2117 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002119 if (data_channel_type_ == DCT_RTP) {
2120 for (const DataCodec& codec : data->codecs()) {
2121 bundle_filter()->AddPayloadType(codec.id);
2122 }
2123 }
2124 last_recv_params_ = recv_params;
2125
2126 // TODO(pthatcher): Move local streams into DataSendParameters, and
2127 // only give it to the media channel once we have a remote
2128 // description too (without a remote description, we won't be able
2129 // to send them anyway).
2130 if (!UpdateLocalStreams_w(data->streams(), action, error_desc)) {
2131 SafeSetError("Failed to set local data description streams.", error_desc);
2132 return false;
2133 }
2134
2135 set_local_content_direction(content->direction());
2136 ChangeState();
2137 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138}
2139
2140bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002141 ContentAction action,
2142 std::string* error_desc) {
Peter Boström9f45a452015-12-08 13:25:57 +01002143 TRACE_EVENT0("webrtc", "DataChannel::SetRemoteContent_w");
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002144 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145
2146 const DataContentDescription* data =
2147 static_cast<const DataContentDescription*>(content);
2148 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002149 if (!data) {
2150 SafeSetError("Can't find data content in remote description.", error_desc);
2151 return false;
2152 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002154 // If the remote data doesn't have codecs and isn't an update, it
2155 // must be empty, so ignore it.
2156 if (!data->has_codecs() && action != CA_UPDATE) {
2157 return true;
2158 }
2159
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002160 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161 return false;
2162 }
2163
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002164 LOG(LS_INFO) << "Setting remote data description";
2165 if (data_channel_type_ == DCT_RTP &&
2166 !SetRtpTransportParameters_w(content, action, CS_REMOTE, error_desc)) {
2167 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168 }
2169
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002170
2171 DataSendParameters send_params = last_send_params_;
2172 RtpSendParametersFromMediaDescription<DataCodec>(data, &send_params);
2173 if (!media_channel()->SetSendParameters(send_params)) {
2174 SafeSetError("Failed to set remote data description send parameters.",
2175 error_desc);
2176 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177 }
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07002178 last_send_params_ = send_params;
2179
2180 // TODO(pthatcher): Move remote streams into DataRecvParameters,
2181 // and only give it to the media channel once we have a local
2182 // description too (without a local description, we won't be able to
2183 // recv them anyway).
2184 if (!UpdateRemoteStreams_w(data->streams(), action, error_desc)) {
2185 SafeSetError("Failed to set remote data description streams.",
2186 error_desc);
2187 return false;
2188 }
2189
2190 set_remote_content_direction(content->direction());
2191 ChangeState();
2192 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193}
2194
2195void DataChannel::ChangeState() {
2196 // Render incoming data if we're the active call, and we have the local
2197 // content. We receive data on the default channel and multiplexed streams.
2198 bool recv = IsReadyToReceive();
2199 if (!media_channel()->SetReceive(recv)) {
2200 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2201 }
2202
2203 // Send outgoing data if we're the active call, we have the remote content,
2204 // and we have had some form of connectivity.
2205 bool send = IsReadyToSend();
2206 if (!media_channel()->SetSend(send)) {
2207 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2208 }
2209
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002210 // Trigger SignalReadyToSendData asynchronously.
2211 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212
2213 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2214}
2215
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002216void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 switch (pmsg->message_id) {
2218 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002219 DataChannelReadyToSendMessageData* data =
2220 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002221 ready_to_send_data_ = data->data();
2222 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002223 delete data;
2224 break;
2225 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226 case MSG_DATARECEIVED: {
2227 DataReceivedMessageData* data =
2228 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2229 SignalDataReceived(this, data->params, data->payload);
2230 delete data;
2231 break;
2232 }
2233 case MSG_CHANNEL_ERROR: {
2234 const DataChannelErrorMessageData* data =
2235 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 delete data;
2237 break;
2238 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002239 case MSG_STREAMCLOSEDREMOTELY: {
Peter Boström0c4e06b2015-10-07 12:23:21 +02002240 rtc::TypedMessageData<uint32_t>* data =
2241 static_cast<rtc::TypedMessageData<uint32_t>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002242 SignalStreamClosedRemotely(data->data());
2243 delete data;
2244 break;
2245 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002246 default:
2247 BaseChannel::OnMessage(pmsg);
2248 break;
2249 }
2250}
2251
2252void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002253 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002254 SignalConnectionMonitor(this, infos);
2255}
2256
2257void DataChannel::StartMediaMonitor(int cms) {
2258 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002259 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002260 media_monitor_->SignalUpdate.connect(
2261 this, &DataChannel::OnMediaMonitorUpdate);
2262 media_monitor_->Start(cms);
2263}
2264
2265void DataChannel::StopMediaMonitor() {
2266 if (media_monitor_) {
2267 media_monitor_->Stop();
2268 media_monitor_->SignalUpdate.disconnect(this);
2269 media_monitor_.reset();
2270 }
2271}
2272
2273void DataChannel::OnMediaMonitorUpdate(
2274 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2275 ASSERT(media_channel == this->media_channel());
2276 SignalMediaMonitor(this, info);
2277}
2278
2279void DataChannel::OnDataReceived(
2280 const ReceiveDataParams& params, const char* data, size_t len) {
2281 DataReceivedMessageData* msg = new DataReceivedMessageData(
2282 params, data, len);
2283 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2284}
2285
Peter Boström0c4e06b2015-10-07 12:23:21 +02002286void DataChannel::OnDataChannelError(uint32_t ssrc,
2287 DataMediaChannel::Error err) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2289 ssrc, err);
2290 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2291}
2292
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002293void DataChannel::OnDataChannelReadyToSend(bool writable) {
2294 // This is usded for congestion control to indicate that the stream is ready
2295 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2296 // that the transport channel is ready.
2297 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2298 new DataChannelReadyToSendMessageData(writable));
2299}
2300
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08002301void DataChannel::GetSrtpCryptoSuites(std::vector<int>* crypto_suites) const {
2302 GetSupportedDataCryptoSuites(crypto_suites);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002303}
2304
2305bool DataChannel::ShouldSetupDtlsSrtp() const {
Guo-wei Shieh1218d7a2015-12-05 09:59:56 -08002306 return (data_channel_type_ == DCT_RTP) && BaseChannel::ShouldSetupDtlsSrtp();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002307}
2308
Peter Boström0c4e06b2015-10-07 12:23:21 +02002309void DataChannel::OnStreamClosedRemotely(uint32_t sid) {
2310 rtc::TypedMessageData<uint32_t>* message =
2311 new rtc::TypedMessageData<uint32_t>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002312 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2313}
2314
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315} // namespace cricket