blob: dbcdbb06b36f7227adf27455696f5c1bdce323a1 [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) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001033 const StreamParams* existing_stream =
1034 GetStreamByIds(local_streams_, it->groupid, it->id);
1035 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 if (media_channel()->AddSendStream(*it)) {
1037 local_streams_.push_back(*it);
1038 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1039 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001040 std::ostringstream desc;
1041 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1042 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043 return false;
1044 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001045 } else if (existing_stream && !it->has_ssrcs()) {
1046 if (!media_channel()->RemoveSendStream(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001047 std::ostringstream desc;
1048 desc << "Failed to remove send stream with ssrc "
1049 << it->first_ssrc() << ".";
1050 SafeSetError(desc.str(), error_desc);
1051 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001053 RemoveStreamBySsrc(&local_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 } else {
1055 LOG(LS_WARNING) << "Ignore unsupported stream update";
1056 }
1057 }
1058 return true;
1059 }
1060 // Else streams are all the streams we want to send.
1061
1062 // Check for streams that have been removed.
1063 bool ret = true;
1064 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1065 it != local_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001066 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001068 std::ostringstream desc;
1069 desc << "Failed to remove send stream with ssrc "
1070 << it->first_ssrc() << ".";
1071 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 ret = false;
1073 }
1074 }
1075 }
1076 // Check for new streams.
1077 for (StreamParamsVec::const_iterator it = streams.begin();
1078 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001079 if (!GetStreamBySsrc(local_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 if (media_channel()->AddSendStream(*it)) {
1081 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1082 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001083 std::ostringstream desc;
1084 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1085 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086 ret = false;
1087 }
1088 }
1089 }
1090 local_streams_ = streams;
1091 return ret;
1092}
1093
1094bool BaseChannel::UpdateRemoteStreams_w(
1095 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001096 ContentAction action,
1097 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1099 action == CA_PRANSWER || action == CA_UPDATE))
1100 return false;
1101
1102 // If this is an update, streams only contain streams that have changed.
1103 if (action == CA_UPDATE) {
1104 for (StreamParamsVec::const_iterator it = streams.begin();
1105 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001106 const StreamParams* existing_stream =
1107 GetStreamByIds(remote_streams_, it->groupid, it->id);
1108 if (!existing_stream && it->has_ssrcs()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 if (AddRecvStream_w(*it)) {
1110 remote_streams_.push_back(*it);
1111 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1112 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001113 std::ostringstream desc;
1114 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1115 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001116 return false;
1117 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001118 } else if (existing_stream && !it->has_ssrcs()) {
1119 if (!RemoveRecvStream_w(existing_stream->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001120 std::ostringstream desc;
1121 desc << "Failed to remove remote stream with ssrc "
1122 << it->first_ssrc() << ".";
1123 SafeSetError(desc.str(), error_desc);
1124 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 }
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001126 RemoveStreamBySsrc(&remote_streams_, existing_stream->first_ssrc());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 } else {
1128 LOG(LS_WARNING) << "Ignore unsupported stream update."
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001129 << " Stream exists? " << (existing_stream != nullptr)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130 << " new stream = " << it->ToString();
1131 }
1132 }
1133 return true;
1134 }
1135 // Else streams are all the streams we want to receive.
1136
1137 // Check for streams that have been removed.
1138 bool ret = true;
1139 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1140 it != remote_streams_.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001141 if (!GetStreamBySsrc(streams, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001143 std::ostringstream desc;
1144 desc << "Failed to remove remote stream with ssrc "
1145 << it->first_ssrc() << ".";
1146 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 ret = false;
1148 }
1149 }
1150 }
1151 // Check for new streams.
1152 for (StreamParamsVec::const_iterator it = streams.begin();
1153 it != streams.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001154 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc())) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001155 if (AddRecvStream_w(*it)) {
1156 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1157 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001158 std::ostringstream desc;
1159 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1160 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 ret = false;
1162 }
1163 }
1164 }
1165 remote_streams_ = streams;
1166 return ret;
1167}
1168
1169bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001170 ContentAction action,
1171 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001172 // Cache secure_required_ for belt and suspenders check on SendPacket
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001173 secure_required_ = content->crypto_required() != CT_NONE;
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001174 // Set local RTP header extensions.
1175 bool ret = SetRecvRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001176 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001177 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001179 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001180
1181 // Call UpdateLocalStreams_w last to make sure as many settings as possible
1182 // are already set when creating streams.
1183 ret &= UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 set_local_content_direction(content->direction());
1185 return ret;
1186}
1187
1188bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001189 ContentAction action,
1190 std::string* error_desc) {
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001191 // Set remote RTP header extensions.
1192 bool ret = SetSendRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001193 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001194 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001196 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001197 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1198 std::ostringstream desc;
1199 desc << "Failed to set max send bandwidth for "
1200 << MediaTypeToString(content->type()) << " content.";
1201 SafeSetError(desc.str(), error_desc);
1202 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 }
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001204
1205 // Call UpdateRemoteStreams_w last to make sure as many settings as possible
1206 // are already set when creating streams.
1207 ret &= UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001208 set_remote_content_direction(content->direction());
1209 return ret;
1210}
1211
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001212void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1213 const std::vector<RtpHeaderExtension>& extensions) {
1214 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001215 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001216 rtp_abs_sendtime_extn_id_ =
1217 send_time_extension ? send_time_extension->id : -1;
1218}
1219
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001220void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 case MSG_RTPPACKET:
1223 case MSG_RTCPPACKET: {
1224 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001225 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 delete data; // because it is Posted
1227 break;
1228 }
1229 case MSG_FIRSTPACKETRECEIVED: {
1230 SignalFirstPacketReceived(this);
1231 break;
1232 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001233 }
1234}
1235
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001236void BaseChannel::FlushRtcpMessages() {
1237 // Flush all remaining RTCP messages. This should only be called in
1238 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001239 ASSERT(rtc::Thread::Current() == worker_thread_);
1240 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001241 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001242 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001243 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001244 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 }
1246}
1247
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001248VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 MediaEngineInterface* media_engine,
1250 VoiceMediaChannel* media_channel,
1251 BaseSession* session,
1252 const std::string& content_name,
1253 bool rtcp)
1254 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1255 rtcp),
1256 received_media_(false) {
1257}
1258
1259VoiceChannel::~VoiceChannel() {
1260 StopAudioMonitor();
1261 StopMediaMonitor();
1262 // this can't be done in the base class, since it calls a virtual
1263 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001264 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001265}
1266
1267bool VoiceChannel::Init() {
1268 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1269 content_name(), "rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1270 if (!BaseChannel::Init(session()->CreateChannel(
1271 content_name(), "rtp", ICE_CANDIDATE_COMPONENT_RTP),
1272 rtcp_channel)) {
1273 return false;
1274 }
1275 media_channel()->SignalMediaError.connect(
1276 this, &VoiceChannel::OnVoiceChannelError);
1277 srtp_filter()->SignalSrtpError.connect(
1278 this, &VoiceChannel::OnSrtpError);
1279 return true;
1280}
1281
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001282bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001283 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1284 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001285}
1286
1287bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001288 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1289 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290}
1291
1292bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001293 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294}
1295
1296// TODO(juberti): Handle early media the right way. We should get an explicit
1297// ringing message telling us to start playing local ringback, which we cancel
1298// if any early media actually arrives. For now, we do the opposite, which is
1299// to wait 1 second for early media, and start playing local ringback if none
1300// arrives.
1301void VoiceChannel::SetEarlyMedia(bool enable) {
1302 if (enable) {
1303 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001304 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1305 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001306 } else {
1307 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001308 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309 }
1310}
1311
1312bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001313 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1314 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315}
1316
1317bool VoiceChannel::PressDTMF(int digit, bool playout) {
1318 int flags = DF_SEND;
1319 if (playout) {
1320 flags |= DF_PLAY;
1321 }
1322 int duration_ms = 160;
1323 return InsertDtmf(0, digit, duration_ms, flags);
1324}
1325
1326bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001327 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1328 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001329}
1330
1331bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1332 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001333 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1334 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001335}
1336
1337bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001338 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1339 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001340}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001341
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001342bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001343 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1344 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345}
1346
1347void VoiceChannel::StartMediaMonitor(int cms) {
1348 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001349 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001350 media_monitor_->SignalUpdate.connect(
1351 this, &VoiceChannel::OnMediaMonitorUpdate);
1352 media_monitor_->Start(cms);
1353}
1354
1355void VoiceChannel::StopMediaMonitor() {
1356 if (media_monitor_) {
1357 media_monitor_->Stop();
1358 media_monitor_->SignalUpdate.disconnect(this);
1359 media_monitor_.reset();
1360 }
1361}
1362
1363void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001364 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365 audio_monitor_
1366 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1367 audio_monitor_->Start(cms);
1368}
1369
1370void VoiceChannel::StopAudioMonitor() {
1371 if (audio_monitor_) {
1372 audio_monitor_->Stop();
1373 audio_monitor_.reset();
1374 }
1375}
1376
1377bool VoiceChannel::IsAudioMonitorRunning() const {
1378 return (audio_monitor_.get() != NULL);
1379}
1380
1381void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1382 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1383 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1384}
1385
1386void VoiceChannel::StopTypingMonitor() {
1387 typing_monitor_.reset();
1388}
1389
1390bool VoiceChannel::IsTypingMonitorRunning() const {
1391 return typing_monitor_;
1392}
1393
1394bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1395 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1396 if (typing_monitor_ && mute)
1397 typing_monitor_->OnChannelMuted();
1398 return ret;
1399}
1400
1401int VoiceChannel::GetInputLevel_w() {
1402 return media_engine()->GetInputLevel();
1403}
1404
1405int VoiceChannel::GetOutputLevel_w() {
1406 return media_channel()->GetOutputLevel();
1407}
1408
1409void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1410 media_channel()->GetActiveStreams(actives);
1411}
1412
1413void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001414 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001415 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001416 int flags) {
1417 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001418
1419 // Set a flag when we've received an RTP packet. If we're waiting for early
1420 // media, this will disable the timeout.
1421 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1422 received_media_ = true;
1423 }
1424}
1425
1426void VoiceChannel::ChangeState() {
1427 // Render incoming data if we're the active call, and we have the local
1428 // content. We receive data on the default channel and multiplexed streams.
1429 bool recv = IsReadyToReceive();
1430 if (!media_channel()->SetPlayout(recv)) {
1431 SendLastMediaError();
1432 }
1433
1434 // Send outgoing data if we're the active call, we have the remote content,
1435 // and we have had some form of connectivity.
1436 bool send = IsReadyToSend();
1437 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1438 if (!media_channel()->SetSend(send_flag)) {
1439 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1440 SendLastMediaError();
1441 }
1442
1443 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1444}
1445
1446const ContentInfo* VoiceChannel::GetFirstContent(
1447 const SessionDescription* sdesc) {
1448 return GetFirstAudioContent(sdesc);
1449}
1450
1451bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001452 ContentAction action,
1453 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001454 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 LOG(LS_INFO) << "Setting local voice description";
1456
1457 const AudioContentDescription* audio =
1458 static_cast<const AudioContentDescription*>(content);
1459 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001460 if (!audio) {
1461 SafeSetError("Can't find audio content in local description.", error_desc);
1462 return false;
1463 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001465 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 // Set local audio codecs (what we want to receive).
1467 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1468 // is set properly.
1469 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001470 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1471 SafeSetError("Failed to set audio receive codecs.", error_desc);
1472 ret = false;
1473 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 }
1475
1476 // If everything worked, see if we can start receiving.
1477 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001478 std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1479 for (; it != audio->codecs().end(); ++it) {
1480 bundle_filter()->AddPayloadType(it->id);
1481 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482 ChangeState();
1483 } else {
1484 LOG(LS_WARNING) << "Failed to set local voice description";
1485 }
1486 return ret;
1487}
1488
1489bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001490 ContentAction action,
1491 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001492 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 LOG(LS_INFO) << "Setting remote voice description";
1494
1495 const AudioContentDescription* audio =
1496 static_cast<const AudioContentDescription*>(content);
1497 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001498 if (!audio) {
1499 SafeSetError("Can't find audio content in remote description.", error_desc);
1500 return false;
1501 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502
1503 bool ret = true;
1504 // Set remote video codecs (what the other side wants to receive).
1505 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001506 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1507 SafeSetError("Failed to set audio send codecs.", error_desc);
1508 ret = false;
1509 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 }
1511
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001512 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513
1514 if (action != CA_UPDATE) {
1515 // Tweak our audio processing settings, if needed.
1516 AudioOptions audio_options;
1517 if (!media_channel()->GetOptions(&audio_options)) {
1518 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1519 } else {
1520 if (audio->conference_mode()) {
1521 audio_options.conference_mode.Set(true);
1522 }
1523 if (audio->agc_minus_10db()) {
1524 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1525 }
1526 if (!media_channel()->SetOptions(audio_options)) {
1527 // Log an error on failure, but don't abort the call.
1528 LOG(LS_ERROR) << "Failed to set voice channel options";
1529 }
1530 }
1531 }
1532
1533 // If everything worked, see if we can start sending.
1534 if (ret) {
1535 ChangeState();
1536 } else {
1537 LOG(LS_WARNING) << "Failed to set remote voice description";
1538 }
1539 return ret;
1540}
1541
1542bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001543 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001544 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1545}
1546
1547bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001548 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 if (play) {
1550 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1551 } else {
1552 LOG(LS_INFO) << "Stopping ringback tone";
1553 }
1554 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1555}
1556
1557void VoiceChannel::HandleEarlyMediaTimeout() {
1558 // This occurs on the main thread, not the worker thread.
1559 if (!received_media_) {
1560 LOG(LS_INFO) << "No early media received before timeout";
1561 SignalEarlyMediaTimeout(this);
1562 }
1563}
1564
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1566 int flags) {
1567 if (!enabled()) {
1568 return false;
1569 }
1570
1571 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1572}
1573
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001575 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1576 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577}
1578
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001579void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001580 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 case MSG_EARLYMEDIATIMEOUT:
1582 HandleEarlyMediaTimeout();
1583 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 case MSG_CHANNEL_ERROR: {
1585 VoiceChannelErrorMessageData* data =
1586 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1587 SignalMediaError(this, data->ssrc, data->error);
1588 delete data;
1589 break;
1590 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 default:
1592 BaseChannel::OnMessage(pmsg);
1593 break;
1594 }
1595}
1596
1597void VoiceChannel::OnConnectionMonitorUpdate(
1598 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
1599 SignalConnectionMonitor(this, infos);
1600}
1601
1602void VoiceChannel::OnMediaMonitorUpdate(
1603 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1604 ASSERT(media_channel == this->media_channel());
1605 SignalMediaMonitor(this, info);
1606}
1607
1608void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1609 const AudioInfo& info) {
1610 SignalAudioMonitor(this, info);
1611}
1612
1613void VoiceChannel::OnVoiceChannelError(
1614 uint32 ssrc, VoiceMediaChannel::Error err) {
1615 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1616 ssrc, err);
1617 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1618}
1619
1620void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1621 SrtpFilter::Error error) {
1622 switch (error) {
1623 case SrtpFilter::ERROR_FAIL:
1624 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1625 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1626 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1627 break;
1628 case SrtpFilter::ERROR_AUTH:
1629 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1630 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1631 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1632 break;
1633 case SrtpFilter::ERROR_REPLAY:
1634 // Only receving channel should have this error.
1635 ASSERT(mode == SrtpFilter::UNPROTECT);
1636 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1637 break;
1638 default:
1639 break;
1640 }
1641}
1642
1643void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1644 GetSupportedAudioCryptoSuites(ciphers);
1645}
1646
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001647VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 MediaEngineInterface* media_engine,
1649 VideoMediaChannel* media_channel,
1650 BaseSession* session,
1651 const std::string& content_name,
1652 bool rtcp,
1653 VoiceChannel* voice_channel)
1654 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1655 rtcp),
1656 voice_channel_(voice_channel),
1657 renderer_(NULL),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001658 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659}
1660
1661bool VideoChannel::Init() {
1662 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1663 content_name(), "video_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1664 if (!BaseChannel::Init(session()->CreateChannel(
1665 content_name(), "video_rtp", ICE_CANDIDATE_COMPONENT_RTP),
1666 rtcp_channel)) {
1667 return false;
1668 }
1669 media_channel()->SignalMediaError.connect(
1670 this, &VideoChannel::OnVideoChannelError);
1671 srtp_filter()->SignalSrtpError.connect(
1672 this, &VideoChannel::OnSrtpError);
1673 return true;
1674}
1675
1676void VoiceChannel::SendLastMediaError() {
1677 uint32 ssrc;
1678 VoiceMediaChannel::Error error;
1679 media_channel()->GetLastMediaError(&ssrc, &error);
1680 SignalMediaError(this, ssrc, error);
1681}
1682
1683VideoChannel::~VideoChannel() {
1684 std::vector<uint32> screencast_ssrcs;
1685 ScreencastMap::iterator iter;
1686 while (!screencast_capturers_.empty()) {
1687 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1688 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1689 << screencast_capturers_.begin()->first;
1690 ASSERT(false);
1691 break;
1692 }
1693 }
1694
1695 StopMediaMonitor();
1696 // this can't be done in the base class, since it calls a virtual
1697 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001698
1699 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001700}
1701
1702bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001703 worker_thread()->Invoke<void>(Bind(
1704 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 return true;
1706}
1707
1708bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001709 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001710}
1711
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001712bool VideoChannel::AddScreencast(uint32 ssrc, VideoCapturer* capturer) {
1713 return worker_thread()->Invoke<bool>(Bind(
1714 &VideoChannel::AddScreencast_w, this, ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715}
1716
1717bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001718 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1719 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720}
1721
1722bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001723 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001724}
1725
1726bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001727 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728}
1729
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001730int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001731 ScreencastDetailsData data(ssrc);
1732 worker_thread()->Invoke<void>(Bind(
1733 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001734 return data.fps;
1735}
1736
1737int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001738 ScreencastDetailsData data(ssrc);
1739 worker_thread()->Invoke<void>(Bind(
1740 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001741 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742}
1743
1744bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001745 worker_thread()->Invoke<void>(Bind(
1746 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 return true;
1748}
1749
1750bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001751 worker_thread()->Invoke<void>(Bind(
1752 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 return true;
1754}
1755
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756void VideoChannel::ChangeState() {
1757 // Render incoming data if we're the active call, and we have the local
1758 // content. We receive data on the default channel and multiplexed streams.
1759 bool recv = IsReadyToReceive();
1760 if (!media_channel()->SetRender(recv)) {
1761 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1762 // TODO(gangji): Report error back to server.
1763 }
1764
1765 // Send outgoing data if we're the active call, we have the remote content,
1766 // and we have had some form of connectivity.
1767 bool send = IsReadyToSend();
1768 if (!media_channel()->SetSend(send)) {
1769 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1770 // TODO(gangji): Report error back to server.
1771 }
1772
1773 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1774}
1775
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001776bool VideoChannel::GetStats(
1777 const StatsOptions& options, VideoMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001778 return InvokeOnWorker(Bind(&VideoMediaChannel::GetStats,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001779 media_channel(), options, stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780}
1781
1782void VideoChannel::StartMediaMonitor(int cms) {
1783 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001784 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785 media_monitor_->SignalUpdate.connect(
1786 this, &VideoChannel::OnMediaMonitorUpdate);
1787 media_monitor_->Start(cms);
1788}
1789
1790void VideoChannel::StopMediaMonitor() {
1791 if (media_monitor_) {
1792 media_monitor_->Stop();
1793 media_monitor_.reset();
1794 }
1795}
1796
1797const ContentInfo* VideoChannel::GetFirstContent(
1798 const SessionDescription* sdesc) {
1799 return GetFirstVideoContent(sdesc);
1800}
1801
1802bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001803 ContentAction action,
1804 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001805 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806 LOG(LS_INFO) << "Setting local video description";
1807
1808 const VideoContentDescription* video =
1809 static_cast<const VideoContentDescription*>(content);
1810 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001811 if (!video) {
1812 SafeSetError("Can't find video content in local description.", error_desc);
1813 return false;
1814 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001816 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 // Set local video codecs (what we want to receive).
1818 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001819 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1820 SafeSetError("Failed to set video receive codecs.", error_desc);
1821 ret = false;
1822 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 }
1824
1825 if (action != CA_UPDATE) {
1826 VideoOptions video_options;
1827 media_channel()->GetOptions(&video_options);
1828 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1829
1830 if (!media_channel()->SetOptions(video_options)) {
1831 // Log an error on failure, but don't abort the call.
1832 LOG(LS_ERROR) << "Failed to set video channel options";
1833 }
1834 }
1835
1836 // If everything worked, see if we can start receiving.
1837 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001838 std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1839 for (; it != video->codecs().end(); ++it) {
1840 bundle_filter()->AddPayloadType(it->id);
1841 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001842 ChangeState();
1843 } else {
1844 LOG(LS_WARNING) << "Failed to set local video description";
1845 }
1846 return ret;
1847}
1848
1849bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001850 ContentAction action,
1851 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001852 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 LOG(LS_INFO) << "Setting remote video description";
1854
1855 const VideoContentDescription* video =
1856 static_cast<const VideoContentDescription*>(content);
1857 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001858 if (!video) {
1859 SafeSetError("Can't find video content in remote description.", error_desc);
1860 return false;
1861 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862
1863 bool ret = true;
1864 // Set remote video codecs (what the other side wants to receive).
1865 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001866 if (!media_channel()->SetSendCodecs(video->codecs())) {
1867 SafeSetError("Failed to set video send codecs.", error_desc);
1868 ret = false;
1869 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 }
1871
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001872 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873
1874 if (action != CA_UPDATE) {
1875 // Tweak our video processing settings, if needed.
1876 VideoOptions video_options;
1877 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001878 if (video->conference_mode()) {
1879 video_options.conference_mode.Set(true);
1880 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1882
1883 if (!media_channel()->SetOptions(video_options)) {
1884 // Log an error on failure, but don't abort the call.
1885 LOG(LS_ERROR) << "Failed to set video channel options";
1886 }
1887 }
1888
1889 // If everything worked, see if we can start sending.
1890 if (ret) {
1891 ChangeState();
1892 } else {
1893 LOG(LS_WARNING) << "Failed to set remote video description";
1894 }
1895 return ret;
1896}
1897
1898bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1899 bool ret = true;
1900 // Set the send format for each of the local streams. If the view request
1901 // does not contain a local stream, set its send format to 0x0, which will
1902 // drop all frames.
1903 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1904 it != local_streams().end(); ++it) {
1905 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1906 StaticVideoViews::const_iterator view;
1907 for (view = request.static_video_views.begin();
1908 view != request.static_video_views.end(); ++view) {
1909 if (view->selector.Matches(*it)) {
1910 format.width = view->width;
1911 format.height = view->height;
1912 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1913 break;
1914 }
1915 }
1916
1917 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1918 }
1919
1920 // Check if the view request has invalid streams.
1921 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1922 it != request.static_video_views.end(); ++it) {
tommi@webrtc.org586f2ed2015-01-22 23:00:41 +00001923 if (!GetStream(local_streams(), it->selector)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 LOG(LS_WARNING) << "View request for ("
1925 << it->selector.ssrc << ", '"
1926 << it->selector.groupid << "', '"
1927 << it->selector.streamid << "'"
1928 << ") is not in the local streams.";
1929 }
1930 }
1931
1932 return ret;
1933}
1934
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001935bool VideoChannel::AddScreencast_w(uint32 ssrc, VideoCapturer* capturer) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001937 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938 }
buildbot@webrtc.org65b98d12014-08-07 22:09:08 +00001939 capturer->SignalStateChange.connect(this, &VideoChannel::OnStateChange);
1940 screencast_capturers_[ssrc] = capturer;
1941 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942}
1943
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1945 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1946 if (iter == screencast_capturers_.end()) {
1947 return false;
1948 }
1949 // Clean up VideoCapturer.
1950 delete iter->second;
1951 screencast_capturers_.erase(iter);
1952 return true;
1953}
1954
1955bool VideoChannel::IsScreencasting_w() const {
1956 return !screencast_capturers_.empty();
1957}
1958
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001959void VideoChannel::GetScreencastDetails_w(
1960 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001961 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001963 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 }
1965 VideoCapturer* capturer = iter->second;
1966 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001967 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1968 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969}
1970
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001972 rtc::WindowEvent we) {
1973 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974 SignalScreencastWindowEvent(ssrc, we);
1975}
1976
1977bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001978 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
1979 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980}
1981
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001982void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 case MSG_SCREENCASTWINDOWEVENT: {
1985 const ScreencastEventMessageData* data =
1986 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
1987 OnScreencastWindowEvent_s(data->ssrc, data->event);
1988 delete data;
1989 break;
1990 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991 case MSG_CHANNEL_ERROR: {
1992 const VideoChannelErrorMessageData* data =
1993 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
1994 SignalMediaError(this, data->ssrc, data->error);
1995 delete data;
1996 break;
1997 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 default:
1999 BaseChannel::OnMessage(pmsg);
2000 break;
2001 }
2002}
2003
2004void VideoChannel::OnConnectionMonitorUpdate(
2005 SocketMonitor *monitor, const std::vector<ConnectionInfo> &infos) {
2006 SignalConnectionMonitor(this, infos);
2007}
2008
2009// TODO(pthatcher): Look into removing duplicate code between
2010// audio, video, and data, perhaps by using templates.
2011void VideoChannel::OnMediaMonitorUpdate(
2012 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2013 ASSERT(media_channel == this->media_channel());
2014 SignalMediaMonitor(this, info);
2015}
2016
2017void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002018 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019 ScreencastEventMessageData* pdata =
2020 new ScreencastEventMessageData(ssrc, event);
2021 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2022}
2023
2024void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2025 // Map capturer events to window events. In the future we may want to simply
2026 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002027 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002029 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002031 we = rtc::WE_MINIMIZE;
2032 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2033 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 } else {
2035 return;
2036 }
2037 previous_we_ = we;
2038
2039 uint32 ssrc = 0;
2040 if (!GetLocalSsrc(capturer, &ssrc)) {
2041 return;
2042 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002043
2044 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002045}
2046
2047bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2048 *ssrc = 0;
2049 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2050 iter != screencast_capturers_.end(); ++iter) {
2051 if (iter->second == capturer) {
2052 *ssrc = iter->first;
2053 return true;
2054 }
2055 }
2056 return false;
2057}
2058
2059void VideoChannel::OnVideoChannelError(uint32 ssrc,
2060 VideoMediaChannel::Error error) {
2061 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2062 ssrc, error);
2063 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2064}
2065
2066void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2067 SrtpFilter::Error error) {
2068 switch (error) {
2069 case SrtpFilter::ERROR_FAIL:
2070 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2071 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2072 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2073 break;
2074 case SrtpFilter::ERROR_AUTH:
2075 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2076 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2077 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2078 break;
2079 case SrtpFilter::ERROR_REPLAY:
2080 // Only receving channel should have this error.
2081 ASSERT(mode == SrtpFilter::UNPROTECT);
2082 // TODO(gangji): Turn on the signaling of replay error once we have
2083 // switched to the new mechanism for doing video retransmissions.
2084 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2085 break;
2086 default:
2087 break;
2088 }
2089}
2090
2091
2092void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2093 GetSupportedVideoCryptoSuites(ciphers);
2094}
2095
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002096DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 DataMediaChannel* media_channel,
2098 BaseSession* session,
2099 const std::string& content_name,
2100 bool rtcp)
2101 // MediaEngine is NULL
2102 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002103 data_channel_type_(cricket::DCT_NONE),
2104 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002105}
2106
2107DataChannel::~DataChannel() {
2108 StopMediaMonitor();
2109 // this can't be done in the base class, since it calls a virtual
2110 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002111
2112 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113}
2114
2115bool DataChannel::Init() {
2116 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
2117 content_name(), "data_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
2118 if (!BaseChannel::Init(session()->CreateChannel(
2119 content_name(), "data_rtp", ICE_CANDIDATE_COMPONENT_RTP),
2120 rtcp_channel)) {
2121 return false;
2122 }
2123 media_channel()->SignalDataReceived.connect(
2124 this, &DataChannel::OnDataReceived);
2125 media_channel()->SignalMediaError.connect(
2126 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002127 media_channel()->SignalReadyToSend.connect(
2128 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002129 media_channel()->SignalStreamClosedRemotely.connect(
2130 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002131 srtp_filter()->SignalSrtpError.connect(
2132 this, &DataChannel::OnSrtpError);
2133 return true;
2134}
2135
2136bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002137 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002139 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2140 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141}
2142
2143const ContentInfo* DataChannel::GetFirstContent(
2144 const SessionDescription* sdesc) {
2145 return GetFirstDataContent(sdesc);
2146}
2147
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002148bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 if (data_channel_type_ == DCT_SCTP) {
2150 // TODO(pthatcher): Do this in a more robust way by checking for
2151 // SCTP or DTLS.
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +00002152 return !IsRtpPacket(packet->data(), packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153 } else if (data_channel_type_ == DCT_RTP) {
2154 return BaseChannel::WantsPacket(rtcp, packet);
2155 }
2156 return false;
2157}
2158
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002159bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2160 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161 // It hasn't been set before, so set it now.
2162 if (data_channel_type_ == DCT_NONE) {
2163 data_channel_type_ = new_data_channel_type;
2164 return true;
2165 }
2166
2167 // It's been set before, but doesn't match. That's bad.
2168 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002169 std::ostringstream desc;
2170 desc << "Data channel type mismatch."
2171 << " Expected " << data_channel_type_
2172 << " Got " << new_data_channel_type;
2173 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 return false;
2175 }
2176
2177 // It's hasn't changed. Nothing to do.
2178 return true;
2179}
2180
2181bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002182 const DataContentDescription* content,
2183 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002184 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2185 (content->protocol() == kMediaProtocolDtlsSctp));
2186 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002187 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188}
2189
2190bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002191 ContentAction action,
2192 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002193 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194 LOG(LS_INFO) << "Setting local data description";
2195
2196 const DataContentDescription* data =
2197 static_cast<const DataContentDescription*>(content);
2198 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002199 if (!data) {
2200 SafeSetError("Can't find data content in local description.", error_desc);
2201 return false;
2202 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203
2204 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002205 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 return false;
2207 }
2208
2209 if (data_channel_type_ == DCT_SCTP) {
2210 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002211 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212 if (ret) {
2213 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002214 // As in SetRemoteContent_w, make sure we set the local SCTP port
2215 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002216 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2217 SafeSetError("Failed to set data receive codecs.", error_desc);
2218 ret = false;
2219 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220 }
2221 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002222 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002223 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002224 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2225 SafeSetError("Failed to set data receive codecs.", error_desc);
2226 ret = false;
2227 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228 }
2229 }
2230
2231 // If everything worked, see if we can start receiving.
2232 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00002233 std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2234 for (; it != data->codecs().end(); ++it) {
2235 bundle_filter()->AddPayloadType(it->id);
2236 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237 ChangeState();
2238 } else {
2239 LOG(LS_WARNING) << "Failed to set local data description";
2240 }
2241 return ret;
2242}
2243
2244bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002245 ContentAction action,
2246 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002247 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248
2249 const DataContentDescription* data =
2250 static_cast<const DataContentDescription*>(content);
2251 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002252 if (!data) {
2253 SafeSetError("Can't find data content in remote description.", error_desc);
2254 return false;
2255 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256
2257 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002258 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259 return false;
2260 }
2261
2262 if (data_channel_type_ == DCT_SCTP) {
2263 LOG(LS_INFO) << "Setting SCTP remote data description";
2264 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002265 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002266 if (ret) {
2267 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002268 // We send the SCTP port number (not to be confused with the underlying
2269 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002270 if (!media_channel()->SetSendCodecs(data->codecs())) {
2271 SafeSetError("Failed to set data send codecs.", error_desc);
2272 ret = false;
2273 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274 }
2275 } else {
2276 // If the remote data doesn't have codecs and isn't an update, it
2277 // must be empty, so ignore it.
2278 if (action != CA_UPDATE && !data->has_codecs()) {
2279 return true;
2280 }
2281 LOG(LS_INFO) << "Setting remote data description";
2282
2283 // Set remote video codecs (what the other side wants to receive).
2284 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002285 if (!media_channel()->SetSendCodecs(data->codecs())) {
2286 SafeSetError("Failed to set data send codecs.", error_desc);
2287 ret = false;
2288 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289 }
2290
2291 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002292 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002293 }
2294
2295 if (action != CA_UPDATE) {
2296 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002297 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2298 std::ostringstream desc;
2299 desc << "Failed to set max send bandwidth for data content.";
2300 SafeSetError(desc.str(), error_desc);
2301 ret = false;
2302 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002303 }
2304 }
2305
2306 // If everything worked, see if we can start sending.
2307 if (ret) {
2308 ChangeState();
2309 } else {
2310 LOG(LS_WARNING) << "Failed to set remote data description";
2311 }
2312 return ret;
2313}
2314
2315void DataChannel::ChangeState() {
2316 // Render incoming data if we're the active call, and we have the local
2317 // content. We receive data on the default channel and multiplexed streams.
2318 bool recv = IsReadyToReceive();
2319 if (!media_channel()->SetReceive(recv)) {
2320 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2321 }
2322
2323 // Send outgoing data if we're the active call, we have the remote content,
2324 // and we have had some form of connectivity.
2325 bool send = IsReadyToSend();
2326 if (!media_channel()->SetSend(send)) {
2327 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2328 }
2329
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002330 // Trigger SignalReadyToSendData asynchronously.
2331 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332
2333 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2334}
2335
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002336void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002337 switch (pmsg->message_id) {
2338 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002339 DataChannelReadyToSendMessageData* data =
2340 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002341 ready_to_send_data_ = data->data();
2342 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343 delete data;
2344 break;
2345 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002346 case MSG_DATARECEIVED: {
2347 DataReceivedMessageData* data =
2348 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2349 SignalDataReceived(this, data->params, data->payload);
2350 delete data;
2351 break;
2352 }
2353 case MSG_CHANNEL_ERROR: {
2354 const DataChannelErrorMessageData* data =
2355 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2356 SignalMediaError(this, data->ssrc, data->error);
2357 delete data;
2358 break;
2359 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002360 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002361 rtc::TypedMessageData<uint32>* data =
2362 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002363 SignalStreamClosedRemotely(data->data());
2364 delete data;
2365 break;
2366 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367 default:
2368 BaseChannel::OnMessage(pmsg);
2369 break;
2370 }
2371}
2372
2373void DataChannel::OnConnectionMonitorUpdate(
2374 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
2375 SignalConnectionMonitor(this, infos);
2376}
2377
2378void DataChannel::StartMediaMonitor(int cms) {
2379 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002380 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002381 media_monitor_->SignalUpdate.connect(
2382 this, &DataChannel::OnMediaMonitorUpdate);
2383 media_monitor_->Start(cms);
2384}
2385
2386void DataChannel::StopMediaMonitor() {
2387 if (media_monitor_) {
2388 media_monitor_->Stop();
2389 media_monitor_->SignalUpdate.disconnect(this);
2390 media_monitor_.reset();
2391 }
2392}
2393
2394void DataChannel::OnMediaMonitorUpdate(
2395 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2396 ASSERT(media_channel == this->media_channel());
2397 SignalMediaMonitor(this, info);
2398}
2399
2400void DataChannel::OnDataReceived(
2401 const ReceiveDataParams& params, const char* data, size_t len) {
2402 DataReceivedMessageData* msg = new DataReceivedMessageData(
2403 params, data, len);
2404 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2405}
2406
2407void DataChannel::OnDataChannelError(
2408 uint32 ssrc, DataMediaChannel::Error err) {
2409 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2410 ssrc, err);
2411 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2412}
2413
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002414void DataChannel::OnDataChannelReadyToSend(bool writable) {
2415 // This is usded for congestion control to indicate that the stream is ready
2416 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2417 // that the transport channel is ready.
2418 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2419 new DataChannelReadyToSendMessageData(writable));
2420}
2421
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002422void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2423 SrtpFilter::Error error) {
2424 switch (error) {
2425 case SrtpFilter::ERROR_FAIL:
2426 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2427 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2428 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2429 break;
2430 case SrtpFilter::ERROR_AUTH:
2431 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2432 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2433 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2434 break;
2435 case SrtpFilter::ERROR_REPLAY:
2436 // Only receving channel should have this error.
2437 ASSERT(mode == SrtpFilter::UNPROTECT);
2438 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2439 break;
2440 default:
2441 break;
2442 }
2443}
2444
2445void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2446 GetSupportedDataCryptoSuites(ciphers);
2447}
2448
2449bool DataChannel::ShouldSetupDtlsSrtp() const {
2450 return (data_channel_type_ == DCT_RTP);
2451}
2452
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002453void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002454 rtc::TypedMessageData<uint32>* message =
2455 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002456 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2457}
2458
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459} // namespace cricket