blob: 089ed48d1361b96a85e5919fb9b79f04f3337b6b [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/session/media/channel.h"
29
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000030#include "talk/media/base/constants.h"
31#include "talk/media/base/rtputils.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000032#include "webrtc/p2p/base/transportchannel.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000033#include "talk/session/media/channelmanager.h"
buildbot@webrtc.org5b1ebac2014-08-07 17:18:00 +000034#include "talk/session/media/typingmonitor.h"
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +000035#include "webrtc/base/bind.h"
36#include "webrtc/base/buffer.h"
37#include "webrtc/base/byteorder.h"
38#include "webrtc/base/common.h"
39#include "webrtc/base/dscp.h"
40#include "webrtc/base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
42namespace cricket {
43
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000044using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000045
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000047 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048 MSG_SCREENCASTWINDOWEVENT,
49 MSG_RTPPACKET,
50 MSG_RTCPPACKET,
51 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000055 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056};
57
58// Value specified in RFC 5764.
59static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
60
61static const int kAgcMinus10db = -10;
62
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000063static void SetSessionError(BaseSession* session, BaseSession::Error error,
64 const std::string& error_desc) {
65 session->SetError(error, error_desc);
66}
67
68static void SafeSetError(const std::string& message, std::string* error_desc) {
69 if (error_desc) {
70 *error_desc = message;
71 }
72}
73
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000074struct PacketMessageData : public rtc::MessageData {
75 rtc::Buffer packet;
76 rtc::DiffServCodePoint dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077};
78
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000079struct ScreencastEventMessageData : public rtc::MessageData {
80 ScreencastEventMessageData(uint32 s, rtc::WindowEvent we)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081 : ssrc(s),
82 event(we) {
83 }
84 uint32 ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000085 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086};
87
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000088struct VoiceChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089 VoiceChannelErrorMessageData(uint32 in_ssrc,
90 VoiceMediaChannel::Error in_error)
91 : ssrc(in_ssrc),
92 error(in_error) {
93 }
94 uint32 ssrc;
95 VoiceMediaChannel::Error error;
96};
97
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000098struct VideoChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 VideoChannelErrorMessageData(uint32 in_ssrc,
100 VideoMediaChannel::Error in_error)
101 : ssrc(in_ssrc),
102 error(in_error) {
103 }
104 uint32 ssrc;
105 VideoMediaChannel::Error error;
106};
107
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000108struct DataChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 DataChannelErrorMessageData(uint32 in_ssrc,
110 DataMediaChannel::Error in_error)
111 : ssrc(in_ssrc),
112 error(in_error) {}
113 uint32 ssrc;
114 DataMediaChannel::Error error;
115};
116
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000118struct VideoChannel::ScreencastDetailsData {
119 explicit ScreencastDetailsData(uint32 s)
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000120 : ssrc(s), fps(0), screencast_max_pixels(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 }
122 uint32 ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000123 int fps;
124 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125};
126
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127static const char* PacketType(bool rtcp) {
128 return (!rtcp) ? "RTP" : "RTCP";
129}
130
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000131static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 // Check the packet size. We could check the header too if needed.
133 return (packet &&
134 packet->length() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
135 packet->length() <= kMaxRtpPacketLen);
136}
137
138static bool IsReceiveContentDirection(MediaContentDirection direction) {
139 return direction == MD_SENDRECV || direction == MD_RECVONLY;
140}
141
142static bool IsSendContentDirection(MediaContentDirection direction) {
143 return direction == MD_SENDRECV || direction == MD_SENDONLY;
144}
145
146static const MediaContentDescription* GetContentDescription(
147 const ContentInfo* cinfo) {
148 if (cinfo == NULL)
149 return NULL;
150 return static_cast<const MediaContentDescription*>(cinfo->description);
151}
152
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000153BaseChannel::BaseChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 MediaEngineInterface* media_engine,
155 MediaChannel* media_channel, BaseSession* session,
156 const std::string& content_name, bool rtcp)
157 : worker_thread_(thread),
158 media_engine_(media_engine),
159 session_(session),
160 media_channel_(media_channel),
161 content_name_(content_name),
162 rtcp_(rtcp),
163 transport_channel_(NULL),
164 rtcp_transport_channel_(NULL),
165 enabled_(false),
166 writable_(false),
167 rtp_ready_to_send_(false),
168 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169 was_ever_writable_(false),
170 local_content_direction_(MD_INACTIVE),
171 remote_content_direction_(MD_INACTIVE),
172 has_received_packet_(false),
173 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000174 secure_required_(false),
175 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000176 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 LOG(LS_INFO) << "Created channel for " << content_name;
178}
179
180BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000181 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000182 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 StopConnectionMonitor();
184 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000185 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 // We must destroy the media channel before the transport channel, otherwise
187 // the media channel may try to send on the dead transport channel. NULLing
188 // is not an effective strategy since the sends will come on another thread.
189 delete media_channel_;
190 set_rtcp_transport_channel(NULL);
191 if (transport_channel_ != NULL)
192 session_->DestroyChannel(content_name_, transport_channel_->component());
193 LOG(LS_INFO) << "Destroyed channel";
194}
195
196bool BaseChannel::Init(TransportChannel* transport_channel,
197 TransportChannel* rtcp_transport_channel) {
198 if (transport_channel == NULL) {
199 return false;
200 }
201 if (rtcp() && rtcp_transport_channel == NULL) {
202 return false;
203 }
204 transport_channel_ = transport_channel;
205
206 if (!SetDtlsSrtpCiphers(transport_channel_, false)) {
207 return false;
208 }
209
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 transport_channel_->SignalWritableState.connect(
211 this, &BaseChannel::OnWritableState);
212 transport_channel_->SignalReadPacket.connect(
213 this, &BaseChannel::OnChannelRead);
214 transport_channel_->SignalReadyToSend.connect(
215 this, &BaseChannel::OnReadyToSend);
216
217 session_->SignalNewLocalDescription.connect(
218 this, &BaseChannel::OnNewLocalDescription);
219 session_->SignalNewRemoteDescription.connect(
220 this, &BaseChannel::OnNewRemoteDescription);
221
222 set_rtcp_transport_channel(rtcp_transport_channel);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000223 // Both RTP and RTCP channels are set, we can call SetInterface on
224 // media channel and it can set network options.
225 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 return true;
227}
228
wu@webrtc.org78187522013-10-07 23:32:02 +0000229void BaseChannel::Deinit() {
230 media_channel_->SetInterface(NULL);
231}
232
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000233bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000234 worker_thread_->Invoke<void>(Bind(
235 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
236 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000237 return true;
238}
239
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240bool BaseChannel::MuteStream(uint32 ssrc, bool mute) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000241 return InvokeOnWorker(Bind(&BaseChannel::MuteStream_w, this, ssrc, mute));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242}
243
244bool BaseChannel::IsStreamMuted(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000245 return InvokeOnWorker(Bind(&BaseChannel::IsStreamMuted_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246}
247
248bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000249 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250}
251
252bool BaseChannel::RemoveRecvStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000253 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000254}
255
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000256bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000257 return InvokeOnWorker(
258 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000259}
260
261bool BaseChannel::RemoveSendStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000262 return InvokeOnWorker(
263 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000264}
265
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000267 ContentAction action,
268 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000269 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
270 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271}
272
273bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000274 ContentAction action,
275 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000276 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
277 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278}
279
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280void BaseChannel::StartConnectionMonitor(int cms) {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000281 // We pass in the BaseChannel instead of the transport_channel_
282 // because if the transport_channel_ changes, the ConnectionMonitor
283 // would be pointing to the wrong TransportChannel.
284 connection_monitor_.reset(new ConnectionMonitor(
285 this, worker_thread(), rtc::Thread::Current()));
286 connection_monitor_->SignalUpdate.connect(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287 this, &BaseChannel::OnConnectionMonitorUpdate);
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000288 connection_monitor_->Start(cms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000289}
290
291void BaseChannel::StopConnectionMonitor() {
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000292 if (connection_monitor_) {
293 connection_monitor_->Stop();
294 connection_monitor_.reset();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 }
296}
297
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +0000298bool BaseChannel::GetConnectionStats(ConnectionInfos* infos) {
299 ASSERT(worker_thread_ == rtc::Thread::Current());
300 return transport_channel_->GetStats(infos);
301}
302
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303void BaseChannel::set_rtcp_transport_channel(TransportChannel* channel) {
304 if (rtcp_transport_channel_ != channel) {
305 if (rtcp_transport_channel_) {
306 session_->DestroyChannel(
307 content_name_, rtcp_transport_channel_->component());
308 }
309 rtcp_transport_channel_ = channel;
310 if (rtcp_transport_channel_) {
311 // TODO(juberti): Propagate this error code
312 VERIFY(SetDtlsSrtpCiphers(rtcp_transport_channel_, true));
313 rtcp_transport_channel_->SignalWritableState.connect(
314 this, &BaseChannel::OnWritableState);
315 rtcp_transport_channel_->SignalReadPacket.connect(
316 this, &BaseChannel::OnChannelRead);
317 rtcp_transport_channel_->SignalReadyToSend.connect(
318 this, &BaseChannel::OnReadyToSend);
319 }
320 }
321}
322
323bool BaseChannel::IsReadyToReceive() const {
324 // Receive data if we are enabled and have local content,
325 return enabled() && IsReceiveContentDirection(local_content_direction_);
326}
327
328bool BaseChannel::IsReadyToSend() const {
329 // Send outgoing data if we are enabled, have local and remote content,
330 // and we have had some form of connectivity.
331 return enabled() &&
332 IsReceiveContentDirection(remote_content_direction_) &&
333 IsSendContentDirection(local_content_direction_) &&
334 was_ever_writable();
335}
336
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000337bool BaseChannel::SendPacket(rtc::Buffer* packet,
338 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000339 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000340}
341
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000342bool BaseChannel::SendRtcp(rtc::Buffer* packet,
343 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000344 return SendPacket(true, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000345}
346
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000347int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000349 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000351 case ST_RTP:
352 channel = transport_channel_;
353 break;
354 case ST_RTCP:
355 channel = rtcp_transport_channel_;
356 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000358 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359}
360
361void BaseChannel::OnWritableState(TransportChannel* channel) {
362 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
363 if (transport_channel_->writable()
364 && (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
365 ChannelWritable_w();
366 } else {
367 ChannelNotWritable_w();
368 }
369}
370
371void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000372 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000373 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000374 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000376 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000377
378 // When using RTCP multiplexing we might get RTCP packets on the RTP
379 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
380 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000381 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000382 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000383}
384
385void BaseChannel::OnReadyToSend(TransportChannel* channel) {
386 SetReadyToSend(channel, true);
387}
388
389void BaseChannel::SetReadyToSend(TransportChannel* channel, bool ready) {
390 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
391 if (channel == transport_channel_) {
392 rtp_ready_to_send_ = ready;
393 }
394 if (channel == rtcp_transport_channel_) {
395 rtcp_ready_to_send_ = ready;
396 }
397
398 if (!ready) {
399 // Notify the MediaChannel when either rtp or rtcp channel can't send.
400 media_channel_->OnReadyToSend(false);
401 } else if (rtp_ready_to_send_ &&
402 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
403 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
404 // Notify the MediaChannel when both rtp and rtcp channel can send.
405 media_channel_->OnReadyToSend(true);
406 }
407}
408
409bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
410 const char* data, size_t len) {
411 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000412 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413}
414
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000415bool BaseChannel::SendPacket(bool rtcp, rtc::Buffer* packet,
416 rtc::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 // SendPacket gets called from MediaEngine, typically on an encoder thread.
418 // If the thread is not our worker thread, we will post to our worker
419 // so that the real work happens on our worker. This avoids us having to
420 // synchronize access to all the pieces of the send path, including
421 // SRTP and the inner workings of the transport channels.
422 // The only downside is that we can't return a proper failure code if
423 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000424 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 // Avoid a copy by transferring the ownership of the packet data.
426 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
427 PacketMessageData* data = new PacketMessageData;
428 packet->TransferTo(&data->packet);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000429 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 worker_thread_->Post(this, message_id, data);
431 return true;
432 }
433
434 // Now that we are on the correct thread, ensure we have a place to send this
435 // packet before doing anything. (We might get RTCP packets that we don't
436 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
437 // transport.
438 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
439 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000440 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441 return false;
442 }
443
444 // Protect ourselves against crazy data.
445 if (!ValidPacket(rtcp, packet)) {
446 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
447 << PacketType(rtcp) << " packet: wrong size="
448 << packet->length();
449 return false;
450 }
451
452 // Signal to the media sink before protecting the packet.
453 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000454 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455 SignalSendPacketPreCrypto(packet->data(), packet->length(), rtcp);
456 }
457
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000458 rtc::PacketOptions options(dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000459 // Protect if needed.
460 if (srtp_filter_.IsActive()) {
461 bool res;
462 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000463 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000465 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
466 // inside libsrtp for a RTP packet. A external HMAC module will be writing
467 // a fake HMAC value. This is ONLY done for a RTP packet.
468 // Socket layer will update rtp sendtime extension header if present in
469 // packet with current time before updating the HMAC.
470#if !defined(ENABLE_EXTERNAL_AUTH)
471 res = srtp_filter_.ProtectRtp(
472 data, len, static_cast<int>(packet->capacity()), &len);
473#else
henrike@webrtc.org05376342014-03-10 15:53:12 +0000474 options.packet_time_params.rtp_sendtime_extension_id =
475 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000476 res = srtp_filter_.ProtectRtp(
477 data, len, static_cast<int>(packet->capacity()), &len,
478 &options.packet_time_params.srtp_packet_index);
479 // If protection succeeds, let's get auth params from srtp.
480 if (res) {
481 uint8* auth_key = NULL;
482 int key_len;
483 res = srtp_filter_.GetRtpAuthParams(
484 &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
485 if (res) {
486 options.packet_time_params.srtp_auth_key.resize(key_len);
487 options.packet_time_params.srtp_auth_key.assign(auth_key,
488 auth_key + key_len);
489 }
490 }
491#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492 if (!res) {
493 int seq_num = -1;
494 uint32 ssrc = 0;
495 GetRtpSeqNum(data, len, &seq_num);
496 GetRtpSsrc(data, len, &ssrc);
497 LOG(LS_ERROR) << "Failed to protect " << content_name_
498 << " RTP packet: size=" << len
499 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
500 return false;
501 }
502 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000503 res = srtp_filter_.ProtectRtcp(data, len,
504 static_cast<int>(packet->capacity()),
505 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000506 if (!res) {
507 int type = -1;
508 GetRtcpType(data, len, &type);
509 LOG(LS_ERROR) << "Failed to protect " << content_name_
510 << " RTCP packet: size=" << len << ", type=" << type;
511 return false;
512 }
513 }
514
515 // Update the length of the packet now that we've added the auth tag.
516 packet->SetLength(len);
517 } else if (secure_required_) {
518 // This is a double check for something that supposedly can't happen.
519 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
520 << " packet when SRTP is inactive and crypto is required";
521
522 ASSERT(false);
523 return false;
524 }
525
526 // Signal to the media sink after protecting the packet.
527 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000528 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000529 SignalSendPacketPostCrypto(packet->data(), packet->length(), rtcp);
530 }
531
532 // Bon voyage.
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000533 int ret = channel->SendPacket(packet->data(), packet->length(), options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
535 if (ret != static_cast<int>(packet->length())) {
536 if (channel->GetError() == EWOULDBLOCK) {
537 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
538 SetReadyToSend(channel, false);
539 }
540 return false;
541 }
542 return true;
543}
544
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000545bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546 // Protect ourselves against crazy data.
547 if (!ValidPacket(rtcp, packet)) {
548 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
549 << PacketType(rtcp) << " packet: wrong size="
550 << packet->length();
551 return false;
552 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000554 // Bundle filter handles both rtp and rtcp packets.
555 return bundle_filter_.DemuxPacket(packet->data(), packet->length(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000556}
557
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000558void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
559 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000560 if (!WantsPacket(rtcp, packet)) {
561 return;
562 }
563
honghaiz@google.coma67ca1a2015-01-28 19:48:33 +0000564 // We are only interested in the first rtp packet because that
565 // indicates the media has started flowing.
566 if (!has_received_packet_ && !rtcp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000567 has_received_packet_ = true;
568 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
569 }
570
571 // Signal to the media sink before unprotecting the packet.
572 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000573 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 SignalRecvPacketPostCrypto(packet->data(), packet->length(), rtcp);
575 }
576
577 // Unprotect the packet, if needed.
578 if (srtp_filter_.IsActive()) {
579 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000580 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 bool res;
582 if (!rtcp) {
583 res = srtp_filter_.UnprotectRtp(data, len, &len);
584 if (!res) {
585 int seq_num = -1;
586 uint32 ssrc = 0;
587 GetRtpSeqNum(data, len, &seq_num);
588 GetRtpSsrc(data, len, &ssrc);
589 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
590 << " RTP packet: size=" << len
591 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
592 return;
593 }
594 } else {
595 res = srtp_filter_.UnprotectRtcp(data, len, &len);
596 if (!res) {
597 int type = -1;
598 GetRtcpType(data, len, &type);
599 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
600 << " RTCP packet: size=" << len << ", type=" << type;
601 return;
602 }
603 }
604
605 packet->SetLength(len);
606 } else if (secure_required_) {
607 // Our session description indicates that SRTP is required, but we got a
608 // packet before our SRTP filter is active. This means either that
609 // a) we got SRTP packets before we received the SDES keys, in which case
610 // we can't decrypt it anyway, or
611 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
612 // channels, so we haven't yet extracted keys, even if DTLS did complete
613 // on the channel that the packets are being sent on. It's really good
614 // practice to wait for both RTP and RTCP to be good to go before sending
615 // media, to prevent weird failure modes, so it's fine for us to just eat
616 // packets here. This is all sidestepped if RTCP mux is used anyway.
617 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
618 << " packet when SRTP is inactive and crypto is required";
619 return;
620 }
621
622 // Signal to the media sink after unprotecting the packet.
623 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000624 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000625 SignalRecvPacketPreCrypto(packet->data(), packet->length(), rtcp);
626 }
627
628 // Push it down to the media channel.
629 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000630 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000632 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 }
634}
635
636void BaseChannel::OnNewLocalDescription(
637 BaseSession* session, ContentAction action) {
638 const ContentInfo* content_info =
639 GetFirstContent(session->local_description());
640 const MediaContentDescription* content_desc =
641 GetContentDescription(content_info);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000642 std::string error_desc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 if (content_desc && content_info && !content_info->rejected &&
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000644 !SetLocalContent(content_desc, action, &error_desc)) {
645 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647 }
648}
649
650void BaseChannel::OnNewRemoteDescription(
651 BaseSession* session, ContentAction action) {
652 const ContentInfo* content_info =
653 GetFirstContent(session->remote_description());
654 const MediaContentDescription* content_desc =
655 GetContentDescription(content_info);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000656 std::string error_desc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 if (content_desc && content_info && !content_info->rejected &&
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000658 !SetRemoteContent(content_desc, action, &error_desc)) {
659 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
660 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 }
662}
663
664void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000665 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 if (enabled_)
667 return;
668
669 LOG(LS_INFO) << "Channel enabled";
670 enabled_ = true;
671 ChangeState();
672}
673
674void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000675 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 if (!enabled_)
677 return;
678
679 LOG(LS_INFO) << "Channel disabled";
680 enabled_ = false;
681 ChangeState();
682}
683
684bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000685 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 bool ret = media_channel()->MuteStream(ssrc, mute);
687 if (ret) {
688 if (mute)
689 muted_streams_.insert(ssrc);
690 else
691 muted_streams_.erase(ssrc);
692 }
693 return ret;
694}
695
696bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000697 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 return muted_streams_.find(ssrc) != muted_streams_.end();
699}
700
701void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000702 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000703 if (writable_)
704 return;
705
706 LOG(LS_INFO) << "Channel socket writable ("
707 << transport_channel_->content_name() << ", "
708 << transport_channel_->component() << ")"
709 << (was_ever_writable_ ? "" : " for the first time");
710
711 std::vector<ConnectionInfo> infos;
712 transport_channel_->GetStats(&infos);
713 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
714 it != infos.end(); ++it) {
715 if (it->best_connection) {
716 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
717 << "->" << it->remote_candidate.ToSensitiveString();
718 break;
719 }
720 }
721
722 // If we're doing DTLS-SRTP, now is the time.
723 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
724 if (!SetupDtlsSrtp(false)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000725 SignalDtlsSetupFailure(this, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000726 return;
727 }
728
729 if (rtcp_transport_channel_) {
730 if (!SetupDtlsSrtp(true)) {
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000731 SignalDtlsSetupFailure(this, true);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000732 return;
733 }
734 }
735 }
736
737 was_ever_writable_ = true;
738 writable_ = true;
739 ChangeState();
740}
741
pthatcher@webrtc.org4eeef582015-03-16 19:34:23 +0000742void BaseChannel::SignalDtlsSetupFailure_w(bool rtcp) {
743 ASSERT(worker_thread() == rtc::Thread::Current());
744 signaling_thread()->Invoke<void>(Bind(
745 &BaseChannel::SignalDtlsSetupFailure_s, this, rtcp));
746}
747
748void BaseChannel::SignalDtlsSetupFailure_s(bool rtcp) {
749 ASSERT(signaling_thread() == rtc::Thread::Current());
750 SignalDtlsSetupFailure(this, rtcp);
751}
752
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
754 std::vector<std::string> ciphers;
755 // We always use the default SRTP ciphers for RTCP, but we may use different
756 // ciphers for RTP depending on the media type.
757 if (!rtcp) {
758 GetSrtpCiphers(&ciphers);
759 } else {
760 GetSupportedDefaultCryptoSuites(&ciphers);
761 }
762 return tc->SetSrtpCiphers(ciphers);
763}
764
765bool BaseChannel::ShouldSetupDtlsSrtp() const {
766 return true;
767}
768
769// This function returns true if either DTLS-SRTP is not in use
770// *or* DTLS-SRTP is successfully set up.
771bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
772 bool ret = false;
773
774 TransportChannel *channel = rtcp_channel ?
775 rtcp_transport_channel_ : transport_channel_;
776
777 // No DTLS
778 if (!channel->IsDtlsActive())
779 return true;
780
781 std::string selected_cipher;
782
783 if (!channel->GetSrtpCipher(&selected_cipher)) {
784 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
785 return false;
786 }
787
788 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
789 << content_name() << " "
790 << PacketType(rtcp_channel);
791
792 // OK, we're now doing DTLS (RFC 5764)
793 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
794 SRTP_MASTER_KEY_SALT_LEN * 2);
795
796 // RFC 5705 exporter using the RFC 5764 parameters
797 if (!channel->ExportKeyingMaterial(
798 kDtlsSrtpExporterLabel,
799 NULL, 0, false,
800 &dtls_buffer[0], dtls_buffer.size())) {
801 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
802 ASSERT(false); // This should never happen
803 return false;
804 }
805
806 // Sync up the keys with the DTLS-SRTP interface
807 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
808 SRTP_MASTER_KEY_SALT_LEN);
809 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
810 SRTP_MASTER_KEY_SALT_LEN);
811 size_t offset = 0;
812 memcpy(&client_write_key[0], &dtls_buffer[offset],
813 SRTP_MASTER_KEY_KEY_LEN);
814 offset += SRTP_MASTER_KEY_KEY_LEN;
815 memcpy(&server_write_key[0], &dtls_buffer[offset],
816 SRTP_MASTER_KEY_KEY_LEN);
817 offset += SRTP_MASTER_KEY_KEY_LEN;
818 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
819 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
820 offset += SRTP_MASTER_KEY_SALT_LEN;
821 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
822 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
823
824 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000825 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000826 if (!channel->GetSslRole(&role)) {
827 LOG(LS_WARNING) << "GetSslRole failed";
828 return false;
829 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000831 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000832 send_key = &server_write_key;
833 recv_key = &client_write_key;
834 } else {
835 send_key = &client_write_key;
836 recv_key = &server_write_key;
837 }
838
839 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000840 ret = srtp_filter_.SetRtcpParams(
841 selected_cipher,
842 &(*send_key)[0],
843 static_cast<int>(send_key->size()),
844 selected_cipher,
845 &(*recv_key)[0],
846 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000847 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000848 ret = srtp_filter_.SetRtpParams(
849 selected_cipher,
850 &(*send_key)[0],
851 static_cast<int>(send_key->size()),
852 selected_cipher,
853 &(*recv_key)[0],
854 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 }
856
857 if (!ret)
858 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
859 else
860 dtls_keyed_ = true;
861
862 return ret;
863}
864
865void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000866 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867 if (!writable_)
868 return;
869
870 LOG(LS_INFO) << "Channel socket not writable ("
871 << transport_channel_->content_name() << ", "
872 << transport_channel_->component() << ")";
873 writable_ = false;
874 ChangeState();
875}
876
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000877// |dtls| will be set to true if DTLS is active for transport channel and
878// crypto is empty.
879bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000880 bool* dtls,
881 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000882 *dtls = transport_channel_->IsDtlsActive();
883 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000884 SafeSetError("Cryptos must be empty when DTLS is active.",
885 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000886 return false;
887 }
888 return true;
889}
890
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000891bool BaseChannel::SetRecvRtpHeaderExtensions_w(
892 const MediaContentDescription* content,
893 MediaChannel* media_channel,
894 std::string* error_desc) {
895 if (content->rtp_header_extensions_set()) {
896 if (!media_channel->SetRecvRtpHeaderExtensions(
897 content->rtp_header_extensions())) {
898 std::ostringstream desc;
899 desc << "Failed to set receive rtp header extensions for "
900 << MediaTypeToString(content->type()) << " content.";
901 SafeSetError(desc.str(), error_desc);
902 return false;
903 }
904 }
905 return true;
906}
907
908bool BaseChannel::SetSendRtpHeaderExtensions_w(
909 const MediaContentDescription* content,
910 MediaChannel* media_channel,
911 std::string* error_desc) {
912 if (content->rtp_header_extensions_set()) {
913 if (!media_channel->SetSendRtpHeaderExtensions(
914 content->rtp_header_extensions())) {
915 std::ostringstream desc;
916 desc << "Failed to set send rtp header extensions for "
917 << MediaTypeToString(content->type()) << " content.";
918 SafeSetError(desc.str(), error_desc);
919 return false;
920 } else {
921 MaybeCacheRtpAbsSendTimeHeaderExtension(content->rtp_header_extensions());
922 }
923 }
924 return true;
925}
926
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000928 ContentAction action,
929 ContentSource src,
930 std::string* error_desc) {
931 if (action == CA_UPDATE) {
932 // no crypto params.
933 return true;
934 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000936 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000937 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
938 if (!ret) {
939 return false;
940 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 switch (action) {
942 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000943 // If DTLS is already active on the channel, we could be renegotiating
944 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000945 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000946 ret = srtp_filter_.SetOffer(cryptos, src);
947 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948 break;
949 case CA_PRANSWER:
950 // If we're doing DTLS-SRTP, we don't want to update the filter
951 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000952 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
954 }
955 break;
956 case CA_ANSWER:
957 // If we're doing DTLS-SRTP, we don't want to update the filter
958 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000959 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 ret = srtp_filter_.SetAnswer(cryptos, src);
961 }
962 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 default:
964 break;
965 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000966 if (!ret) {
967 SafeSetError("Failed to setup SRTP filter.", error_desc);
968 return false;
969 }
970 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971}
972
973bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000974 ContentSource src,
975 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 bool ret = false;
977 switch (action) {
978 case CA_OFFER:
979 ret = rtcp_mux_filter_.SetOffer(enable, src);
980 break;
981 case CA_PRANSWER:
982 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
983 break;
984 case CA_ANSWER:
985 ret = rtcp_mux_filter_.SetAnswer(enable, src);
986 if (ret && rtcp_mux_filter_.IsActive()) {
987 // We activated RTCP mux, close down the RTCP transport.
988 set_rtcp_transport_channel(NULL);
989 }
990 break;
991 case CA_UPDATE:
992 // No RTCP mux info.
993 ret = true;
994 default:
995 break;
996 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000997 if (!ret) {
998 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
999 return false;
1000 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1002 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1003 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001004 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 // If the RTP transport is already writable, then so are we.
1006 if (transport_channel_->writable()) {
1007 ChannelWritable_w();
1008 }
1009 }
1010
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001011 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012}
1013
1014bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001015 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 if (!media_channel()->AddRecvStream(sp))
1017 return false;
1018
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001019 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020}
1021
1022bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001023 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001024 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 return media_channel()->RemoveRecvStream(ssrc);
1026}
1027
1028bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001029 ContentAction action,
1030 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1032 action == CA_PRANSWER || action == CA_UPDATE))
1033 return false;
1034
1035 // If this is an update, streams only contain streams that have changed.
1036 if (action == CA_UPDATE) {
1037 for (StreamParamsVec::const_iterator it = streams.begin();
1038 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001039 const StreamParams* existing_stream =
1040 GetStreamByIds(local_streams_, it->groupid, it->id);
1041 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 if (media_channel()->AddSendStream(*it)) {
1043 local_streams_.push_back(*it);
1044 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1045 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001046 std::ostringstream desc;
1047 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1048 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 return false;
1050 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001051 } else if (existing_stream && !it->has_ssrcs()) {
1052 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001053 std::ostringstream desc;
1054 desc << "Failed to remove send stream with ssrc "
1055 << it->first_ssrc() << ".";
1056 SafeSetError(desc.str(), error_desc);
1057 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001059 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 } else {
1061 LOG(LS_WARNING) << "Ignore unsupported stream update";
1062 }
1063 }
1064 return true;
1065 }
1066 // Else streams are all the streams we want to send.
1067
1068 // Check for streams that have been removed.
1069 bool ret = true;
1070 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1071 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001072 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001074 std::ostringstream desc;
1075 desc << "Failed to remove send stream with ssrc "
1076 << it->first_ssrc() << ".";
1077 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078 ret = false;
1079 }
1080 }
1081 }
1082 // Check for new streams.
1083 for (StreamParamsVec::const_iterator it = streams.begin();
1084 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001085 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086 if (media_channel()->AddSendStream(*it)) {
1087 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1088 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001089 std::ostringstream desc;
1090 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1091 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 ret = false;
1093 }
1094 }
1095 }
1096 local_streams_ = streams;
1097 return ret;
1098}
1099
1100bool BaseChannel::UpdateRemoteStreams_w(
1101 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001102 ContentAction action,
1103 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1105 action == CA_PRANSWER || action == CA_UPDATE))
1106 return false;
1107
1108 // If this is an update, streams only contain streams that have changed.
1109 if (action == CA_UPDATE) {
1110 for (StreamParamsVec::const_iterator it = streams.begin();
1111 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001112 const StreamParams* existing_stream =
1113 GetStreamByIds(remote_streams_, it->groupid, it->id);
1114 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 if (AddRecvStream_w(*it)) {
1116 remote_streams_.push_back(*it);
1117 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1118 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001119 std::ostringstream desc;
1120 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1121 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122 return false;
1123 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001124 } else if (existing_stream && !it->has_ssrcs()) {
1125 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001126 std::ostringstream desc;
1127 desc << "Failed to remove remote stream with ssrc "
1128 << it->first_ssrc() << ".";
1129 SafeSetError(desc.str(), error_desc);
1130 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001132 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001133 } else {
1134 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001135 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 << " new stream = " << it->ToString();
1137 }
1138 }
1139 return true;
1140 }
1141 // Else streams are all the streams we want to receive.
1142
1143 // Check for streams that have been removed.
1144 bool ret = true;
1145 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1146 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001147 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001149 std::ostringstream desc;
1150 desc << "Failed to remove remote stream with ssrc "
1151 << it->first_ssrc() << ".";
1152 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153 ret = false;
1154 }
1155 }
1156 }
1157 // Check for new streams.
1158 for (StreamParamsVec::const_iterator it = streams.begin();
1159 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001160 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 if (AddRecvStream_w(*it)) {
1162 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1163 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001164 std::ostringstream desc;
1165 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1166 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001167 ret = false;
1168 }
1169 }
1170 }
1171 remote_streams_ = streams;
1172 return ret;
1173}
1174
1175bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001176 ContentAction action,
1177 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178 // Cache secure_required_ for belt and suspenders check on SendPacket
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001179 secure_required_ = content->crypto_required() != CT_NONE;
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001180 // Set local RTP header extensions.
1181 bool ret = SetRecvRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001183 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001185 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001186
1187 // Call UpdateLocalStreams_w last to make sure as many settings as possible
1188 // are already set when creating streams.
1189 ret &= UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 set_local_content_direction(content->direction());
1191 return ret;
1192}
1193
1194bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001195 ContentAction action,
1196 std::string* error_desc) {
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001197 // Set remote RTP header extensions.
1198 bool ret = SetSendRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001199 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001200 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001202 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001203 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1204 std::ostringstream desc;
1205 desc << "Failed to set max send bandwidth for "
1206 << MediaTypeToString(content->type()) << " content.";
1207 SafeSetError(desc.str(), error_desc);
1208 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001209 }
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001210
1211 // Call UpdateRemoteStreams_w last to make sure as many settings as possible
1212 // are already set when creating streams.
1213 ret &= UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 set_remote_content_direction(content->direction());
1215 return ret;
1216}
1217
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001218void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1219 const std::vector<RtpHeaderExtension>& extensions) {
1220 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001221 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001222 rtp_abs_sendtime_extn_id_ =
1223 send_time_extension ? send_time_extension->id : -1;
1224}
1225
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001226void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228 case MSG_RTPPACKET:
1229 case MSG_RTCPPACKET: {
1230 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001231 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001232 delete data; // because it is Posted
1233 break;
1234 }
1235 case MSG_FIRSTPACKETRECEIVED: {
1236 SignalFirstPacketReceived(this);
1237 break;
1238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239 }
1240}
1241
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242void BaseChannel::FlushRtcpMessages() {
1243 // Flush all remaining RTCP messages. This should only be called in
1244 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001245 ASSERT(rtc::Thread::Current() == worker_thread_);
1246 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001247 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001248 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001250 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001251 }
1252}
1253
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001254VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255 MediaEngineInterface* media_engine,
1256 VoiceMediaChannel* media_channel,
1257 BaseSession* session,
1258 const std::string& content_name,
1259 bool rtcp)
1260 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1261 rtcp),
1262 received_media_(false) {
1263}
1264
1265VoiceChannel::~VoiceChannel() {
1266 StopAudioMonitor();
1267 StopMediaMonitor();
1268 // this can't be done in the base class, since it calls a virtual
1269 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001270 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001271}
1272
1273bool VoiceChannel::Init() {
1274 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1275 content_name(), "rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1276 if (!BaseChannel::Init(session()->CreateChannel(
1277 content_name(), "rtp", ICE_CANDIDATE_COMPONENT_RTP),
1278 rtcp_channel)) {
1279 return false;
1280 }
1281 media_channel()->SignalMediaError.connect(
1282 this, &VoiceChannel::OnVoiceChannelError);
1283 srtp_filter()->SignalSrtpError.connect(
1284 this, &VoiceChannel::OnSrtpError);
1285 return true;
1286}
1287
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001288bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001289 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1290 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001291}
1292
1293bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001294 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1295 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001296}
1297
1298bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001299 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300}
1301
1302// TODO(juberti): Handle early media the right way. We should get an explicit
1303// ringing message telling us to start playing local ringback, which we cancel
1304// if any early media actually arrives. For now, we do the opposite, which is
1305// to wait 1 second for early media, and start playing local ringback if none
1306// arrives.
1307void VoiceChannel::SetEarlyMedia(bool enable) {
1308 if (enable) {
1309 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001310 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1311 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 } else {
1313 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001314 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 }
1316}
1317
1318bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001319 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1320 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321}
1322
1323bool VoiceChannel::PressDTMF(int digit, bool playout) {
1324 int flags = DF_SEND;
1325 if (playout) {
1326 flags |= DF_PLAY;
1327 }
1328 int duration_ms = 160;
1329 return InsertDtmf(0, digit, duration_ms, flags);
1330}
1331
1332bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001333 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1334 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335}
1336
1337bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1338 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001339 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1340 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341}
1342
1343bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001344 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1345 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001347
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001349 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1350 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351}
1352
1353void VoiceChannel::StartMediaMonitor(int cms) {
1354 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001355 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001356 media_monitor_->SignalUpdate.connect(
1357 this, &VoiceChannel::OnMediaMonitorUpdate);
1358 media_monitor_->Start(cms);
1359}
1360
1361void VoiceChannel::StopMediaMonitor() {
1362 if (media_monitor_) {
1363 media_monitor_->Stop();
1364 media_monitor_->SignalUpdate.disconnect(this);
1365 media_monitor_.reset();
1366 }
1367}
1368
1369void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001370 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371 audio_monitor_
1372 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1373 audio_monitor_->Start(cms);
1374}
1375
1376void VoiceChannel::StopAudioMonitor() {
1377 if (audio_monitor_) {
1378 audio_monitor_->Stop();
1379 audio_monitor_.reset();
1380 }
1381}
1382
1383bool VoiceChannel::IsAudioMonitorRunning() const {
1384 return (audio_monitor_.get() != NULL);
1385}
1386
1387void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1388 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1389 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1390}
1391
1392void VoiceChannel::StopTypingMonitor() {
1393 typing_monitor_.reset();
1394}
1395
1396bool VoiceChannel::IsTypingMonitorRunning() const {
1397 return typing_monitor_;
1398}
1399
1400bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1401 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1402 if (typing_monitor_ && mute)
1403 typing_monitor_->OnChannelMuted();
1404 return ret;
1405}
1406
1407int VoiceChannel::GetInputLevel_w() {
1408 return media_engine()->GetInputLevel();
1409}
1410
1411int VoiceChannel::GetOutputLevel_w() {
1412 return media_channel()->GetOutputLevel();
1413}
1414
1415void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1416 media_channel()->GetActiveStreams(actives);
1417}
1418
1419void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001420 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001421 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001422 int flags) {
1423 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001424
1425 // Set a flag when we've received an RTP packet. If we're waiting for early
1426 // media, this will disable the timeout.
1427 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1428 received_media_ = true;
1429 }
1430}
1431
1432void VoiceChannel::ChangeState() {
1433 // Render incoming data if we're the active call, and we have the local
1434 // content. We receive data on the default channel and multiplexed streams.
1435 bool recv = IsReadyToReceive();
1436 if (!media_channel()->SetPlayout(recv)) {
1437 SendLastMediaError();
1438 }
1439
1440 // Send outgoing data if we're the active call, we have the remote content,
1441 // and we have had some form of connectivity.
1442 bool send = IsReadyToSend();
1443 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1444 if (!media_channel()->SetSend(send_flag)) {
1445 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1446 SendLastMediaError();
1447 }
1448
1449 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1450}
1451
1452const ContentInfo* VoiceChannel::GetFirstContent(
1453 const SessionDescription* sdesc) {
1454 return GetFirstAudioContent(sdesc);
1455}
1456
1457bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001458 ContentAction action,
1459 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001460 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461 LOG(LS_INFO) << "Setting local voice description";
1462
1463 const AudioContentDescription* audio =
1464 static_cast<const AudioContentDescription*>(content);
1465 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001466 if (!audio) {
1467 SafeSetError("Can't find audio content in local description.", error_desc);
1468 return false;
1469 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001471 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 // Set local audio codecs (what we want to receive).
1473 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1474 // is set properly.
1475 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001476 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1477 SafeSetError("Failed to set audio receive codecs.", error_desc);
1478 ret = false;
1479 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480 }
1481
1482 // If everything worked, see if we can start receiving.
1483 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001484 std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1485 for (; it != audio->codecs().end(); ++it) {
1486 bundle_filter()->AddPayloadType(it->id);
1487 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 ChangeState();
1489 } else {
1490 LOG(LS_WARNING) << "Failed to set local voice description";
1491 }
1492 return ret;
1493}
1494
1495bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001496 ContentAction action,
1497 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001498 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 LOG(LS_INFO) << "Setting remote voice description";
1500
1501 const AudioContentDescription* audio =
1502 static_cast<const AudioContentDescription*>(content);
1503 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001504 if (!audio) {
1505 SafeSetError("Can't find audio content in remote description.", error_desc);
1506 return false;
1507 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508
1509 bool ret = true;
1510 // Set remote video codecs (what the other side wants to receive).
1511 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001512 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1513 SafeSetError("Failed to set audio send codecs.", error_desc);
1514 ret = false;
1515 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516 }
1517
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001518 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519
1520 if (action != CA_UPDATE) {
1521 // Tweak our audio processing settings, if needed.
1522 AudioOptions audio_options;
1523 if (!media_channel()->GetOptions(&audio_options)) {
1524 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1525 } else {
1526 if (audio->conference_mode()) {
1527 audio_options.conference_mode.Set(true);
1528 }
1529 if (audio->agc_minus_10db()) {
1530 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1531 }
1532 if (!media_channel()->SetOptions(audio_options)) {
1533 // Log an error on failure, but don't abort the call.
1534 LOG(LS_ERROR) << "Failed to set voice channel options";
1535 }
1536 }
1537 }
1538
1539 // If everything worked, see if we can start sending.
1540 if (ret) {
1541 ChangeState();
1542 } else {
1543 LOG(LS_WARNING) << "Failed to set remote voice description";
1544 }
1545 return ret;
1546}
1547
1548bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001549 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1551}
1552
1553bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001554 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001555 if (play) {
1556 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1557 } else {
1558 LOG(LS_INFO) << "Stopping ringback tone";
1559 }
1560 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1561}
1562
1563void VoiceChannel::HandleEarlyMediaTimeout() {
1564 // This occurs on the main thread, not the worker thread.
1565 if (!received_media_) {
1566 LOG(LS_INFO) << "No early media received before timeout";
1567 SignalEarlyMediaTimeout(this);
1568 }
1569}
1570
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1572 int flags) {
1573 if (!enabled()) {
1574 return false;
1575 }
1576
1577 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1578}
1579
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001581 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1582 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583}
1584
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001585void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 case MSG_EARLYMEDIATIMEOUT:
1588 HandleEarlyMediaTimeout();
1589 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 case MSG_CHANNEL_ERROR: {
1591 VoiceChannelErrorMessageData* data =
1592 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1593 SignalMediaError(this, data->ssrc, data->error);
1594 delete data;
1595 break;
1596 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 default:
1598 BaseChannel::OnMessage(pmsg);
1599 break;
1600 }
1601}
1602
1603void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001604 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 SignalConnectionMonitor(this, infos);
1606}
1607
1608void VoiceChannel::OnMediaMonitorUpdate(
1609 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1610 ASSERT(media_channel == this->media_channel());
1611 SignalMediaMonitor(this, info);
1612}
1613
1614void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1615 const AudioInfo& info) {
1616 SignalAudioMonitor(this, info);
1617}
1618
1619void VoiceChannel::OnVoiceChannelError(
1620 uint32 ssrc, VoiceMediaChannel::Error err) {
1621 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1622 ssrc, err);
1623 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1624}
1625
1626void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1627 SrtpFilter::Error error) {
1628 switch (error) {
1629 case SrtpFilter::ERROR_FAIL:
1630 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1631 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1632 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1633 break;
1634 case SrtpFilter::ERROR_AUTH:
1635 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1636 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1637 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1638 break;
1639 case SrtpFilter::ERROR_REPLAY:
1640 // Only receving channel should have this error.
1641 ASSERT(mode == SrtpFilter::UNPROTECT);
1642 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1643 break;
1644 default:
1645 break;
1646 }
1647}
1648
1649void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1650 GetSupportedAudioCryptoSuites(ciphers);
1651}
1652
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001653VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001654 MediaEngineInterface* media_engine,
1655 VideoMediaChannel* media_channel,
1656 BaseSession* session,
1657 const std::string& content_name,
1658 bool rtcp,
1659 VoiceChannel* voice_channel)
1660 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1661 rtcp),
1662 voice_channel_(voice_channel),
1663 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001664 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665}
1666
1667bool VideoChannel::Init() {
1668 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1669 content_name(), "video_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1670 if (!BaseChannel::Init(session()->CreateChannel(
1671 content_name(), "video_rtp", ICE_CANDIDATE_COMPONENT_RTP),
1672 rtcp_channel)) {
1673 return false;
1674 }
1675 media_channel()->SignalMediaError.connect(
1676 this, &VideoChannel::OnVideoChannelError);
1677 srtp_filter()->SignalSrtpError.connect(
1678 this, &VideoChannel::OnSrtpError);
1679 return true;
1680}
1681
1682void VoiceChannel::SendLastMediaError() {
1683 uint32 ssrc;
1684 VoiceMediaChannel::Error error;
1685 media_channel()->GetLastMediaError(&ssrc, &error);
1686 SignalMediaError(this, ssrc, error);
1687}
1688
1689VideoChannel::~VideoChannel() {
1690 std::vector<uint32> screencast_ssrcs;
1691 ScreencastMap::iterator iter;
1692 while (!screencast_capturers_.empty()) {
1693 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1694 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1695 << screencast_capturers_.begin()->first;
1696 ASSERT(false);
1697 break;
1698 }
1699 }
1700
1701 StopMediaMonitor();
1702 // this can't be done in the base class, since it calls a virtual
1703 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001704
1705 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706}
1707
1708bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001709 worker_thread()->Invoke<void>(Bind(
1710 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711 return true;
1712}
1713
1714bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001715 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716}
1717
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001718bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1719 return worker_thread()->Invoke<bool>(Bind(
1720 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001721}
1722
1723bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001724 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1725 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726}
1727
1728bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001729 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730}
1731
1732bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001733 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734}
1735
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001736int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001737 ScreencastDetailsData data(ssrc);
1738 worker_thread()->Invoke<void>(Bind(
1739 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001740 return data.fps;
1741}
1742
1743int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001744 ScreencastDetailsData data(ssrc);
1745 worker_thread()->Invoke<void>(Bind(
1746 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001747 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748}
1749
1750bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001751 worker_thread()->Invoke<void>(Bind(
1752 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 return true;
1754}
1755
1756bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001757 worker_thread()->Invoke<void>(Bind(
1758 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 return true;
1760}
1761
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762void VideoChannel::ChangeState() {
1763 // Render incoming data if we're the active call, and we have the local
1764 // content. We receive data on the default channel and multiplexed streams.
1765 bool recv = IsReadyToReceive();
1766 if (!media_channel()->SetRender(recv)) {
1767 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1768 // TODO(gangji): Report error back to server.
1769 }
1770
1771 // Send outgoing data if we're the active call, we have the remote content,
1772 // and we have had some form of connectivity.
1773 bool send = IsReadyToSend();
1774 if (!media_channel()->SetSend(send)) {
1775 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1776 // TODO(gangji): Report error back to server.
1777 }
1778
1779 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1780}
1781
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001782bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1783 return InvokeOnWorker(
1784 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785}
1786
1787void VideoChannel::StartMediaMonitor(int cms) {
1788 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001789 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 media_monitor_->SignalUpdate.connect(
1791 this, &VideoChannel::OnMediaMonitorUpdate);
1792 media_monitor_->Start(cms);
1793}
1794
1795void VideoChannel::StopMediaMonitor() {
1796 if (media_monitor_) {
1797 media_monitor_->Stop();
1798 media_monitor_.reset();
1799 }
1800}
1801
1802const ContentInfo* VideoChannel::GetFirstContent(
1803 const SessionDescription* sdesc) {
1804 return GetFirstVideoContent(sdesc);
1805}
1806
1807bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001808 ContentAction action,
1809 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001810 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 LOG(LS_INFO) << "Setting local video description";
1812
1813 const VideoContentDescription* video =
1814 static_cast<const VideoContentDescription*>(content);
1815 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001816 if (!video) {
1817 SafeSetError("Can't find video content in local description.", error_desc);
1818 return false;
1819 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001821 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 // Set local video codecs (what we want to receive).
1823 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001824 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1825 SafeSetError("Failed to set video receive codecs.", error_desc);
1826 ret = false;
1827 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828 }
1829
1830 if (action != CA_UPDATE) {
1831 VideoOptions video_options;
1832 media_channel()->GetOptions(&video_options);
1833 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1834
1835 if (!media_channel()->SetOptions(video_options)) {
1836 // Log an error on failure, but don't abort the call.
1837 LOG(LS_ERROR) << "Failed to set video channel options";
1838 }
1839 }
1840
1841 // If everything worked, see if we can start receiving.
1842 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001843 std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1844 for (; it != video->codecs().end(); ++it) {
1845 bundle_filter()->AddPayloadType(it->id);
1846 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 ChangeState();
1848 } else {
1849 LOG(LS_WARNING) << "Failed to set local video description";
1850 }
1851 return ret;
1852}
1853
1854bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001855 ContentAction action,
1856 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001857 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 LOG(LS_INFO) << "Setting remote video description";
1859
1860 const VideoContentDescription* video =
1861 static_cast<const VideoContentDescription*>(content);
1862 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001863 if (!video) {
1864 SafeSetError("Can't find video content in remote description.", error_desc);
1865 return false;
1866 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867
1868 bool ret = true;
1869 // Set remote video codecs (what the other side wants to receive).
1870 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001871 if (!media_channel()->SetSendCodecs(video->codecs())) {
1872 SafeSetError("Failed to set video send codecs.", error_desc);
1873 ret = false;
1874 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 }
1876
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001877 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878
1879 if (action != CA_UPDATE) {
1880 // Tweak our video processing settings, if needed.
1881 VideoOptions video_options;
1882 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001883 if (video->conference_mode()) {
1884 video_options.conference_mode.Set(true);
1885 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1887
1888 if (!media_channel()->SetOptions(video_options)) {
1889 // Log an error on failure, but don't abort the call.
1890 LOG(LS_ERROR) << "Failed to set video channel options";
1891 }
1892 }
1893
1894 // If everything worked, see if we can start sending.
1895 if (ret) {
1896 ChangeState();
1897 } else {
1898 LOG(LS_WARNING) << "Failed to set remote video description";
1899 }
1900 return ret;
1901}
1902
1903bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1904 bool ret = true;
1905 // Set the send format for each of the local streams. If the view request
1906 // does not contain a local stream, set its send format to 0x0, which will
1907 // drop all frames.
1908 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1909 it != local_streams().end(); ++it) {
1910 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1911 StaticVideoViews::const_iterator view;
1912 for (view = request.static_video_views.begin();
1913 view != request.static_video_views.end(); ++view) {
1914 if (view->selector.Matches(*it)) {
1915 format.width = view->width;
1916 format.height = view->height;
1917 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1918 break;
1919 }
1920 }
1921
1922 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1923 }
1924
1925 // Check if the view request has invalid streams.
1926 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1927 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001928 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929 LOG(LS_WARNING) << "View request for ("
1930 << it->selector.ssrc << ", '"
1931 << it->selector.groupid << "', '"
1932 << it->selector.streamid << "'"
1933 << ") is not in the local streams.";
1934 }
1935 }
1936
1937 return ret;
1938}
1939
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001940bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001942 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001944 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1945 screencast_capturers_[ssrc] = capturer;
1946 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947}
1948
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001949bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1950 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1951 if (iter == screencast_capturers_.end()) {
1952 return false;
1953 }
1954 // Clean up VideoCapturer.
1955 delete iter->second;
1956 screencast_capturers_.erase(iter);
1957 return true;
1958}
1959
1960bool VideoChannel::IsScreencasting_w() const {
1961 return !screencast_capturers_.empty();
1962}
1963
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001964void VideoChannel::GetScreencastDetails_w(
1965 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001966 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001968 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969 }
1970 VideoCapturer* capturer = iter->second;
1971 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001972 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1973 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974}
1975
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001977 rtc::WindowEvent we) {
1978 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 SignalScreencastWindowEvent(ssrc, we);
1980}
1981
1982bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001983 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
1984 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001985}
1986
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001987void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989 case MSG_SCREENCASTWINDOWEVENT: {
1990 const ScreencastEventMessageData* data =
1991 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1992 OnScreencastWindowEvent_s(data->ssrc, data->event);
1993 delete data;
1994 break;
1995 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996 case MSG_CHANNEL_ERROR: {
1997 const VideoChannelErrorMessageData* data =
1998 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
1999 SignalMediaError(this, data->ssrc, data->error);
2000 delete data;
2001 break;
2002 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002003 default:
2004 BaseChannel::OnMessage(pmsg);
2005 break;
2006 }
2007}
2008
2009void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002010 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 SignalConnectionMonitor(this, infos);
2012}
2013
2014// TODO(pthatcher): Look into removing duplicate code between
2015// audio, video, and data, perhaps by using templates.
2016void VideoChannel::OnMediaMonitorUpdate(
2017 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2018 ASSERT(media_channel == this->media_channel());
2019 SignalMediaMonitor(this, info);
2020}
2021
2022void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002023 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024 ScreencastEventMessageData* pdata =
2025 new ScreencastEventMessageData(ssrc, event);
2026 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2027}
2028
2029void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2030 // Map capturer events to window events. In the future we may want to simply
2031 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002032 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002034 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002036 we = rtc::WE_MINIMIZE;
2037 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2038 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039 } else {
2040 return;
2041 }
2042 previous_we_ = we;
2043
2044 uint32 ssrc = 0;
2045 if (!GetLocalSsrc(capturer, &ssrc)) {
2046 return;
2047 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002048
2049 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050}
2051
2052bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2053 *ssrc = 0;
2054 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2055 iter != screencast_capturers_.end(); ++iter) {
2056 if (iter->second == capturer) {
2057 *ssrc = iter->first;
2058 return true;
2059 }
2060 }
2061 return false;
2062}
2063
2064void VideoChannel::OnVideoChannelError(uint32 ssrc,
2065 VideoMediaChannel::Error error) {
2066 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2067 ssrc, error);
2068 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2069}
2070
2071void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2072 SrtpFilter::Error error) {
2073 switch (error) {
2074 case SrtpFilter::ERROR_FAIL:
2075 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2076 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2077 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2078 break;
2079 case SrtpFilter::ERROR_AUTH:
2080 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2081 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2082 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2083 break;
2084 case SrtpFilter::ERROR_REPLAY:
2085 // Only receving channel should have this error.
2086 ASSERT(mode == SrtpFilter::UNPROTECT);
2087 // TODO(gangji): Turn on the signaling of replay error once we have
2088 // switched to the new mechanism for doing video retransmissions.
2089 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2090 break;
2091 default:
2092 break;
2093 }
2094}
2095
2096
2097void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2098 GetSupportedVideoCryptoSuites(ciphers);
2099}
2100
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002101DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102 DataMediaChannel* media_channel,
2103 BaseSession* session,
2104 const std::string& content_name,
2105 bool rtcp)
2106 // MediaEngine is NULL
2107 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002108 data_channel_type_(cricket::DCT_NONE),
2109 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002110}
2111
2112DataChannel::~DataChannel() {
2113 StopMediaMonitor();
2114 // this can't be done in the base class, since it calls a virtual
2115 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002116
2117 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118}
2119
2120bool DataChannel::Init() {
2121 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
2122 content_name(), "data_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
2123 if (!BaseChannel::Init(session()->CreateChannel(
2124 content_name(), "data_rtp", ICE_CANDIDATE_COMPONENT_RTP),
2125 rtcp_channel)) {
2126 return false;
2127 }
2128 media_channel()->SignalDataReceived.connect(
2129 this, &DataChannel::OnDataReceived);
2130 media_channel()->SignalMediaError.connect(
2131 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002132 media_channel()->SignalReadyToSend.connect(
2133 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002134 media_channel()->SignalStreamClosedRemotely.connect(
2135 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136 srtp_filter()->SignalSrtpError.connect(
2137 this, &DataChannel::OnSrtpError);
2138 return true;
2139}
2140
2141bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002142 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002144 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2145 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146}
2147
2148const ContentInfo* DataChannel::GetFirstContent(
2149 const SessionDescription* sdesc) {
2150 return GetFirstDataContent(sdesc);
2151}
2152
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002153bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154 if (data_channel_type_ == DCT_SCTP) {
2155 // TODO(pthatcher): Do this in a more robust way by checking for
2156 // SCTP or DTLS.
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +00002157 return !IsRtpPacket(packet->data(), packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002158 } else if (data_channel_type_ == DCT_RTP) {
2159 return BaseChannel::WantsPacket(rtcp, packet);
2160 }
2161 return false;
2162}
2163
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002164bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2165 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166 // It hasn't been set before, so set it now.
2167 if (data_channel_type_ == DCT_NONE) {
2168 data_channel_type_ = new_data_channel_type;
2169 return true;
2170 }
2171
2172 // It's been set before, but doesn't match. That's bad.
2173 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002174 std::ostringstream desc;
2175 desc << "Data channel type mismatch."
2176 << " Expected " << data_channel_type_
2177 << " Got " << new_data_channel_type;
2178 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179 return false;
2180 }
2181
2182 // It's hasn't changed. Nothing to do.
2183 return true;
2184}
2185
2186bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002187 const DataContentDescription* content,
2188 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2190 (content->protocol() == kMediaProtocolDtlsSctp));
2191 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002192 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193}
2194
2195bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002196 ContentAction action,
2197 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002198 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199 LOG(LS_INFO) << "Setting local data description";
2200
2201 const DataContentDescription* data =
2202 static_cast<const DataContentDescription*>(content);
2203 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002204 if (!data) {
2205 SafeSetError("Can't find data content in local description.", error_desc);
2206 return false;
2207 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208
2209 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002210 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 return false;
2212 }
2213
2214 if (data_channel_type_ == DCT_SCTP) {
2215 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002216 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 if (ret) {
2218 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002219 // As in SetRemoteContent_w, make sure we set the local SCTP port
2220 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002221 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2222 SafeSetError("Failed to set data receive codecs.", error_desc);
2223 ret = false;
2224 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 }
2226 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002227 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002229 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2230 SafeSetError("Failed to set data receive codecs.", error_desc);
2231 ret = false;
2232 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233 }
2234 }
2235
2236 // If everything worked, see if we can start receiving.
2237 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00002238 std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2239 for (; it != data->codecs().end(); ++it) {
2240 bundle_filter()->AddPayloadType(it->id);
2241 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242 ChangeState();
2243 } else {
2244 LOG(LS_WARNING) << "Failed to set local data description";
2245 }
2246 return ret;
2247}
2248
2249bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002250 ContentAction action,
2251 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002252 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253
2254 const DataContentDescription* data =
2255 static_cast<const DataContentDescription*>(content);
2256 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002257 if (!data) {
2258 SafeSetError("Can't find data content in remote description.", error_desc);
2259 return false;
2260 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002261
2262 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002263 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002264 return false;
2265 }
2266
2267 if (data_channel_type_ == DCT_SCTP) {
2268 LOG(LS_INFO) << "Setting SCTP remote data description";
2269 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002270 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002271 if (ret) {
2272 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002273 // We send the SCTP port number (not to be confused with the underlying
2274 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002275 if (!media_channel()->SetSendCodecs(data->codecs())) {
2276 SafeSetError("Failed to set data send codecs.", error_desc);
2277 ret = false;
2278 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279 }
2280 } else {
2281 // If the remote data doesn't have codecs and isn't an update, it
2282 // must be empty, so ignore it.
2283 if (action != CA_UPDATE && !data->has_codecs()) {
2284 return true;
2285 }
2286 LOG(LS_INFO) << "Setting remote data description";
2287
2288 // Set remote video codecs (what the other side wants to receive).
2289 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002290 if (!media_channel()->SetSendCodecs(data->codecs())) {
2291 SafeSetError("Failed to set data send codecs.", error_desc);
2292 ret = false;
2293 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002294 }
2295
2296 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002297 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002298 }
2299
2300 if (action != CA_UPDATE) {
2301 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002302 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2303 std::ostringstream desc;
2304 desc << "Failed to set max send bandwidth for data content.";
2305 SafeSetError(desc.str(), error_desc);
2306 ret = false;
2307 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 }
2309 }
2310
2311 // If everything worked, see if we can start sending.
2312 if (ret) {
2313 ChangeState();
2314 } else {
2315 LOG(LS_WARNING) << "Failed to set remote data description";
2316 }
2317 return ret;
2318}
2319
2320void DataChannel::ChangeState() {
2321 // Render incoming data if we're the active call, and we have the local
2322 // content. We receive data on the default channel and multiplexed streams.
2323 bool recv = IsReadyToReceive();
2324 if (!media_channel()->SetReceive(recv)) {
2325 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2326 }
2327
2328 // Send outgoing data if we're the active call, we have the remote content,
2329 // and we have had some form of connectivity.
2330 bool send = IsReadyToSend();
2331 if (!media_channel()->SetSend(send)) {
2332 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2333 }
2334
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002335 // Trigger SignalReadyToSendData asynchronously.
2336 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002337
2338 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2339}
2340
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002341void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342 switch (pmsg->message_id) {
2343 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002344 DataChannelReadyToSendMessageData* data =
2345 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002346 ready_to_send_data_ = data->data();
2347 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 delete data;
2349 break;
2350 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002351 case MSG_DATARECEIVED: {
2352 DataReceivedMessageData* data =
2353 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2354 SignalDataReceived(this, data->params, data->payload);
2355 delete data;
2356 break;
2357 }
2358 case MSG_CHANNEL_ERROR: {
2359 const DataChannelErrorMessageData* data =
2360 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2361 SignalMediaError(this, data->ssrc, data->error);
2362 delete data;
2363 break;
2364 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002365 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002366 rtc::TypedMessageData<uint32>* data =
2367 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002368 SignalStreamClosedRemotely(data->data());
2369 delete data;
2370 break;
2371 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002372 default:
2373 BaseChannel::OnMessage(pmsg);
2374 break;
2375 }
2376}
2377
2378void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002379 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002380 SignalConnectionMonitor(this, infos);
2381}
2382
2383void DataChannel::StartMediaMonitor(int cms) {
2384 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002385 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002386 media_monitor_->SignalUpdate.connect(
2387 this, &DataChannel::OnMediaMonitorUpdate);
2388 media_monitor_->Start(cms);
2389}
2390
2391void DataChannel::StopMediaMonitor() {
2392 if (media_monitor_) {
2393 media_monitor_->Stop();
2394 media_monitor_->SignalUpdate.disconnect(this);
2395 media_monitor_.reset();
2396 }
2397}
2398
2399void DataChannel::OnMediaMonitorUpdate(
2400 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2401 ASSERT(media_channel == this->media_channel());
2402 SignalMediaMonitor(this, info);
2403}
2404
2405void DataChannel::OnDataReceived(
2406 const ReceiveDataParams& params, const char* data, size_t len) {
2407 DataReceivedMessageData* msg = new DataReceivedMessageData(
2408 params, data, len);
2409 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2410}
2411
2412void DataChannel::OnDataChannelError(
2413 uint32 ssrc, DataMediaChannel::Error err) {
2414 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2415 ssrc, err);
2416 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2417}
2418
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002419void DataChannel::OnDataChannelReadyToSend(bool writable) {
2420 // This is usded for congestion control to indicate that the stream is ready
2421 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2422 // that the transport channel is ready.
2423 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2424 new DataChannelReadyToSendMessageData(writable));
2425}
2426
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002427void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2428 SrtpFilter::Error error) {
2429 switch (error) {
2430 case SrtpFilter::ERROR_FAIL:
2431 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2432 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2433 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2434 break;
2435 case SrtpFilter::ERROR_AUTH:
2436 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2437 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2438 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2439 break;
2440 case SrtpFilter::ERROR_REPLAY:
2441 // Only receving channel should have this error.
2442 ASSERT(mode == SrtpFilter::UNPROTECT);
2443 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2444 break;
2445 default:
2446 break;
2447 }
2448}
2449
2450void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2451 GetSupportedDataCryptoSuites(ciphers);
2452}
2453
2454bool DataChannel::ShouldSetupDtlsSrtp() const {
2455 return (data_channel_type_ == DCT_RTP);
2456}
2457
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002458void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002459 rtc::TypedMessageData<uint32>* message =
2460 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002461 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2462}
2463
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002464} // namespace cricket