blob: 67bd2da5d9190c718fca0c4d7e59fbf9069a59a9 [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.orgd4e598d2014-07-29 17:36:52 +000030#include "webrtc/base/bind.h"
31#include "webrtc/base/buffer.h"
32#include "webrtc/base/byteorder.h"
33#include "webrtc/base/common.h"
34#include "webrtc/base/dscp.h"
35#include "webrtc/base/logging.h"
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +000036#include "talk/media/base/constants.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037#include "talk/media/base/rtputils.h"
38#include "talk/p2p/base/transportchannel.h"
39#include "talk/session/media/channelmanager.h"
40#include "talk/session/media/mediamessages.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041#include "talk/session/media/typingmonitor.h"
42
43
44namespace cricket {
45
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000046using rtc::Bind;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000047
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000049 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 MSG_SCREENCASTWINDOWEVENT,
51 MSG_RTPPACKET,
52 MSG_RTCPPACKET,
53 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 MSG_FIRSTPACKETRECEIVED,
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +000057 MSG_STREAMCLOSEDREMOTELY,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058};
59
60// Value specified in RFC 5764.
61static const char kDtlsSrtpExporterLabel[] = "EXTRACTOR-dtls_srtp";
62
63static const int kAgcMinus10db = -10;
64
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +000065static void SetSessionError(BaseSession* session, BaseSession::Error error,
66 const std::string& error_desc) {
67 session->SetError(error, error_desc);
68}
69
70static void SafeSetError(const std::string& message, std::string* error_desc) {
71 if (error_desc) {
72 *error_desc = message;
73 }
74}
75
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076// TODO(hellner): use the device manager for creation of screen capturers when
77// the cl enabling it has landed.
78class NullScreenCapturerFactory : public VideoChannel::ScreenCapturerFactory {
79 public:
80 VideoCapturer* CreateScreenCapturer(const ScreencastId& window) {
81 return NULL;
82 }
83};
84
85
86VideoChannel::ScreenCapturerFactory* CreateScreenCapturerFactory() {
87 return new NullScreenCapturerFactory();
88}
89
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000090struct PacketMessageData : public rtc::MessageData {
91 rtc::Buffer packet;
92 rtc::DiffServCodePoint dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093};
94
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000095struct ScreencastEventMessageData : public rtc::MessageData {
96 ScreencastEventMessageData(uint32 s, rtc::WindowEvent we)
henrike@webrtc.org28e20752013-07-10 00:45:36 +000097 : ssrc(s),
98 event(we) {
99 }
100 uint32 ssrc;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000101 rtc::WindowEvent event;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102};
103
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000104struct VoiceChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 VoiceChannelErrorMessageData(uint32 in_ssrc,
106 VoiceMediaChannel::Error in_error)
107 : ssrc(in_ssrc),
108 error(in_error) {
109 }
110 uint32 ssrc;
111 VoiceMediaChannel::Error error;
112};
113
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000114struct VideoChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115 VideoChannelErrorMessageData(uint32 in_ssrc,
116 VideoMediaChannel::Error in_error)
117 : ssrc(in_ssrc),
118 error(in_error) {
119 }
120 uint32 ssrc;
121 VideoMediaChannel::Error error;
122};
123
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000124struct DataChannelErrorMessageData : public rtc::MessageData {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 DataChannelErrorMessageData(uint32 in_ssrc,
126 DataMediaChannel::Error in_error)
127 : ssrc(in_ssrc),
128 error(in_error) {}
129 uint32 ssrc;
130 DataMediaChannel::Error error;
131};
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000134struct VideoChannel::ScreencastDetailsData {
135 explicit ScreencastDetailsData(uint32 s)
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000136 : ssrc(s), fps(0), screencast_max_pixels(0) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137 }
138 uint32 ssrc;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000139 int fps;
140 int screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141};
142
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143static const char* PacketType(bool rtcp) {
144 return (!rtcp) ? "RTP" : "RTCP";
145}
146
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000147static bool ValidPacket(bool rtcp, const rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 // Check the packet size. We could check the header too if needed.
149 return (packet &&
150 packet->length() >= (!rtcp ? kMinRtpPacketLen : kMinRtcpPacketLen) &&
151 packet->length() <= kMaxRtpPacketLen);
152}
153
154static bool IsReceiveContentDirection(MediaContentDirection direction) {
155 return direction == MD_SENDRECV || direction == MD_RECVONLY;
156}
157
158static bool IsSendContentDirection(MediaContentDirection direction) {
159 return direction == MD_SENDRECV || direction == MD_SENDONLY;
160}
161
162static const MediaContentDescription* GetContentDescription(
163 const ContentInfo* cinfo) {
164 if (cinfo == NULL)
165 return NULL;
166 return static_cast<const MediaContentDescription*>(cinfo->description);
167}
168
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000169BaseChannel::BaseChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170 MediaEngineInterface* media_engine,
171 MediaChannel* media_channel, BaseSession* session,
172 const std::string& content_name, bool rtcp)
173 : worker_thread_(thread),
174 media_engine_(media_engine),
175 session_(session),
176 media_channel_(media_channel),
177 content_name_(content_name),
178 rtcp_(rtcp),
179 transport_channel_(NULL),
180 rtcp_transport_channel_(NULL),
181 enabled_(false),
182 writable_(false),
183 rtp_ready_to_send_(false),
184 rtcp_ready_to_send_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 was_ever_writable_(false),
186 local_content_direction_(MD_INACTIVE),
187 remote_content_direction_(MD_INACTIVE),
188 has_received_packet_(false),
189 dtls_keyed_(false),
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000190 secure_required_(false),
191 rtp_abs_sendtime_extn_id_(-1) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000192 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 LOG(LS_INFO) << "Created channel for " << content_name;
194}
195
196BaseChannel::~BaseChannel() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000197 ASSERT(worker_thread_ == rtc::Thread::Current());
wu@webrtc.org78187522013-10-07 23:32:02 +0000198 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 StopConnectionMonitor();
200 FlushRtcpMessages(); // Send any outstanding RTCP packets.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000201 worker_thread_->Clear(this); // eats any outstanding messages or packets
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 // We must destroy the media channel before the transport channel, otherwise
203 // the media channel may try to send on the dead transport channel. NULLing
204 // is not an effective strategy since the sends will come on another thread.
205 delete media_channel_;
206 set_rtcp_transport_channel(NULL);
207 if (transport_channel_ != NULL)
208 session_->DestroyChannel(content_name_, transport_channel_->component());
209 LOG(LS_INFO) << "Destroyed channel";
210}
211
212bool BaseChannel::Init(TransportChannel* transport_channel,
213 TransportChannel* rtcp_transport_channel) {
214 if (transport_channel == NULL) {
215 return false;
216 }
217 if (rtcp() && rtcp_transport_channel == NULL) {
218 return false;
219 }
220 transport_channel_ = transport_channel;
221
222 if (!SetDtlsSrtpCiphers(transport_channel_, false)) {
223 return false;
224 }
225
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 transport_channel_->SignalWritableState.connect(
227 this, &BaseChannel::OnWritableState);
228 transport_channel_->SignalReadPacket.connect(
229 this, &BaseChannel::OnChannelRead);
230 transport_channel_->SignalReadyToSend.connect(
231 this, &BaseChannel::OnReadyToSend);
232
233 session_->SignalNewLocalDescription.connect(
234 this, &BaseChannel::OnNewLocalDescription);
235 session_->SignalNewRemoteDescription.connect(
236 this, &BaseChannel::OnNewRemoteDescription);
237
238 set_rtcp_transport_channel(rtcp_transport_channel);
wu@webrtc.orgde305012013-10-31 15:40:38 +0000239 // Both RTP and RTCP channels are set, we can call SetInterface on
240 // media channel and it can set network options.
241 media_channel_->SetInterface(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 return true;
243}
244
wu@webrtc.org78187522013-10-07 23:32:02 +0000245void BaseChannel::Deinit() {
246 media_channel_->SetInterface(NULL);
247}
248
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000249bool BaseChannel::Enable(bool enable) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000250 worker_thread_->Invoke<void>(Bind(
251 enable ? &BaseChannel::EnableMedia_w : &BaseChannel::DisableMedia_w,
252 this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 return true;
254}
255
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000256bool BaseChannel::MuteStream(uint32 ssrc, bool mute) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000257 return InvokeOnWorker(Bind(&BaseChannel::MuteStream_w, this, ssrc, mute));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000258}
259
260bool BaseChannel::IsStreamMuted(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000261 return InvokeOnWorker(Bind(&BaseChannel::IsStreamMuted_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000262}
263
264bool BaseChannel::AddRecvStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000265 return InvokeOnWorker(Bind(&BaseChannel::AddRecvStream_w, this, sp));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000266}
267
268bool BaseChannel::RemoveRecvStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000269 return InvokeOnWorker(Bind(&BaseChannel::RemoveRecvStream_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270}
271
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000272bool BaseChannel::AddSendStream(const StreamParams& sp) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000273 return InvokeOnWorker(
274 Bind(&MediaChannel::AddSendStream, media_channel(), sp));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000275}
276
277bool BaseChannel::RemoveSendStream(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000278 return InvokeOnWorker(
279 Bind(&MediaChannel::RemoveSendStream, media_channel(), ssrc));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +0000280}
281
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000282bool BaseChannel::SetLocalContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000283 ContentAction action,
284 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000285 return InvokeOnWorker(Bind(&BaseChannel::SetLocalContent_w,
286 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000287}
288
289bool BaseChannel::SetRemoteContent(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000290 ContentAction action,
291 std::string* error_desc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000292 return InvokeOnWorker(Bind(&BaseChannel::SetRemoteContent_w,
293 this, content, action, error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000294}
295
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000296void BaseChannel::StartConnectionMonitor(int cms) {
297 socket_monitor_.reset(new SocketMonitor(transport_channel_,
298 worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000299 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 socket_monitor_->SignalUpdate.connect(
301 this, &BaseChannel::OnConnectionMonitorUpdate);
302 socket_monitor_->Start(cms);
303}
304
305void BaseChannel::StopConnectionMonitor() {
306 if (socket_monitor_) {
307 socket_monitor_->Stop();
308 socket_monitor_.reset();
309 }
310}
311
312void BaseChannel::set_rtcp_transport_channel(TransportChannel* channel) {
313 if (rtcp_transport_channel_ != channel) {
314 if (rtcp_transport_channel_) {
315 session_->DestroyChannel(
316 content_name_, rtcp_transport_channel_->component());
317 }
318 rtcp_transport_channel_ = channel;
319 if (rtcp_transport_channel_) {
320 // TODO(juberti): Propagate this error code
321 VERIFY(SetDtlsSrtpCiphers(rtcp_transport_channel_, true));
322 rtcp_transport_channel_->SignalWritableState.connect(
323 this, &BaseChannel::OnWritableState);
324 rtcp_transport_channel_->SignalReadPacket.connect(
325 this, &BaseChannel::OnChannelRead);
326 rtcp_transport_channel_->SignalReadyToSend.connect(
327 this, &BaseChannel::OnReadyToSend);
328 }
329 }
330}
331
332bool BaseChannel::IsReadyToReceive() const {
333 // Receive data if we are enabled and have local content,
334 return enabled() && IsReceiveContentDirection(local_content_direction_);
335}
336
337bool BaseChannel::IsReadyToSend() const {
338 // Send outgoing data if we are enabled, have local and remote content,
339 // and we have had some form of connectivity.
340 return enabled() &&
341 IsReceiveContentDirection(remote_content_direction_) &&
342 IsSendContentDirection(local_content_direction_) &&
343 was_ever_writable();
344}
345
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000346bool BaseChannel::SendPacket(rtc::Buffer* packet,
347 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000348 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349}
350
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000351bool BaseChannel::SendRtcp(rtc::Buffer* packet,
352 rtc::DiffServCodePoint dscp) {
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000353 return SendPacket(true, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354}
355
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000356int BaseChannel::SetOption(SocketType type, rtc::Socket::Option opt,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 int value) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000358 TransportChannel* channel = NULL;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 switch (type) {
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000360 case ST_RTP:
361 channel = transport_channel_;
362 break;
363 case ST_RTCP:
364 channel = rtcp_transport_channel_;
365 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000366 }
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000367 return channel ? channel->SetOption(opt, value) : -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000368}
369
370void BaseChannel::OnWritableState(TransportChannel* channel) {
371 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
372 if (transport_channel_->writable()
373 && (!rtcp_transport_channel_ || rtcp_transport_channel_->writable())) {
374 ChannelWritable_w();
375 } else {
376 ChannelNotWritable_w();
377 }
378}
379
380void BaseChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000381 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000382 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000383 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000385 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000386
387 // When using RTCP multiplexing we might get RTCP packets on the RTP
388 // transport. We feed RTP traffic into the demuxer to determine if it is RTCP.
389 bool rtcp = PacketIsRtcp(channel, data, len);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000390 rtc::Buffer packet(data, len);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000391 HandlePacket(rtcp, &packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000392}
393
394void BaseChannel::OnReadyToSend(TransportChannel* channel) {
395 SetReadyToSend(channel, true);
396}
397
398void BaseChannel::SetReadyToSend(TransportChannel* channel, bool ready) {
399 ASSERT(channel == transport_channel_ || channel == rtcp_transport_channel_);
400 if (channel == transport_channel_) {
401 rtp_ready_to_send_ = ready;
402 }
403 if (channel == rtcp_transport_channel_) {
404 rtcp_ready_to_send_ = ready;
405 }
406
407 if (!ready) {
408 // Notify the MediaChannel when either rtp or rtcp channel can't send.
409 media_channel_->OnReadyToSend(false);
410 } else if (rtp_ready_to_send_ &&
411 // In the case of rtcp mux |rtcp_transport_channel_| will be null.
412 (rtcp_ready_to_send_ || !rtcp_transport_channel_)) {
413 // Notify the MediaChannel when both rtp and rtcp channel can send.
414 media_channel_->OnReadyToSend(true);
415 }
416}
417
418bool BaseChannel::PacketIsRtcp(const TransportChannel* channel,
419 const char* data, size_t len) {
420 return (channel == rtcp_transport_channel_ ||
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000421 rtcp_mux_filter_.DemuxRtcp(data, static_cast<int>(len)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422}
423
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000424bool BaseChannel::SendPacket(bool rtcp, rtc::Buffer* packet,
425 rtc::DiffServCodePoint dscp) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000426 // SendPacket gets called from MediaEngine, typically on an encoder thread.
427 // If the thread is not our worker thread, we will post to our worker
428 // so that the real work happens on our worker. This avoids us having to
429 // synchronize access to all the pieces of the send path, including
430 // SRTP and the inner workings of the transport channels.
431 // The only downside is that we can't return a proper failure code if
432 // needed. Since UDP is unreliable anyway, this should be a non-issue.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000433 if (rtc::Thread::Current() != worker_thread_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434 // Avoid a copy by transferring the ownership of the packet data.
435 int message_id = (!rtcp) ? MSG_RTPPACKET : MSG_RTCPPACKET;
436 PacketMessageData* data = new PacketMessageData;
437 packet->TransferTo(&data->packet);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000438 data->dscp = dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 worker_thread_->Post(this, message_id, data);
440 return true;
441 }
442
443 // Now that we are on the correct thread, ensure we have a place to send this
444 // packet before doing anything. (We might get RTCP packets that we don't
445 // intend to send.) If we've negotiated RTCP mux, send RTCP over the RTP
446 // transport.
447 TransportChannel* channel = (!rtcp || rtcp_mux_filter_.IsActive()) ?
448 transport_channel_ : rtcp_transport_channel_;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000449 if (!channel || !channel->writable()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000450 return false;
451 }
452
453 // Protect ourselves against crazy data.
454 if (!ValidPacket(rtcp, packet)) {
455 LOG(LS_ERROR) << "Dropping outgoing " << content_name_ << " "
456 << PacketType(rtcp) << " packet: wrong size="
457 << packet->length();
458 return false;
459 }
460
461 // Signal to the media sink before protecting the packet.
462 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000463 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000464 SignalSendPacketPreCrypto(packet->data(), packet->length(), rtcp);
465 }
466
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000467 rtc::PacketOptions options(dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000468 // Protect if needed.
469 if (srtp_filter_.IsActive()) {
470 bool res;
471 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000472 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000474 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
475 // inside libsrtp for a RTP packet. A external HMAC module will be writing
476 // a fake HMAC value. This is ONLY done for a RTP packet.
477 // Socket layer will update rtp sendtime extension header if present in
478 // packet with current time before updating the HMAC.
479#if !defined(ENABLE_EXTERNAL_AUTH)
480 res = srtp_filter_.ProtectRtp(
481 data, len, static_cast<int>(packet->capacity()), &len);
482#else
henrike@webrtc.org05376342014-03-10 15:53:12 +0000483 options.packet_time_params.rtp_sendtime_extension_id =
484 rtp_abs_sendtime_extn_id_;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000485 res = srtp_filter_.ProtectRtp(
486 data, len, static_cast<int>(packet->capacity()), &len,
487 &options.packet_time_params.srtp_packet_index);
488 // If protection succeeds, let's get auth params from srtp.
489 if (res) {
490 uint8* auth_key = NULL;
491 int key_len;
492 res = srtp_filter_.GetRtpAuthParams(
493 &auth_key, &key_len, &options.packet_time_params.srtp_auth_tag_len);
494 if (res) {
495 options.packet_time_params.srtp_auth_key.resize(key_len);
496 options.packet_time_params.srtp_auth_key.assign(auth_key,
497 auth_key + key_len);
498 }
499 }
500#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501 if (!res) {
502 int seq_num = -1;
503 uint32 ssrc = 0;
504 GetRtpSeqNum(data, len, &seq_num);
505 GetRtpSsrc(data, len, &ssrc);
506 LOG(LS_ERROR) << "Failed to protect " << content_name_
507 << " RTP packet: size=" << len
508 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
509 return false;
510 }
511 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000512 res = srtp_filter_.ProtectRtcp(data, len,
513 static_cast<int>(packet->capacity()),
514 &len);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 if (!res) {
516 int type = -1;
517 GetRtcpType(data, len, &type);
518 LOG(LS_ERROR) << "Failed to protect " << content_name_
519 << " RTCP packet: size=" << len << ", type=" << type;
520 return false;
521 }
522 }
523
524 // Update the length of the packet now that we've added the auth tag.
525 packet->SetLength(len);
526 } else if (secure_required_) {
527 // This is a double check for something that supposedly can't happen.
528 LOG(LS_ERROR) << "Can't send outgoing " << PacketType(rtcp)
529 << " packet when SRTP is inactive and crypto is required";
530
531 ASSERT(false);
532 return false;
533 }
534
535 // Signal to the media sink after protecting the packet.
536 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000537 rtc::CritScope cs(&signal_send_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 SignalSendPacketPostCrypto(packet->data(), packet->length(), rtcp);
539 }
540
541 // Bon voyage.
mallinath@webrtc.org385857d2014-02-14 00:56:12 +0000542 int ret = channel->SendPacket(packet->data(), packet->length(), options,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 (secure() && secure_dtls()) ? PF_SRTP_BYPASS : 0);
544 if (ret != static_cast<int>(packet->length())) {
545 if (channel->GetError() == EWOULDBLOCK) {
546 LOG(LS_WARNING) << "Got EWOULDBLOCK from socket.";
547 SetReadyToSend(channel, false);
548 }
549 return false;
550 }
551 return true;
552}
553
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000554bool BaseChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000555 // Protect ourselves against crazy data.
556 if (!ValidPacket(rtcp, packet)) {
557 LOG(LS_ERROR) << "Dropping incoming " << content_name_ << " "
558 << PacketType(rtcp) << " packet: wrong size="
559 << packet->length();
560 return false;
561 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000562
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +0000563 // Bundle filter handles both rtp and rtcp packets.
564 return bundle_filter_.DemuxPacket(packet->data(), packet->length(), rtcp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000565}
566
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000567void BaseChannel::HandlePacket(bool rtcp, rtc::Buffer* packet,
568 const rtc::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000569 if (!WantsPacket(rtcp, packet)) {
570 return;
571 }
572
buildbot@webrtc.org6bfd6192014-05-15 16:15:59 +0000573 if (!has_received_packet_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000574 has_received_packet_ = true;
575 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
576 }
577
578 // Signal to the media sink before unprotecting the packet.
579 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000580 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000581 SignalRecvPacketPostCrypto(packet->data(), packet->length(), rtcp);
582 }
583
584 // Unprotect the packet, if needed.
585 if (srtp_filter_.IsActive()) {
586 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000587 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000588 bool res;
589 if (!rtcp) {
590 res = srtp_filter_.UnprotectRtp(data, len, &len);
591 if (!res) {
592 int seq_num = -1;
593 uint32 ssrc = 0;
594 GetRtpSeqNum(data, len, &seq_num);
595 GetRtpSsrc(data, len, &ssrc);
596 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
597 << " RTP packet: size=" << len
598 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
599 return;
600 }
601 } else {
602 res = srtp_filter_.UnprotectRtcp(data, len, &len);
603 if (!res) {
604 int type = -1;
605 GetRtcpType(data, len, &type);
606 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
607 << " RTCP packet: size=" << len << ", type=" << type;
608 return;
609 }
610 }
611
612 packet->SetLength(len);
613 } else if (secure_required_) {
614 // Our session description indicates that SRTP is required, but we got a
615 // packet before our SRTP filter is active. This means either that
616 // a) we got SRTP packets before we received the SDES keys, in which case
617 // we can't decrypt it anyway, or
618 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
619 // channels, so we haven't yet extracted keys, even if DTLS did complete
620 // on the channel that the packets are being sent on. It's really good
621 // practice to wait for both RTP and RTCP to be good to go before sending
622 // media, to prevent weird failure modes, so it's fine for us to just eat
623 // packets here. This is all sidestepped if RTCP mux is used anyway.
624 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
625 << " packet when SRTP is inactive and crypto is required";
626 return;
627 }
628
629 // Signal to the media sink after unprotecting the packet.
630 {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000631 rtc::CritScope cs(&signal_recv_packet_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632 SignalRecvPacketPreCrypto(packet->data(), packet->length(), rtcp);
633 }
634
635 // Push it down to the media channel.
636 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000637 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000638 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000639 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 }
641}
642
643void BaseChannel::OnNewLocalDescription(
644 BaseSession* session, ContentAction action) {
645 const ContentInfo* content_info =
646 GetFirstContent(session->local_description());
647 const MediaContentDescription* content_desc =
648 GetContentDescription(content_info);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000649 std::string error_desc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650 if (content_desc && content_info && !content_info->rejected &&
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000651 !SetLocalContent(content_desc, action, &error_desc)) {
652 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000654 }
655}
656
657void BaseChannel::OnNewRemoteDescription(
658 BaseSession* session, ContentAction action) {
659 const ContentInfo* content_info =
660 GetFirstContent(session->remote_description());
661 const MediaContentDescription* content_desc =
662 GetContentDescription(content_info);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000663 std::string error_desc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664 if (content_desc && content_info && !content_info->rejected &&
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000665 !SetRemoteContent(content_desc, action, &error_desc)) {
666 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
667 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000668 }
669}
670
671void BaseChannel::EnableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000672 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000673 if (enabled_)
674 return;
675
676 LOG(LS_INFO) << "Channel enabled";
677 enabled_ = true;
678 ChangeState();
679}
680
681void BaseChannel::DisableMedia_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000682 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683 if (!enabled_)
684 return;
685
686 LOG(LS_INFO) << "Channel disabled";
687 enabled_ = false;
688 ChangeState();
689}
690
691bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000692 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693 bool ret = media_channel()->MuteStream(ssrc, mute);
694 if (ret) {
695 if (mute)
696 muted_streams_.insert(ssrc);
697 else
698 muted_streams_.erase(ssrc);
699 }
700 return ret;
701}
702
703bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000704 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 return muted_streams_.find(ssrc) != muted_streams_.end();
706}
707
708void BaseChannel::ChannelWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000709 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000710 if (writable_)
711 return;
712
713 LOG(LS_INFO) << "Channel socket writable ("
714 << transport_channel_->content_name() << ", "
715 << transport_channel_->component() << ")"
716 << (was_ever_writable_ ? "" : " for the first time");
717
718 std::vector<ConnectionInfo> infos;
719 transport_channel_->GetStats(&infos);
720 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
721 it != infos.end(); ++it) {
722 if (it->best_connection) {
723 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
724 << "->" << it->remote_candidate.ToSensitiveString();
725 break;
726 }
727 }
728
729 // If we're doing DTLS-SRTP, now is the time.
730 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
731 if (!SetupDtlsSrtp(false)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000732 const std::string error_desc =
733 "Couldn't set up DTLS-SRTP on RTP channel.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000734 // Sent synchronously.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000735 signaling_thread()->Invoke<void>(Bind(
736 &SetSessionError,
737 session_,
738 BaseSession::ERROR_TRANSPORT,
739 error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 return;
741 }
742
743 if (rtcp_transport_channel_) {
744 if (!SetupDtlsSrtp(true)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000745 const std::string error_desc =
746 "Couldn't set up DTLS-SRTP on RTCP channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000747 // Sent synchronously.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000748 signaling_thread()->Invoke<void>(Bind(
749 &SetSessionError,
750 session_,
751 BaseSession::ERROR_TRANSPORT,
752 error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 return;
754 }
755 }
756 }
757
758 was_ever_writable_ = true;
759 writable_ = true;
760 ChangeState();
761}
762
763bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
764 std::vector<std::string> ciphers;
765 // We always use the default SRTP ciphers for RTCP, but we may use different
766 // ciphers for RTP depending on the media type.
767 if (!rtcp) {
768 GetSrtpCiphers(&ciphers);
769 } else {
770 GetSupportedDefaultCryptoSuites(&ciphers);
771 }
772 return tc->SetSrtpCiphers(ciphers);
773}
774
775bool BaseChannel::ShouldSetupDtlsSrtp() const {
776 return true;
777}
778
779// This function returns true if either DTLS-SRTP is not in use
780// *or* DTLS-SRTP is successfully set up.
781bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
782 bool ret = false;
783
784 TransportChannel *channel = rtcp_channel ?
785 rtcp_transport_channel_ : transport_channel_;
786
787 // No DTLS
788 if (!channel->IsDtlsActive())
789 return true;
790
791 std::string selected_cipher;
792
793 if (!channel->GetSrtpCipher(&selected_cipher)) {
794 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
795 return false;
796 }
797
798 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
799 << content_name() << " "
800 << PacketType(rtcp_channel);
801
802 // OK, we're now doing DTLS (RFC 5764)
803 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
804 SRTP_MASTER_KEY_SALT_LEN * 2);
805
806 // RFC 5705 exporter using the RFC 5764 parameters
807 if (!channel->ExportKeyingMaterial(
808 kDtlsSrtpExporterLabel,
809 NULL, 0, false,
810 &dtls_buffer[0], dtls_buffer.size())) {
811 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
812 ASSERT(false); // This should never happen
813 return false;
814 }
815
816 // Sync up the keys with the DTLS-SRTP interface
817 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
818 SRTP_MASTER_KEY_SALT_LEN);
819 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
820 SRTP_MASTER_KEY_SALT_LEN);
821 size_t offset = 0;
822 memcpy(&client_write_key[0], &dtls_buffer[offset],
823 SRTP_MASTER_KEY_KEY_LEN);
824 offset += SRTP_MASTER_KEY_KEY_LEN;
825 memcpy(&server_write_key[0], &dtls_buffer[offset],
826 SRTP_MASTER_KEY_KEY_LEN);
827 offset += SRTP_MASTER_KEY_KEY_LEN;
828 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
829 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
830 offset += SRTP_MASTER_KEY_SALT_LEN;
831 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
832 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
833
834 std::vector<unsigned char> *send_key, *recv_key;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000835 rtc::SSLRole role;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000836 if (!channel->GetSslRole(&role)) {
837 LOG(LS_WARNING) << "GetSslRole failed";
838 return false;
839 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000840
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000841 if (role == rtc::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000842 send_key = &server_write_key;
843 recv_key = &client_write_key;
844 } else {
845 send_key = &client_write_key;
846 recv_key = &server_write_key;
847 }
848
849 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000850 ret = srtp_filter_.SetRtcpParams(
851 selected_cipher,
852 &(*send_key)[0],
853 static_cast<int>(send_key->size()),
854 selected_cipher,
855 &(*recv_key)[0],
856 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000858 ret = srtp_filter_.SetRtpParams(
859 selected_cipher,
860 &(*send_key)[0],
861 static_cast<int>(send_key->size()),
862 selected_cipher,
863 &(*recv_key)[0],
864 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 }
866
867 if (!ret)
868 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
869 else
870 dtls_keyed_ = true;
871
872 return ret;
873}
874
875void BaseChannel::ChannelNotWritable_w() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000876 ASSERT(worker_thread_ == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000877 if (!writable_)
878 return;
879
880 LOG(LS_INFO) << "Channel socket not writable ("
881 << transport_channel_->content_name() << ", "
882 << transport_channel_->component() << ")";
883 writable_ = false;
884 ChangeState();
885}
886
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000887// |dtls| will be set to true if DTLS is active for transport channel and
888// crypto is empty.
889bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000890 bool* dtls,
891 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000892 *dtls = transport_channel_->IsDtlsActive();
893 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000894 SafeSetError("Cryptos must be empty when DTLS is active.",
895 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000896 return false;
897 }
898 return true;
899}
900
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +0000901bool BaseChannel::SetRecvRtpHeaderExtensions_w(
902 const MediaContentDescription* content,
903 MediaChannel* media_channel,
904 std::string* error_desc) {
905 if (content->rtp_header_extensions_set()) {
906 if (!media_channel->SetRecvRtpHeaderExtensions(
907 content->rtp_header_extensions())) {
908 std::ostringstream desc;
909 desc << "Failed to set receive rtp header extensions for "
910 << MediaTypeToString(content->type()) << " content.";
911 SafeSetError(desc.str(), error_desc);
912 return false;
913 }
914 }
915 return true;
916}
917
918bool BaseChannel::SetSendRtpHeaderExtensions_w(
919 const MediaContentDescription* content,
920 MediaChannel* media_channel,
921 std::string* error_desc) {
922 if (content->rtp_header_extensions_set()) {
923 if (!media_channel->SetSendRtpHeaderExtensions(
924 content->rtp_header_extensions())) {
925 std::ostringstream desc;
926 desc << "Failed to set send rtp header extensions for "
927 << MediaTypeToString(content->type()) << " content.";
928 SafeSetError(desc.str(), error_desc);
929 return false;
930 } else {
931 MaybeCacheRtpAbsSendTimeHeaderExtension(content->rtp_header_extensions());
932 }
933 }
934 return true;
935}
936
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000938 ContentAction action,
939 ContentSource src,
940 std::string* error_desc) {
941 if (action == CA_UPDATE) {
942 // no crypto params.
943 return true;
944 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000946 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000947 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
948 if (!ret) {
949 return false;
950 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 switch (action) {
952 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000953 // If DTLS is already active on the channel, we could be renegotiating
954 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000955 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000956 ret = srtp_filter_.SetOffer(cryptos, src);
957 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 break;
959 case CA_PRANSWER:
960 // If we're doing DTLS-SRTP, we don't want to update the filter
961 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000962 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
964 }
965 break;
966 case CA_ANSWER:
967 // If we're doing DTLS-SRTP, we don't want to update the filter
968 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000969 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 ret = srtp_filter_.SetAnswer(cryptos, src);
971 }
972 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 default:
974 break;
975 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000976 if (!ret) {
977 SafeSetError("Failed to setup SRTP filter.", error_desc);
978 return false;
979 }
980 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981}
982
983bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000984 ContentSource src,
985 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986 bool ret = false;
987 switch (action) {
988 case CA_OFFER:
989 ret = rtcp_mux_filter_.SetOffer(enable, src);
990 break;
991 case CA_PRANSWER:
992 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
993 break;
994 case CA_ANSWER:
995 ret = rtcp_mux_filter_.SetAnswer(enable, src);
996 if (ret && rtcp_mux_filter_.IsActive()) {
997 // We activated RTCP mux, close down the RTCP transport.
998 set_rtcp_transport_channel(NULL);
999 }
1000 break;
1001 case CA_UPDATE:
1002 // No RTCP mux info.
1003 ret = true;
1004 default:
1005 break;
1006 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001007 if (!ret) {
1008 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
1009 return false;
1010 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
1012 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
1013 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001014 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 // If the RTP transport is already writable, then so are we.
1016 if (transport_channel_->writable()) {
1017 ChannelWritable_w();
1018 }
1019 }
1020
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001021 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001022}
1023
1024bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001025 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001026 if (!media_channel()->AddRecvStream(sp))
1027 return false;
1028
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001029 return bundle_filter_.AddStream(sp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030}
1031
1032bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001033 ASSERT(worker_thread() == rtc::Thread::Current());
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001034 bundle_filter_.RemoveStream(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035 return media_channel()->RemoveRecvStream(ssrc);
1036}
1037
1038bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001039 ContentAction action,
1040 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1042 action == CA_PRANSWER || action == CA_UPDATE))
1043 return false;
1044
1045 // If this is an update, streams only contain streams that have changed.
1046 if (action == CA_UPDATE) {
1047 for (StreamParamsVec::const_iterator it = streams.begin();
1048 it != streams.end(); ++it) {
1049 StreamParams existing_stream;
1050 bool stream_exist = GetStreamByIds(local_streams_, it->groupid,
1051 it->id, &existing_stream);
1052 if (!stream_exist && it->has_ssrcs()) {
1053 if (media_channel()->AddSendStream(*it)) {
1054 local_streams_.push_back(*it);
1055 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1056 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001057 std::ostringstream desc;
1058 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1059 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001060 return false;
1061 }
1062 } else if (stream_exist && !it->has_ssrcs()) {
1063 if (!media_channel()->RemoveSendStream(existing_stream.first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001064 std::ostringstream desc;
1065 desc << "Failed to remove send stream with ssrc "
1066 << it->first_ssrc() << ".";
1067 SafeSetError(desc.str(), error_desc);
1068 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 }
1070 RemoveStreamBySsrc(&local_streams_, existing_stream.first_ssrc());
1071 } else {
1072 LOG(LS_WARNING) << "Ignore unsupported stream update";
1073 }
1074 }
1075 return true;
1076 }
1077 // Else streams are all the streams we want to send.
1078
1079 // Check for streams that have been removed.
1080 bool ret = true;
1081 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1082 it != local_streams_.end(); ++it) {
1083 if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1084 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001085 std::ostringstream desc;
1086 desc << "Failed to remove send stream with ssrc "
1087 << it->first_ssrc() << ".";
1088 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089 ret = false;
1090 }
1091 }
1092 }
1093 // Check for new streams.
1094 for (StreamParamsVec::const_iterator it = streams.begin();
1095 it != streams.end(); ++it) {
1096 if (!GetStreamBySsrc(local_streams_, it->first_ssrc(), NULL)) {
1097 if (media_channel()->AddSendStream(*it)) {
1098 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1099 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001100 std::ostringstream desc;
1101 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1102 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 ret = false;
1104 }
1105 }
1106 }
1107 local_streams_ = streams;
1108 return ret;
1109}
1110
1111bool BaseChannel::UpdateRemoteStreams_w(
1112 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001113 ContentAction action,
1114 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1116 action == CA_PRANSWER || action == CA_UPDATE))
1117 return false;
1118
1119 // If this is an update, streams only contain streams that have changed.
1120 if (action == CA_UPDATE) {
1121 for (StreamParamsVec::const_iterator it = streams.begin();
1122 it != streams.end(); ++it) {
1123 StreamParams existing_stream;
1124 bool stream_exists = GetStreamByIds(remote_streams_, it->groupid,
1125 it->id, &existing_stream);
1126 if (!stream_exists && it->has_ssrcs()) {
1127 if (AddRecvStream_w(*it)) {
1128 remote_streams_.push_back(*it);
1129 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1130 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001131 std::ostringstream desc;
1132 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1133 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001134 return false;
1135 }
1136 } else if (stream_exists && !it->has_ssrcs()) {
1137 if (!RemoveRecvStream_w(existing_stream.first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001138 std::ostringstream desc;
1139 desc << "Failed to remove remote stream with ssrc "
1140 << it->first_ssrc() << ".";
1141 SafeSetError(desc.str(), error_desc);
1142 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001143 }
1144 RemoveStreamBySsrc(&remote_streams_, existing_stream.first_ssrc());
1145 } else {
1146 LOG(LS_WARNING) << "Ignore unsupported stream update."
1147 << " Stream exists? " << stream_exists
1148 << " existing stream = " << existing_stream.ToString()
1149 << " new stream = " << it->ToString();
1150 }
1151 }
1152 return true;
1153 }
1154 // Else streams are all the streams we want to receive.
1155
1156 // Check for streams that have been removed.
1157 bool ret = true;
1158 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1159 it != remote_streams_.end(); ++it) {
1160 if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1161 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001162 std::ostringstream desc;
1163 desc << "Failed to remove remote stream with ssrc "
1164 << it->first_ssrc() << ".";
1165 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 ret = false;
1167 }
1168 }
1169 }
1170 // Check for new streams.
1171 for (StreamParamsVec::const_iterator it = streams.begin();
1172 it != streams.end(); ++it) {
1173 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc(), NULL)) {
1174 if (AddRecvStream_w(*it)) {
1175 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1176 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001177 std::ostringstream desc;
1178 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1179 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 ret = false;
1181 }
1182 }
1183 }
1184 remote_streams_ = streams;
1185 return ret;
1186}
1187
1188bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001189 ContentAction action,
1190 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 // Cache secure_required_ for belt and suspenders check on SendPacket
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001192 secure_required_ = content->crypto_required() != CT_NONE;
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001193 // Set local RTP header extensions.
1194 bool ret = SetRecvRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001196 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001198 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001199
1200 // Call UpdateLocalStreams_w last to make sure as many settings as possible
1201 // are already set when creating streams.
1202 ret &= UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 set_local_content_direction(content->direction());
1204 return ret;
1205}
1206
1207bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001208 ContentAction action,
1209 std::string* error_desc) {
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001210 // Set remote RTP header extensions.
1211 bool ret = SetSendRtpHeaderExtensions_w(content, media_channel(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001213 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001215 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001216 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1217 std::ostringstream desc;
1218 desc << "Failed to set max send bandwidth for "
1219 << MediaTypeToString(content->type()) << " content.";
1220 SafeSetError(desc.str(), error_desc);
1221 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001222 }
buildbot@webrtc.org75ce9202014-06-20 12:30:24 +00001223
1224 // Call UpdateRemoteStreams_w last to make sure as many settings as possible
1225 // are already set when creating streams.
1226 ret &= UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 set_remote_content_direction(content->direction());
1228 return ret;
1229}
1230
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001231void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1232 const std::vector<RtpHeaderExtension>& extensions) {
1233 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001234 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001235 rtp_abs_sendtime_extn_id_ =
1236 send_time_extension ? send_time_extension->id : -1;
1237}
1238
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001239void BaseChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001241 case MSG_RTPPACKET:
1242 case MSG_RTCPPACKET: {
1243 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001244 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001245 delete data; // because it is Posted
1246 break;
1247 }
1248 case MSG_FIRSTPACKETRECEIVED: {
1249 SignalFirstPacketReceived(this);
1250 break;
1251 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252 }
1253}
1254
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001255void BaseChannel::FlushRtcpMessages() {
1256 // Flush all remaining RTCP messages. This should only be called in
1257 // destructor.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001258 ASSERT(rtc::Thread::Current() == worker_thread_);
1259 rtc::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001260 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001261 for (rtc::MessageList::iterator it = rtcp_messages.begin();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001262 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001263 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264 }
1265}
1266
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001267VoiceChannel::VoiceChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001268 MediaEngineInterface* media_engine,
1269 VoiceMediaChannel* media_channel,
1270 BaseSession* session,
1271 const std::string& content_name,
1272 bool rtcp)
1273 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1274 rtcp),
1275 received_media_(false) {
1276}
1277
1278VoiceChannel::~VoiceChannel() {
1279 StopAudioMonitor();
1280 StopMediaMonitor();
1281 // this can't be done in the base class, since it calls a virtual
1282 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001283 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001284}
1285
1286bool VoiceChannel::Init() {
1287 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1288 content_name(), "rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1289 if (!BaseChannel::Init(session()->CreateChannel(
1290 content_name(), "rtp", ICE_CANDIDATE_COMPONENT_RTP),
1291 rtcp_channel)) {
1292 return false;
1293 }
1294 media_channel()->SignalMediaError.connect(
1295 this, &VoiceChannel::OnVoiceChannelError);
1296 srtp_filter()->SignalSrtpError.connect(
1297 this, &VoiceChannel::OnSrtpError);
1298 return true;
1299}
1300
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001301bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001302 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1303 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001304}
1305
1306bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001307 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1308 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001309}
1310
1311bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001312 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313}
1314
1315// TODO(juberti): Handle early media the right way. We should get an explicit
1316// ringing message telling us to start playing local ringback, which we cancel
1317// if any early media actually arrives. For now, we do the opposite, which is
1318// to wait 1 second for early media, and start playing local ringback if none
1319// arrives.
1320void VoiceChannel::SetEarlyMedia(bool enable) {
1321 if (enable) {
1322 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001323 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1324 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 } else {
1326 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001327 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 }
1329}
1330
1331bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001332 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1333 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001334}
1335
1336bool VoiceChannel::PressDTMF(int digit, bool playout) {
1337 int flags = DF_SEND;
1338 if (playout) {
1339 flags |= DF_PLAY;
1340 }
1341 int duration_ms = 160;
1342 return InsertDtmf(0, digit, duration_ms, flags);
1343}
1344
1345bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001346 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1347 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001348}
1349
1350bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1351 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001352 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1353 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001354}
1355
1356bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001357 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1358 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001359}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001360
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001362 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1363 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001364}
1365
1366void VoiceChannel::StartMediaMonitor(int cms) {
1367 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001368 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 media_monitor_->SignalUpdate.connect(
1370 this, &VoiceChannel::OnMediaMonitorUpdate);
1371 media_monitor_->Start(cms);
1372}
1373
1374void VoiceChannel::StopMediaMonitor() {
1375 if (media_monitor_) {
1376 media_monitor_->Stop();
1377 media_monitor_->SignalUpdate.disconnect(this);
1378 media_monitor_.reset();
1379 }
1380}
1381
1382void VoiceChannel::StartAudioMonitor(int cms) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001383 audio_monitor_.reset(new AudioMonitor(this, rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 audio_monitor_
1385 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1386 audio_monitor_->Start(cms);
1387}
1388
1389void VoiceChannel::StopAudioMonitor() {
1390 if (audio_monitor_) {
1391 audio_monitor_->Stop();
1392 audio_monitor_.reset();
1393 }
1394}
1395
1396bool VoiceChannel::IsAudioMonitorRunning() const {
1397 return (audio_monitor_.get() != NULL);
1398}
1399
1400void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1401 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1402 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1403}
1404
1405void VoiceChannel::StopTypingMonitor() {
1406 typing_monitor_.reset();
1407}
1408
1409bool VoiceChannel::IsTypingMonitorRunning() const {
1410 return typing_monitor_;
1411}
1412
1413bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1414 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1415 if (typing_monitor_ && mute)
1416 typing_monitor_->OnChannelMuted();
1417 return ret;
1418}
1419
1420int VoiceChannel::GetInputLevel_w() {
1421 return media_engine()->GetInputLevel();
1422}
1423
1424int VoiceChannel::GetOutputLevel_w() {
1425 return media_channel()->GetOutputLevel();
1426}
1427
1428void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1429 media_channel()->GetActiveStreams(actives);
1430}
1431
1432void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001433 const char* data, size_t len,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001434 const rtc::PacketTime& packet_time,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001435 int flags) {
1436 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001437
1438 // Set a flag when we've received an RTP packet. If we're waiting for early
1439 // media, this will disable the timeout.
1440 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1441 received_media_ = true;
1442 }
1443}
1444
1445void VoiceChannel::ChangeState() {
1446 // Render incoming data if we're the active call, and we have the local
1447 // content. We receive data on the default channel and multiplexed streams.
1448 bool recv = IsReadyToReceive();
1449 if (!media_channel()->SetPlayout(recv)) {
1450 SendLastMediaError();
1451 }
1452
1453 // Send outgoing data if we're the active call, we have the remote content,
1454 // and we have had some form of connectivity.
1455 bool send = IsReadyToSend();
1456 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1457 if (!media_channel()->SetSend(send_flag)) {
1458 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1459 SendLastMediaError();
1460 }
1461
1462 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1463}
1464
1465const ContentInfo* VoiceChannel::GetFirstContent(
1466 const SessionDescription* sdesc) {
1467 return GetFirstAudioContent(sdesc);
1468}
1469
1470bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001471 ContentAction action,
1472 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001473 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 LOG(LS_INFO) << "Setting local voice description";
1475
1476 const AudioContentDescription* audio =
1477 static_cast<const AudioContentDescription*>(content);
1478 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001479 if (!audio) {
1480 SafeSetError("Can't find audio content in local description.", error_desc);
1481 return false;
1482 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001484 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 // Set local audio codecs (what we want to receive).
1486 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1487 // is set properly.
1488 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001489 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1490 SafeSetError("Failed to set audio receive codecs.", error_desc);
1491 ret = false;
1492 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 }
1494
1495 // If everything worked, see if we can start receiving.
1496 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001497 std::vector<AudioCodec>::const_iterator it = audio->codecs().begin();
1498 for (; it != audio->codecs().end(); ++it) {
1499 bundle_filter()->AddPayloadType(it->id);
1500 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501 ChangeState();
1502 } else {
1503 LOG(LS_WARNING) << "Failed to set local voice description";
1504 }
1505 return ret;
1506}
1507
1508bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001509 ContentAction action,
1510 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001511 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 LOG(LS_INFO) << "Setting remote voice description";
1513
1514 const AudioContentDescription* audio =
1515 static_cast<const AudioContentDescription*>(content);
1516 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001517 if (!audio) {
1518 SafeSetError("Can't find audio content in remote description.", error_desc);
1519 return false;
1520 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521
1522 bool ret = true;
1523 // Set remote video codecs (what the other side wants to receive).
1524 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001525 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1526 SafeSetError("Failed to set audio send codecs.", error_desc);
1527 ret = false;
1528 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529 }
1530
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001531 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532
1533 if (action != CA_UPDATE) {
1534 // Tweak our audio processing settings, if needed.
1535 AudioOptions audio_options;
1536 if (!media_channel()->GetOptions(&audio_options)) {
1537 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1538 } else {
1539 if (audio->conference_mode()) {
1540 audio_options.conference_mode.Set(true);
1541 }
1542 if (audio->agc_minus_10db()) {
1543 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1544 }
1545 if (!media_channel()->SetOptions(audio_options)) {
1546 // Log an error on failure, but don't abort the call.
1547 LOG(LS_ERROR) << "Failed to set voice channel options";
1548 }
1549 }
1550 }
1551
1552 // If everything worked, see if we can start sending.
1553 if (ret) {
1554 ChangeState();
1555 } else {
1556 LOG(LS_WARNING) << "Failed to set remote voice description";
1557 }
1558 return ret;
1559}
1560
1561bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001562 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001563 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1564}
1565
1566bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001567 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 if (play) {
1569 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1570 } else {
1571 LOG(LS_INFO) << "Stopping ringback tone";
1572 }
1573 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1574}
1575
1576void VoiceChannel::HandleEarlyMediaTimeout() {
1577 // This occurs on the main thread, not the worker thread.
1578 if (!received_media_) {
1579 LOG(LS_INFO) << "No early media received before timeout";
1580 SignalEarlyMediaTimeout(this);
1581 }
1582}
1583
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1585 int flags) {
1586 if (!enabled()) {
1587 return false;
1588 }
1589
1590 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1591}
1592
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001594 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1595 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596}
1597
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001598void VoiceChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001599 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 case MSG_EARLYMEDIATIMEOUT:
1601 HandleEarlyMediaTimeout();
1602 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 case MSG_CHANNEL_ERROR: {
1604 VoiceChannelErrorMessageData* data =
1605 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1606 SignalMediaError(this, data->ssrc, data->error);
1607 delete data;
1608 break;
1609 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610 default:
1611 BaseChannel::OnMessage(pmsg);
1612 break;
1613 }
1614}
1615
1616void VoiceChannel::OnConnectionMonitorUpdate(
1617 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
1618 SignalConnectionMonitor(this, infos);
1619}
1620
1621void VoiceChannel::OnMediaMonitorUpdate(
1622 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1623 ASSERT(media_channel == this->media_channel());
1624 SignalMediaMonitor(this, info);
1625}
1626
1627void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1628 const AudioInfo& info) {
1629 SignalAudioMonitor(this, info);
1630}
1631
1632void VoiceChannel::OnVoiceChannelError(
1633 uint32 ssrc, VoiceMediaChannel::Error err) {
1634 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1635 ssrc, err);
1636 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1637}
1638
1639void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1640 SrtpFilter::Error error) {
1641 switch (error) {
1642 case SrtpFilter::ERROR_FAIL:
1643 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1644 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1645 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1646 break;
1647 case SrtpFilter::ERROR_AUTH:
1648 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1649 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1650 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1651 break;
1652 case SrtpFilter::ERROR_REPLAY:
1653 // Only receving channel should have this error.
1654 ASSERT(mode == SrtpFilter::UNPROTECT);
1655 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1656 break;
1657 default:
1658 break;
1659 }
1660}
1661
1662void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1663 GetSupportedAudioCryptoSuites(ciphers);
1664}
1665
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001666VideoChannel::VideoChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 MediaEngineInterface* media_engine,
1668 VideoMediaChannel* media_channel,
1669 BaseSession* session,
1670 const std::string& content_name,
1671 bool rtcp,
1672 VoiceChannel* voice_channel)
1673 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1674 rtcp),
1675 voice_channel_(voice_channel),
1676 renderer_(NULL),
1677 screencapture_factory_(CreateScreenCapturerFactory()),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001678 previous_we_(rtc::WE_CLOSE) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679}
1680
1681bool VideoChannel::Init() {
1682 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1683 content_name(), "video_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1684 if (!BaseChannel::Init(session()->CreateChannel(
1685 content_name(), "video_rtp", ICE_CANDIDATE_COMPONENT_RTP),
1686 rtcp_channel)) {
1687 return false;
1688 }
1689 media_channel()->SignalMediaError.connect(
1690 this, &VideoChannel::OnVideoChannelError);
1691 srtp_filter()->SignalSrtpError.connect(
1692 this, &VideoChannel::OnSrtpError);
1693 return true;
1694}
1695
1696void VoiceChannel::SendLastMediaError() {
1697 uint32 ssrc;
1698 VoiceMediaChannel::Error error;
1699 media_channel()->GetLastMediaError(&ssrc, &error);
1700 SignalMediaError(this, ssrc, error);
1701}
1702
1703VideoChannel::~VideoChannel() {
1704 std::vector<uint32> screencast_ssrcs;
1705 ScreencastMap::iterator iter;
1706 while (!screencast_capturers_.empty()) {
1707 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1708 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1709 << screencast_capturers_.begin()->first;
1710 ASSERT(false);
1711 break;
1712 }
1713 }
1714
1715 StopMediaMonitor();
1716 // this can't be done in the base class, since it calls a virtual
1717 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001718
1719 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720}
1721
1722bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001723 worker_thread()->Invoke<void>(Bind(
1724 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 return true;
1726}
1727
1728bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001729 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730}
1731
1732VideoCapturer* VideoChannel::AddScreencast(
1733 uint32 ssrc, const ScreencastId& id) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001734 return worker_thread()->Invoke<VideoCapturer*>(Bind(
1735 &VideoChannel::AddScreencast_w, this, ssrc, id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736}
1737
1738bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001739 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1740 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741}
1742
1743bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001744 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745}
1746
1747bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001748 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749}
1750
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001751int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001752 ScreencastDetailsData data(ssrc);
1753 worker_thread()->Invoke<void>(Bind(
1754 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001755 return data.fps;
1756}
1757
1758int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001759 ScreencastDetailsData data(ssrc);
1760 worker_thread()->Invoke<void>(Bind(
1761 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001762 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763}
1764
1765bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001766 worker_thread()->Invoke<void>(Bind(
1767 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 return true;
1769}
1770
1771bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001772 worker_thread()->Invoke<void>(Bind(
1773 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 return true;
1775}
1776
1777void VideoChannel::SetScreenCaptureFactory(
1778 ScreenCapturerFactory* screencapture_factory) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001779 worker_thread()->Invoke<void>(Bind(
1780 &VideoChannel::SetScreenCaptureFactory_w,
1781 this, screencapture_factory));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782}
1783
1784void VideoChannel::ChangeState() {
1785 // Render incoming data if we're the active call, and we have the local
1786 // content. We receive data on the default channel and multiplexed streams.
1787 bool recv = IsReadyToReceive();
1788 if (!media_channel()->SetRender(recv)) {
1789 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1790 // TODO(gangji): Report error back to server.
1791 }
1792
1793 // Send outgoing data if we're the active call, we have the remote content,
1794 // and we have had some form of connectivity.
1795 bool send = IsReadyToSend();
1796 if (!media_channel()->SetSend(send)) {
1797 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1798 // TODO(gangji): Report error back to server.
1799 }
1800
1801 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1802}
1803
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001804bool VideoChannel::GetStats(
1805 const StatsOptions& options, VideoMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001806 return InvokeOnWorker(Bind(&VideoMediaChannel::GetStats,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001807 media_channel(), options, stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808}
1809
1810void VideoChannel::StartMediaMonitor(int cms) {
1811 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001812 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 media_monitor_->SignalUpdate.connect(
1814 this, &VideoChannel::OnMediaMonitorUpdate);
1815 media_monitor_->Start(cms);
1816}
1817
1818void VideoChannel::StopMediaMonitor() {
1819 if (media_monitor_) {
1820 media_monitor_->Stop();
1821 media_monitor_.reset();
1822 }
1823}
1824
1825const ContentInfo* VideoChannel::GetFirstContent(
1826 const SessionDescription* sdesc) {
1827 return GetFirstVideoContent(sdesc);
1828}
1829
1830bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001831 ContentAction action,
1832 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001833 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001834 LOG(LS_INFO) << "Setting local video description";
1835
1836 const VideoContentDescription* video =
1837 static_cast<const VideoContentDescription*>(content);
1838 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001839 if (!video) {
1840 SafeSetError("Can't find video content in local description.", error_desc);
1841 return false;
1842 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001844 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 // Set local video codecs (what we want to receive).
1846 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001847 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1848 SafeSetError("Failed to set video receive codecs.", error_desc);
1849 ret = false;
1850 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 }
1852
1853 if (action != CA_UPDATE) {
1854 VideoOptions video_options;
1855 media_channel()->GetOptions(&video_options);
1856 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1857
1858 if (!media_channel()->SetOptions(video_options)) {
1859 // Log an error on failure, but don't abort the call.
1860 LOG(LS_ERROR) << "Failed to set video channel options";
1861 }
1862 }
1863
1864 // If everything worked, see if we can start receiving.
1865 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00001866 std::vector<VideoCodec>::const_iterator it = video->codecs().begin();
1867 for (; it != video->codecs().end(); ++it) {
1868 bundle_filter()->AddPayloadType(it->id);
1869 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 ChangeState();
1871 } else {
1872 LOG(LS_WARNING) << "Failed to set local video description";
1873 }
1874 return ret;
1875}
1876
1877bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001878 ContentAction action,
1879 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001880 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001881 LOG(LS_INFO) << "Setting remote video description";
1882
1883 const VideoContentDescription* video =
1884 static_cast<const VideoContentDescription*>(content);
1885 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001886 if (!video) {
1887 SafeSetError("Can't find video content in remote description.", error_desc);
1888 return false;
1889 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890
1891 bool ret = true;
1892 // Set remote video codecs (what the other side wants to receive).
1893 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001894 if (!media_channel()->SetSendCodecs(video->codecs())) {
1895 SafeSetError("Failed to set video send codecs.", error_desc);
1896 ret = false;
1897 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898 }
1899
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001900 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901
1902 if (action != CA_UPDATE) {
1903 // Tweak our video processing settings, if needed.
1904 VideoOptions video_options;
1905 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001906 if (video->conference_mode()) {
1907 video_options.conference_mode.Set(true);
1908 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001909 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1910
1911 if (!media_channel()->SetOptions(video_options)) {
1912 // Log an error on failure, but don't abort the call.
1913 LOG(LS_ERROR) << "Failed to set video channel options";
1914 }
1915 }
1916
1917 // If everything worked, see if we can start sending.
1918 if (ret) {
1919 ChangeState();
1920 } else {
1921 LOG(LS_WARNING) << "Failed to set remote video description";
1922 }
1923 return ret;
1924}
1925
1926bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1927 bool ret = true;
1928 // Set the send format for each of the local streams. If the view request
1929 // does not contain a local stream, set its send format to 0x0, which will
1930 // drop all frames.
1931 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1932 it != local_streams().end(); ++it) {
1933 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1934 StaticVideoViews::const_iterator view;
1935 for (view = request.static_video_views.begin();
1936 view != request.static_video_views.end(); ++view) {
1937 if (view->selector.Matches(*it)) {
1938 format.width = view->width;
1939 format.height = view->height;
1940 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1941 break;
1942 }
1943 }
1944
1945 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1946 }
1947
1948 // Check if the view request has invalid streams.
1949 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1950 it != request.static_video_views.end(); ++it) {
1951 if (!GetStream(local_streams(), it->selector, NULL)) {
1952 LOG(LS_WARNING) << "View request for ("
1953 << it->selector.ssrc << ", '"
1954 << it->selector.groupid << "', '"
1955 << it->selector.streamid << "'"
1956 << ") is not in the local streams.";
1957 }
1958 }
1959
1960 return ret;
1961}
1962
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963VideoCapturer* VideoChannel::AddScreencast_w(
1964 uint32 ssrc, const ScreencastId& id) {
1965 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
1966 return NULL;
1967 }
1968 VideoCapturer* screen_capturer =
1969 screencapture_factory_->CreateScreenCapturer(id);
1970 if (!screen_capturer) {
1971 return NULL;
1972 }
1973 screen_capturer->SignalStateChange.connect(this,
1974 &VideoChannel::OnStateChange);
1975 screencast_capturers_[ssrc] = screen_capturer;
1976 return screen_capturer;
1977}
1978
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1980 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1981 if (iter == screencast_capturers_.end()) {
1982 return false;
1983 }
1984 // Clean up VideoCapturer.
1985 delete iter->second;
1986 screencast_capturers_.erase(iter);
1987 return true;
1988}
1989
1990bool VideoChannel::IsScreencasting_w() const {
1991 return !screencast_capturers_.empty();
1992}
1993
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001994void VideoChannel::GetScreencastDetails_w(
1995 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001996 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001998 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 }
2000 VideoCapturer* capturer = iter->second;
2001 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002002 data->fps = VideoFormat::IntervalToFps(video_format->interval);
2003 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004}
2005
2006void VideoChannel::SetScreenCaptureFactory_w(
2007 ScreenCapturerFactory* screencapture_factory) {
2008 if (screencapture_factory == NULL) {
2009 screencapture_factory_.reset(CreateScreenCapturerFactory());
2010 } else {
2011 screencapture_factory_.reset(screencapture_factory);
2012 }
2013}
2014
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002016 rtc::WindowEvent we) {
2017 ASSERT(signaling_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018 SignalScreencastWindowEvent(ssrc, we);
2019}
2020
2021bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002022 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
2023 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024}
2025
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002026void VideoChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 case MSG_SCREENCASTWINDOWEVENT: {
2029 const ScreencastEventMessageData* data =
2030 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
2031 OnScreencastWindowEvent_s(data->ssrc, data->event);
2032 delete data;
2033 break;
2034 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035 case MSG_CHANNEL_ERROR: {
2036 const VideoChannelErrorMessageData* data =
2037 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
2038 SignalMediaError(this, data->ssrc, data->error);
2039 delete data;
2040 break;
2041 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042 default:
2043 BaseChannel::OnMessage(pmsg);
2044 break;
2045 }
2046}
2047
2048void VideoChannel::OnConnectionMonitorUpdate(
2049 SocketMonitor *monitor, const std::vector<ConnectionInfo> &infos) {
2050 SignalConnectionMonitor(this, infos);
2051}
2052
2053// TODO(pthatcher): Look into removing duplicate code between
2054// audio, video, and data, perhaps by using templates.
2055void VideoChannel::OnMediaMonitorUpdate(
2056 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2057 ASSERT(media_channel == this->media_channel());
2058 SignalMediaMonitor(this, info);
2059}
2060
2061void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002062 rtc::WindowEvent event) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063 ScreencastEventMessageData* pdata =
2064 new ScreencastEventMessageData(ssrc, event);
2065 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2066}
2067
2068void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2069 // Map capturer events to window events. In the future we may want to simply
2070 // pass these events up directly.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002071 rtc::WindowEvent we;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002072 if (ev == CS_STOPPED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002073 we = rtc::WE_CLOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 } else if (ev == CS_PAUSED) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002075 we = rtc::WE_MINIMIZE;
2076 } else if (ev == CS_RUNNING && previous_we_ == rtc::WE_MINIMIZE) {
2077 we = rtc::WE_RESTORE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 } else {
2079 return;
2080 }
2081 previous_we_ = we;
2082
2083 uint32 ssrc = 0;
2084 if (!GetLocalSsrc(capturer, &ssrc)) {
2085 return;
2086 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002087
2088 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089}
2090
2091bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2092 *ssrc = 0;
2093 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2094 iter != screencast_capturers_.end(); ++iter) {
2095 if (iter->second == capturer) {
2096 *ssrc = iter->first;
2097 return true;
2098 }
2099 }
2100 return false;
2101}
2102
2103void VideoChannel::OnVideoChannelError(uint32 ssrc,
2104 VideoMediaChannel::Error error) {
2105 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2106 ssrc, error);
2107 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2108}
2109
2110void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2111 SrtpFilter::Error error) {
2112 switch (error) {
2113 case SrtpFilter::ERROR_FAIL:
2114 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2115 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2116 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2117 break;
2118 case SrtpFilter::ERROR_AUTH:
2119 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2120 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2121 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2122 break;
2123 case SrtpFilter::ERROR_REPLAY:
2124 // Only receving channel should have this error.
2125 ASSERT(mode == SrtpFilter::UNPROTECT);
2126 // TODO(gangji): Turn on the signaling of replay error once we have
2127 // switched to the new mechanism for doing video retransmissions.
2128 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2129 break;
2130 default:
2131 break;
2132 }
2133}
2134
2135
2136void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2137 GetSupportedVideoCryptoSuites(ciphers);
2138}
2139
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002140DataChannel::DataChannel(rtc::Thread* thread,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141 DataMediaChannel* media_channel,
2142 BaseSession* session,
2143 const std::string& content_name,
2144 bool rtcp)
2145 // MediaEngine is NULL
2146 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002147 data_channel_type_(cricket::DCT_NONE),
2148 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149}
2150
2151DataChannel::~DataChannel() {
2152 StopMediaMonitor();
2153 // this can't be done in the base class, since it calls a virtual
2154 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002155
2156 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157}
2158
2159bool DataChannel::Init() {
2160 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
2161 content_name(), "data_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
2162 if (!BaseChannel::Init(session()->CreateChannel(
2163 content_name(), "data_rtp", ICE_CANDIDATE_COMPONENT_RTP),
2164 rtcp_channel)) {
2165 return false;
2166 }
2167 media_channel()->SignalDataReceived.connect(
2168 this, &DataChannel::OnDataReceived);
2169 media_channel()->SignalMediaError.connect(
2170 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002171 media_channel()->SignalReadyToSend.connect(
2172 this, &DataChannel::OnDataChannelReadyToSend);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002173 media_channel()->SignalStreamClosedRemotely.connect(
2174 this, &DataChannel::OnStreamClosedRemotely);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175 srtp_filter()->SignalSrtpError.connect(
2176 this, &DataChannel::OnSrtpError);
2177 return true;
2178}
2179
2180bool DataChannel::SendData(const SendDataParams& params,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002181 const rtc::Buffer& payload,
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002183 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2184 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185}
2186
2187const ContentInfo* DataChannel::GetFirstContent(
2188 const SessionDescription* sdesc) {
2189 return GetFirstDataContent(sdesc);
2190}
2191
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002192bool DataChannel::WantsPacket(bool rtcp, rtc::Buffer* packet) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193 if (data_channel_type_ == DCT_SCTP) {
2194 // TODO(pthatcher): Do this in a more robust way by checking for
2195 // SCTP or DTLS.
buildbot@webrtc.org1ef789d2014-06-19 23:54:12 +00002196 return !IsRtpPacket(packet->data(), packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002197 } else if (data_channel_type_ == DCT_RTP) {
2198 return BaseChannel::WantsPacket(rtcp, packet);
2199 }
2200 return false;
2201}
2202
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002203bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2204 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205 // It hasn't been set before, so set it now.
2206 if (data_channel_type_ == DCT_NONE) {
2207 data_channel_type_ = new_data_channel_type;
2208 return true;
2209 }
2210
2211 // It's been set before, but doesn't match. That's bad.
2212 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002213 std::ostringstream desc;
2214 desc << "Data channel type mismatch."
2215 << " Expected " << data_channel_type_
2216 << " Got " << new_data_channel_type;
2217 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 return false;
2219 }
2220
2221 // It's hasn't changed. Nothing to do.
2222 return true;
2223}
2224
2225bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002226 const DataContentDescription* content,
2227 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2229 (content->protocol() == kMediaProtocolDtlsSctp));
2230 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002231 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002232}
2233
2234bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002235 ContentAction action,
2236 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002237 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002238 LOG(LS_INFO) << "Setting local data description";
2239
2240 const DataContentDescription* data =
2241 static_cast<const DataContentDescription*>(content);
2242 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002243 if (!data) {
2244 SafeSetError("Can't find data content in local description.", error_desc);
2245 return false;
2246 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247
2248 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002249 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002250 return false;
2251 }
2252
2253 if (data_channel_type_ == DCT_SCTP) {
2254 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002255 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002256 if (ret) {
2257 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002258 // As in SetRemoteContent_w, make sure we set the local SCTP port
2259 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002260 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2261 SafeSetError("Failed to set data receive codecs.", error_desc);
2262 ret = false;
2263 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002264 }
2265 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002266 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002268 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2269 SafeSetError("Failed to set data receive codecs.", error_desc);
2270 ret = false;
2271 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272 }
2273 }
2274
2275 // If everything worked, see if we can start receiving.
2276 if (ret) {
buildbot@webrtc.org5ee0f052014-05-05 20:18:08 +00002277 std::vector<DataCodec>::const_iterator it = data->codecs().begin();
2278 for (; it != data->codecs().end(); ++it) {
2279 bundle_filter()->AddPayloadType(it->id);
2280 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002281 ChangeState();
2282 } else {
2283 LOG(LS_WARNING) << "Failed to set local data description";
2284 }
2285 return ret;
2286}
2287
2288bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002289 ContentAction action,
2290 std::string* error_desc) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002291 ASSERT(worker_thread() == rtc::Thread::Current());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292
2293 const DataContentDescription* data =
2294 static_cast<const DataContentDescription*>(content);
2295 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002296 if (!data) {
2297 SafeSetError("Can't find data content in remote description.", error_desc);
2298 return false;
2299 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300
2301 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002302 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002303 return false;
2304 }
2305
2306 if (data_channel_type_ == DCT_SCTP) {
2307 LOG(LS_INFO) << "Setting SCTP remote data description";
2308 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002309 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310 if (ret) {
2311 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002312 // We send the SCTP port number (not to be confused with the underlying
2313 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002314 if (!media_channel()->SetSendCodecs(data->codecs())) {
2315 SafeSetError("Failed to set data send codecs.", error_desc);
2316 ret = false;
2317 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002318 }
2319 } else {
2320 // If the remote data doesn't have codecs and isn't an update, it
2321 // must be empty, so ignore it.
2322 if (action != CA_UPDATE && !data->has_codecs()) {
2323 return true;
2324 }
2325 LOG(LS_INFO) << "Setting remote data description";
2326
2327 // Set remote video codecs (what the other side wants to receive).
2328 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002329 if (!media_channel()->SetSendCodecs(data->codecs())) {
2330 SafeSetError("Failed to set data send codecs.", error_desc);
2331 ret = false;
2332 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333 }
2334
2335 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002336 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002337 }
2338
2339 if (action != CA_UPDATE) {
2340 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002341 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2342 std::ostringstream desc;
2343 desc << "Failed to set max send bandwidth for data content.";
2344 SafeSetError(desc.str(), error_desc);
2345 ret = false;
2346 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002347 }
2348 }
2349
2350 // If everything worked, see if we can start sending.
2351 if (ret) {
2352 ChangeState();
2353 } else {
2354 LOG(LS_WARNING) << "Failed to set remote data description";
2355 }
2356 return ret;
2357}
2358
2359void DataChannel::ChangeState() {
2360 // Render incoming data if we're the active call, and we have the local
2361 // content. We receive data on the default channel and multiplexed streams.
2362 bool recv = IsReadyToReceive();
2363 if (!media_channel()->SetReceive(recv)) {
2364 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2365 }
2366
2367 // Send outgoing data if we're the active call, we have the remote content,
2368 // and we have had some form of connectivity.
2369 bool send = IsReadyToSend();
2370 if (!media_channel()->SetSend(send)) {
2371 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2372 }
2373
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002374 // Trigger SignalReadyToSendData asynchronously.
2375 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002376
2377 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2378}
2379
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002380void DataChannel::OnMessage(rtc::Message *pmsg) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002381 switch (pmsg->message_id) {
2382 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002383 DataChannelReadyToSendMessageData* data =
2384 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002385 ready_to_send_data_ = data->data();
2386 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002387 delete data;
2388 break;
2389 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002390 case MSG_DATARECEIVED: {
2391 DataReceivedMessageData* data =
2392 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2393 SignalDataReceived(this, data->params, data->payload);
2394 delete data;
2395 break;
2396 }
2397 case MSG_CHANNEL_ERROR: {
2398 const DataChannelErrorMessageData* data =
2399 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2400 SignalMediaError(this, data->ssrc, data->error);
2401 delete data;
2402 break;
2403 }
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002404 case MSG_STREAMCLOSEDREMOTELY: {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002405 rtc::TypedMessageData<uint32>* data =
2406 static_cast<rtc::TypedMessageData<uint32>*>(pmsg->pdata);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002407 SignalStreamClosedRemotely(data->data());
2408 delete data;
2409 break;
2410 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002411 default:
2412 BaseChannel::OnMessage(pmsg);
2413 break;
2414 }
2415}
2416
2417void DataChannel::OnConnectionMonitorUpdate(
2418 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
2419 SignalConnectionMonitor(this, infos);
2420}
2421
2422void DataChannel::StartMediaMonitor(int cms) {
2423 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002424 rtc::Thread::Current()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002425 media_monitor_->SignalUpdate.connect(
2426 this, &DataChannel::OnMediaMonitorUpdate);
2427 media_monitor_->Start(cms);
2428}
2429
2430void DataChannel::StopMediaMonitor() {
2431 if (media_monitor_) {
2432 media_monitor_->Stop();
2433 media_monitor_->SignalUpdate.disconnect(this);
2434 media_monitor_.reset();
2435 }
2436}
2437
2438void DataChannel::OnMediaMonitorUpdate(
2439 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2440 ASSERT(media_channel == this->media_channel());
2441 SignalMediaMonitor(this, info);
2442}
2443
2444void DataChannel::OnDataReceived(
2445 const ReceiveDataParams& params, const char* data, size_t len) {
2446 DataReceivedMessageData* msg = new DataReceivedMessageData(
2447 params, data, len);
2448 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2449}
2450
2451void DataChannel::OnDataChannelError(
2452 uint32 ssrc, DataMediaChannel::Error err) {
2453 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2454 ssrc, err);
2455 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2456}
2457
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002458void DataChannel::OnDataChannelReadyToSend(bool writable) {
2459 // This is usded for congestion control to indicate that the stream is ready
2460 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2461 // that the transport channel is ready.
2462 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2463 new DataChannelReadyToSendMessageData(writable));
2464}
2465
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002466void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2467 SrtpFilter::Error error) {
2468 switch (error) {
2469 case SrtpFilter::ERROR_FAIL:
2470 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2471 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2472 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2473 break;
2474 case SrtpFilter::ERROR_AUTH:
2475 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2476 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2477 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2478 break;
2479 case SrtpFilter::ERROR_REPLAY:
2480 // Only receving channel should have this error.
2481 ASSERT(mode == SrtpFilter::UNPROTECT);
2482 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2483 break;
2484 default:
2485 break;
2486 }
2487}
2488
2489void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2490 GetSupportedDataCryptoSuites(ciphers);
2491}
2492
2493bool DataChannel::ShouldSetupDtlsSrtp() const {
2494 return (data_channel_type_ == DCT_RTP);
2495}
2496
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002497void DataChannel::OnStreamClosedRemotely(uint32 sid) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002498 rtc::TypedMessageData<uint32>* message =
2499 new rtc::TypedMessageData<uint32>(sid);
buildbot@webrtc.org1d66be22014-05-29 22:54:24 +00002500 signaling_thread()->Post(this, MSG_STREAMCLOSEDREMOTELY, message);
2501}
2502
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002503} // namespace cricket