blob: c31fe45e24d8ae1ce683833deddf71520e2a84ed [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) {
281 socket_monitor_.reset(new SocketMonitor(transport_channel_,
282 worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000283 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000284 socket_monitor_->SignalUpdate.connect(
285 this, &BaseChannel::OnConnectionMonitorUpdate);
286 socket_monitor_->Start(cms);
287}
288
289void BaseChannel::StopConnectionMonitor() {
290 if (socket_monitor_) {
291 socket_monitor_->Stop();
292 socket_monitor_.reset();
293 }
294}
295
296void BaseChannel::set_rtcp_transport_channel(TransportChannel* channel) {
297 if (rtcp_transport_channel_ != channel) {
298 if (rtcp_transport_channel_) {
299 session_->DestroyChannel(
300 content_name_, rtcp_transport_channel_->component());
301 }
302 rtcp_transport_channel_ = channel;
303 if (rtcp_transport_channel_) {
304 // TODO(juberti): Propagate this error code
305 VERIFY(SetDtlsSrtpCiphers(rtcp_transport_channel_, true));
306 rtcp_transport_channel_->SignalWritableState.connect(
307 this, &BaseChannel::OnWritableState);
308 rtcp_transport_channel_->SignalReadPacket.connect(
309 this, &BaseChannel::OnChannelRead);
310 rtcp_transport_channel_->SignalReadyToSend.connect(
311 this, &BaseChannel::OnReadyToSend);
312 }
313 }
314}
315
316bool BaseChannel::IsReadyToReceive() const {
317 // Receive data if we are enabled and have local content,
318 return enabled() && IsReceiveContentDirection(local_content_direction_);
319}
320
321bool BaseChannel::IsReadyToSend() const {
322 // Send outgoing data if we are enabled, have local and remote content,
323 // and we have had some form of connectivity.
324 return enabled() &&
325 IsReceiveContentDirection(remote_content_direction_) &&
326 IsSendContentDirection(local_content_direction_) &&
327 was_ever_writable();
328}
329
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000330bool BaseChannel::SendPacket(rtc::Buffer* packet,
331 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000332 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333}
334
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000335bool BaseChannel::SendRtcp(rtc::Buffer* packet,
336 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000337 return SendPacket(true, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000338}
339
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000340int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000342 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000343 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000344 case ST_RTP:
345 channel = transport_channel_;
346 break;
347 case ST_RTCP:
348 channel = rtcp_transport_channel_;
349 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000351 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352}
353
354void BaseChannel::OnWritableState(TransportChannel* channel) {
355 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
356 if (transport_channel_->writable()
357 && (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
358 ChannelWritable_w();
359 } else {
360 ChannelNotWritable_w();
361 }
362}
363
364void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000365 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000366 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000367 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000369 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000370
371 // When using RTCP multiplexing we might get RTCP packets on the RTP
372 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
373 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000374 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000375 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376}
377
378void BaseChannel::OnReadyToSend(TransportChannel* channel) {
379 SetReadyToSend(channel, true);
380}
381
382void BaseChannel::SetReadyToSend(TransportChannel* channel, bool ready) {
383 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
384 if (channel == transport_channel_) {
385 rtp_ready_to_send_ = ready;
386 }
387 if (channel == rtcp_transport_channel_) {
388 rtcp_ready_to_send_ = ready;
389 }
390
391 if (!ready) {
392 // Notify the MediaChannel when either rtp or rtcp channel can't send.
393 media_channel_->OnReadyToSend(false);
394 } else if (rtp_ready_to_send_ &&
395 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
396 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
397 // Notify the MediaChannel when both rtp and rtcp channel can send.
398 media_channel_->OnReadyToSend(true);
399 }
400}
401
402bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
403 const char* data, size_t len) {
404 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000405 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406}
407
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000408bool BaseChannel::SendPacket(bool rtcp, rtc::Buffer* packet,
409 rtc::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410 // SendPacket gets called from MediaEngine, typically on an encoder thread.
411 // If the thread is not our worker thread, we will post to our worker
412 // so that the real work happens on our worker. This avoids us having to
413 // synchronize access to all the pieces of the send path, including
414 // SRTP and the inner workings of the transport channels.
415 // The only downside is that we can't return a proper failure code if
416 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000417 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000418 // Avoid a copy by transferring the ownership of the packet data.
419 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
420 PacketMessageData* data = new PacketMessageData;
421 packet->TransferTo(&data->packet);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000422 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 worker_thread_->Post(this, message_id, data);
424 return true;
425 }
426
427 // Now that we are on the correct thread, ensure we have a place to send this
428 // packet before doing anything. (We might get RTCP packets that we don't
429 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
430 // transport.
431 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
432 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000433 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 return false;
435 }
436
437 // Protect ourselves against crazy data.
438 if (!ValidPacket(rtcp, packet)) {
439 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
440 << PacketType(rtcp) << " packet: wrong size="
441 << packet->length();
442 return false;
443 }
444
445 // Signal to the media sink before protecting the packet.
446 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000447 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448 SignalSendPacketPreCrypto(packet->data(), packet->length(), rtcp);
449 }
450
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000451 rtc::PacketOptions options(dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000452 // Protect if needed.
453 if (srtp_filter_.IsActive()) {
454 bool res;
455 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000456 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000457 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000458 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
459 // inside libsrtp for a RTP packet. A external HMAC module will be writing
460 // a fake HMAC value. This is ONLY done for a RTP packet.
461 // Socket layer will update rtp sendtime extension header if present in
462 // packet with current time before updating the HMAC.
463#if !defined(ENABLE_EXTERNAL_AUTH)
464 res = srtp_filter_.ProtectRtp(
465 data, len, static_cast<int>(packet->capacity()), &len);
466#else
henrike@webrtc.org05376342014-03-10 15:53:12 +0000467 options.packet_time_params.rtp_sendtime_extension_id =
468 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000469 res = srtp_filter_.ProtectRtp(
470 data, len, static_cast<int>(packet->capacity()), &len,
471 &options.packet_time_params.srtp_packet_index);
472 // If protection succeeds, let's get auth params from srtp.
473 if (res) {
474 uint8* auth_key = NULL;
475 int key_len;
476 res = srtp_filter_.GetRtpAuthParams(
477 &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
478 if (res) {
479 options.packet_time_params.srtp_auth_key.resize(key_len);
480 options.packet_time_params.srtp_auth_key.assign(auth_key,
481 auth_key + key_len);
482 }
483 }
484#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000485 if (!res) {
486 int seq_num = -1;
487 uint32 ssrc = 0;
488 GetRtpSeqNum(data, len, &seq_num);
489 GetRtpSsrc(data, len, &ssrc);
490 LOG(LS_ERROR) << "Failed to protect " << content_name_
491 << " RTP packet: size=" << len
492 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
493 return false;
494 }
495 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000496 res = srtp_filter_.ProtectRtcp(data, len,
497 static_cast<int>(packet->capacity()),
498 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499 if (!res) {
500 int type = -1;
501 GetRtcpType(data, len, &type);
502 LOG(LS_ERROR) << "Failed to protect " << content_name_
503 << " RTCP packet: size=" << len << ", type=" << type;
504 return false;
505 }
506 }
507
508 // Update the length of the packet now that we've added the auth tag.
509 packet->SetLength(len);
510 } else if (secure_required_) {
511 // This is a double check for something that supposedly can't happen.
512 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
513 << " packet when SRTP is inactive and crypto is required";
514
515 ASSERT(false);
516 return false;
517 }
518
519 // Signal to the media sink after protecting the packet.
520 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000521 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522 SignalSendPacketPostCrypto(packet->data(), packet->length(), rtcp);
523 }
524
525 // Bon voyage.
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000526 int ret = channel->SendPacket(packet->data(), packet->length(), options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000527 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
528 if (ret != static_cast<int>(packet->length())) {
529 if (channel->GetError() == EWOULDBLOCK) {
530 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
531 SetReadyToSend(channel, false);
532 }
533 return false;
534 }
535 return true;
536}
537
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000538bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000539 // Protect ourselves against crazy data.
540 if (!ValidPacket(rtcp, packet)) {
541 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
542 << PacketType(rtcp) << " packet: wrong size="
543 << packet->length();
544 return false;
545 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000546
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000547 // Bundle filter handles both rtp and rtcp packets.
548 return bundle_filter_.DemuxPacket(packet->data(), packet->length(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000549}
550
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000551void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
552 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000553 if (!WantsPacket(rtcp, packet)) {
554 return;
555 }
556
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000557 if (!has_received_packet_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000558 has_received_packet_ = true;
559 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
560 }
561
562 // Signal to the media sink before unprotecting the packet.
563 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000564 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565 SignalRecvPacketPostCrypto(packet->data(), packet->length(), rtcp);
566 }
567
568 // Unprotect the packet, if needed.
569 if (srtp_filter_.IsActive()) {
570 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000571 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000572 bool res;
573 if (!rtcp) {
574 res = srtp_filter_.UnprotectRtp(data, len, &len);
575 if (!res) {
576 int seq_num = -1;
577 uint32 ssrc = 0;
578 GetRtpSeqNum(data, len, &seq_num);
579 GetRtpSsrc(data, len, &ssrc);
580 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
581 << " RTP packet: size=" << len
582 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
583 return;
584 }
585 } else {
586 res = srtp_filter_.UnprotectRtcp(data, len, &len);
587 if (!res) {
588 int type = -1;
589 GetRtcpType(data, len, &type);
590 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
591 << " RTCP packet: size=" << len << ", type=" << type;
592 return;
593 }
594 }
595
596 packet->SetLength(len);
597 } else if (secure_required_) {
598 // Our session description indicates that SRTP is required, but we got a
599 // packet before our SRTP filter is active. This means either that
600 // a) we got SRTP packets before we received the SDES keys, in which case
601 // we can't decrypt it anyway, or
602 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
603 // channels, so we haven't yet extracted keys, even if DTLS did complete
604 // on the channel that the packets are being sent on. It's really good
605 // practice to wait for both RTP and RTCP to be good to go before sending
606 // media, to prevent weird failure modes, so it's fine for us to just eat
607 // packets here. This is all sidestepped if RTCP mux is used anyway.
608 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
609 << " packet when SRTP is inactive and crypto is required";
610 return;
611 }
612
613 // Signal to the media sink after unprotecting the packet.
614 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000615 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000616 SignalRecvPacketPreCrypto(packet->data(), packet->length(), rtcp);
617 }
618
619 // Push it down to the media channel.
620 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000621 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000623 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624 }
625}
626
627void BaseChannel::OnNewLocalDescription(
628 BaseSession* session, ContentAction action) {
629 const ContentInfo* content_info =
630 GetFirstContent(session->local_description());
631 const MediaContentDescription* content_desc =
632 GetContentDescription(content_info);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000633 std::string error_desc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000634 if (content_desc && content_info && !content_info->rejected &&
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000635 !SetLocalContent(content_desc, action, &error_desc)) {
636 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 }
639}
640
641void BaseChannel::OnNewRemoteDescription(
642 BaseSession* session, ContentAction action) {
643 const ContentInfo* content_info =
644 GetFirstContent(session->remote_description());
645 const MediaContentDescription* content_desc =
646 GetContentDescription(content_info);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000647 std::string error_desc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 if (content_desc && content_info && !content_info->rejected &&
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000649 !SetRemoteContent(content_desc, action, &error_desc)) {
650 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
651 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000652 }
653}
654
655void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000656 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 if (enabled_)
658 return;
659
660 LOG(LS_INFO) << "Channel enabled";
661 enabled_ = true;
662 ChangeState();
663}
664
665void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000666 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 if (!enabled_)
668 return;
669
670 LOG(LS_INFO) << "Channel disabled";
671 enabled_ = false;
672 ChangeState();
673}
674
675bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000676 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 bool ret = media_channel()->MuteStream(ssrc, mute);
678 if (ret) {
679 if (mute)
680 muted_streams_.insert(ssrc);
681 else
682 muted_streams_.erase(ssrc);
683 }
684 return ret;
685}
686
687bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000688 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000689 return muted_streams_.find(ssrc) != muted_streams_.end();
690}
691
692void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000693 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000694 if (writable_)
695 return;
696
697 LOG(LS_INFO) << "Channel socket writable ("
698 << transport_channel_->content_name() << ", "
699 << transport_channel_->component() << ")"
700 << (was_ever_writable_ ? "" : " for the first time");
701
702 std::vector<ConnectionInfo> infos;
703 transport_channel_->GetStats(&infos);
704 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
705 it != infos.end(); ++it) {
706 if (it->best_connection) {
707 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
708 << "->" << it->remote_candidate.ToSensitiveString();
709 break;
710 }
711 }
712
713 // If we're doing DTLS-SRTP, now is the time.
714 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
715 if (!SetupDtlsSrtp(false)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000716 const std::string error_desc =
717 "Couldn't set up DTLS-SRTP on RTP channel.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000718 // Sent synchronously.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000719 signaling_thread()->Invoke<void>(Bind(
720 &SetSessionError,
721 session_,
722 BaseSession::ERROR_TRANSPORT,
723 error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000724 return;
725 }
726
727 if (rtcp_transport_channel_) {
728 if (!SetupDtlsSrtp(true)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000729 const std::string error_desc =
730 "Couldn't set up DTLS-SRTP on RTCP channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000731 // Sent synchronously.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000732 signaling_thread()->Invoke<void>(Bind(
733 &SetSessionError,
734 session_,
735 BaseSession::ERROR_TRANSPORT,
736 error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000737 return;
738 }
739 }
740 }
741
742 was_ever_writable_ = true;
743 writable_ = true;
744 ChangeState();
745}
746
747bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
748 std::vector<std::string> ciphers;
749 // We always use the default SRTP ciphers for RTCP, but we may use different
750 // ciphers for RTP depending on the media type.
751 if (!rtcp) {
752 GetSrtpCiphers(&ciphers);
753 } else {
754 GetSupportedDefaultCryptoSuites(&ciphers);
755 }
756 return tc->SetSrtpCiphers(ciphers);
757}
758
759bool BaseChannel::ShouldSetupDtlsSrtp() const {
760 return true;
761}
762
763// This function returns true if either DTLS-SRTP is not in use
764// *or* DTLS-SRTP is successfully set up.
765bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
766 bool ret = false;
767
768 TransportChannel *channel = rtcp_channel ?
769 rtcp_transport_channel_ : transport_channel_;
770
771 // No DTLS
772 if (!channel->IsDtlsActive())
773 return true;
774
775 std::string selected_cipher;
776
777 if (!channel->GetSrtpCipher(&selected_cipher)) {
778 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
779 return false;
780 }
781
782 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
783 << content_name() << " "
784 << PacketType(rtcp_channel);
785
786 // OK, we're now doing DTLS (RFC 5764)
787 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
788 SRTP_MASTER_KEY_SALT_LEN * 2);
789
790 // RFC 5705 exporter using the RFC 5764 parameters
791 if (!channel->ExportKeyingMaterial(
792 kDtlsSrtpExporterLabel,
793 NULL, 0, false,
794 &dtls_buffer[0], dtls_buffer.size())) {
795 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
796 ASSERT(false); // This should never happen
797 return false;
798 }
799
800 // Sync up the keys with the DTLS-SRTP interface
801 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
802 SRTP_MASTER_KEY_SALT_LEN);
803 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
804 SRTP_MASTER_KEY_SALT_LEN);
805 size_t offset = 0;
806 memcpy(&client_write_key[0], &dtls_buffer[offset],
807 SRTP_MASTER_KEY_KEY_LEN);
808 offset += SRTP_MASTER_KEY_KEY_LEN;
809 memcpy(&server_write_key[0], &dtls_buffer[offset],
810 SRTP_MASTER_KEY_KEY_LEN);
811 offset += SRTP_MASTER_KEY_KEY_LEN;
812 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
813 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
814 offset += SRTP_MASTER_KEY_SALT_LEN;
815 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
816 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
817
818 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000819 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000820 if (!channel->GetSslRole(&role)) {
821 LOG(LS_WARNING) << "GetSslRole failed";
822 return false;
823 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000825 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000826 send_key = &server_write_key;
827 recv_key = &client_write_key;
828 } else {
829 send_key = &client_write_key;
830 recv_key = &server_write_key;
831 }
832
833 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000834 ret = srtp_filter_.SetRtcpParams(
835 selected_cipher,
836 &(*send_key)[0],
837 static_cast<int>(send_key->size()),
838 selected_cipher,
839 &(*recv_key)[0],
840 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000842 ret = srtp_filter_.SetRtpParams(
843 selected_cipher,
844 &(*send_key)[0],
845 static_cast<int>(send_key->size()),
846 selected_cipher,
847 &(*recv_key)[0],
848 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000849 }
850
851 if (!ret)
852 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
853 else
854 dtls_keyed_ = true;
855
856 return ret;
857}
858
859void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000860 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 if (!writable_)
862 return;
863
864 LOG(LS_INFO) << "Channel socket not writable ("
865 << transport_channel_->content_name() << ", "
866 << transport_channel_->component() << ")";
867 writable_ = false;
868 ChangeState();
869}
870
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000871// |dtls| will be set to true if DTLS is active for transport channel and
872// crypto is empty.
873bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000874 bool* dtls,
875 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000876 *dtls = transport_channel_->IsDtlsActive();
877 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000878 SafeSetError("Cryptos must be empty when DTLS is active.",
879 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000880 return false;
881 }
882 return true;
883}
884
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000885bool BaseChannel::SetRecvRtpHeaderExtensions_w(
886 const MediaContentDescription* content,
887 MediaChannel* media_channel,
888 std::string* error_desc) {
889 if (content->rtp_header_extensions_set()) {
890 if (!media_channel->SetRecvRtpHeaderExtensions(
891 content->rtp_header_extensions())) {
892 std::ostringstream desc;
893 desc << "Failed to set receive rtp header extensions for "
894 << MediaTypeToString(content->type()) << " content.";
895 SafeSetError(desc.str(), error_desc);
896 return false;
897 }
898 }
899 return true;
900}
901
902bool BaseChannel::SetSendRtpHeaderExtensions_w(
903 const MediaContentDescription* content,
904 MediaChannel* media_channel,
905 std::string* error_desc) {
906 if (content->rtp_header_extensions_set()) {
907 if (!media_channel->SetSendRtpHeaderExtensions(
908 content->rtp_header_extensions())) {
909 std::ostringstream desc;
910 desc << "Failed to set send rtp header extensions for "
911 << MediaTypeToString(content->type()) << " content.";
912 SafeSetError(desc.str(), error_desc);
913 return false;
914 } else {
915 MaybeCacheRtpAbsSendTimeHeaderExtension(content->rtp_header_extensions());
916 }
917 }
918 return true;
919}
920
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000922 ContentAction action,
923 ContentSource src,
924 std::string* error_desc) {
925 if (action == CA_UPDATE) {
926 // no crypto params.
927 return true;
928 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000930 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000931 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
932 if (!ret) {
933 return false;
934 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 switch (action) {
936 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000937 // If DTLS is already active on the channel, we could be renegotiating
938 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000939 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000940 ret = srtp_filter_.SetOffer(cryptos, src);
941 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 break;
943 case CA_PRANSWER:
944 // If we're doing DTLS-SRTP, we don't want to update the filter
945 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000946 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
948 }
949 break;
950 case CA_ANSWER:
951 // If we're doing DTLS-SRTP, we don't want to update the filter
952 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000953 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 ret = srtp_filter_.SetAnswer(cryptos, src);
955 }
956 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 default:
958 break;
959 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000960 if (!ret) {
961 SafeSetError("Failed to setup SRTP filter.", error_desc);
962 return false;
963 }
964 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965}
966
967bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000968 ContentSource src,
969 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 bool ret = false;
971 switch (action) {
972 case CA_OFFER:
973 ret = rtcp_mux_filter_.SetOffer(enable, src);
974 break;
975 case CA_PRANSWER:
976 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
977 break;
978 case CA_ANSWER:
979 ret = rtcp_mux_filter_.SetAnswer(enable, src);
980 if (ret && rtcp_mux_filter_.IsActive()) {
981 // We activated RTCP mux, close down the RTCP transport.
982 set_rtcp_transport_channel(NULL);
983 }
984 break;
985 case CA_UPDATE:
986 // No RTCP mux info.
987 ret = true;
988 default:
989 break;
990 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000991 if (!ret) {
992 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
993 return false;
994 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
996 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
997 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000998 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 // If the RTP transport is already writable, then so are we.
1000 if (transport_channel_->writable()) {
1001 ChannelWritable_w();
1002 }
1003 }
1004
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001005 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006}
1007
1008bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001009 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 if (!media_channel()->AddRecvStream(sp))
1011 return false;
1012
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001013 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014}
1015
1016bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001017 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001018 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 return media_channel()->RemoveRecvStream(ssrc);
1020}
1021
1022bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001023 ContentAction action,
1024 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1026 action == CA_PRANSWER || action == CA_UPDATE))
1027 return false;
1028
1029 // If this is an update, streams only contain streams that have changed.
1030 if (action == CA_UPDATE) {
1031 for (StreamParamsVec::const_iterator it = streams.begin();
1032 it != streams.end(); ++it) {
1033 StreamParams existing_stream;
1034 bool stream_exist = GetStreamByIds(local_streams_, it->groupid,
1035 it->id, &existing_stream);
1036 if (!stream_exist && it->has_ssrcs()) {
1037 if (media_channel()->AddSendStream(*it)) {
1038 local_streams_.push_back(*it);
1039 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1040 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001041 std::ostringstream desc;
1042 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1043 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 return false;
1045 }
1046 } else if (stream_exist && !it->has_ssrcs()) {
1047 if (!media_channel()->RemoveSendStream(existing_stream.first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001048 std::ostringstream desc;
1049 desc << "Failed to remove send stream with ssrc "
1050 << it->first_ssrc() << ".";
1051 SafeSetError(desc.str(), error_desc);
1052 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 }
1054 RemoveStreamBySsrc(&local_streams_, existing_stream.first_ssrc());
1055 } else {
1056 LOG(LS_WARNING) << "Ignore unsupported stream update";
1057 }
1058 }
1059 return true;
1060 }
1061 // Else streams are all the streams we want to send.
1062
1063 // Check for streams that have been removed.
1064 bool ret = true;
1065 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1066 it != local_streams_.end(); ++it) {
1067 if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1068 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001069 std::ostringstream desc;
1070 desc << "Failed to remove send stream with ssrc "
1071 << it->first_ssrc() << ".";
1072 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 ret = false;
1074 }
1075 }
1076 }
1077 // Check for new streams.
1078 for (StreamParamsVec::const_iterator it = streams.begin();
1079 it != streams.end(); ++it) {
1080 if (!GetStreamBySsrc(local_streams_, it->first_ssrc(), NULL)) {
1081 if (media_channel()->AddSendStream(*it)) {
1082 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1083 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001084 std::ostringstream desc;
1085 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1086 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087 ret = false;
1088 }
1089 }
1090 }
1091 local_streams_ = streams;
1092 return ret;
1093}
1094
1095bool BaseChannel::UpdateRemoteStreams_w(
1096 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001097 ContentAction action,
1098 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1100 action == CA_PRANSWER || action == CA_UPDATE))
1101 return false;
1102
1103 // If this is an update, streams only contain streams that have changed.
1104 if (action == CA_UPDATE) {
1105 for (StreamParamsVec::const_iterator it = streams.begin();
1106 it != streams.end(); ++it) {
1107 StreamParams existing_stream;
1108 bool stream_exists = GetStreamByIds(remote_streams_, it->groupid,
1109 it->id, &existing_stream);
1110 if (!stream_exists && it->has_ssrcs()) {
1111 if (AddRecvStream_w(*it)) {
1112 remote_streams_.push_back(*it);
1113 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1114 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001115 std::ostringstream desc;
1116 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1117 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001118 return false;
1119 }
1120 } else if (stream_exists && !it->has_ssrcs()) {
1121 if (!RemoveRecvStream_w(existing_stream.first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001122 std::ostringstream desc;
1123 desc << "Failed to remove remote stream with ssrc "
1124 << it->first_ssrc() << ".";
1125 SafeSetError(desc.str(), error_desc);
1126 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 }
1128 RemoveStreamBySsrc(&remote_streams_, existing_stream.first_ssrc());
1129 } else {
1130 LOG(LS_WARNING) << "Ignore unsupported stream update."
1131 << " Stream exists? " << stream_exists
1132 << " existing stream = " << existing_stream.ToString()
1133 << " new stream = " << it->ToString();
1134 }
1135 }
1136 return true;
1137 }
1138 // Else streams are all the streams we want to receive.
1139
1140 // Check for streams that have been removed.
1141 bool ret = true;
1142 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1143 it != remote_streams_.end(); ++it) {
1144 if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1145 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001146 std::ostringstream desc;
1147 desc << "Failed to remove remote stream with ssrc "
1148 << it->first_ssrc() << ".";
1149 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 ret = false;
1151 }
1152 }
1153 }
1154 // Check for new streams.
1155 for (StreamParamsVec::const_iterator it = streams.begin();
1156 it != streams.end(); ++it) {
1157 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc(), NULL)) {
1158 if (AddRecvStream_w(*it)) {
1159 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1160 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001161 std::ostringstream desc;
1162 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1163 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 ret = false;
1165 }
1166 }
1167 }
1168 remote_streams_ = streams;
1169 return ret;
1170}
1171
1172bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001173 ContentAction action,
1174 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001175 // Cache secure_required_ for belt and suspenders check on SendPacket
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001176 secure_required_ = content->crypto_required() != CT_NONE;
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001177 // Set local RTP header extensions.
1178 bool ret = SetRecvRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001179 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001180 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001182 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001183
1184 // Call UpdateLocalStreams_w last to make sure as many settings as possible
1185 // are already set when creating streams.
1186 ret &= UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 set_local_content_direction(content->direction());
1188 return ret;
1189}
1190
1191bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001192 ContentAction action,
1193 std::string* error_desc) {
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001194 // Set remote RTP header extensions.
1195 bool ret = SetSendRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001197 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001198 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001199 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001200 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1201 std::ostringstream desc;
1202 desc << "Failed to set max send bandwidth for "
1203 << MediaTypeToString(content->type()) << " content.";
1204 SafeSetError(desc.str(), error_desc);
1205 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 }
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001207
1208 // Call UpdateRemoteStreams_w last to make sure as many settings as possible
1209 // are already set when creating streams.
1210 ret &= UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 set_remote_content_direction(content->direction());
1212 return ret;
1213}
1214
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001215void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1216 const std::vector<RtpHeaderExtension>& extensions) {
1217 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001218 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001219 rtp_abs_sendtime_extn_id_ =
1220 send_time_extension ? send_time_extension->id : -1;
1221}
1222
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001223void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 case MSG_RTPPACKET:
1226 case MSG_RTCPPACKET: {
1227 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001228 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229 delete data; // because it is Posted
1230 break;
1231 }
1232 case MSG_FIRSTPACKETRECEIVED: {
1233 SignalFirstPacketReceived(this);
1234 break;
1235 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236 }
1237}
1238
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001239void BaseChannel::FlushRtcpMessages() {
1240 // Flush all remaining RTCP messages. This should only be called in
1241 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001242 ASSERT(rtc::Thread::Current() == worker_thread_);
1243 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001244 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001245 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001247 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001248 }
1249}
1250
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001251VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 MediaEngineInterface* media_engine,
1253 VoiceMediaChannel* media_channel,
1254 BaseSession* session,
1255 const std::string& content_name,
1256 bool rtcp)
1257 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1258 rtcp),
1259 received_media_(false) {
1260}
1261
1262VoiceChannel::~VoiceChannel() {
1263 StopAudioMonitor();
1264 StopMediaMonitor();
1265 // this can't be done in the base class, since it calls a virtual
1266 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001267 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268}
1269
1270bool VoiceChannel::Init() {
1271 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1272 content_name(), "rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1273 if (!BaseChannel::Init(session()->CreateChannel(
1274 content_name(), "rtp", ICE_CANDIDATE_COMPONENT_RTP),
1275 rtcp_channel)) {
1276 return false;
1277 }
1278 media_channel()->SignalMediaError.connect(
1279 this, &VoiceChannel::OnVoiceChannelError);
1280 srtp_filter()->SignalSrtpError.connect(
1281 this, &VoiceChannel::OnSrtpError);
1282 return true;
1283}
1284
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001285bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001286 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1287 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001288}
1289
1290bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001291 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1292 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001293}
1294
1295bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001296 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001297}
1298
1299// TODO(juberti): Handle early media the right way. We should get an explicit
1300// ringing message telling us to start playing local ringback, which we cancel
1301// if any early media actually arrives. For now, we do the opposite, which is
1302// to wait 1 second for early media, and start playing local ringback if none
1303// arrives.
1304void VoiceChannel::SetEarlyMedia(bool enable) {
1305 if (enable) {
1306 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001307 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1308 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 } else {
1310 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001311 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 }
1313}
1314
1315bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001316 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1317 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318}
1319
1320bool VoiceChannel::PressDTMF(int digit, bool playout) {
1321 int flags = DF_SEND;
1322 if (playout) {
1323 flags |= DF_PLAY;
1324 }
1325 int duration_ms = 160;
1326 return InsertDtmf(0, digit, duration_ms, flags);
1327}
1328
1329bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001330 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1331 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001332}
1333
1334bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1335 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001336 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1337 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001338}
1339
1340bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001341 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1342 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001344
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001346 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1347 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348}
1349
1350void VoiceChannel::StartMediaMonitor(int cms) {
1351 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001352 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353 media_monitor_->SignalUpdate.connect(
1354 this, &VoiceChannel::OnMediaMonitorUpdate);
1355 media_monitor_->Start(cms);
1356}
1357
1358void VoiceChannel::StopMediaMonitor() {
1359 if (media_monitor_) {
1360 media_monitor_->Stop();
1361 media_monitor_->SignalUpdate.disconnect(this);
1362 media_monitor_.reset();
1363 }
1364}
1365
1366void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001367 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 audio_monitor_
1369 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1370 audio_monitor_->Start(cms);
1371}
1372
1373void VoiceChannel::StopAudioMonitor() {
1374 if (audio_monitor_) {
1375 audio_monitor_->Stop();
1376 audio_monitor_.reset();
1377 }
1378}
1379
1380bool VoiceChannel::IsAudioMonitorRunning() const {
1381 return (audio_monitor_.get() != NULL);
1382}
1383
1384void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1385 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1386 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1387}
1388
1389void VoiceChannel::StopTypingMonitor() {
1390 typing_monitor_.reset();
1391}
1392
1393bool VoiceChannel::IsTypingMonitorRunning() const {
1394 return typing_monitor_;
1395}
1396
1397bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1398 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1399 if (typing_monitor_ && mute)
1400 typing_monitor_->OnChannelMuted();
1401 return ret;
1402}
1403
1404int VoiceChannel::GetInputLevel_w() {
1405 return media_engine()->GetInputLevel();
1406}
1407
1408int VoiceChannel::GetOutputLevel_w() {
1409 return media_channel()->GetOutputLevel();
1410}
1411
1412void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1413 media_channel()->GetActiveStreams(actives);
1414}
1415
1416void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001417 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001418 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001419 int flags) {
1420 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421
1422 // Set a flag when we've received an RTP packet. If we're waiting for early
1423 // media, this will disable the timeout.
1424 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1425 received_media_ = true;
1426 }
1427}
1428
1429void VoiceChannel::ChangeState() {
1430 // Render incoming data if we're the active call, and we have the local
1431 // content. We receive data on the default channel and multiplexed streams.
1432 bool recv = IsReadyToReceive();
1433 if (!media_channel()->SetPlayout(recv)) {
1434 SendLastMediaError();
1435 }
1436
1437 // Send outgoing data if we're the active call, we have the remote content,
1438 // and we have had some form of connectivity.
1439 bool send = IsReadyToSend();
1440 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1441 if (!media_channel()->SetSend(send_flag)) {
1442 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1443 SendLastMediaError();
1444 }
1445
1446 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1447}
1448
1449const ContentInfo* VoiceChannel::GetFirstContent(
1450 const SessionDescription* sdesc) {
1451 return GetFirstAudioContent(sdesc);
1452}
1453
1454bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001455 ContentAction action,
1456 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001457 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 LOG(LS_INFO) << "Setting local voice description";
1459
1460 const AudioContentDescription* audio =
1461 static_cast<const AudioContentDescription*>(content);
1462 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001463 if (!audio) {
1464 SafeSetError("Can't find audio content in local description.", error_desc);
1465 return false;
1466 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001468 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469 // Set local audio codecs (what we want to receive).
1470 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1471 // is set properly.
1472 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001473 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1474 SafeSetError("Failed to set audio receive codecs.", error_desc);
1475 ret = false;
1476 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 }
1478
1479 // If everything worked, see if we can start receiving.
1480 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001481 std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1482 for (; it != audio->codecs().end(); ++it) {
1483 bundle_filter()->AddPayloadType(it->id);
1484 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 ChangeState();
1486 } else {
1487 LOG(LS_WARNING) << "Failed to set local voice description";
1488 }
1489 return ret;
1490}
1491
1492bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001493 ContentAction action,
1494 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001495 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 LOG(LS_INFO) << "Setting remote voice description";
1497
1498 const AudioContentDescription* audio =
1499 static_cast<const AudioContentDescription*>(content);
1500 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001501 if (!audio) {
1502 SafeSetError("Can't find audio content in remote description.", error_desc);
1503 return false;
1504 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505
1506 bool ret = true;
1507 // Set remote video codecs (what the other side wants to receive).
1508 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001509 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1510 SafeSetError("Failed to set audio send codecs.", error_desc);
1511 ret = false;
1512 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513 }
1514
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001515 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516
1517 if (action != CA_UPDATE) {
1518 // Tweak our audio processing settings, if needed.
1519 AudioOptions audio_options;
1520 if (!media_channel()->GetOptions(&audio_options)) {
1521 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1522 } else {
1523 if (audio->conference_mode()) {
1524 audio_options.conference_mode.Set(true);
1525 }
1526 if (audio->agc_minus_10db()) {
1527 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1528 }
1529 if (!media_channel()->SetOptions(audio_options)) {
1530 // Log an error on failure, but don't abort the call.
1531 LOG(LS_ERROR) << "Failed to set voice channel options";
1532 }
1533 }
1534 }
1535
1536 // If everything worked, see if we can start sending.
1537 if (ret) {
1538 ChangeState();
1539 } else {
1540 LOG(LS_WARNING) << "Failed to set remote voice description";
1541 }
1542 return ret;
1543}
1544
1545bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001546 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1548}
1549
1550bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001551 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 if (play) {
1553 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1554 } else {
1555 LOG(LS_INFO) << "Stopping ringback tone";
1556 }
1557 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1558}
1559
1560void VoiceChannel::HandleEarlyMediaTimeout() {
1561 // This occurs on the main thread, not the worker thread.
1562 if (!received_media_) {
1563 LOG(LS_INFO) << "No early media received before timeout";
1564 SignalEarlyMediaTimeout(this);
1565 }
1566}
1567
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1569 int flags) {
1570 if (!enabled()) {
1571 return false;
1572 }
1573
1574 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1575}
1576
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001578 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1579 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580}
1581
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001582void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001583 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 case MSG_EARLYMEDIATIMEOUT:
1585 HandleEarlyMediaTimeout();
1586 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001587 case MSG_CHANNEL_ERROR: {
1588 VoiceChannelErrorMessageData* data =
1589 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1590 SignalMediaError(this, data->ssrc, data->error);
1591 delete data;
1592 break;
1593 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001594 default:
1595 BaseChannel::OnMessage(pmsg);
1596 break;
1597 }
1598}
1599
1600void VoiceChannel::OnConnectionMonitorUpdate(
1601 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
1602 SignalConnectionMonitor(this, infos);
1603}
1604
1605void VoiceChannel::OnMediaMonitorUpdate(
1606 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1607 ASSERT(media_channel == this->media_channel());
1608 SignalMediaMonitor(this, info);
1609}
1610
1611void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1612 const AudioInfo& info) {
1613 SignalAudioMonitor(this, info);
1614}
1615
1616void VoiceChannel::OnVoiceChannelError(
1617 uint32 ssrc, VoiceMediaChannel::Error err) {
1618 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1619 ssrc, err);
1620 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1621}
1622
1623void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1624 SrtpFilter::Error error) {
1625 switch (error) {
1626 case SrtpFilter::ERROR_FAIL:
1627 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1628 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1629 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1630 break;
1631 case SrtpFilter::ERROR_AUTH:
1632 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1633 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1634 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1635 break;
1636 case SrtpFilter::ERROR_REPLAY:
1637 // Only receving channel should have this error.
1638 ASSERT(mode == SrtpFilter::UNPROTECT);
1639 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1640 break;
1641 default:
1642 break;
1643 }
1644}
1645
1646void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1647 GetSupportedAudioCryptoSuites(ciphers);
1648}
1649
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001650VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 MediaEngineInterface* media_engine,
1652 VideoMediaChannel* media_channel,
1653 BaseSession* session,
1654 const std::string& content_name,
1655 bool rtcp,
1656 VoiceChannel* voice_channel)
1657 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1658 rtcp),
1659 voice_channel_(voice_channel),
1660 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001661 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662}
1663
1664bool VideoChannel::Init() {
1665 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1666 content_name(), "video_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1667 if (!BaseChannel::Init(session()->CreateChannel(
1668 content_name(), "video_rtp", ICE_CANDIDATE_COMPONENT_RTP),
1669 rtcp_channel)) {
1670 return false;
1671 }
1672 media_channel()->SignalMediaError.connect(
1673 this, &VideoChannel::OnVideoChannelError);
1674 srtp_filter()->SignalSrtpError.connect(
1675 this, &VideoChannel::OnSrtpError);
1676 return true;
1677}
1678
1679void VoiceChannel::SendLastMediaError() {
1680 uint32 ssrc;
1681 VoiceMediaChannel::Error error;
1682 media_channel()->GetLastMediaError(&ssrc, &error);
1683 SignalMediaError(this, ssrc, error);
1684}
1685
1686VideoChannel::~VideoChannel() {
1687 std::vector<uint32> screencast_ssrcs;
1688 ScreencastMap::iterator iter;
1689 while (!screencast_capturers_.empty()) {
1690 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1691 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1692 << screencast_capturers_.begin()->first;
1693 ASSERT(false);
1694 break;
1695 }
1696 }
1697
1698 StopMediaMonitor();
1699 // this can't be done in the base class, since it calls a virtual
1700 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001701
1702 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703}
1704
1705bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001706 worker_thread()->Invoke<void>(Bind(
1707 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001708 return true;
1709}
1710
1711bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001712 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713}
1714
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001715bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1716 return worker_thread()->Invoke<bool>(Bind(
1717 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718}
1719
1720bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001721 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1722 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001723}
1724
1725bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001726 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727}
1728
1729bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001730 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001731}
1732
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001733int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001734 ScreencastDetailsData data(ssrc);
1735 worker_thread()->Invoke<void>(Bind(
1736 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001737 return data.fps;
1738}
1739
1740int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001741 ScreencastDetailsData data(ssrc);
1742 worker_thread()->Invoke<void>(Bind(
1743 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001744 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745}
1746
1747bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001748 worker_thread()->Invoke<void>(Bind(
1749 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 return true;
1751}
1752
1753bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001754 worker_thread()->Invoke<void>(Bind(
1755 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 return true;
1757}
1758
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759void VideoChannel::ChangeState() {
1760 // Render incoming data if we're the active call, and we have the local
1761 // content. We receive data on the default channel and multiplexed streams.
1762 bool recv = IsReadyToReceive();
1763 if (!media_channel()->SetRender(recv)) {
1764 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1765 // TODO(gangji): Report error back to server.
1766 }
1767
1768 // Send outgoing data if we're the active call, we have the remote content,
1769 // and we have had some form of connectivity.
1770 bool send = IsReadyToSend();
1771 if (!media_channel()->SetSend(send)) {
1772 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1773 // TODO(gangji): Report error back to server.
1774 }
1775
1776 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1777}
1778
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001779bool VideoChannel::GetStats(
1780 const StatsOptions& options, VideoMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001781 return InvokeOnWorker(Bind(&VideoMediaChannel::GetStats,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001782 media_channel(), options, stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783}
1784
1785void VideoChannel::StartMediaMonitor(int cms) {
1786 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001787 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 media_monitor_->SignalUpdate.connect(
1789 this, &VideoChannel::OnMediaMonitorUpdate);
1790 media_monitor_->Start(cms);
1791}
1792
1793void VideoChannel::StopMediaMonitor() {
1794 if (media_monitor_) {
1795 media_monitor_->Stop();
1796 media_monitor_.reset();
1797 }
1798}
1799
1800const ContentInfo* VideoChannel::GetFirstContent(
1801 const SessionDescription* sdesc) {
1802 return GetFirstVideoContent(sdesc);
1803}
1804
1805bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001806 ContentAction action,
1807 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001808 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 LOG(LS_INFO) << "Setting local video description";
1810
1811 const VideoContentDescription* video =
1812 static_cast<const VideoContentDescription*>(content);
1813 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001814 if (!video) {
1815 SafeSetError("Can't find video content in local description.", error_desc);
1816 return false;
1817 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001819 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 // Set local video codecs (what we want to receive).
1821 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001822 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1823 SafeSetError("Failed to set video receive codecs.", error_desc);
1824 ret = false;
1825 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 }
1827
1828 if (action != CA_UPDATE) {
1829 VideoOptions video_options;
1830 media_channel()->GetOptions(&video_options);
1831 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1832
1833 if (!media_channel()->SetOptions(video_options)) {
1834 // Log an error on failure, but don't abort the call.
1835 LOG(LS_ERROR) << "Failed to set video channel options";
1836 }
1837 }
1838
1839 // If everything worked, see if we can start receiving.
1840 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001841 std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1842 for (; it != video->codecs().end(); ++it) {
1843 bundle_filter()->AddPayloadType(it->id);
1844 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 ChangeState();
1846 } else {
1847 LOG(LS_WARNING) << "Failed to set local video description";
1848 }
1849 return ret;
1850}
1851
1852bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001853 ContentAction action,
1854 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001855 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 LOG(LS_INFO) << "Setting remote video description";
1857
1858 const VideoContentDescription* video =
1859 static_cast<const VideoContentDescription*>(content);
1860 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001861 if (!video) {
1862 SafeSetError("Can't find video content in remote description.", error_desc);
1863 return false;
1864 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865
1866 bool ret = true;
1867 // Set remote video codecs (what the other side wants to receive).
1868 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001869 if (!media_channel()->SetSendCodecs(video->codecs())) {
1870 SafeSetError("Failed to set video send codecs.", error_desc);
1871 ret = false;
1872 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 }
1874
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001875 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876
1877 if (action != CA_UPDATE) {
1878 // Tweak our video processing settings, if needed.
1879 VideoOptions video_options;
1880 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001881 if (video->conference_mode()) {
1882 video_options.conference_mode.Set(true);
1883 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001884 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1885
1886 if (!media_channel()->SetOptions(video_options)) {
1887 // Log an error on failure, but don't abort the call.
1888 LOG(LS_ERROR) << "Failed to set video channel options";
1889 }
1890 }
1891
1892 // If everything worked, see if we can start sending.
1893 if (ret) {
1894 ChangeState();
1895 } else {
1896 LOG(LS_WARNING) << "Failed to set remote video description";
1897 }
1898 return ret;
1899}
1900
1901bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1902 bool ret = true;
1903 // Set the send format for each of the local streams. If the view request
1904 // does not contain a local stream, set its send format to 0x0, which will
1905 // drop all frames.
1906 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1907 it != local_streams().end(); ++it) {
1908 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1909 StaticVideoViews::const_iterator view;
1910 for (view = request.static_video_views.begin();
1911 view != request.static_video_views.end(); ++view) {
1912 if (view->selector.Matches(*it)) {
1913 format.width = view->width;
1914 format.height = view->height;
1915 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1916 break;
1917 }
1918 }
1919
1920 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1921 }
1922
1923 // Check if the view request has invalid streams.
1924 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1925 it != request.static_video_views.end(); ++it) {
1926 if (!GetStream(local_streams(), it->selector, NULL)) {
1927 LOG(LS_WARNING) << "View request for ("
1928 << it->selector.ssrc << ", '"
1929 << it->selector.groupid << "', '"
1930 << it->selector.streamid << "'"
1931 << ") is not in the local streams.";
1932 }
1933 }
1934
1935 return ret;
1936}
1937
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001938bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001940 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001942 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1943 screencast_capturers_[ssrc] = capturer;
1944 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945}
1946
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1948 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1949 if (iter == screencast_capturers_.end()) {
1950 return false;
1951 }
1952 // Clean up VideoCapturer.
1953 delete iter->second;
1954 screencast_capturers_.erase(iter);
1955 return true;
1956}
1957
1958bool VideoChannel::IsScreencasting_w() const {
1959 return !screencast_capturers_.empty();
1960}
1961
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001962void VideoChannel::GetScreencastDetails_w(
1963 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001964 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001966 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 }
1968 VideoCapturer* capturer = iter->second;
1969 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001970 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1971 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972}
1973
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001975 rtc::WindowEvent we) {
1976 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001977 SignalScreencastWindowEvent(ssrc, we);
1978}
1979
1980bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001981 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
1982 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983}
1984
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001985void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001987 case MSG_SCREENCASTWINDOWEVENT: {
1988 const ScreencastEventMessageData* data =
1989 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1990 OnScreencastWindowEvent_s(data->ssrc, data->event);
1991 delete data;
1992 break;
1993 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994 case MSG_CHANNEL_ERROR: {
1995 const VideoChannelErrorMessageData* data =
1996 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
1997 SignalMediaError(this, data->ssrc, data->error);
1998 delete data;
1999 break;
2000 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 default:
2002 BaseChannel::OnMessage(pmsg);
2003 break;
2004 }
2005}
2006
2007void VideoChannel::OnConnectionMonitorUpdate(
2008 SocketMonitor *monitor, const std::vector<ConnectionInfo> &infos) {
2009 SignalConnectionMonitor(this, infos);
2010}
2011
2012// TODO(pthatcher): Look into removing duplicate code between
2013// audio, video, and data, perhaps by using templates.
2014void VideoChannel::OnMediaMonitorUpdate(
2015 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2016 ASSERT(media_channel == this->media_channel());
2017 SignalMediaMonitor(this, info);
2018}
2019
2020void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002021 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022 ScreencastEventMessageData* pdata =
2023 new ScreencastEventMessageData(ssrc, event);
2024 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2025}
2026
2027void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2028 // Map capturer events to window events. In the future we may want to simply
2029 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002030 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002032 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002034 we = rtc::WE_MINIMIZE;
2035 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2036 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037 } else {
2038 return;
2039 }
2040 previous_we_ = we;
2041
2042 uint32 ssrc = 0;
2043 if (!GetLocalSsrc(capturer, &ssrc)) {
2044 return;
2045 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002046
2047 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048}
2049
2050bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2051 *ssrc = 0;
2052 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2053 iter != screencast_capturers_.end(); ++iter) {
2054 if (iter->second == capturer) {
2055 *ssrc = iter->first;
2056 return true;
2057 }
2058 }
2059 return false;
2060}
2061
2062void VideoChannel::OnVideoChannelError(uint32 ssrc,
2063 VideoMediaChannel::Error error) {
2064 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2065 ssrc, error);
2066 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2067}
2068
2069void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2070 SrtpFilter::Error error) {
2071 switch (error) {
2072 case SrtpFilter::ERROR_FAIL:
2073 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2074 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2075 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2076 break;
2077 case SrtpFilter::ERROR_AUTH:
2078 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2079 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2080 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2081 break;
2082 case SrtpFilter::ERROR_REPLAY:
2083 // Only receving channel should have this error.
2084 ASSERT(mode == SrtpFilter::UNPROTECT);
2085 // TODO(gangji): Turn on the signaling of replay error once we have
2086 // switched to the new mechanism for doing video retransmissions.
2087 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2088 break;
2089 default:
2090 break;
2091 }
2092}
2093
2094
2095void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2096 GetSupportedVideoCryptoSuites(ciphers);
2097}
2098
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002099DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 DataMediaChannel* media_channel,
2101 BaseSession* session,
2102 const std::string& content_name,
2103 bool rtcp)
2104 // MediaEngine is NULL
2105 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002106 data_channel_type_(cricket::DCT_NONE),
2107 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108}
2109
2110DataChannel::~DataChannel() {
2111 StopMediaMonitor();
2112 // this can't be done in the base class, since it calls a virtual
2113 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002114
2115 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116}
2117
2118bool DataChannel::Init() {
2119 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
2120 content_name(), "data_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
2121 if (!BaseChannel::Init(session()->CreateChannel(
2122 content_name(), "data_rtp", ICE_CANDIDATE_COMPONENT_RTP),
2123 rtcp_channel)) {
2124 return false;
2125 }
2126 media_channel()->SignalDataReceived.connect(
2127 this, &DataChannel::OnDataReceived);
2128 media_channel()->SignalMediaError.connect(
2129 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002130 media_channel()->SignalReadyToSend.connect(
2131 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002132 media_channel()->SignalStreamClosedRemotely.connect(
2133 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002134 srtp_filter()->SignalSrtpError.connect(
2135 this, &DataChannel::OnSrtpError);
2136 return true;
2137}
2138
2139bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002140 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002142 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2143 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144}
2145
2146const ContentInfo* DataChannel::GetFirstContent(
2147 const SessionDescription* sdesc) {
2148 return GetFirstDataContent(sdesc);
2149}
2150
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002151bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152 if (data_channel_type_ == DCT_SCTP) {
2153 // TODO(pthatcher): Do this in a more robust way by checking for
2154 // SCTP or DTLS.
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +00002155 return !IsRtpPacket(packet->data(), packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156 } else if (data_channel_type_ == DCT_RTP) {
2157 return BaseChannel::WantsPacket(rtcp, packet);
2158 }
2159 return false;
2160}
2161
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002162bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2163 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 // It hasn't been set before, so set it now.
2165 if (data_channel_type_ == DCT_NONE) {
2166 data_channel_type_ = new_data_channel_type;
2167 return true;
2168 }
2169
2170 // It's been set before, but doesn't match. That's bad.
2171 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002172 std::ostringstream desc;
2173 desc << "Data channel type mismatch."
2174 << " Expected " << data_channel_type_
2175 << " Got " << new_data_channel_type;
2176 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177 return false;
2178 }
2179
2180 // It's hasn't changed. Nothing to do.
2181 return true;
2182}
2183
2184bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002185 const DataContentDescription* content,
2186 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002187 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2188 (content->protocol() == kMediaProtocolDtlsSctp));
2189 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002190 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002191}
2192
2193bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002194 ContentAction action,
2195 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002196 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 LOG(LS_INFO) << "Setting local data description";
2198
2199 const DataContentDescription* data =
2200 static_cast<const DataContentDescription*>(content);
2201 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002202 if (!data) {
2203 SafeSetError("Can't find data content in local description.", error_desc);
2204 return false;
2205 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206
2207 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002208 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209 return false;
2210 }
2211
2212 if (data_channel_type_ == DCT_SCTP) {
2213 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002214 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215 if (ret) {
2216 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002217 // As in SetRemoteContent_w, make sure we set the local SCTP port
2218 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002219 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2220 SafeSetError("Failed to set data receive codecs.", error_desc);
2221 ret = false;
2222 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002223 }
2224 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002225 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002227 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2228 SafeSetError("Failed to set data receive codecs.", error_desc);
2229 ret = false;
2230 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231 }
2232 }
2233
2234 // If everything worked, see if we can start receiving.
2235 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00002236 std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2237 for (; it != data->codecs().end(); ++it) {
2238 bundle_filter()->AddPayloadType(it->id);
2239 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002240 ChangeState();
2241 } else {
2242 LOG(LS_WARNING) << "Failed to set local data description";
2243 }
2244 return ret;
2245}
2246
2247bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002248 ContentAction action,
2249 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002250 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251
2252 const DataContentDescription* data =
2253 static_cast<const DataContentDescription*>(content);
2254 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002255 if (!data) {
2256 SafeSetError("Can't find data content in remote description.", error_desc);
2257 return false;
2258 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259
2260 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002261 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002262 return false;
2263 }
2264
2265 if (data_channel_type_ == DCT_SCTP) {
2266 LOG(LS_INFO) << "Setting SCTP remote data description";
2267 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002268 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002269 if (ret) {
2270 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002271 // We send the SCTP port number (not to be confused with the underlying
2272 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002273 if (!media_channel()->SetSendCodecs(data->codecs())) {
2274 SafeSetError("Failed to set data send codecs.", error_desc);
2275 ret = false;
2276 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002277 }
2278 } else {
2279 // If the remote data doesn't have codecs and isn't an update, it
2280 // must be empty, so ignore it.
2281 if (action != CA_UPDATE && !data->has_codecs()) {
2282 return true;
2283 }
2284 LOG(LS_INFO) << "Setting remote data description";
2285
2286 // Set remote video codecs (what the other side wants to receive).
2287 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002288 if (!media_channel()->SetSendCodecs(data->codecs())) {
2289 SafeSetError("Failed to set data send codecs.", error_desc);
2290 ret = false;
2291 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 }
2293
2294 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002295 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 }
2297
2298 if (action != CA_UPDATE) {
2299 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002300 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2301 std::ostringstream desc;
2302 desc << "Failed to set max send bandwidth for data content.";
2303 SafeSetError(desc.str(), error_desc);
2304 ret = false;
2305 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002306 }
2307 }
2308
2309 // If everything worked, see if we can start sending.
2310 if (ret) {
2311 ChangeState();
2312 } else {
2313 LOG(LS_WARNING) << "Failed to set remote data description";
2314 }
2315 return ret;
2316}
2317
2318void DataChannel::ChangeState() {
2319 // Render incoming data if we're the active call, and we have the local
2320 // content. We receive data on the default channel and multiplexed streams.
2321 bool recv = IsReadyToReceive();
2322 if (!media_channel()->SetReceive(recv)) {
2323 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2324 }
2325
2326 // Send outgoing data if we're the active call, we have the remote content,
2327 // and we have had some form of connectivity.
2328 bool send = IsReadyToSend();
2329 if (!media_channel()->SetSend(send)) {
2330 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2331 }
2332
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002333 // Trigger SignalReadyToSendData asynchronously.
2334 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002335
2336 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2337}
2338
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002339void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002340 switch (pmsg->message_id) {
2341 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002342 DataChannelReadyToSendMessageData* data =
2343 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002344 ready_to_send_data_ = data->data();
2345 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002346 delete data;
2347 break;
2348 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002349 case MSG_DATARECEIVED: {
2350 DataReceivedMessageData* data =
2351 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2352 SignalDataReceived(this, data->params, data->payload);
2353 delete data;
2354 break;
2355 }
2356 case MSG_CHANNEL_ERROR: {
2357 const DataChannelErrorMessageData* data =
2358 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2359 SignalMediaError(this, data->ssrc, data->error);
2360 delete data;
2361 break;
2362 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002363 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002364 rtc::TypedMessageData<uint32>* data =
2365 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002366 SignalStreamClosedRemotely(data->data());
2367 delete data;
2368 break;
2369 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002370 default:
2371 BaseChannel::OnMessage(pmsg);
2372 break;
2373 }
2374}
2375
2376void DataChannel::OnConnectionMonitorUpdate(
2377 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
2378 SignalConnectionMonitor(this, infos);
2379}
2380
2381void DataChannel::StartMediaMonitor(int cms) {
2382 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002383 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384 media_monitor_->SignalUpdate.connect(
2385 this, &DataChannel::OnMediaMonitorUpdate);
2386 media_monitor_->Start(cms);
2387}
2388
2389void DataChannel::StopMediaMonitor() {
2390 if (media_monitor_) {
2391 media_monitor_->Stop();
2392 media_monitor_->SignalUpdate.disconnect(this);
2393 media_monitor_.reset();
2394 }
2395}
2396
2397void DataChannel::OnMediaMonitorUpdate(
2398 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2399 ASSERT(media_channel == this->media_channel());
2400 SignalMediaMonitor(this, info);
2401}
2402
2403void DataChannel::OnDataReceived(
2404 const ReceiveDataParams& params, const char* data, size_t len) {
2405 DataReceivedMessageData* msg = new DataReceivedMessageData(
2406 params, data, len);
2407 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2408}
2409
2410void DataChannel::OnDataChannelError(
2411 uint32 ssrc, DataMediaChannel::Error err) {
2412 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2413 ssrc, err);
2414 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2415}
2416
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002417void DataChannel::OnDataChannelReadyToSend(bool writable) {
2418 // This is usded for congestion control to indicate that the stream is ready
2419 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2420 // that the transport channel is ready.
2421 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2422 new DataChannelReadyToSendMessageData(writable));
2423}
2424
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002425void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2426 SrtpFilter::Error error) {
2427 switch (error) {
2428 case SrtpFilter::ERROR_FAIL:
2429 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2430 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2431 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2432 break;
2433 case SrtpFilter::ERROR_AUTH:
2434 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2435 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2436 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2437 break;
2438 case SrtpFilter::ERROR_REPLAY:
2439 // Only receving channel should have this error.
2440 ASSERT(mode == SrtpFilter::UNPROTECT);
2441 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2442 break;
2443 default:
2444 break;
2445 }
2446}
2447
2448void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2449 GetSupportedDataCryptoSuites(ciphers);
2450}
2451
2452bool DataChannel::ShouldSetupDtlsSrtp() const {
2453 return (data_channel_type_ == DCT_RTP);
2454}
2455
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002456void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002457 rtc::TypedMessageData<uint32>* message =
2458 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002459 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2460}
2461
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002462} // namespace cricket