blob: 025a6ad458b5f8e2fbf6d8bf53bbc5ad6cda9e0b [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)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000725 const std::string error_desc =
726 "Couldn't set up DTLS-SRTP on RTP channel.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000727 // Sent synchronously.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000728 signaling_thread()->Invoke<void>(Bind(
729 &SetSessionError,
730 session_,
731 BaseSession::ERROR_TRANSPORT,
732 error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000733 return;
734 }
735
736 if (rtcp_transport_channel_) {
737 if (!SetupDtlsSrtp(true)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000738 const std::string error_desc =
739 "Couldn't set up DTLS-SRTP on RTCP channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 // Sent synchronously.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000741 signaling_thread()->Invoke<void>(Bind(
742 &SetSessionError,
743 session_,
744 BaseSession::ERROR_TRANSPORT,
745 error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 return;
747 }
748 }
749 }
750
751 was_ever_writable_ = true;
752 writable_ = true;
753 ChangeState();
754}
755
756bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
757 std::vector<std::string> ciphers;
758 // We always use the default SRTP ciphers for RTCP, but we may use different
759 // ciphers for RTP depending on the media type.
760 if (!rtcp) {
761 GetSrtpCiphers(&ciphers);
762 } else {
763 GetSupportedDefaultCryptoSuites(&ciphers);
764 }
765 return tc->SetSrtpCiphers(ciphers);
766}
767
768bool BaseChannel::ShouldSetupDtlsSrtp() const {
769 return true;
770}
771
772// This function returns true if either DTLS-SRTP is not in use
773// *or* DTLS-SRTP is successfully set up.
774bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
775 bool ret = false;
776
777 TransportChannel *channel = rtcp_channel ?
778 rtcp_transport_channel_ : transport_channel_;
779
780 // No DTLS
781 if (!channel->IsDtlsActive())
782 return true;
783
784 std::string selected_cipher;
785
786 if (!channel->GetSrtpCipher(&selected_cipher)) {
787 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
788 return false;
789 }
790
791 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
792 << content_name() << " "
793 << PacketType(rtcp_channel);
794
795 // OK, we're now doing DTLS (RFC 5764)
796 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
797 SRTP_MASTER_KEY_SALT_LEN * 2);
798
799 // RFC 5705 exporter using the RFC 5764 parameters
800 if (!channel->ExportKeyingMaterial(
801 kDtlsSrtpExporterLabel,
802 NULL, 0, false,
803 &dtls_buffer[0], dtls_buffer.size())) {
804 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
805 ASSERT(false); // This should never happen
806 return false;
807 }
808
809 // Sync up the keys with the DTLS-SRTP interface
810 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
811 SRTP_MASTER_KEY_SALT_LEN);
812 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
813 SRTP_MASTER_KEY_SALT_LEN);
814 size_t offset = 0;
815 memcpy(&client_write_key[0], &dtls_buffer[offset],
816 SRTP_MASTER_KEY_KEY_LEN);
817 offset += SRTP_MASTER_KEY_KEY_LEN;
818 memcpy(&server_write_key[0], &dtls_buffer[offset],
819 SRTP_MASTER_KEY_KEY_LEN);
820 offset += SRTP_MASTER_KEY_KEY_LEN;
821 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
822 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
823 offset += SRTP_MASTER_KEY_SALT_LEN;
824 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
825 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
826
827 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000828 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000829 if (!channel->GetSslRole(&role)) {
830 LOG(LS_WARNING) << "GetSslRole failed";
831 return false;
832 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000833
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000834 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000835 send_key = &server_write_key;
836 recv_key = &client_write_key;
837 } else {
838 send_key = &client_write_key;
839 recv_key = &server_write_key;
840 }
841
842 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000843 ret = srtp_filter_.SetRtcpParams(
844 selected_cipher,
845 &(*send_key)[0],
846 static_cast<int>(send_key->size()),
847 selected_cipher,
848 &(*recv_key)[0],
849 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000851 ret = srtp_filter_.SetRtpParams(
852 selected_cipher,
853 &(*send_key)[0],
854 static_cast<int>(send_key->size()),
855 selected_cipher,
856 &(*recv_key)[0],
857 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000858 }
859
860 if (!ret)
861 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
862 else
863 dtls_keyed_ = true;
864
865 return ret;
866}
867
868void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000869 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000870 if (!writable_)
871 return;
872
873 LOG(LS_INFO) << "Channel socket not writable ("
874 << transport_channel_->content_name() << ", "
875 << transport_channel_->component() << ")";
876 writable_ = false;
877 ChangeState();
878}
879
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000880// |dtls| will be set to true if DTLS is active for transport channel and
881// crypto is empty.
882bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000883 bool* dtls,
884 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000885 *dtls = transport_channel_->IsDtlsActive();
886 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000887 SafeSetError("Cryptos must be empty when DTLS is active.",
888 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000889 return false;
890 }
891 return true;
892}
893
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000894bool BaseChannel::SetRecvRtpHeaderExtensions_w(
895 const MediaContentDescription* content,
896 MediaChannel* media_channel,
897 std::string* error_desc) {
898 if (content->rtp_header_extensions_set()) {
899 if (!media_channel->SetRecvRtpHeaderExtensions(
900 content->rtp_header_extensions())) {
901 std::ostringstream desc;
902 desc << "Failed to set receive rtp header extensions for "
903 << MediaTypeToString(content->type()) << " content.";
904 SafeSetError(desc.str(), error_desc);
905 return false;
906 }
907 }
908 return true;
909}
910
911bool BaseChannel::SetSendRtpHeaderExtensions_w(
912 const MediaContentDescription* content,
913 MediaChannel* media_channel,
914 std::string* error_desc) {
915 if (content->rtp_header_extensions_set()) {
916 if (!media_channel->SetSendRtpHeaderExtensions(
917 content->rtp_header_extensions())) {
918 std::ostringstream desc;
919 desc << "Failed to set send rtp header extensions for "
920 << MediaTypeToString(content->type()) << " content.";
921 SafeSetError(desc.str(), error_desc);
922 return false;
923 } else {
924 MaybeCacheRtpAbsSendTimeHeaderExtension(content->rtp_header_extensions());
925 }
926 }
927 return true;
928}
929
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000931 ContentAction action,
932 ContentSource src,
933 std::string* error_desc) {
934 if (action == CA_UPDATE) {
935 // no crypto params.
936 return true;
937 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000939 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000940 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
941 if (!ret) {
942 return false;
943 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 switch (action) {
945 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000946 // If DTLS is already active on the channel, we could be renegotiating
947 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000948 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000949 ret = srtp_filter_.SetOffer(cryptos, src);
950 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 break;
952 case CA_PRANSWER:
953 // If we're doing DTLS-SRTP, we don't want to update the filter
954 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000955 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
957 }
958 break;
959 case CA_ANSWER:
960 // If we're doing DTLS-SRTP, we don't want to update the filter
961 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000962 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 ret = srtp_filter_.SetAnswer(cryptos, src);
964 }
965 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 default:
967 break;
968 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000969 if (!ret) {
970 SafeSetError("Failed to setup SRTP filter.", error_desc);
971 return false;
972 }
973 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974}
975
976bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000977 ContentSource src,
978 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 bool ret = false;
980 switch (action) {
981 case CA_OFFER:
982 ret = rtcp_mux_filter_.SetOffer(enable, src);
983 break;
984 case CA_PRANSWER:
985 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
986 break;
987 case CA_ANSWER:
988 ret = rtcp_mux_filter_.SetAnswer(enable, src);
989 if (ret && rtcp_mux_filter_.IsActive()) {
990 // We activated RTCP mux, close down the RTCP transport.
991 set_rtcp_transport_channel(NULL);
992 }
993 break;
994 case CA_UPDATE:
995 // No RTCP mux info.
996 ret = true;
997 default:
998 break;
999 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001000 if (!ret) {
1001 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1002 return false;
1003 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1005 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1006 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001007 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008 // If the RTP transport is already writable, then so are we.
1009 if (transport_channel_->writable()) {
1010 ChannelWritable_w();
1011 }
1012 }
1013
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001014 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015}
1016
1017bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001018 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 if (!media_channel()->AddRecvStream(sp))
1020 return false;
1021
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001022 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023}
1024
1025bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001026 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001027 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028 return media_channel()->RemoveRecvStream(ssrc);
1029}
1030
1031bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001032 ContentAction action,
1033 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1035 action == CA_PRANSWER || action == CA_UPDATE))
1036 return false;
1037
1038 // If this is an update, streams only contain streams that have changed.
1039 if (action == CA_UPDATE) {
1040 for (StreamParamsVec::const_iterator it = streams.begin();
1041 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001042 const StreamParams* existing_stream =
1043 GetStreamByIds(local_streams_, it->groupid, it->id);
1044 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 if (media_channel()->AddSendStream(*it)) {
1046 local_streams_.push_back(*it);
1047 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1048 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001049 std::ostringstream desc;
1050 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1051 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 return false;
1053 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001054 } else if (existing_stream && !it->has_ssrcs()) {
1055 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001056 std::ostringstream desc;
1057 desc << "Failed to remove send stream with ssrc "
1058 << it->first_ssrc() << ".";
1059 SafeSetError(desc.str(), error_desc);
1060 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001062 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 } else {
1064 LOG(LS_WARNING) << "Ignore unsupported stream update";
1065 }
1066 }
1067 return true;
1068 }
1069 // Else streams are all the streams we want to send.
1070
1071 // Check for streams that have been removed.
1072 bool ret = true;
1073 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1074 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001075 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001077 std::ostringstream desc;
1078 desc << "Failed to remove send stream with ssrc "
1079 << it->first_ssrc() << ".";
1080 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001081 ret = false;
1082 }
1083 }
1084 }
1085 // Check for new streams.
1086 for (StreamParamsVec::const_iterator it = streams.begin();
1087 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001088 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 if (media_channel()->AddSendStream(*it)) {
1090 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1091 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001092 std::ostringstream desc;
1093 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1094 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 ret = false;
1096 }
1097 }
1098 }
1099 local_streams_ = streams;
1100 return ret;
1101}
1102
1103bool BaseChannel::UpdateRemoteStreams_w(
1104 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001105 ContentAction action,
1106 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001107 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1108 action == CA_PRANSWER || action == CA_UPDATE))
1109 return false;
1110
1111 // If this is an update, streams only contain streams that have changed.
1112 if (action == CA_UPDATE) {
1113 for (StreamParamsVec::const_iterator it = streams.begin();
1114 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001115 const StreamParams* existing_stream =
1116 GetStreamByIds(remote_streams_, it->groupid, it->id);
1117 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 if (AddRecvStream_w(*it)) {
1119 remote_streams_.push_back(*it);
1120 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1121 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001122 std::ostringstream desc;
1123 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1124 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 return false;
1126 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001127 } else if (existing_stream && !it->has_ssrcs()) {
1128 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001129 std::ostringstream desc;
1130 desc << "Failed to remove remote stream with ssrc "
1131 << it->first_ssrc() << ".";
1132 SafeSetError(desc.str(), error_desc);
1133 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001135 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 } else {
1137 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001138 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139 << " new stream = " << it->ToString();
1140 }
1141 }
1142 return true;
1143 }
1144 // Else streams are all the streams we want to receive.
1145
1146 // Check for streams that have been removed.
1147 bool ret = true;
1148 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1149 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001150 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001151 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001152 std::ostringstream desc;
1153 desc << "Failed to remove remote stream with ssrc "
1154 << it->first_ssrc() << ".";
1155 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156 ret = false;
1157 }
1158 }
1159 }
1160 // Check for new streams.
1161 for (StreamParamsVec::const_iterator it = streams.begin();
1162 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001163 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 if (AddRecvStream_w(*it)) {
1165 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1166 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001167 std::ostringstream desc;
1168 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1169 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001170 ret = false;
1171 }
1172 }
1173 }
1174 remote_streams_ = streams;
1175 return ret;
1176}
1177
1178bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001179 ContentAction action,
1180 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 // Cache secure_required_ for belt and suspenders check on SendPacket
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001182 secure_required_ = content->crypto_required() != CT_NONE;
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001183 // Set local RTP header extensions.
1184 bool ret = SetRecvRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001185 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001186 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001188 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001189
1190 // Call UpdateLocalStreams_w last to make sure as many settings as possible
1191 // are already set when creating streams.
1192 ret &= UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 set_local_content_direction(content->direction());
1194 return ret;
1195}
1196
1197bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001198 ContentAction action,
1199 std::string* error_desc) {
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001200 // Set remote RTP header extensions.
1201 bool ret = SetSendRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001202 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001203 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001205 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001206 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1207 std::ostringstream desc;
1208 desc << "Failed to set max send bandwidth for "
1209 << MediaTypeToString(content->type()) << " content.";
1210 SafeSetError(desc.str(), error_desc);
1211 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 }
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001213
1214 // Call UpdateRemoteStreams_w last to make sure as many settings as possible
1215 // are already set when creating streams.
1216 ret &= UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 set_remote_content_direction(content->direction());
1218 return ret;
1219}
1220
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001221void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1222 const std::vector<RtpHeaderExtension>& extensions) {
1223 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001224 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001225 rtp_abs_sendtime_extn_id_ =
1226 send_time_extension ? send_time_extension->id : -1;
1227}
1228
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001229void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001231 case MSG_RTPPACKET:
1232 case MSG_RTCPPACKET: {
1233 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001234 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001235 delete data; // because it is Posted
1236 break;
1237 }
1238 case MSG_FIRSTPACKETRECEIVED: {
1239 SignalFirstPacketReceived(this);
1240 break;
1241 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001242 }
1243}
1244
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245void BaseChannel::FlushRtcpMessages() {
1246 // Flush all remaining RTCP messages. This should only be called in
1247 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001248 ASSERT(rtc::Thread::Current() == worker_thread_);
1249 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001250 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001251 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001253 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254 }
1255}
1256
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001257VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001258 MediaEngineInterface* media_engine,
1259 VoiceMediaChannel* media_channel,
1260 BaseSession* session,
1261 const std::string& content_name,
1262 bool rtcp)
1263 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1264 rtcp),
1265 received_media_(false) {
1266}
1267
1268VoiceChannel::~VoiceChannel() {
1269 StopAudioMonitor();
1270 StopMediaMonitor();
1271 // this can't be done in the base class, since it calls a virtual
1272 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001273 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001274}
1275
1276bool VoiceChannel::Init() {
1277 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1278 content_name(), "rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1279 if (!BaseChannel::Init(session()->CreateChannel(
1280 content_name(), "rtp", ICE_CANDIDATE_COMPONENT_RTP),
1281 rtcp_channel)) {
1282 return false;
1283 }
1284 media_channel()->SignalMediaError.connect(
1285 this, &VoiceChannel::OnVoiceChannelError);
1286 srtp_filter()->SignalSrtpError.connect(
1287 this, &VoiceChannel::OnSrtpError);
1288 return true;
1289}
1290
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001291bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001292 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1293 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001294}
1295
1296bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001297 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1298 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001299}
1300
1301bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001302 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303}
1304
1305// TODO(juberti): Handle early media the right way. We should get an explicit
1306// ringing message telling us to start playing local ringback, which we cancel
1307// if any early media actually arrives. For now, we do the opposite, which is
1308// to wait 1 second for early media, and start playing local ringback if none
1309// arrives.
1310void VoiceChannel::SetEarlyMedia(bool enable) {
1311 if (enable) {
1312 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001313 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1314 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315 } else {
1316 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001317 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318 }
1319}
1320
1321bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001322 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1323 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001324}
1325
1326bool VoiceChannel::PressDTMF(int digit, bool playout) {
1327 int flags = DF_SEND;
1328 if (playout) {
1329 flags |= DF_PLAY;
1330 }
1331 int duration_ms = 160;
1332 return InsertDtmf(0, digit, duration_ms, flags);
1333}
1334
1335bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001336 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1337 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338}
1339
1340bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1341 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001342 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1343 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344}
1345
1346bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001347 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1348 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001350
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001351bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001352 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1353 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354}
1355
1356void VoiceChannel::StartMediaMonitor(int cms) {
1357 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001358 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359 media_monitor_->SignalUpdate.connect(
1360 this, &VoiceChannel::OnMediaMonitorUpdate);
1361 media_monitor_->Start(cms);
1362}
1363
1364void VoiceChannel::StopMediaMonitor() {
1365 if (media_monitor_) {
1366 media_monitor_->Stop();
1367 media_monitor_->SignalUpdate.disconnect(this);
1368 media_monitor_.reset();
1369 }
1370}
1371
1372void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001373 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374 audio_monitor_
1375 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1376 audio_monitor_->Start(cms);
1377}
1378
1379void VoiceChannel::StopAudioMonitor() {
1380 if (audio_monitor_) {
1381 audio_monitor_->Stop();
1382 audio_monitor_.reset();
1383 }
1384}
1385
1386bool VoiceChannel::IsAudioMonitorRunning() const {
1387 return (audio_monitor_.get() != NULL);
1388}
1389
1390void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1391 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1392 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1393}
1394
1395void VoiceChannel::StopTypingMonitor() {
1396 typing_monitor_.reset();
1397}
1398
1399bool VoiceChannel::IsTypingMonitorRunning() const {
1400 return typing_monitor_;
1401}
1402
1403bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1404 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1405 if (typing_monitor_ && mute)
1406 typing_monitor_->OnChannelMuted();
1407 return ret;
1408}
1409
1410int VoiceChannel::GetInputLevel_w() {
1411 return media_engine()->GetInputLevel();
1412}
1413
1414int VoiceChannel::GetOutputLevel_w() {
1415 return media_channel()->GetOutputLevel();
1416}
1417
1418void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1419 media_channel()->GetActiveStreams(actives);
1420}
1421
1422void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001423 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001424 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001425 int flags) {
1426 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427
1428 // Set a flag when we've received an RTP packet. If we're waiting for early
1429 // media, this will disable the timeout.
1430 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1431 received_media_ = true;
1432 }
1433}
1434
1435void VoiceChannel::ChangeState() {
1436 // Render incoming data if we're the active call, and we have the local
1437 // content. We receive data on the default channel and multiplexed streams.
1438 bool recv = IsReadyToReceive();
1439 if (!media_channel()->SetPlayout(recv)) {
1440 SendLastMediaError();
1441 }
1442
1443 // Send outgoing data if we're the active call, we have the remote content,
1444 // and we have had some form of connectivity.
1445 bool send = IsReadyToSend();
1446 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1447 if (!media_channel()->SetSend(send_flag)) {
1448 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1449 SendLastMediaError();
1450 }
1451
1452 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1453}
1454
1455const ContentInfo* VoiceChannel::GetFirstContent(
1456 const SessionDescription* sdesc) {
1457 return GetFirstAudioContent(sdesc);
1458}
1459
1460bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001461 ContentAction action,
1462 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001463 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464 LOG(LS_INFO) << "Setting local voice description";
1465
1466 const AudioContentDescription* audio =
1467 static_cast<const AudioContentDescription*>(content);
1468 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001469 if (!audio) {
1470 SafeSetError("Can't find audio content in local description.", error_desc);
1471 return false;
1472 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001474 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 // Set local audio codecs (what we want to receive).
1476 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1477 // is set properly.
1478 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001479 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1480 SafeSetError("Failed to set audio receive codecs.", error_desc);
1481 ret = false;
1482 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483 }
1484
1485 // If everything worked, see if we can start receiving.
1486 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001487 std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1488 for (; it != audio->codecs().end(); ++it) {
1489 bundle_filter()->AddPayloadType(it->id);
1490 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491 ChangeState();
1492 } else {
1493 LOG(LS_WARNING) << "Failed to set local voice description";
1494 }
1495 return ret;
1496}
1497
1498bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001499 ContentAction action,
1500 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001501 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 LOG(LS_INFO) << "Setting remote voice description";
1503
1504 const AudioContentDescription* audio =
1505 static_cast<const AudioContentDescription*>(content);
1506 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001507 if (!audio) {
1508 SafeSetError("Can't find audio content in remote description.", error_desc);
1509 return false;
1510 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511
1512 bool ret = true;
1513 // Set remote video codecs (what the other side wants to receive).
1514 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001515 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1516 SafeSetError("Failed to set audio send codecs.", error_desc);
1517 ret = false;
1518 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 }
1520
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001521 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522
1523 if (action != CA_UPDATE) {
1524 // Tweak our audio processing settings, if needed.
1525 AudioOptions audio_options;
1526 if (!media_channel()->GetOptions(&audio_options)) {
1527 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1528 } else {
1529 if (audio->conference_mode()) {
1530 audio_options.conference_mode.Set(true);
1531 }
1532 if (audio->agc_minus_10db()) {
1533 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1534 }
1535 if (!media_channel()->SetOptions(audio_options)) {
1536 // Log an error on failure, but don't abort the call.
1537 LOG(LS_ERROR) << "Failed to set voice channel options";
1538 }
1539 }
1540 }
1541
1542 // If everything worked, see if we can start sending.
1543 if (ret) {
1544 ChangeState();
1545 } else {
1546 LOG(LS_WARNING) << "Failed to set remote voice description";
1547 }
1548 return ret;
1549}
1550
1551bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001552 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1554}
1555
1556bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001557 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 if (play) {
1559 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1560 } else {
1561 LOG(LS_INFO) << "Stopping ringback tone";
1562 }
1563 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1564}
1565
1566void VoiceChannel::HandleEarlyMediaTimeout() {
1567 // This occurs on the main thread, not the worker thread.
1568 if (!received_media_) {
1569 LOG(LS_INFO) << "No early media received before timeout";
1570 SignalEarlyMediaTimeout(this);
1571 }
1572}
1573
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1575 int flags) {
1576 if (!enabled()) {
1577 return false;
1578 }
1579
1580 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1581}
1582
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001584 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1585 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586}
1587
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001588void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001589 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001590 case MSG_EARLYMEDIATIMEOUT:
1591 HandleEarlyMediaTimeout();
1592 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593 case MSG_CHANNEL_ERROR: {
1594 VoiceChannelErrorMessageData* data =
1595 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1596 SignalMediaError(this, data->ssrc, data->error);
1597 delete data;
1598 break;
1599 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 default:
1601 BaseChannel::OnMessage(pmsg);
1602 break;
1603 }
1604}
1605
1606void VoiceChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00001607 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608 SignalConnectionMonitor(this, infos);
1609}
1610
1611void VoiceChannel::OnMediaMonitorUpdate(
1612 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1613 ASSERT(media_channel == this->media_channel());
1614 SignalMediaMonitor(this, info);
1615}
1616
1617void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1618 const AudioInfo& info) {
1619 SignalAudioMonitor(this, info);
1620}
1621
1622void VoiceChannel::OnVoiceChannelError(
1623 uint32 ssrc, VoiceMediaChannel::Error err) {
1624 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1625 ssrc, err);
1626 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1627}
1628
1629void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1630 SrtpFilter::Error error) {
1631 switch (error) {
1632 case SrtpFilter::ERROR_FAIL:
1633 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1634 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1635 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1636 break;
1637 case SrtpFilter::ERROR_AUTH:
1638 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1639 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1640 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1641 break;
1642 case SrtpFilter::ERROR_REPLAY:
1643 // Only receving channel should have this error.
1644 ASSERT(mode == SrtpFilter::UNPROTECT);
1645 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1646 break;
1647 default:
1648 break;
1649 }
1650}
1651
1652void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1653 GetSupportedAudioCryptoSuites(ciphers);
1654}
1655
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001656VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 MediaEngineInterface* media_engine,
1658 VideoMediaChannel* media_channel,
1659 BaseSession* session,
1660 const std::string& content_name,
1661 bool rtcp,
1662 VoiceChannel* voice_channel)
1663 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1664 rtcp),
1665 voice_channel_(voice_channel),
1666 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001667 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001668}
1669
1670bool VideoChannel::Init() {
1671 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1672 content_name(), "video_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1673 if (!BaseChannel::Init(session()->CreateChannel(
1674 content_name(), "video_rtp", ICE_CANDIDATE_COMPONENT_RTP),
1675 rtcp_channel)) {
1676 return false;
1677 }
1678 media_channel()->SignalMediaError.connect(
1679 this, &VideoChannel::OnVideoChannelError);
1680 srtp_filter()->SignalSrtpError.connect(
1681 this, &VideoChannel::OnSrtpError);
1682 return true;
1683}
1684
1685void VoiceChannel::SendLastMediaError() {
1686 uint32 ssrc;
1687 VoiceMediaChannel::Error error;
1688 media_channel()->GetLastMediaError(&ssrc, &error);
1689 SignalMediaError(this, ssrc, error);
1690}
1691
1692VideoChannel::~VideoChannel() {
1693 std::vector<uint32> screencast_ssrcs;
1694 ScreencastMap::iterator iter;
1695 while (!screencast_capturers_.empty()) {
1696 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1697 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1698 << screencast_capturers_.begin()->first;
1699 ASSERT(false);
1700 break;
1701 }
1702 }
1703
1704 StopMediaMonitor();
1705 // this can't be done in the base class, since it calls a virtual
1706 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001707
1708 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709}
1710
1711bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001712 worker_thread()->Invoke<void>(Bind(
1713 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001714 return true;
1715}
1716
1717bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001718 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719}
1720
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001721bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1722 return worker_thread()->Invoke<bool>(Bind(
1723 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724}
1725
1726bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001727 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1728 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729}
1730
1731bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001732 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001733}
1734
1735bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001736 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737}
1738
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001739int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001740 ScreencastDetailsData data(ssrc);
1741 worker_thread()->Invoke<void>(Bind(
1742 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001743 return data.fps;
1744}
1745
1746int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001747 ScreencastDetailsData data(ssrc);
1748 worker_thread()->Invoke<void>(Bind(
1749 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001750 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751}
1752
1753bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001754 worker_thread()->Invoke<void>(Bind(
1755 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 return true;
1757}
1758
1759bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001760 worker_thread()->Invoke<void>(Bind(
1761 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001762 return true;
1763}
1764
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765void VideoChannel::ChangeState() {
1766 // Render incoming data if we're the active call, and we have the local
1767 // content. We receive data on the default channel and multiplexed streams.
1768 bool recv = IsReadyToReceive();
1769 if (!media_channel()->SetRender(recv)) {
1770 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1771 // TODO(gangji): Report error back to server.
1772 }
1773
1774 // Send outgoing data if we're the active call, we have the remote content,
1775 // and we have had some form of connectivity.
1776 bool send = IsReadyToSend();
1777 if (!media_channel()->SetSend(send)) {
1778 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1779 // TODO(gangji): Report error back to server.
1780 }
1781
1782 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1783}
1784
pbos@webrtc.org058b1f12015-03-04 08:54:32 +00001785bool VideoChannel::GetStats(VideoMediaInfo* stats) {
1786 return InvokeOnWorker(
1787 Bind(&VideoMediaChannel::GetStats, media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788}
1789
1790void VideoChannel::StartMediaMonitor(int cms) {
1791 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001792 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 media_monitor_->SignalUpdate.connect(
1794 this, &VideoChannel::OnMediaMonitorUpdate);
1795 media_monitor_->Start(cms);
1796}
1797
1798void VideoChannel::StopMediaMonitor() {
1799 if (media_monitor_) {
1800 media_monitor_->Stop();
1801 media_monitor_.reset();
1802 }
1803}
1804
1805const ContentInfo* VideoChannel::GetFirstContent(
1806 const SessionDescription* sdesc) {
1807 return GetFirstVideoContent(sdesc);
1808}
1809
1810bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001811 ContentAction action,
1812 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001813 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 LOG(LS_INFO) << "Setting local video description";
1815
1816 const VideoContentDescription* video =
1817 static_cast<const VideoContentDescription*>(content);
1818 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001819 if (!video) {
1820 SafeSetError("Can't find video content in local description.", error_desc);
1821 return false;
1822 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001824 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 // Set local video codecs (what we want to receive).
1826 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001827 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1828 SafeSetError("Failed to set video receive codecs.", error_desc);
1829 ret = false;
1830 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 }
1832
1833 if (action != CA_UPDATE) {
1834 VideoOptions video_options;
1835 media_channel()->GetOptions(&video_options);
1836 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1837
1838 if (!media_channel()->SetOptions(video_options)) {
1839 // Log an error on failure, but don't abort the call.
1840 LOG(LS_ERROR) << "Failed to set video channel options";
1841 }
1842 }
1843
1844 // If everything worked, see if we can start receiving.
1845 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001846 std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1847 for (; it != video->codecs().end(); ++it) {
1848 bundle_filter()->AddPayloadType(it->id);
1849 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 ChangeState();
1851 } else {
1852 LOG(LS_WARNING) << "Failed to set local video description";
1853 }
1854 return ret;
1855}
1856
1857bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001858 ContentAction action,
1859 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001860 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 LOG(LS_INFO) << "Setting remote video description";
1862
1863 const VideoContentDescription* video =
1864 static_cast<const VideoContentDescription*>(content);
1865 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001866 if (!video) {
1867 SafeSetError("Can't find video content in remote description.", error_desc);
1868 return false;
1869 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870
1871 bool ret = true;
1872 // Set remote video codecs (what the other side wants to receive).
1873 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001874 if (!media_channel()->SetSendCodecs(video->codecs())) {
1875 SafeSetError("Failed to set video send codecs.", error_desc);
1876 ret = false;
1877 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878 }
1879
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001880 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881
1882 if (action != CA_UPDATE) {
1883 // Tweak our video processing settings, if needed.
1884 VideoOptions video_options;
1885 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001886 if (video->conference_mode()) {
1887 video_options.conference_mode.Set(true);
1888 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1890
1891 if (!media_channel()->SetOptions(video_options)) {
1892 // Log an error on failure, but don't abort the call.
1893 LOG(LS_ERROR) << "Failed to set video channel options";
1894 }
1895 }
1896
1897 // If everything worked, see if we can start sending.
1898 if (ret) {
1899 ChangeState();
1900 } else {
1901 LOG(LS_WARNING) << "Failed to set remote video description";
1902 }
1903 return ret;
1904}
1905
1906bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1907 bool ret = true;
1908 // Set the send format for each of the local streams. If the view request
1909 // does not contain a local stream, set its send format to 0x0, which will
1910 // drop all frames.
1911 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1912 it != local_streams().end(); ++it) {
1913 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1914 StaticVideoViews::const_iterator view;
1915 for (view = request.static_video_views.begin();
1916 view != request.static_video_views.end(); ++view) {
1917 if (view->selector.Matches(*it)) {
1918 format.width = view->width;
1919 format.height = view->height;
1920 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1921 break;
1922 }
1923 }
1924
1925 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1926 }
1927
1928 // Check if the view request has invalid streams.
1929 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1930 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001931 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 LOG(LS_WARNING) << "View request for ("
1933 << it->selector.ssrc << ", '"
1934 << it->selector.groupid << "', '"
1935 << it->selector.streamid << "'"
1936 << ") is not in the local streams.";
1937 }
1938 }
1939
1940 return ret;
1941}
1942
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001943bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001945 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001947 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1948 screencast_capturers_[ssrc] = capturer;
1949 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950}
1951
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1953 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1954 if (iter == screencast_capturers_.end()) {
1955 return false;
1956 }
1957 // Clean up VideoCapturer.
1958 delete iter->second;
1959 screencast_capturers_.erase(iter);
1960 return true;
1961}
1962
1963bool VideoChannel::IsScreencasting_w() const {
1964 return !screencast_capturers_.empty();
1965}
1966
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001967void VideoChannel::GetScreencastDetails_w(
1968 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001969 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001971 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972 }
1973 VideoCapturer* capturer = iter->second;
1974 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001975 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1976 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977}
1978
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001980 rtc::WindowEvent we) {
1981 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 SignalScreencastWindowEvent(ssrc, we);
1983}
1984
1985bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001986 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
1987 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988}
1989
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001990void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 case MSG_SCREENCASTWINDOWEVENT: {
1993 const ScreencastEventMessageData* data =
1994 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1995 OnScreencastWindowEvent_s(data->ssrc, data->event);
1996 delete data;
1997 break;
1998 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 case MSG_CHANNEL_ERROR: {
2000 const VideoChannelErrorMessageData* data =
2001 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
2002 SignalMediaError(this, data->ssrc, data->error);
2003 delete data;
2004 break;
2005 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002006 default:
2007 BaseChannel::OnMessage(pmsg);
2008 break;
2009 }
2010}
2011
2012void VideoChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002013 ConnectionMonitor* monitor, const std::vector<ConnectionInfo> &infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 SignalConnectionMonitor(this, infos);
2015}
2016
2017// TODO(pthatcher): Look into removing duplicate code between
2018// audio, video, and data, perhaps by using templates.
2019void VideoChannel::OnMediaMonitorUpdate(
2020 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2021 ASSERT(media_channel == this->media_channel());
2022 SignalMediaMonitor(this, info);
2023}
2024
2025void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002026 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027 ScreencastEventMessageData* pdata =
2028 new ScreencastEventMessageData(ssrc, event);
2029 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2030}
2031
2032void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2033 // Map capturer events to window events. In the future we may want to simply
2034 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002035 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002037 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002039 we = rtc::WE_MINIMIZE;
2040 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2041 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 } else {
2043 return;
2044 }
2045 previous_we_ = we;
2046
2047 uint32 ssrc = 0;
2048 if (!GetLocalSsrc(capturer, &ssrc)) {
2049 return;
2050 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002051
2052 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053}
2054
2055bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2056 *ssrc = 0;
2057 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2058 iter != screencast_capturers_.end(); ++iter) {
2059 if (iter->second == capturer) {
2060 *ssrc = iter->first;
2061 return true;
2062 }
2063 }
2064 return false;
2065}
2066
2067void VideoChannel::OnVideoChannelError(uint32 ssrc,
2068 VideoMediaChannel::Error error) {
2069 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2070 ssrc, error);
2071 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2072}
2073
2074void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2075 SrtpFilter::Error error) {
2076 switch (error) {
2077 case SrtpFilter::ERROR_FAIL:
2078 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2079 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2080 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2081 break;
2082 case SrtpFilter::ERROR_AUTH:
2083 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2084 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2085 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2086 break;
2087 case SrtpFilter::ERROR_REPLAY:
2088 // Only receving channel should have this error.
2089 ASSERT(mode == SrtpFilter::UNPROTECT);
2090 // TODO(gangji): Turn on the signaling of replay error once we have
2091 // switched to the new mechanism for doing video retransmissions.
2092 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2093 break;
2094 default:
2095 break;
2096 }
2097}
2098
2099
2100void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2101 GetSupportedVideoCryptoSuites(ciphers);
2102}
2103
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002104DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105 DataMediaChannel* media_channel,
2106 BaseSession* session,
2107 const std::string& content_name,
2108 bool rtcp)
2109 // MediaEngine is NULL
2110 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002111 data_channel_type_(cricket::DCT_NONE),
2112 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113}
2114
2115DataChannel::~DataChannel() {
2116 StopMediaMonitor();
2117 // this can't be done in the base class, since it calls a virtual
2118 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002119
2120 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002121}
2122
2123bool DataChannel::Init() {
2124 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
2125 content_name(), "data_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
2126 if (!BaseChannel::Init(session()->CreateChannel(
2127 content_name(), "data_rtp", ICE_CANDIDATE_COMPONENT_RTP),
2128 rtcp_channel)) {
2129 return false;
2130 }
2131 media_channel()->SignalDataReceived.connect(
2132 this, &DataChannel::OnDataReceived);
2133 media_channel()->SignalMediaError.connect(
2134 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002135 media_channel()->SignalReadyToSend.connect(
2136 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002137 media_channel()->SignalStreamClosedRemotely.connect(
2138 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 srtp_filter()->SignalSrtpError.connect(
2140 this, &DataChannel::OnSrtpError);
2141 return true;
2142}
2143
2144bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002145 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002147 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2148 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149}
2150
2151const ContentInfo* DataChannel::GetFirstContent(
2152 const SessionDescription* sdesc) {
2153 return GetFirstDataContent(sdesc);
2154}
2155
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002156bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 if (data_channel_type_ == DCT_SCTP) {
2158 // TODO(pthatcher): Do this in a more robust way by checking for
2159 // SCTP or DTLS.
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +00002160 return !IsRtpPacket(packet->data(), packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161 } else if (data_channel_type_ == DCT_RTP) {
2162 return BaseChannel::WantsPacket(rtcp, packet);
2163 }
2164 return false;
2165}
2166
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002167bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2168 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 // It hasn't been set before, so set it now.
2170 if (data_channel_type_ == DCT_NONE) {
2171 data_channel_type_ = new_data_channel_type;
2172 return true;
2173 }
2174
2175 // It's been set before, but doesn't match. That's bad.
2176 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002177 std::ostringstream desc;
2178 desc << "Data channel type mismatch."
2179 << " Expected " << data_channel_type_
2180 << " Got " << new_data_channel_type;
2181 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182 return false;
2183 }
2184
2185 // It's hasn't changed. Nothing to do.
2186 return true;
2187}
2188
2189bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002190 const DataContentDescription* content,
2191 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2193 (content->protocol() == kMediaProtocolDtlsSctp));
2194 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002195 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196}
2197
2198bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002199 ContentAction action,
2200 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002201 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 LOG(LS_INFO) << "Setting local data description";
2203
2204 const DataContentDescription* data =
2205 static_cast<const DataContentDescription*>(content);
2206 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002207 if (!data) {
2208 SafeSetError("Can't find data content in local description.", error_desc);
2209 return false;
2210 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211
2212 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002213 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 return false;
2215 }
2216
2217 if (data_channel_type_ == DCT_SCTP) {
2218 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002219 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220 if (ret) {
2221 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002222 // As in SetRemoteContent_w, make sure we set the local SCTP port
2223 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002224 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2225 SafeSetError("Failed to set data receive codecs.", error_desc);
2226 ret = false;
2227 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228 }
2229 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002230 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002232 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2233 SafeSetError("Failed to set data receive codecs.", error_desc);
2234 ret = false;
2235 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 }
2237 }
2238
2239 // If everything worked, see if we can start receiving.
2240 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00002241 std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2242 for (; it != data->codecs().end(); ++it) {
2243 bundle_filter()->AddPayloadType(it->id);
2244 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245 ChangeState();
2246 } else {
2247 LOG(LS_WARNING) << "Failed to set local data description";
2248 }
2249 return ret;
2250}
2251
2252bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002253 ContentAction action,
2254 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002255 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256
2257 const DataContentDescription* data =
2258 static_cast<const DataContentDescription*>(content);
2259 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002260 if (!data) {
2261 SafeSetError("Can't find data content in remote description.", error_desc);
2262 return false;
2263 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002264
2265 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002266 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267 return false;
2268 }
2269
2270 if (data_channel_type_ == DCT_SCTP) {
2271 LOG(LS_INFO) << "Setting SCTP remote data description";
2272 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002273 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274 if (ret) {
2275 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002276 // We send the SCTP port number (not to be confused with the underlying
2277 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002278 if (!media_channel()->SetSendCodecs(data->codecs())) {
2279 SafeSetError("Failed to set data send codecs.", error_desc);
2280 ret = false;
2281 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 }
2283 } else {
2284 // If the remote data doesn't have codecs and isn't an update, it
2285 // must be empty, so ignore it.
2286 if (action != CA_UPDATE && !data->has_codecs()) {
2287 return true;
2288 }
2289 LOG(LS_INFO) << "Setting remote data description";
2290
2291 // Set remote video codecs (what the other side wants to receive).
2292 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002293 if (!media_channel()->SetSendCodecs(data->codecs())) {
2294 SafeSetError("Failed to set data send codecs.", error_desc);
2295 ret = false;
2296 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 }
2298
2299 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002300 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301 }
2302
2303 if (action != CA_UPDATE) {
2304 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002305 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2306 std::ostringstream desc;
2307 desc << "Failed to set max send bandwidth for data content.";
2308 SafeSetError(desc.str(), error_desc);
2309 ret = false;
2310 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311 }
2312 }
2313
2314 // If everything worked, see if we can start sending.
2315 if (ret) {
2316 ChangeState();
2317 } else {
2318 LOG(LS_WARNING) << "Failed to set remote data description";
2319 }
2320 return ret;
2321}
2322
2323void DataChannel::ChangeState() {
2324 // Render incoming data if we're the active call, and we have the local
2325 // content. We receive data on the default channel and multiplexed streams.
2326 bool recv = IsReadyToReceive();
2327 if (!media_channel()->SetReceive(recv)) {
2328 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2329 }
2330
2331 // Send outgoing data if we're the active call, we have the remote content,
2332 // and we have had some form of connectivity.
2333 bool send = IsReadyToSend();
2334 if (!media_channel()->SetSend(send)) {
2335 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2336 }
2337
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002338 // Trigger SignalReadyToSendData asynchronously.
2339 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002340
2341 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2342}
2343
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002344void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002345 switch (pmsg->message_id) {
2346 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002347 DataChannelReadyToSendMessageData* data =
2348 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002349 ready_to_send_data_ = data->data();
2350 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002351 delete data;
2352 break;
2353 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002354 case MSG_DATARECEIVED: {
2355 DataReceivedMessageData* data =
2356 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2357 SignalDataReceived(this, data->params, data->payload);
2358 delete data;
2359 break;
2360 }
2361 case MSG_CHANNEL_ERROR: {
2362 const DataChannelErrorMessageData* data =
2363 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2364 SignalMediaError(this, data->ssrc, data->error);
2365 delete data;
2366 break;
2367 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002368 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002369 rtc::TypedMessageData<uint32>* data =
2370 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002371 SignalStreamClosedRemotely(data->data());
2372 delete data;
2373 break;
2374 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002375 default:
2376 BaseChannel::OnMessage(pmsg);
2377 break;
2378 }
2379}
2380
2381void DataChannel::OnConnectionMonitorUpdate(
pthatcher@webrtc.orgb4aac132015-03-13 18:25:21 +00002382 ConnectionMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002383 SignalConnectionMonitor(this, infos);
2384}
2385
2386void DataChannel::StartMediaMonitor(int cms) {
2387 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002388 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002389 media_monitor_->SignalUpdate.connect(
2390 this, &DataChannel::OnMediaMonitorUpdate);
2391 media_monitor_->Start(cms);
2392}
2393
2394void DataChannel::StopMediaMonitor() {
2395 if (media_monitor_) {
2396 media_monitor_->Stop();
2397 media_monitor_->SignalUpdate.disconnect(this);
2398 media_monitor_.reset();
2399 }
2400}
2401
2402void DataChannel::OnMediaMonitorUpdate(
2403 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2404 ASSERT(media_channel == this->media_channel());
2405 SignalMediaMonitor(this, info);
2406}
2407
2408void DataChannel::OnDataReceived(
2409 const ReceiveDataParams& params, const char* data, size_t len) {
2410 DataReceivedMessageData* msg = new DataReceivedMessageData(
2411 params, data, len);
2412 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2413}
2414
2415void DataChannel::OnDataChannelError(
2416 uint32 ssrc, DataMediaChannel::Error err) {
2417 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2418 ssrc, err);
2419 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2420}
2421
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002422void DataChannel::OnDataChannelReadyToSend(bool writable) {
2423 // This is usded for congestion control to indicate that the stream is ready
2424 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2425 // that the transport channel is ready.
2426 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2427 new DataChannelReadyToSendMessageData(writable));
2428}
2429
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002430void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2431 SrtpFilter::Error error) {
2432 switch (error) {
2433 case SrtpFilter::ERROR_FAIL:
2434 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2435 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2436 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2437 break;
2438 case SrtpFilter::ERROR_AUTH:
2439 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2440 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2441 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2442 break;
2443 case SrtpFilter::ERROR_REPLAY:
2444 // Only receving channel should have this error.
2445 ASSERT(mode == SrtpFilter::UNPROTECT);
2446 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2447 break;
2448 default:
2449 break;
2450 }
2451}
2452
2453void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2454 GetSupportedDataCryptoSuites(ciphers);
2455}
2456
2457bool DataChannel::ShouldSetupDtlsSrtp() const {
2458 return (data_channel_type_ == DCT_RTP);
2459}
2460
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002461void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002462 rtc::TypedMessageData<uint32>* message =
2463 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002464 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2465}
2466
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467} // namespace cricket