blob: d3762c4e9ea91a33a02921553c8247f47f078dbf [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
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000030#include "talk/base/bind.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031#include "talk/base/buffer.h"
32#include "talk/base/byteorder.h"
33#include "talk/base/common.h"
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000034#include "talk/base/dscp.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035#include "talk/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"
41#include "talk/session/media/rtcpmuxfilter.h"
42#include "talk/session/media/typingmonitor.h"
43
44
45namespace cricket {
46
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000047using talk_base::Bind;
48
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049enum {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000050 MSG_EARLYMEDIATIMEOUT = 1,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051 MSG_SCREENCASTWINDOWEVENT,
52 MSG_RTPPACKET,
53 MSG_RTCPPACKET,
54 MSG_CHANNEL_ERROR,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055 MSG_READYTOSENDDATA,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056 MSG_DATARECEIVED,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057 MSG_FIRSTPACKETRECEIVED,
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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090struct PacketMessageData : public talk_base::MessageData {
91 talk_base::Buffer packet;
mallinath@webrtc.org1112c302013-09-23 20:34:45 +000092 talk_base::DiffServCodePoint dscp;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093};
94
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095struct ScreencastEventMessageData : public talk_base::MessageData {
96 ScreencastEventMessageData(uint32 s, talk_base::WindowEvent we)
97 : ssrc(s),
98 event(we) {
99 }
100 uint32 ssrc;
101 talk_base::WindowEvent event;
102};
103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104struct VoiceChannelErrorMessageData : public talk_base::MessageData {
105 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
114struct VideoChannelErrorMessageData : public talk_base::MessageData {
115 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
124struct DataChannelErrorMessageData : public talk_base::MessageData {
125 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
147static bool ValidPacket(bool rtcp, const talk_base::Buffer* packet) {
148 // 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
169BaseChannel::BaseChannel(talk_base::Thread* thread,
170 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) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 ASSERT(worker_thread_ == talk_base::Thread::Current());
193 LOG(LS_INFO) << "Created channel for " << content_name;
194}
195
196BaseChannel::~BaseChannel() {
197 ASSERT(worker_thread_ == talk_base::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(),
299 talk_base::Thread::Current()));
300 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
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000346bool BaseChannel::SendPacket(talk_base::Buffer* packet,
347 talk_base::DiffServCodePoint dscp) {
348 return SendPacket(false, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349}
350
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000351bool BaseChannel::SendRtcp(talk_base::Buffer* packet,
352 talk_base::DiffServCodePoint dscp) {
353 return SendPacket(true, packet, dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000354}
355
356int BaseChannel::SetOption(SocketType type, talk_base::Socket::Option opt,
357 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,
382 const talk_base::PacketTime& packet_time,
383 int flags) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000384 // OnChannelRead gets called from P2PSocket; now pass data to MediaEngine
385 ASSERT(worker_thread_ == talk_base::Thread::Current());
386
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);
390 talk_base::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
mallinath@webrtc.org1112c302013-09-23 20:34:45 +0000424bool BaseChannel::SendPacket(bool rtcp, talk_base::Buffer* packet,
425 talk_base::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.
433 if (talk_base::Thread::Current() != worker_thread_) {
434 // 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 {
463 talk_base::CritScope cs(&signal_send_packet_cs_);
464 SignalSendPacketPreCrypto(packet->data(), packet->length(), rtcp);
465 }
466
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000467 talk_base::PacketOptions options(dscp);
468 options.packet_time_params.rtp_sendtime_extension_id =
469 rtp_abs_sendtime_extn_id_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000470 // Protect if needed.
471 if (srtp_filter_.IsActive()) {
472 bool res;
473 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000474 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 if (!rtcp) {
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +0000476 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done
477 // inside libsrtp for a RTP packet. A external HMAC module will be writing
478 // a fake HMAC value. This is ONLY done for a RTP packet.
479 // Socket layer will update rtp sendtime extension header if present in
480 // packet with current time before updating the HMAC.
481#if !defined(ENABLE_EXTERNAL_AUTH)
482 res = srtp_filter_.ProtectRtp(
483 data, len, static_cast<int>(packet->capacity()), &len);
484#else
485 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 {
537 talk_base::CritScope cs(&signal_send_packet_cs_);
538 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
554bool BaseChannel::WantsPacket(bool rtcp, talk_base::Buffer* packet) {
555 // 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 }
562 // If this channel is suppose to handle RTP data, that is determined by
563 // checking against ssrc filter. This is necessary to do it here to avoid
564 // double decryption.
565 if (ssrc_filter_.IsActive() &&
566 !ssrc_filter_.DemuxPacket(packet->data(), packet->length(), rtcp)) {
567 return false;
568 }
569
570 return true;
571}
572
wu@webrtc.orga9890802013-12-13 00:21:03 +0000573void BaseChannel::HandlePacket(bool rtcp, talk_base::Buffer* packet,
574 const talk_base::PacketTime& packet_time) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000575 if (!WantsPacket(rtcp, packet)) {
576 return;
577 }
578
579 if (!has_received_packet_) {
580 has_received_packet_ = true;
581 signaling_thread()->Post(this, MSG_FIRSTPACKETRECEIVED);
582 }
583
584 // Signal to the media sink before unprotecting the packet.
585 {
586 talk_base::CritScope cs(&signal_recv_packet_cs_);
587 SignalRecvPacketPostCrypto(packet->data(), packet->length(), rtcp);
588 }
589
590 // Unprotect the packet, if needed.
591 if (srtp_filter_.IsActive()) {
592 char* data = packet->data();
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000593 int len = static_cast<int>(packet->length());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000594 bool res;
595 if (!rtcp) {
596 res = srtp_filter_.UnprotectRtp(data, len, &len);
597 if (!res) {
598 int seq_num = -1;
599 uint32 ssrc = 0;
600 GetRtpSeqNum(data, len, &seq_num);
601 GetRtpSsrc(data, len, &ssrc);
602 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
603 << " RTP packet: size=" << len
604 << ", seqnum=" << seq_num << ", SSRC=" << ssrc;
605 return;
606 }
607 } else {
608 res = srtp_filter_.UnprotectRtcp(data, len, &len);
609 if (!res) {
610 int type = -1;
611 GetRtcpType(data, len, &type);
612 LOG(LS_ERROR) << "Failed to unprotect " << content_name_
613 << " RTCP packet: size=" << len << ", type=" << type;
614 return;
615 }
616 }
617
618 packet->SetLength(len);
619 } else if (secure_required_) {
620 // Our session description indicates that SRTP is required, but we got a
621 // packet before our SRTP filter is active. This means either that
622 // a) we got SRTP packets before we received the SDES keys, in which case
623 // we can't decrypt it anyway, or
624 // b) we got SRTP packets before DTLS completed on both the RTP and RTCP
625 // channels, so we haven't yet extracted keys, even if DTLS did complete
626 // on the channel that the packets are being sent on. It's really good
627 // practice to wait for both RTP and RTCP to be good to go before sending
628 // media, to prevent weird failure modes, so it's fine for us to just eat
629 // packets here. This is all sidestepped if RTCP mux is used anyway.
630 LOG(LS_WARNING) << "Can't process incoming " << PacketType(rtcp)
631 << " packet when SRTP is inactive and crypto is required";
632 return;
633 }
634
635 // Signal to the media sink after unprotecting the packet.
636 {
637 talk_base::CritScope cs(&signal_recv_packet_cs_);
638 SignalRecvPacketPreCrypto(packet->data(), packet->length(), rtcp);
639 }
640
641 // Push it down to the media channel.
642 if (!rtcp) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000643 media_channel_->OnPacketReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 } else {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000645 media_channel_->OnRtcpReceived(packet, packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000646 }
647}
648
649void BaseChannel::OnNewLocalDescription(
650 BaseSession* session, ContentAction action) {
651 const ContentInfo* content_info =
652 GetFirstContent(session->local_description());
653 const MediaContentDescription* content_desc =
654 GetContentDescription(content_info);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000655 std::string error_desc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000656 if (content_desc && content_info && !content_info->rejected &&
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000657 !SetLocalContent(content_desc, action, &error_desc)) {
658 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 LOG(LS_ERROR) << "Failure in SetLocalContent with action " << action;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000660 }
661}
662
663void BaseChannel::OnNewRemoteDescription(
664 BaseSession* session, ContentAction action) {
665 const ContentInfo* content_info =
666 GetFirstContent(session->remote_description());
667 const MediaContentDescription* content_desc =
668 GetContentDescription(content_info);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000669 std::string error_desc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670 if (content_desc && content_info && !content_info->rejected &&
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000671 !SetRemoteContent(content_desc, action, &error_desc)) {
672 SetSessionError(session_, BaseSession::ERROR_CONTENT, error_desc);
673 LOG(LS_ERROR) << "Failure in SetRemoteContent with action " << action;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674 }
675}
676
677void BaseChannel::EnableMedia_w() {
678 ASSERT(worker_thread_ == talk_base::Thread::Current());
679 if (enabled_)
680 return;
681
682 LOG(LS_INFO) << "Channel enabled";
683 enabled_ = true;
684 ChangeState();
685}
686
687void BaseChannel::DisableMedia_w() {
688 ASSERT(worker_thread_ == talk_base::Thread::Current());
689 if (!enabled_)
690 return;
691
692 LOG(LS_INFO) << "Channel disabled";
693 enabled_ = false;
694 ChangeState();
695}
696
697bool BaseChannel::MuteStream_w(uint32 ssrc, bool mute) {
698 ASSERT(worker_thread_ == talk_base::Thread::Current());
699 bool ret = media_channel()->MuteStream(ssrc, mute);
700 if (ret) {
701 if (mute)
702 muted_streams_.insert(ssrc);
703 else
704 muted_streams_.erase(ssrc);
705 }
706 return ret;
707}
708
709bool BaseChannel::IsStreamMuted_w(uint32 ssrc) {
710 ASSERT(worker_thread_ == talk_base::Thread::Current());
711 return muted_streams_.find(ssrc) != muted_streams_.end();
712}
713
714void BaseChannel::ChannelWritable_w() {
715 ASSERT(worker_thread_ == talk_base::Thread::Current());
716 if (writable_)
717 return;
718
719 LOG(LS_INFO) << "Channel socket writable ("
720 << transport_channel_->content_name() << ", "
721 << transport_channel_->component() << ")"
722 << (was_ever_writable_ ? "" : " for the first time");
723
724 std::vector<ConnectionInfo> infos;
725 transport_channel_->GetStats(&infos);
726 for (std::vector<ConnectionInfo>::const_iterator it = infos.begin();
727 it != infos.end(); ++it) {
728 if (it->best_connection) {
729 LOG(LS_INFO) << "Using " << it->local_candidate.ToSensitiveString()
730 << "->" << it->remote_candidate.ToSensitiveString();
731 break;
732 }
733 }
734
735 // If we're doing DTLS-SRTP, now is the time.
736 if (!was_ever_writable_ && ShouldSetupDtlsSrtp()) {
737 if (!SetupDtlsSrtp(false)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000738 const std::string error_desc =
739 "Couldn't set up DTLS-SRTP on RTP channel.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000740 // Sent synchronously.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000741 signaling_thread()->Invoke<void>(Bind(
742 &SetSessionError,
743 session_,
744 BaseSession::ERROR_TRANSPORT,
745 error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000746 return;
747 }
748
749 if (rtcp_transport_channel_) {
750 if (!SetupDtlsSrtp(true)) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000751 const std::string error_desc =
752 "Couldn't set up DTLS-SRTP on RTCP channel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000753 // Sent synchronously.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000754 signaling_thread()->Invoke<void>(Bind(
755 &SetSessionError,
756 session_,
757 BaseSession::ERROR_TRANSPORT,
758 error_desc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000759 return;
760 }
761 }
762 }
763
764 was_ever_writable_ = true;
765 writable_ = true;
766 ChangeState();
767}
768
769bool BaseChannel::SetDtlsSrtpCiphers(TransportChannel *tc, bool rtcp) {
770 std::vector<std::string> ciphers;
771 // We always use the default SRTP ciphers for RTCP, but we may use different
772 // ciphers for RTP depending on the media type.
773 if (!rtcp) {
774 GetSrtpCiphers(&ciphers);
775 } else {
776 GetSupportedDefaultCryptoSuites(&ciphers);
777 }
778 return tc->SetSrtpCiphers(ciphers);
779}
780
781bool BaseChannel::ShouldSetupDtlsSrtp() const {
782 return true;
783}
784
785// This function returns true if either DTLS-SRTP is not in use
786// *or* DTLS-SRTP is successfully set up.
787bool BaseChannel::SetupDtlsSrtp(bool rtcp_channel) {
788 bool ret = false;
789
790 TransportChannel *channel = rtcp_channel ?
791 rtcp_transport_channel_ : transport_channel_;
792
793 // No DTLS
794 if (!channel->IsDtlsActive())
795 return true;
796
797 std::string selected_cipher;
798
799 if (!channel->GetSrtpCipher(&selected_cipher)) {
800 LOG(LS_ERROR) << "No DTLS-SRTP selected cipher";
801 return false;
802 }
803
804 LOG(LS_INFO) << "Installing keys from DTLS-SRTP on "
805 << content_name() << " "
806 << PacketType(rtcp_channel);
807
808 // OK, we're now doing DTLS (RFC 5764)
809 std::vector<unsigned char> dtls_buffer(SRTP_MASTER_KEY_KEY_LEN * 2 +
810 SRTP_MASTER_KEY_SALT_LEN * 2);
811
812 // RFC 5705 exporter using the RFC 5764 parameters
813 if (!channel->ExportKeyingMaterial(
814 kDtlsSrtpExporterLabel,
815 NULL, 0, false,
816 &dtls_buffer[0], dtls_buffer.size())) {
817 LOG(LS_WARNING) << "DTLS-SRTP key export failed";
818 ASSERT(false); // This should never happen
819 return false;
820 }
821
822 // Sync up the keys with the DTLS-SRTP interface
823 std::vector<unsigned char> client_write_key(SRTP_MASTER_KEY_KEY_LEN +
824 SRTP_MASTER_KEY_SALT_LEN);
825 std::vector<unsigned char> server_write_key(SRTP_MASTER_KEY_KEY_LEN +
826 SRTP_MASTER_KEY_SALT_LEN);
827 size_t offset = 0;
828 memcpy(&client_write_key[0], &dtls_buffer[offset],
829 SRTP_MASTER_KEY_KEY_LEN);
830 offset += SRTP_MASTER_KEY_KEY_LEN;
831 memcpy(&server_write_key[0], &dtls_buffer[offset],
832 SRTP_MASTER_KEY_KEY_LEN);
833 offset += SRTP_MASTER_KEY_KEY_LEN;
834 memcpy(&client_write_key[SRTP_MASTER_KEY_KEY_LEN],
835 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
836 offset += SRTP_MASTER_KEY_SALT_LEN;
837 memcpy(&server_write_key[SRTP_MASTER_KEY_KEY_LEN],
838 &dtls_buffer[offset], SRTP_MASTER_KEY_SALT_LEN);
839
840 std::vector<unsigned char> *send_key, *recv_key;
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000841 talk_base::SSLRole role;
842 if (!channel->GetSslRole(&role)) {
843 LOG(LS_WARNING) << "GetSslRole failed";
844 return false;
845 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000846
sergeyu@chromium.org0be6aa02013-08-23 23:21:25 +0000847 if (role == talk_base::SSL_SERVER) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000848 send_key = &server_write_key;
849 recv_key = &client_write_key;
850 } else {
851 send_key = &client_write_key;
852 recv_key = &server_write_key;
853 }
854
855 if (rtcp_channel) {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000856 ret = srtp_filter_.SetRtcpParams(
857 selected_cipher,
858 &(*send_key)[0],
859 static_cast<int>(send_key->size()),
860 selected_cipher,
861 &(*recv_key)[0],
862 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000863 } else {
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000864 ret = srtp_filter_.SetRtpParams(
865 selected_cipher,
866 &(*send_key)[0],
867 static_cast<int>(send_key->size()),
868 selected_cipher,
869 &(*recv_key)[0],
870 static_cast<int>(recv_key->size()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 }
872
873 if (!ret)
874 LOG(LS_WARNING) << "DTLS-SRTP key installation failed";
875 else
876 dtls_keyed_ = true;
877
878 return ret;
879}
880
881void BaseChannel::ChannelNotWritable_w() {
882 ASSERT(worker_thread_ == talk_base::Thread::Current());
883 if (!writable_)
884 return;
885
886 LOG(LS_INFO) << "Channel socket not writable ("
887 << transport_channel_->content_name() << ", "
888 << transport_channel_->component() << ")";
889 writable_ = false;
890 ChangeState();
891}
892
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000893// |dtls| will be set to true if DTLS is active for transport channel and
894// crypto is empty.
895bool BaseChannel::CheckSrtpConfig(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000896 bool* dtls,
897 std::string* error_desc) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000898 *dtls = transport_channel_->IsDtlsActive();
899 if (*dtls && !cryptos.empty()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000900 SafeSetError("Cryptos must be empty when DTLS is active.",
901 error_desc);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000902 return false;
903 }
904 return true;
905}
906
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907bool BaseChannel::SetSrtp_w(const std::vector<CryptoParams>& cryptos,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000908 ContentAction action,
909 ContentSource src,
910 std::string* error_desc) {
911 if (action == CA_UPDATE) {
912 // no crypto params.
913 return true;
914 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 bool ret = false;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000916 bool dtls = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000917 ret = CheckSrtpConfig(cryptos, &dtls, error_desc);
918 if (!ret) {
919 return false;
920 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 switch (action) {
922 case CA_OFFER:
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000923 // If DTLS is already active on the channel, we could be renegotiating
924 // here. We don't update the srtp filter.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000925 if (!dtls) {
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000926 ret = srtp_filter_.SetOffer(cryptos, src);
927 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 break;
929 case CA_PRANSWER:
930 // If we're doing DTLS-SRTP, we don't want to update the filter
931 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000932 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933 ret = srtp_filter_.SetProvisionalAnswer(cryptos, src);
934 }
935 break;
936 case CA_ANSWER:
937 // If we're doing DTLS-SRTP, we don't want to update the filter
938 // with an answer, because we already have SRTP parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000939 if (!dtls) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 ret = srtp_filter_.SetAnswer(cryptos, src);
941 }
942 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 default:
944 break;
945 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000946 if (!ret) {
947 SafeSetError("Failed to setup SRTP filter.", error_desc);
948 return false;
949 }
950 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951}
952
953bool BaseChannel::SetRtcpMux_w(bool enable, ContentAction action,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000954 ContentSource src,
955 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956 bool ret = false;
957 switch (action) {
958 case CA_OFFER:
959 ret = rtcp_mux_filter_.SetOffer(enable, src);
960 break;
961 case CA_PRANSWER:
962 ret = rtcp_mux_filter_.SetProvisionalAnswer(enable, src);
963 break;
964 case CA_ANSWER:
965 ret = rtcp_mux_filter_.SetAnswer(enable, src);
966 if (ret && rtcp_mux_filter_.IsActive()) {
967 // We activated RTCP mux, close down the RTCP transport.
968 set_rtcp_transport_channel(NULL);
969 }
970 break;
971 case CA_UPDATE:
972 // No RTCP mux info.
973 ret = true;
974 default:
975 break;
976 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000977 if (!ret) {
978 SafeSetError("Failed to setup RTCP mux filter.", error_desc);
979 return false;
980 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 // |rtcp_mux_filter_| can be active if |action| is CA_PRANSWER or
982 // CA_ANSWER, but we only want to tear down the RTCP transport channel if we
983 // received a final answer.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000984 if (rtcp_mux_filter_.IsActive()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 // If the RTP transport is already writable, then so are we.
986 if (transport_channel_->writable()) {
987 ChannelWritable_w();
988 }
989 }
990
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +0000991 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992}
993
994bool BaseChannel::AddRecvStream_w(const StreamParams& sp) {
995 ASSERT(worker_thread() == talk_base::Thread::Current());
996 if (!media_channel()->AddRecvStream(sp))
997 return false;
998
999 return ssrc_filter_.AddStream(sp);
1000}
1001
1002bool BaseChannel::RemoveRecvStream_w(uint32 ssrc) {
1003 ASSERT(worker_thread() == talk_base::Thread::Current());
1004 ssrc_filter_.RemoveStream(ssrc);
1005 return media_channel()->RemoveRecvStream(ssrc);
1006}
1007
1008bool BaseChannel::UpdateLocalStreams_w(const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001009 ContentAction action,
1010 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1012 action == CA_PRANSWER || action == CA_UPDATE))
1013 return false;
1014
1015 // If this is an update, streams only contain streams that have changed.
1016 if (action == CA_UPDATE) {
1017 for (StreamParamsVec::const_iterator it = streams.begin();
1018 it != streams.end(); ++it) {
1019 StreamParams existing_stream;
1020 bool stream_exist = GetStreamByIds(local_streams_, it->groupid,
1021 it->id, &existing_stream);
1022 if (!stream_exist && it->has_ssrcs()) {
1023 if (media_channel()->AddSendStream(*it)) {
1024 local_streams_.push_back(*it);
1025 LOG(LS_INFO) << "Add send stream ssrc: " << it->first_ssrc();
1026 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001027 std::ostringstream desc;
1028 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1029 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030 return false;
1031 }
1032 } else if (stream_exist && !it->has_ssrcs()) {
1033 if (!media_channel()->RemoveSendStream(existing_stream.first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001034 std::ostringstream desc;
1035 desc << "Failed to remove send stream with ssrc "
1036 << it->first_ssrc() << ".";
1037 SafeSetError(desc.str(), error_desc);
1038 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039 }
1040 RemoveStreamBySsrc(&local_streams_, existing_stream.first_ssrc());
1041 } else {
1042 LOG(LS_WARNING) << "Ignore unsupported stream update";
1043 }
1044 }
1045 return true;
1046 }
1047 // Else streams are all the streams we want to send.
1048
1049 // Check for streams that have been removed.
1050 bool ret = true;
1051 for (StreamParamsVec::const_iterator it = local_streams_.begin();
1052 it != local_streams_.end(); ++it) {
1053 if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1054 if (!media_channel()->RemoveSendStream(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001055 std::ostringstream desc;
1056 desc << "Failed to remove send stream with ssrc "
1057 << it->first_ssrc() << ".";
1058 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 ret = false;
1060 }
1061 }
1062 }
1063 // Check for new streams.
1064 for (StreamParamsVec::const_iterator it = streams.begin();
1065 it != streams.end(); ++it) {
1066 if (!GetStreamBySsrc(local_streams_, it->first_ssrc(), NULL)) {
1067 if (media_channel()->AddSendStream(*it)) {
1068 LOG(LS_INFO) << "Add send ssrc: " << it->ssrcs[0];
1069 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001070 std::ostringstream desc;
1071 desc << "Failed to add send stream ssrc: " << it->first_ssrc();
1072 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 ret = false;
1074 }
1075 }
1076 }
1077 local_streams_ = streams;
1078 return ret;
1079}
1080
1081bool BaseChannel::UpdateRemoteStreams_w(
1082 const std::vector<StreamParams>& streams,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001083 ContentAction action,
1084 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085 if (!VERIFY(action == CA_OFFER || action == CA_ANSWER ||
1086 action == CA_PRANSWER || action == CA_UPDATE))
1087 return false;
1088
1089 // If this is an update, streams only contain streams that have changed.
1090 if (action == CA_UPDATE) {
1091 for (StreamParamsVec::const_iterator it = streams.begin();
1092 it != streams.end(); ++it) {
1093 StreamParams existing_stream;
1094 bool stream_exists = GetStreamByIds(remote_streams_, it->groupid,
1095 it->id, &existing_stream);
1096 if (!stream_exists && it->has_ssrcs()) {
1097 if (AddRecvStream_w(*it)) {
1098 remote_streams_.push_back(*it);
1099 LOG(LS_INFO) << "Add remote stream ssrc: " << it->first_ssrc();
1100 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001101 std::ostringstream desc;
1102 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1103 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 return false;
1105 }
1106 } else if (stream_exists && !it->has_ssrcs()) {
1107 if (!RemoveRecvStream_w(existing_stream.first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001108 std::ostringstream desc;
1109 desc << "Failed to remove remote stream with ssrc "
1110 << it->first_ssrc() << ".";
1111 SafeSetError(desc.str(), error_desc);
1112 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001113 }
1114 RemoveStreamBySsrc(&remote_streams_, existing_stream.first_ssrc());
1115 } else {
1116 LOG(LS_WARNING) << "Ignore unsupported stream update."
1117 << " Stream exists? " << stream_exists
1118 << " existing stream = " << existing_stream.ToString()
1119 << " new stream = " << it->ToString();
1120 }
1121 }
1122 return true;
1123 }
1124 // Else streams are all the streams we want to receive.
1125
1126 // Check for streams that have been removed.
1127 bool ret = true;
1128 for (StreamParamsVec::const_iterator it = remote_streams_.begin();
1129 it != remote_streams_.end(); ++it) {
1130 if (!GetStreamBySsrc(streams, it->first_ssrc(), NULL)) {
1131 if (!RemoveRecvStream_w(it->first_ssrc())) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001132 std::ostringstream desc;
1133 desc << "Failed to remove remote stream with ssrc "
1134 << it->first_ssrc() << ".";
1135 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 ret = false;
1137 }
1138 }
1139 }
1140 // Check for new streams.
1141 for (StreamParamsVec::const_iterator it = streams.begin();
1142 it != streams.end(); ++it) {
1143 if (!GetStreamBySsrc(remote_streams_, it->first_ssrc(), NULL)) {
1144 if (AddRecvStream_w(*it)) {
1145 LOG(LS_INFO) << "Add remote ssrc: " << it->ssrcs[0];
1146 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001147 std::ostringstream desc;
1148 desc << "Failed to add remote stream ssrc: " << it->first_ssrc();
1149 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001150 ret = false;
1151 }
1152 }
1153 }
1154 remote_streams_ = streams;
1155 return ret;
1156}
1157
1158bool BaseChannel::SetBaseLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001159 ContentAction action,
1160 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 // Cache secure_required_ for belt and suspenders check on SendPacket
henrike@webrtc.orgb90991d2014-03-04 19:54:57 +00001162 secure_required_ = content->crypto_required() != CT_NONE;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001163 bool ret = UpdateLocalStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001164 // Set local SRTP parameters (what we will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001165 ret &= SetSrtp_w(content->cryptos(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001166 // Set local RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001167 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_LOCAL, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001168 // Set local RTP header extensions.
1169 if (content->rtp_header_extensions_set()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001170 if (!media_channel()->SetRecvRtpHeaderExtensions(
1171 content->rtp_header_extensions())) {
1172 std::ostringstream desc;
1173 desc << "Failed to set receive rtp header extensions for "
1174 << MediaTypeToString(content->type()) << " content.";
1175 SafeSetError(desc.str(), error_desc);
1176 ret = false;
1177 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001178 }
1179 set_local_content_direction(content->direction());
1180 return ret;
1181}
1182
1183bool BaseChannel::SetBaseRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001184 ContentAction action,
1185 std::string* error_desc) {
1186 bool ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 // Set remote SRTP parameters (what the other side will encrypt with).
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001188 ret &= SetSrtp_w(content->cryptos(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 // Set remote RTCP mux parameters.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001190 ret &= SetRtcpMux_w(content->rtcp_mux(), action, CS_REMOTE, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001191 // Set remote RTP header extensions.
1192 if (content->rtp_header_extensions_set()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001193 if (!media_channel()->SetSendRtpHeaderExtensions(
1194 content->rtp_header_extensions())) {
1195 std::ostringstream desc;
1196 desc << "Failed to set send rtp header extensions for "
1197 << MediaTypeToString(content->type()) << " content.";
1198 SafeSetError(desc.str(), error_desc);
1199 ret = false;
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001200 } else {
1201 MaybeCacheRtpAbsSendTimeHeaderExtension(content->rtp_header_extensions());
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001202 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001203 }
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001204
1205 if (!media_channel()->SetMaxSendBandwidth(content->bandwidth())) {
1206 std::ostringstream desc;
1207 desc << "Failed to set max send bandwidth for "
1208 << MediaTypeToString(content->type()) << " content.";
1209 SafeSetError(desc.str(), error_desc);
1210 ret = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 }
1212 set_remote_content_direction(content->direction());
1213 return ret;
1214}
1215
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001216void BaseChannel::MaybeCacheRtpAbsSendTimeHeaderExtension(
1217 const std::vector<RtpHeaderExtension>& extensions) {
1218 const RtpHeaderExtension* send_time_extension =
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001219 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.orgd43aa9d2014-02-21 23:43:24 +00001220 rtp_abs_sendtime_extn_id_ =
1221 send_time_extension ? send_time_extension->id : -1;
1222}
1223
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001224void BaseChannel::OnMessage(talk_base::Message *pmsg) {
1225 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001226 case MSG_RTPPACKET:
1227 case MSG_RTCPPACKET: {
1228 PacketMessageData* data = static_cast<PacketMessageData*>(pmsg->pdata);
mallinath@webrtc.org1112c302013-09-23 20:34:45 +00001229 SendPacket(pmsg->message_id == MSG_RTCPPACKET, &data->packet, data->dscp);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001230 delete data; // because it is Posted
1231 break;
1232 }
1233 case MSG_FIRSTPACKETRECEIVED: {
1234 SignalFirstPacketReceived(this);
1235 break;
1236 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001237 }
1238}
1239
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001240void BaseChannel::FlushRtcpMessages() {
1241 // Flush all remaining RTCP messages. This should only be called in
1242 // destructor.
1243 ASSERT(talk_base::Thread::Current() == worker_thread_);
1244 talk_base::MessageList rtcp_messages;
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001245 worker_thread_->Clear(this, MSG_RTCPPACKET, &rtcp_messages);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001246 for (talk_base::MessageList::iterator it = rtcp_messages.begin();
1247 it != rtcp_messages.end(); ++it) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001248 worker_thread_->Send(this, MSG_RTCPPACKET, it->pdata);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001249 }
1250}
1251
1252VoiceChannel::VoiceChannel(talk_base::Thread* thread,
1253 MediaEngineInterface* media_engine,
1254 VoiceMediaChannel* media_channel,
1255 BaseSession* session,
1256 const std::string& content_name,
1257 bool rtcp)
1258 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1259 rtcp),
1260 received_media_(false) {
1261}
1262
1263VoiceChannel::~VoiceChannel() {
1264 StopAudioMonitor();
1265 StopMediaMonitor();
1266 // this can't be done in the base class, since it calls a virtual
1267 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001268 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001269}
1270
1271bool VoiceChannel::Init() {
1272 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1273 content_name(), "rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1274 if (!BaseChannel::Init(session()->CreateChannel(
1275 content_name(), "rtp", ICE_CANDIDATE_COMPONENT_RTP),
1276 rtcp_channel)) {
1277 return false;
1278 }
1279 media_channel()->SignalMediaError.connect(
1280 this, &VoiceChannel::OnVoiceChannelError);
1281 srtp_filter()->SignalSrtpError.connect(
1282 this, &VoiceChannel::OnSrtpError);
1283 return true;
1284}
1285
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001286bool VoiceChannel::SetRemoteRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001287 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetRemoteRenderer,
1288 media_channel(), ssrc, renderer));
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001289}
1290
1291bool VoiceChannel::SetLocalRenderer(uint32 ssrc, AudioRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001292 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetLocalRenderer,
1293 media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001294}
1295
1296bool VoiceChannel::SetRingbackTone(const void* buf, int len) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001297 return InvokeOnWorker(Bind(&VoiceChannel::SetRingbackTone_w, this, buf, len));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298}
1299
1300// TODO(juberti): Handle early media the right way. We should get an explicit
1301// ringing message telling us to start playing local ringback, which we cancel
1302// if any early media actually arrives. For now, we do the opposite, which is
1303// to wait 1 second for early media, and start playing local ringback if none
1304// arrives.
1305void VoiceChannel::SetEarlyMedia(bool enable) {
1306 if (enable) {
1307 // Start the early media timeout
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001308 worker_thread()->PostDelayed(kEarlyMediaTimeout, this,
1309 MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310 } else {
1311 // Stop the timeout if currently going.
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001312 worker_thread()->Clear(this, MSG_EARLYMEDIATIMEOUT);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 }
1314}
1315
1316bool VoiceChannel::PlayRingbackTone(uint32 ssrc, bool play, bool loop) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001317 return InvokeOnWorker(Bind(&VoiceChannel::PlayRingbackTone_w,
1318 this, ssrc, play, loop));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001319}
1320
1321bool VoiceChannel::PressDTMF(int digit, bool playout) {
1322 int flags = DF_SEND;
1323 if (playout) {
1324 flags |= DF_PLAY;
1325 }
1326 int duration_ms = 160;
1327 return InsertDtmf(0, digit, duration_ms, flags);
1328}
1329
1330bool VoiceChannel::CanInsertDtmf() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001331 return InvokeOnWorker(Bind(&VoiceMediaChannel::CanInsertDtmf,
1332 media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333}
1334
1335bool VoiceChannel::InsertDtmf(uint32 ssrc, int event_code, int duration,
1336 int flags) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001337 return InvokeOnWorker(Bind(&VoiceChannel::InsertDtmf_w, this,
1338 ssrc, event_code, duration, flags));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001339}
1340
1341bool VoiceChannel::SetOutputScaling(uint32 ssrc, double left, double right) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001342 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOutputScaling,
1343 media_channel(), ssrc, left, right));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344}
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001345
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346bool VoiceChannel::GetStats(VoiceMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001347 return InvokeOnWorker(Bind(&VoiceMediaChannel::GetStats,
1348 media_channel(), stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001349}
1350
1351void VoiceChannel::StartMediaMonitor(int cms) {
1352 media_monitor_.reset(new VoiceMediaMonitor(media_channel(), worker_thread(),
1353 talk_base::Thread::Current()));
1354 media_monitor_->SignalUpdate.connect(
1355 this, &VoiceChannel::OnMediaMonitorUpdate);
1356 media_monitor_->Start(cms);
1357}
1358
1359void VoiceChannel::StopMediaMonitor() {
1360 if (media_monitor_) {
1361 media_monitor_->Stop();
1362 media_monitor_->SignalUpdate.disconnect(this);
1363 media_monitor_.reset();
1364 }
1365}
1366
1367void VoiceChannel::StartAudioMonitor(int cms) {
1368 audio_monitor_.reset(new AudioMonitor(this, talk_base::Thread::Current()));
1369 audio_monitor_
1370 ->SignalUpdate.connect(this, &VoiceChannel::OnAudioMonitorUpdate);
1371 audio_monitor_->Start(cms);
1372}
1373
1374void VoiceChannel::StopAudioMonitor() {
1375 if (audio_monitor_) {
1376 audio_monitor_->Stop();
1377 audio_monitor_.reset();
1378 }
1379}
1380
1381bool VoiceChannel::IsAudioMonitorRunning() const {
1382 return (audio_monitor_.get() != NULL);
1383}
1384
1385void VoiceChannel::StartTypingMonitor(const TypingMonitorOptions& settings) {
1386 typing_monitor_.reset(new TypingMonitor(this, worker_thread(), settings));
1387 SignalAutoMuted.repeat(typing_monitor_->SignalMuted);
1388}
1389
1390void VoiceChannel::StopTypingMonitor() {
1391 typing_monitor_.reset();
1392}
1393
1394bool VoiceChannel::IsTypingMonitorRunning() const {
1395 return typing_monitor_;
1396}
1397
1398bool VoiceChannel::MuteStream_w(uint32 ssrc, bool mute) {
1399 bool ret = BaseChannel::MuteStream_w(ssrc, mute);
1400 if (typing_monitor_ && mute)
1401 typing_monitor_->OnChannelMuted();
1402 return ret;
1403}
1404
1405int VoiceChannel::GetInputLevel_w() {
1406 return media_engine()->GetInputLevel();
1407}
1408
1409int VoiceChannel::GetOutputLevel_w() {
1410 return media_channel()->GetOutputLevel();
1411}
1412
1413void VoiceChannel::GetActiveStreams_w(AudioInfo::StreamList* actives) {
1414 media_channel()->GetActiveStreams(actives);
1415}
1416
1417void VoiceChannel::OnChannelRead(TransportChannel* channel,
wu@webrtc.orga9890802013-12-13 00:21:03 +00001418 const char* data, size_t len,
1419 const talk_base::PacketTime& packet_time,
1420 int flags) {
1421 BaseChannel::OnChannelRead(channel, data, len, packet_time, flags);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422
1423 // Set a flag when we've received an RTP packet. If we're waiting for early
1424 // media, this will disable the timeout.
1425 if (!received_media_ && !PacketIsRtcp(channel, data, len)) {
1426 received_media_ = true;
1427 }
1428}
1429
1430void VoiceChannel::ChangeState() {
1431 // Render incoming data if we're the active call, and we have the local
1432 // content. We receive data on the default channel and multiplexed streams.
1433 bool recv = IsReadyToReceive();
1434 if (!media_channel()->SetPlayout(recv)) {
1435 SendLastMediaError();
1436 }
1437
1438 // Send outgoing data if we're the active call, we have the remote content,
1439 // and we have had some form of connectivity.
1440 bool send = IsReadyToSend();
1441 SendFlags send_flag = send ? SEND_MICROPHONE : SEND_NOTHING;
1442 if (!media_channel()->SetSend(send_flag)) {
1443 LOG(LS_ERROR) << "Failed to SetSend " << send_flag << " on voice channel";
1444 SendLastMediaError();
1445 }
1446
1447 LOG(LS_INFO) << "Changing voice state, recv=" << recv << " send=" << send;
1448}
1449
1450const ContentInfo* VoiceChannel::GetFirstContent(
1451 const SessionDescription* sdesc) {
1452 return GetFirstAudioContent(sdesc);
1453}
1454
1455bool VoiceChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001456 ContentAction action,
1457 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 ASSERT(worker_thread() == talk_base::Thread::Current());
1459 LOG(LS_INFO) << "Setting local voice description";
1460
1461 const AudioContentDescription* audio =
1462 static_cast<const AudioContentDescription*>(content);
1463 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001464 if (!audio) {
1465 SafeSetError("Can't find audio content in local description.", error_desc);
1466 return false;
1467 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001469 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470 // Set local audio codecs (what we want to receive).
1471 // TODO(whyuan): Change action != CA_UPDATE to !audio->partial() when partial
1472 // is set properly.
1473 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001474 if (!media_channel()->SetRecvCodecs(audio->codecs())) {
1475 SafeSetError("Failed to set audio receive codecs.", error_desc);
1476 ret = false;
1477 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478 }
1479
1480 // If everything worked, see if we can start receiving.
1481 if (ret) {
1482 ChangeState();
1483 } else {
1484 LOG(LS_WARNING) << "Failed to set local voice description";
1485 }
1486 return ret;
1487}
1488
1489bool VoiceChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001490 ContentAction action,
1491 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 ASSERT(worker_thread() == talk_base::Thread::Current());
1493 LOG(LS_INFO) << "Setting remote voice description";
1494
1495 const AudioContentDescription* audio =
1496 static_cast<const AudioContentDescription*>(content);
1497 ASSERT(audio != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001498 if (!audio) {
1499 SafeSetError("Can't find audio content in remote description.", error_desc);
1500 return false;
1501 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502
1503 bool ret = true;
1504 // Set remote video codecs (what the other side wants to receive).
1505 if (action != CA_UPDATE || audio->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001506 if (!media_channel()->SetSendCodecs(audio->codecs())) {
1507 SafeSetError("Failed to set audio send codecs.", error_desc);
1508 ret = false;
1509 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 }
1511
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001512 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513
1514 if (action != CA_UPDATE) {
1515 // Tweak our audio processing settings, if needed.
1516 AudioOptions audio_options;
1517 if (!media_channel()->GetOptions(&audio_options)) {
1518 LOG(LS_WARNING) << "Can not set audio options from on remote content.";
1519 } else {
1520 if (audio->conference_mode()) {
1521 audio_options.conference_mode.Set(true);
1522 }
1523 if (audio->agc_minus_10db()) {
1524 audio_options.adjust_agc_delta.Set(kAgcMinus10db);
1525 }
1526 if (!media_channel()->SetOptions(audio_options)) {
1527 // Log an error on failure, but don't abort the call.
1528 LOG(LS_ERROR) << "Failed to set voice channel options";
1529 }
1530 }
1531 }
1532
1533 // If everything worked, see if we can start sending.
1534 if (ret) {
1535 ChangeState();
1536 } else {
1537 LOG(LS_WARNING) << "Failed to set remote voice description";
1538 }
1539 return ret;
1540}
1541
1542bool VoiceChannel::SetRingbackTone_w(const void* buf, int len) {
1543 ASSERT(worker_thread() == talk_base::Thread::Current());
1544 return media_channel()->SetRingbackTone(static_cast<const char*>(buf), len);
1545}
1546
1547bool VoiceChannel::PlayRingbackTone_w(uint32 ssrc, bool play, bool loop) {
1548 ASSERT(worker_thread() == talk_base::Thread::Current());
1549 if (play) {
1550 LOG(LS_INFO) << "Playing ringback tone, loop=" << loop;
1551 } else {
1552 LOG(LS_INFO) << "Stopping ringback tone";
1553 }
1554 return media_channel()->PlayRingbackTone(ssrc, play, loop);
1555}
1556
1557void VoiceChannel::HandleEarlyMediaTimeout() {
1558 // This occurs on the main thread, not the worker thread.
1559 if (!received_media_) {
1560 LOG(LS_INFO) << "No early media received before timeout";
1561 SignalEarlyMediaTimeout(this);
1562 }
1563}
1564
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565bool VoiceChannel::InsertDtmf_w(uint32 ssrc, int event, int duration,
1566 int flags) {
1567 if (!enabled()) {
1568 return false;
1569 }
1570
1571 return media_channel()->InsertDtmf(ssrc, event, duration, flags);
1572}
1573
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574bool VoiceChannel::SetChannelOptions(const AudioOptions& options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001575 return InvokeOnWorker(Bind(&VoiceMediaChannel::SetOptions,
1576 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577}
1578
1579void VoiceChannel::OnMessage(talk_base::Message *pmsg) {
1580 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 case MSG_EARLYMEDIATIMEOUT:
1582 HandleEarlyMediaTimeout();
1583 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 case MSG_CHANNEL_ERROR: {
1585 VoiceChannelErrorMessageData* data =
1586 static_cast<VoiceChannelErrorMessageData*>(pmsg->pdata);
1587 SignalMediaError(this, data->ssrc, data->error);
1588 delete data;
1589 break;
1590 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 default:
1592 BaseChannel::OnMessage(pmsg);
1593 break;
1594 }
1595}
1596
1597void VoiceChannel::OnConnectionMonitorUpdate(
1598 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
1599 SignalConnectionMonitor(this, infos);
1600}
1601
1602void VoiceChannel::OnMediaMonitorUpdate(
1603 VoiceMediaChannel* media_channel, const VoiceMediaInfo& info) {
1604 ASSERT(media_channel == this->media_channel());
1605 SignalMediaMonitor(this, info);
1606}
1607
1608void VoiceChannel::OnAudioMonitorUpdate(AudioMonitor* monitor,
1609 const AudioInfo& info) {
1610 SignalAudioMonitor(this, info);
1611}
1612
1613void VoiceChannel::OnVoiceChannelError(
1614 uint32 ssrc, VoiceMediaChannel::Error err) {
1615 VoiceChannelErrorMessageData* data = new VoiceChannelErrorMessageData(
1616 ssrc, err);
1617 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
1618}
1619
1620void VoiceChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
1621 SrtpFilter::Error error) {
1622 switch (error) {
1623 case SrtpFilter::ERROR_FAIL:
1624 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1625 VoiceMediaChannel::ERROR_REC_SRTP_ERROR :
1626 VoiceMediaChannel::ERROR_PLAY_SRTP_ERROR);
1627 break;
1628 case SrtpFilter::ERROR_AUTH:
1629 OnVoiceChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
1630 VoiceMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
1631 VoiceMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
1632 break;
1633 case SrtpFilter::ERROR_REPLAY:
1634 // Only receving channel should have this error.
1635 ASSERT(mode == SrtpFilter::UNPROTECT);
1636 OnVoiceChannelError(ssrc, VoiceMediaChannel::ERROR_PLAY_SRTP_REPLAY);
1637 break;
1638 default:
1639 break;
1640 }
1641}
1642
1643void VoiceChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
1644 GetSupportedAudioCryptoSuites(ciphers);
1645}
1646
1647VideoChannel::VideoChannel(talk_base::Thread* thread,
1648 MediaEngineInterface* media_engine,
1649 VideoMediaChannel* media_channel,
1650 BaseSession* session,
1651 const std::string& content_name,
1652 bool rtcp,
1653 VoiceChannel* voice_channel)
1654 : BaseChannel(thread, media_engine, media_channel, session, content_name,
1655 rtcp),
1656 voice_channel_(voice_channel),
1657 renderer_(NULL),
1658 screencapture_factory_(CreateScreenCapturerFactory()),
1659 previous_we_(talk_base::WE_CLOSE) {
1660}
1661
1662bool VideoChannel::Init() {
1663 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
1664 content_name(), "video_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
1665 if (!BaseChannel::Init(session()->CreateChannel(
1666 content_name(), "video_rtp", ICE_CANDIDATE_COMPONENT_RTP),
1667 rtcp_channel)) {
1668 return false;
1669 }
1670 media_channel()->SignalMediaError.connect(
1671 this, &VideoChannel::OnVideoChannelError);
1672 srtp_filter()->SignalSrtpError.connect(
1673 this, &VideoChannel::OnSrtpError);
1674 return true;
1675}
1676
1677void VoiceChannel::SendLastMediaError() {
1678 uint32 ssrc;
1679 VoiceMediaChannel::Error error;
1680 media_channel()->GetLastMediaError(&ssrc, &error);
1681 SignalMediaError(this, ssrc, error);
1682}
1683
1684VideoChannel::~VideoChannel() {
1685 std::vector<uint32> screencast_ssrcs;
1686 ScreencastMap::iterator iter;
1687 while (!screencast_capturers_.empty()) {
1688 if (!RemoveScreencast(screencast_capturers_.begin()->first)) {
1689 LOG(LS_ERROR) << "Unable to delete screencast with ssrc "
1690 << screencast_capturers_.begin()->first;
1691 ASSERT(false);
1692 break;
1693 }
1694 }
1695
1696 StopMediaMonitor();
1697 // this can't be done in the base class, since it calls a virtual
1698 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00001699
1700 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701}
1702
1703bool VideoChannel::SetRenderer(uint32 ssrc, VideoRenderer* renderer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001704 worker_thread()->Invoke<void>(Bind(
1705 &VideoMediaChannel::SetRenderer, media_channel(), ssrc, renderer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 return true;
1707}
1708
1709bool VideoChannel::ApplyViewRequest(const ViewRequest& request) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001710 return InvokeOnWorker(Bind(&VideoChannel::ApplyViewRequest_w, this, request));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711}
1712
1713VideoCapturer* VideoChannel::AddScreencast(
1714 uint32 ssrc, const ScreencastId& id) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001715 return worker_thread()->Invoke<VideoCapturer*>(Bind(
1716 &VideoChannel::AddScreencast_w, this, ssrc, id));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717}
1718
1719bool VideoChannel::SetCapturer(uint32 ssrc, VideoCapturer* capturer) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001720 return InvokeOnWorker(Bind(&VideoMediaChannel::SetCapturer,
1721 media_channel(), ssrc, capturer));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722}
1723
1724bool VideoChannel::RemoveScreencast(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001725 return InvokeOnWorker(Bind(&VideoChannel::RemoveScreencast_w, this, ssrc));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726}
1727
1728bool VideoChannel::IsScreencasting() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001729 return InvokeOnWorker(Bind(&VideoChannel::IsScreencasting_w, this));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730}
1731
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001732int VideoChannel::GetScreencastFps(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001733 ScreencastDetailsData data(ssrc);
1734 worker_thread()->Invoke<void>(Bind(
1735 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001736 return data.fps;
1737}
1738
1739int VideoChannel::GetScreencastMaxPixels(uint32 ssrc) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001740 ScreencastDetailsData data(ssrc);
1741 worker_thread()->Invoke<void>(Bind(
1742 &VideoChannel::GetScreencastDetails_w, this, &data));
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001743 return data.screencast_max_pixels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001744}
1745
1746bool VideoChannel::SendIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001747 worker_thread()->Invoke<void>(Bind(
1748 &VideoMediaChannel::SendIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 return true;
1750}
1751
1752bool VideoChannel::RequestIntraFrame() {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001753 worker_thread()->Invoke<void>(Bind(
1754 &VideoMediaChannel::RequestIntraFrame, media_channel()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 return true;
1756}
1757
1758void VideoChannel::SetScreenCaptureFactory(
1759 ScreenCapturerFactory* screencapture_factory) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001760 worker_thread()->Invoke<void>(Bind(
1761 &VideoChannel::SetScreenCaptureFactory_w,
1762 this, screencapture_factory));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763}
1764
1765void VideoChannel::ChangeState() {
1766 // Render incoming data if we're the active call, and we have the local
1767 // content. We receive data on the default channel and multiplexed streams.
1768 bool recv = IsReadyToReceive();
1769 if (!media_channel()->SetRender(recv)) {
1770 LOG(LS_ERROR) << "Failed to SetRender on video channel";
1771 // TODO(gangji): Report error back to server.
1772 }
1773
1774 // Send outgoing data if we're the active call, we have the remote content,
1775 // and we have had some form of connectivity.
1776 bool send = IsReadyToSend();
1777 if (!media_channel()->SetSend(send)) {
1778 LOG(LS_ERROR) << "Failed to SetSend on video channel";
1779 // TODO(gangji): Report error back to server.
1780 }
1781
1782 LOG(LS_INFO) << "Changing video state, recv=" << recv << " send=" << send;
1783}
1784
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001785bool VideoChannel::GetStats(
1786 const StatsOptions& options, VideoMediaInfo* stats) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001787 return InvokeOnWorker(Bind(&VideoMediaChannel::GetStats,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +00001788 media_channel(), options, stats));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789}
1790
1791void VideoChannel::StartMediaMonitor(int cms) {
1792 media_monitor_.reset(new VideoMediaMonitor(media_channel(), worker_thread(),
1793 talk_base::Thread::Current()));
1794 media_monitor_->SignalUpdate.connect(
1795 this, &VideoChannel::OnMediaMonitorUpdate);
1796 media_monitor_->Start(cms);
1797}
1798
1799void VideoChannel::StopMediaMonitor() {
1800 if (media_monitor_) {
1801 media_monitor_->Stop();
1802 media_monitor_.reset();
1803 }
1804}
1805
1806const ContentInfo* VideoChannel::GetFirstContent(
1807 const SessionDescription* sdesc) {
1808 return GetFirstVideoContent(sdesc);
1809}
1810
1811bool VideoChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001812 ContentAction action,
1813 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814 ASSERT(worker_thread() == talk_base::Thread::Current());
1815 LOG(LS_INFO) << "Setting local video description";
1816
1817 const VideoContentDescription* video =
1818 static_cast<const VideoContentDescription*>(content);
1819 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001820 if (!video) {
1821 SafeSetError("Can't find video content in local description.", error_desc);
1822 return false;
1823 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001825 bool ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 // Set local video codecs (what we want to receive).
1827 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001828 if (!media_channel()->SetRecvCodecs(video->codecs())) {
1829 SafeSetError("Failed to set video receive codecs.", error_desc);
1830 ret = false;
1831 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 }
1833
1834 if (action != CA_UPDATE) {
1835 VideoOptions video_options;
1836 media_channel()->GetOptions(&video_options);
1837 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1838
1839 if (!media_channel()->SetOptions(video_options)) {
1840 // Log an error on failure, but don't abort the call.
1841 LOG(LS_ERROR) << "Failed to set video channel options";
1842 }
1843 }
1844
1845 // If everything worked, see if we can start receiving.
1846 if (ret) {
1847 ChangeState();
1848 } else {
1849 LOG(LS_WARNING) << "Failed to set local video description";
1850 }
1851 return ret;
1852}
1853
1854bool VideoChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001855 ContentAction action,
1856 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 ASSERT(worker_thread() == talk_base::Thread::Current());
1858 LOG(LS_INFO) << "Setting remote video description";
1859
1860 const VideoContentDescription* video =
1861 static_cast<const VideoContentDescription*>(content);
1862 ASSERT(video != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001863 if (!video) {
1864 SafeSetError("Can't find video content in remote description.", error_desc);
1865 return false;
1866 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867
1868 bool ret = true;
1869 // Set remote video codecs (what the other side wants to receive).
1870 if (action != CA_UPDATE || video->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001871 if (!media_channel()->SetSendCodecs(video->codecs())) {
1872 SafeSetError("Failed to set video send codecs.", error_desc);
1873 ret = false;
1874 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 }
1876
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00001877 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878
1879 if (action != CA_UPDATE) {
1880 // Tweak our video processing settings, if needed.
1881 VideoOptions video_options;
1882 media_channel()->GetOptions(&video_options);
wu@webrtc.org9caf2762013-12-11 18:25:07 +00001883 if (video->conference_mode()) {
1884 video_options.conference_mode.Set(true);
1885 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 video_options.buffered_mode_latency.Set(video->buffered_mode_latency());
1887
1888 if (!media_channel()->SetOptions(video_options)) {
1889 // Log an error on failure, but don't abort the call.
1890 LOG(LS_ERROR) << "Failed to set video channel options";
1891 }
1892 }
1893
1894 // If everything worked, see if we can start sending.
1895 if (ret) {
1896 ChangeState();
1897 } else {
1898 LOG(LS_WARNING) << "Failed to set remote video description";
1899 }
1900 return ret;
1901}
1902
1903bool VideoChannel::ApplyViewRequest_w(const ViewRequest& request) {
1904 bool ret = true;
1905 // Set the send format for each of the local streams. If the view request
1906 // does not contain a local stream, set its send format to 0x0, which will
1907 // drop all frames.
1908 for (std::vector<StreamParams>::const_iterator it = local_streams().begin();
1909 it != local_streams().end(); ++it) {
1910 VideoFormat format(0, 0, 0, cricket::FOURCC_I420);
1911 StaticVideoViews::const_iterator view;
1912 for (view = request.static_video_views.begin();
1913 view != request.static_video_views.end(); ++view) {
1914 if (view->selector.Matches(*it)) {
1915 format.width = view->width;
1916 format.height = view->height;
1917 format.interval = cricket::VideoFormat::FpsToInterval(view->framerate);
1918 break;
1919 }
1920 }
1921
1922 ret &= media_channel()->SetSendStreamFormat(it->first_ssrc(), format);
1923 }
1924
1925 // Check if the view request has invalid streams.
1926 for (StaticVideoViews::const_iterator it = request.static_video_views.begin();
1927 it != request.static_video_views.end(); ++it) {
1928 if (!GetStream(local_streams(), it->selector, NULL)) {
1929 LOG(LS_WARNING) << "View request for ("
1930 << it->selector.ssrc << ", '"
1931 << it->selector.groupid << "', '"
1932 << it->selector.streamid << "'"
1933 << ") is not in the local streams.";
1934 }
1935 }
1936
1937 return ret;
1938}
1939
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001940VideoCapturer* VideoChannel::AddScreencast_w(
1941 uint32 ssrc, const ScreencastId& id) {
1942 if (screencast_capturers_.find(ssrc) != screencast_capturers_.end()) {
1943 return NULL;
1944 }
1945 VideoCapturer* screen_capturer =
1946 screencapture_factory_->CreateScreenCapturer(id);
1947 if (!screen_capturer) {
1948 return NULL;
1949 }
1950 screen_capturer->SignalStateChange.connect(this,
1951 &VideoChannel::OnStateChange);
1952 screencast_capturers_[ssrc] = screen_capturer;
1953 return screen_capturer;
1954}
1955
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956bool VideoChannel::RemoveScreencast_w(uint32 ssrc) {
1957 ScreencastMap::iterator iter = screencast_capturers_.find(ssrc);
1958 if (iter == screencast_capturers_.end()) {
1959 return false;
1960 }
1961 // Clean up VideoCapturer.
1962 delete iter->second;
1963 screencast_capturers_.erase(iter);
1964 return true;
1965}
1966
1967bool VideoChannel::IsScreencasting_w() const {
1968 return !screencast_capturers_.empty();
1969}
1970
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001971void VideoChannel::GetScreencastDetails_w(
1972 ScreencastDetailsData* data) const {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001973 ScreencastMap::const_iterator iter = screencast_capturers_.find(data->ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974 if (iter == screencast_capturers_.end()) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001975 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 }
1977 VideoCapturer* capturer = iter->second;
1978 const VideoFormat* video_format = capturer->GetCaptureFormat();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001979 data->fps = VideoFormat::IntervalToFps(video_format->interval);
1980 data->screencast_max_pixels = capturer->screencast_max_pixels();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981}
1982
1983void VideoChannel::SetScreenCaptureFactory_w(
1984 ScreenCapturerFactory* screencapture_factory) {
1985 if (screencapture_factory == NULL) {
1986 screencapture_factory_.reset(CreateScreenCapturerFactory());
1987 } else {
1988 screencapture_factory_.reset(screencapture_factory);
1989 }
1990}
1991
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992void VideoChannel::OnScreencastWindowEvent_s(uint32 ssrc,
1993 talk_base::WindowEvent we) {
1994 ASSERT(signaling_thread() == talk_base::Thread::Current());
1995 SignalScreencastWindowEvent(ssrc, we);
1996}
1997
1998bool VideoChannel::SetChannelOptions(const VideoOptions &options) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00001999 return InvokeOnWorker(Bind(&VideoMediaChannel::SetOptions,
2000 media_channel(), options));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001}
2002
2003void VideoChannel::OnMessage(talk_base::Message *pmsg) {
2004 switch (pmsg->message_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 case MSG_SCREENCASTWINDOWEVENT: {
2006 const ScreencastEventMessageData* data =
2007 static_cast<ScreencastEventMessageData*>(pmsg->pdata);
2008 OnScreencastWindowEvent_s(data->ssrc, data->event);
2009 delete data;
2010 break;
2011 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 case MSG_CHANNEL_ERROR: {
2013 const VideoChannelErrorMessageData* data =
2014 static_cast<VideoChannelErrorMessageData*>(pmsg->pdata);
2015 SignalMediaError(this, data->ssrc, data->error);
2016 delete data;
2017 break;
2018 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019 default:
2020 BaseChannel::OnMessage(pmsg);
2021 break;
2022 }
2023}
2024
2025void VideoChannel::OnConnectionMonitorUpdate(
2026 SocketMonitor *monitor, const std::vector<ConnectionInfo> &infos) {
2027 SignalConnectionMonitor(this, infos);
2028}
2029
2030// TODO(pthatcher): Look into removing duplicate code between
2031// audio, video, and data, perhaps by using templates.
2032void VideoChannel::OnMediaMonitorUpdate(
2033 VideoMediaChannel* media_channel, const VideoMediaInfo &info) {
2034 ASSERT(media_channel == this->media_channel());
2035 SignalMediaMonitor(this, info);
2036}
2037
2038void VideoChannel::OnScreencastWindowEvent(uint32 ssrc,
2039 talk_base::WindowEvent event) {
2040 ScreencastEventMessageData* pdata =
2041 new ScreencastEventMessageData(ssrc, event);
2042 signaling_thread()->Post(this, MSG_SCREENCASTWINDOWEVENT, pdata);
2043}
2044
2045void VideoChannel::OnStateChange(VideoCapturer* capturer, CaptureState ev) {
2046 // Map capturer events to window events. In the future we may want to simply
2047 // pass these events up directly.
2048 talk_base::WindowEvent we;
2049 if (ev == CS_STOPPED) {
2050 we = talk_base::WE_CLOSE;
2051 } else if (ev == CS_PAUSED) {
2052 we = talk_base::WE_MINIMIZE;
2053 } else if (ev == CS_RUNNING && previous_we_ == talk_base::WE_MINIMIZE) {
2054 we = talk_base::WE_RESTORE;
2055 } else {
2056 return;
2057 }
2058 previous_we_ = we;
2059
2060 uint32 ssrc = 0;
2061 if (!GetLocalSsrc(capturer, &ssrc)) {
2062 return;
2063 }
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002064
2065 OnScreencastWindowEvent(ssrc, we);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066}
2067
2068bool VideoChannel::GetLocalSsrc(const VideoCapturer* capturer, uint32* ssrc) {
2069 *ssrc = 0;
2070 for (ScreencastMap::iterator iter = screencast_capturers_.begin();
2071 iter != screencast_capturers_.end(); ++iter) {
2072 if (iter->second == capturer) {
2073 *ssrc = iter->first;
2074 return true;
2075 }
2076 }
2077 return false;
2078}
2079
2080void VideoChannel::OnVideoChannelError(uint32 ssrc,
2081 VideoMediaChannel::Error error) {
2082 VideoChannelErrorMessageData* data = new VideoChannelErrorMessageData(
2083 ssrc, error);
2084 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2085}
2086
2087void VideoChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2088 SrtpFilter::Error error) {
2089 switch (error) {
2090 case SrtpFilter::ERROR_FAIL:
2091 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2092 VideoMediaChannel::ERROR_REC_SRTP_ERROR :
2093 VideoMediaChannel::ERROR_PLAY_SRTP_ERROR);
2094 break;
2095 case SrtpFilter::ERROR_AUTH:
2096 OnVideoChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2097 VideoMediaChannel::ERROR_REC_SRTP_AUTH_FAILED :
2098 VideoMediaChannel::ERROR_PLAY_SRTP_AUTH_FAILED);
2099 break;
2100 case SrtpFilter::ERROR_REPLAY:
2101 // Only receving channel should have this error.
2102 ASSERT(mode == SrtpFilter::UNPROTECT);
2103 // TODO(gangji): Turn on the signaling of replay error once we have
2104 // switched to the new mechanism for doing video retransmissions.
2105 // OnVideoChannelError(ssrc, VideoMediaChannel::ERROR_PLAY_SRTP_REPLAY);
2106 break;
2107 default:
2108 break;
2109 }
2110}
2111
2112
2113void VideoChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2114 GetSupportedVideoCryptoSuites(ciphers);
2115}
2116
2117DataChannel::DataChannel(talk_base::Thread* thread,
2118 DataMediaChannel* media_channel,
2119 BaseSession* session,
2120 const std::string& content_name,
2121 bool rtcp)
2122 // MediaEngine is NULL
2123 : BaseChannel(thread, NULL, media_channel, session, content_name, rtcp),
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002124 data_channel_type_(cricket::DCT_NONE),
2125 ready_to_send_data_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126}
2127
2128DataChannel::~DataChannel() {
2129 StopMediaMonitor();
2130 // this can't be done in the base class, since it calls a virtual
2131 DisableMedia_w();
wu@webrtc.org78187522013-10-07 23:32:02 +00002132
2133 Deinit();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002134}
2135
2136bool DataChannel::Init() {
2137 TransportChannel* rtcp_channel = rtcp() ? session()->CreateChannel(
2138 content_name(), "data_rtcp", ICE_CANDIDATE_COMPONENT_RTCP) : NULL;
2139 if (!BaseChannel::Init(session()->CreateChannel(
2140 content_name(), "data_rtp", ICE_CANDIDATE_COMPONENT_RTP),
2141 rtcp_channel)) {
2142 return false;
2143 }
2144 media_channel()->SignalDataReceived.connect(
2145 this, &DataChannel::OnDataReceived);
2146 media_channel()->SignalMediaError.connect(
2147 this, &DataChannel::OnDataChannelError);
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002148 media_channel()->SignalReadyToSend.connect(
2149 this, &DataChannel::OnDataChannelReadyToSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002150 srtp_filter()->SignalSrtpError.connect(
2151 this, &DataChannel::OnSrtpError);
2152 return true;
2153}
2154
2155bool DataChannel::SendData(const SendDataParams& params,
2156 const talk_base::Buffer& payload,
2157 SendDataResult* result) {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002158 return InvokeOnWorker(Bind(&DataMediaChannel::SendData,
2159 media_channel(), params, payload, result));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160}
2161
2162const ContentInfo* DataChannel::GetFirstContent(
2163 const SessionDescription* sdesc) {
2164 return GetFirstDataContent(sdesc);
2165}
2166
2167
2168static bool IsRtpPacket(const talk_base::Buffer* packet) {
2169 int version;
2170 if (!GetRtpVersion(packet->data(), packet->length(), &version)) {
2171 return false;
2172 }
2173
2174 return version == 2;
2175}
2176
2177bool DataChannel::WantsPacket(bool rtcp, talk_base::Buffer* packet) {
2178 if (data_channel_type_ == DCT_SCTP) {
2179 // TODO(pthatcher): Do this in a more robust way by checking for
2180 // SCTP or DTLS.
2181 return !IsRtpPacket(packet);
2182 } else if (data_channel_type_ == DCT_RTP) {
2183 return BaseChannel::WantsPacket(rtcp, packet);
2184 }
2185 return false;
2186}
2187
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002188bool DataChannel::SetDataChannelType(DataChannelType new_data_channel_type,
2189 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002190 // It hasn't been set before, so set it now.
2191 if (data_channel_type_ == DCT_NONE) {
2192 data_channel_type_ = new_data_channel_type;
2193 return true;
2194 }
2195
2196 // It's been set before, but doesn't match. That's bad.
2197 if (data_channel_type_ != new_data_channel_type) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002198 std::ostringstream desc;
2199 desc << "Data channel type mismatch."
2200 << " Expected " << data_channel_type_
2201 << " Got " << new_data_channel_type;
2202 SafeSetError(desc.str(), error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203 return false;
2204 }
2205
2206 // It's hasn't changed. Nothing to do.
2207 return true;
2208}
2209
2210bool DataChannel::SetDataChannelTypeFromContent(
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002211 const DataContentDescription* content,
2212 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 bool is_sctp = ((content->protocol() == kMediaProtocolSctp) ||
2214 (content->protocol() == kMediaProtocolDtlsSctp));
2215 DataChannelType data_channel_type = is_sctp ? DCT_SCTP : DCT_RTP;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002216 return SetDataChannelType(data_channel_type, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217}
2218
2219bool DataChannel::SetLocalContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002220 ContentAction action,
2221 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222 ASSERT(worker_thread() == talk_base::Thread::Current());
2223 LOG(LS_INFO) << "Setting local data description";
2224
2225 const DataContentDescription* data =
2226 static_cast<const DataContentDescription*>(content);
2227 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002228 if (!data) {
2229 SafeSetError("Can't find data content in local description.", error_desc);
2230 return false;
2231 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002232
2233 bool ret = false;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002234 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235 return false;
2236 }
2237
2238 if (data_channel_type_ == DCT_SCTP) {
2239 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002240 ret = UpdateLocalStreams_w(data->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002241 if (ret) {
2242 set_local_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002243 // As in SetRemoteContent_w, make sure we set the local SCTP port
2244 // number as specified in our DataContentDescription.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002245 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2246 SafeSetError("Failed to set data receive codecs.", error_desc);
2247 ret = false;
2248 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002249 }
2250 } else {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002251 ret = SetBaseLocalContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252
2253 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002254 if (!media_channel()->SetRecvCodecs(data->codecs())) {
2255 SafeSetError("Failed to set data receive codecs.", error_desc);
2256 ret = false;
2257 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002258 }
2259 }
2260
2261 // If everything worked, see if we can start receiving.
2262 if (ret) {
2263 ChangeState();
2264 } else {
2265 LOG(LS_WARNING) << "Failed to set local data description";
2266 }
2267 return ret;
2268}
2269
2270bool DataChannel::SetRemoteContent_w(const MediaContentDescription* content,
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002271 ContentAction action,
2272 std::string* error_desc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002273 ASSERT(worker_thread() == talk_base::Thread::Current());
2274
2275 const DataContentDescription* data =
2276 static_cast<const DataContentDescription*>(content);
2277 ASSERT(data != NULL);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002278 if (!data) {
2279 SafeSetError("Can't find data content in remote description.", error_desc);
2280 return false;
2281 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282
2283 bool ret = true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002284 if (!SetDataChannelTypeFromContent(data, error_desc)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 return false;
2286 }
2287
2288 if (data_channel_type_ == DCT_SCTP) {
2289 LOG(LS_INFO) << "Setting SCTP remote data description";
2290 // SCTP data channels don't need the rest of the stuff.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002291 ret = UpdateRemoteStreams_w(content->streams(), action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 if (ret) {
2293 set_remote_content_direction(content->direction());
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +00002294 // We send the SCTP port number (not to be confused with the underlying
2295 // UDP port number) as a codec parameter. Make sure it gets there.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002296 if (!media_channel()->SetSendCodecs(data->codecs())) {
2297 SafeSetError("Failed to set data send codecs.", error_desc);
2298 ret = false;
2299 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300 }
2301 } else {
2302 // If the remote data doesn't have codecs and isn't an update, it
2303 // must be empty, so ignore it.
2304 if (action != CA_UPDATE && !data->has_codecs()) {
2305 return true;
2306 }
2307 LOG(LS_INFO) << "Setting remote data description";
2308
2309 // Set remote video codecs (what the other side wants to receive).
2310 if (action != CA_UPDATE || data->has_codecs()) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002311 if (!media_channel()->SetSendCodecs(data->codecs())) {
2312 SafeSetError("Failed to set data send codecs.", error_desc);
2313 ret = false;
2314 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315 }
2316
2317 if (ret) {
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002318 ret &= SetBaseRemoteContent_w(content, action, error_desc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319 }
2320
2321 if (action != CA_UPDATE) {
2322 int bandwidth_bps = data->bandwidth();
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002323 if (!media_channel()->SetMaxSendBandwidth(bandwidth_bps)) {
2324 std::ostringstream desc;
2325 desc << "Failed to set max send bandwidth for data content.";
2326 SafeSetError(desc.str(), error_desc);
2327 ret = false;
2328 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 }
2330 }
2331
2332 // If everything worked, see if we can start sending.
2333 if (ret) {
2334 ChangeState();
2335 } else {
2336 LOG(LS_WARNING) << "Failed to set remote data description";
2337 }
2338 return ret;
2339}
2340
2341void DataChannel::ChangeState() {
2342 // Render incoming data if we're the active call, and we have the local
2343 // content. We receive data on the default channel and multiplexed streams.
2344 bool recv = IsReadyToReceive();
2345 if (!media_channel()->SetReceive(recv)) {
2346 LOG(LS_ERROR) << "Failed to SetReceive on data channel";
2347 }
2348
2349 // Send outgoing data if we're the active call, we have the remote content,
2350 // and we have had some form of connectivity.
2351 bool send = IsReadyToSend();
2352 if (!media_channel()->SetSend(send)) {
2353 LOG(LS_ERROR) << "Failed to SetSend on data channel";
2354 }
2355
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +00002356 // Trigger SignalReadyToSendData asynchronously.
2357 OnDataChannelReadyToSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002358
2359 LOG(LS_INFO) << "Changing data state, recv=" << recv << " send=" << send;
2360}
2361
2362void DataChannel::OnMessage(talk_base::Message *pmsg) {
2363 switch (pmsg->message_id) {
2364 case MSG_READYTOSENDDATA: {
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002365 DataChannelReadyToSendMessageData* data =
2366 static_cast<DataChannelReadyToSendMessageData*>(pmsg->pdata);
wu@webrtc.org07a6fbe2013-11-04 18:41:34 +00002367 ready_to_send_data_ = data->data();
2368 SignalReadyToSendData(ready_to_send_data_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369 delete data;
2370 break;
2371 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002372 case MSG_DATARECEIVED: {
2373 DataReceivedMessageData* data =
2374 static_cast<DataReceivedMessageData*>(pmsg->pdata);
2375 SignalDataReceived(this, data->params, data->payload);
2376 delete data;
2377 break;
2378 }
2379 case MSG_CHANNEL_ERROR: {
2380 const DataChannelErrorMessageData* data =
2381 static_cast<DataChannelErrorMessageData*>(pmsg->pdata);
2382 SignalMediaError(this, data->ssrc, data->error);
2383 delete data;
2384 break;
2385 }
2386 default:
2387 BaseChannel::OnMessage(pmsg);
2388 break;
2389 }
2390}
2391
2392void DataChannel::OnConnectionMonitorUpdate(
2393 SocketMonitor* monitor, const std::vector<ConnectionInfo>& infos) {
2394 SignalConnectionMonitor(this, infos);
2395}
2396
2397void DataChannel::StartMediaMonitor(int cms) {
2398 media_monitor_.reset(new DataMediaMonitor(media_channel(), worker_thread(),
2399 talk_base::Thread::Current()));
2400 media_monitor_->SignalUpdate.connect(
2401 this, &DataChannel::OnMediaMonitorUpdate);
2402 media_monitor_->Start(cms);
2403}
2404
2405void DataChannel::StopMediaMonitor() {
2406 if (media_monitor_) {
2407 media_monitor_->Stop();
2408 media_monitor_->SignalUpdate.disconnect(this);
2409 media_monitor_.reset();
2410 }
2411}
2412
2413void DataChannel::OnMediaMonitorUpdate(
2414 DataMediaChannel* media_channel, const DataMediaInfo& info) {
2415 ASSERT(media_channel == this->media_channel());
2416 SignalMediaMonitor(this, info);
2417}
2418
2419void DataChannel::OnDataReceived(
2420 const ReceiveDataParams& params, const char* data, size_t len) {
2421 DataReceivedMessageData* msg = new DataReceivedMessageData(
2422 params, data, len);
2423 signaling_thread()->Post(this, MSG_DATARECEIVED, msg);
2424}
2425
2426void DataChannel::OnDataChannelError(
2427 uint32 ssrc, DataMediaChannel::Error err) {
2428 DataChannelErrorMessageData* data = new DataChannelErrorMessageData(
2429 ssrc, err);
2430 signaling_thread()->Post(this, MSG_CHANNEL_ERROR, data);
2431}
2432
wu@webrtc.orgd64719d2013-08-01 00:00:07 +00002433void DataChannel::OnDataChannelReadyToSend(bool writable) {
2434 // This is usded for congestion control to indicate that the stream is ready
2435 // to send by the MediaChannel, as opposed to OnReadyToSend, which indicates
2436 // that the transport channel is ready.
2437 signaling_thread()->Post(this, MSG_READYTOSENDDATA,
2438 new DataChannelReadyToSendMessageData(writable));
2439}
2440
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002441void DataChannel::OnSrtpError(uint32 ssrc, SrtpFilter::Mode mode,
2442 SrtpFilter::Error error) {
2443 switch (error) {
2444 case SrtpFilter::ERROR_FAIL:
2445 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2446 DataMediaChannel::ERROR_SEND_SRTP_ERROR :
2447 DataMediaChannel::ERROR_RECV_SRTP_ERROR);
2448 break;
2449 case SrtpFilter::ERROR_AUTH:
2450 OnDataChannelError(ssrc, (mode == SrtpFilter::PROTECT) ?
2451 DataMediaChannel::ERROR_SEND_SRTP_AUTH_FAILED :
2452 DataMediaChannel::ERROR_RECV_SRTP_AUTH_FAILED);
2453 break;
2454 case SrtpFilter::ERROR_REPLAY:
2455 // Only receving channel should have this error.
2456 ASSERT(mode == SrtpFilter::UNPROTECT);
2457 OnDataChannelError(ssrc, DataMediaChannel::ERROR_RECV_SRTP_REPLAY);
2458 break;
2459 default:
2460 break;
2461 }
2462}
2463
2464void DataChannel::GetSrtpCiphers(std::vector<std::string>* ciphers) const {
2465 GetSupportedDataCryptoSuites(ciphers);
2466}
2467
2468bool DataChannel::ShouldSetupDtlsSrtp() const {
2469 return (data_channel_type_ == DCT_RTP);
2470}
2471
2472} // namespace cricket